package game; import game.model.Card; import game.model.Game; import game.model.Player; import game.model.UserInterface; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Random; public class Controller { private static Random rand = new Random(System.currentTimeMillis()); List freeUIs = new ArrayList(); List games = new ArrayList(); List cards = new ArrayList(); public synchronized void addUI(UserInterface ui) { System.out.println("Adding user interface for: " + ui.getPlayerName()); if (freeUIs.size() > 0) { UserInterface otherUI = freeUIs.remove(0); Player p1 = new Player(otherUI, 250); Player p2 = new Player(ui, 250); // Initialize both UI's with the players and static commands ui.initializeGame(p2, p1, new DrawCommand(), new NextPhaseCommand(), new SelfFighter(p2)); otherUI.initializeGame(p1, p2, new DrawCommand(), new NextPhaseCommand(), new SelfFighter(p1)); // Create a new game with all the cards available final Game g = new GameImpl(Collections.synchronizedList(new ArrayList(cards)), p1, p2); new Thread(new Runnable() { public void run() { g.play(); } }).start(); // play the game, don't block the OSGi thread games.add(g); } else { freeUIs.add(ui); } } public synchronized void removeUI(UserInterface ui) { // called when a user interface service is removed } public void addCard(Card card) { synchronized (cards) { cards.add(card); // also add the new card to any game deck in progress in a random place for (Game g : games) { System.out.println("Shuffling new card into existing game " + card); List deck = g.getDeck(); deck.add(rand.nextInt(deck.size()), card); } } } public void removeCard(Card card) { // called when a card service is removed } }