public static void main(String[] args) { Game game = Game.getInstance(); game.addPlayer(new Player("Juancito")); game.addPlayer(new Player("Pedrito")); System.out.println(Game.getInstance().getPlayers()); }
public static void main(String[] args) { Player andreas = new Player("Andreas"); Player oana = new Player("Oana"); Game game = new Game(); game.addPlayer(andreas); game.addPlayer(oana); System.out.println(game); while (!game.isFinished()) { System.out.println( andreas.getName() + " takes " + game.listSets().get(0).get(0) + ", " + game.listSets().get(0).get(1) + ", " + game.listSets().get(0).get(2)); game.takeSet( andreas, game.listSets().get(0).get(0), game.listSets().get(0).get(1), game.listSets().get(0).get(2)); } System.out.println(game); }
@Test public void greenPlayerHasFourStonesAtStart() { Game game = new Ludo(); game.addPlayer(); Player green = game.addPlayer(); assertEquals(4, Length.of(green.stones())); for (Stone each : green.stones()) { assertEquals(Color.GREEN, each.color); assertEquals(true, each.atStart()); } }
// initialize objects for game class private void initGame() { Game.setBank(new Bank()); Game.setBoard(new Board()); CardsChance chance = new CardsChance(); chance.fillValues(); chance.shuffleCards(); Game.setCardsChance(chance); CardsCommunity community = new CardsCommunity(); community.fillValues(); community.shuffleCards(); Game.setCardsCommunity(community); // ask for players and fill them up int choice; do { choice = UI.askNgetInt("How many players (2-4)?"); } while (choice > 4 || choice < 2); Game.setNumPlayers(choice); for (int i = 0; i < Game.getNumPlayers(); i++) { Game.addPlayer(UI.askNgetString("Enter name for player #" + (i + 1))); } // display all players UI.displayChoiceItems( "Players playing", UI.playersToItems(Game.getAllPlayingPlayers()), false, false); }
/** Run game server */ public void run() { // Server socket try { this.listener = new ServerSocket(Integer.parseInt(this.config.getConfig("gamePort"))); } catch (IOException e) { e.printStackTrace(); } // Listen for new connections try { while (true) { Game game = new Game(); System.out.println("Waiting for players..."); // Wait for red user game.addPlayer(game.new Player(listener.accept(), "red")); // Wait for blue user game.addPlayer(game.new Player(listener.accept(), "blue")); // Wait for yellow user game.addPlayer(game.new Player(listener.accept(), "yellow")); // Wait for green user game.addPlayer(game.new Player(listener.accept(), "green")); game.broadcast("STARTGAME"); // Start game game.start(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { listener.close(); } catch (IOException e) { System.out.println("Error closing game server socket"); e.printStackTrace(); } } }
@Test public void redPlayerHasFourStonesAtStart() { Game game = new Ludo(); Player red = game.addPlayer(); assertEquals(4, Length.of(red.stones())); for (Stone each : red.stones()) { assertEquals(Color.RED, each.color); assertEquals(true, each.atStart()); } }
/** * Add a player to the game with the indicated sessionName. * * <p>If the sessionName doesn't correspond to any gamemetadata, this is a problem. * * <p>Otherwise, look to see if a game is already active. If so, make sure that it has room. If * not, it's a problem. If it does, add the player * * <p>If there is no active game, create one, add the user, and start it. * * @param sessionName Should correspond to some identifier in gamemetadata * @param user The user to add * @return IF the game was successfully joined, return that game object. IF it wasn't, return a * null */ public Game addPlayer(String sessionName, User user) { if (idToGameMap == null) { LOGGER.severe("The idToGameMap in the ActiveGameController is null. It shouldn't be."); return null; } // Look up the game Game game = idToGameMap.get(sessionName); if (game == null) { LOGGER.severe("No game corresponded to the request id of '" + sessionName + "'."); return null; } // Make sure there is room to add the player if (game.stillHasRoom()) { game.addPlayer(user); user.setGame(game); return game; } else { LOGGER.severe("Game has no more room. Race condition?"); return null; } }
public static void startUpRoleChooser() { System.out.print("Welcome to Mafia! How many players? "); Scanner s = new Scanner(System.in); String in = ""; int numPlayers = 0; while (numPlayers <= 0) { in = s.nextLine(); try { numPlayers = Integer.parseInt(in); if (numPlayers <= 0) { System.out.print("Please input a positive integer: "); } } catch (NumberFormatException E) { System.out.print("Please input an integer: "); } } System.out.println(); game = new Game(); int numRolesRemaining = numPlayers; // number of roles left to assign String[] roles = { "mafia", "godfathers", "framers", "cops", "doctors", "bombs", "drunks", "vigilantes", "grannies", "fools", "hookers", "serialkillers", "villager" }; int[] numOfEachRole = new int[roles.length]; // array for the number of people with that role int i = 0; while (numRolesRemaining > 0) { if (i == roles.length - 1) { // in case gone through all the roles and still have extras numOfEachRole[i] = numRolesRemaining; numRolesRemaining = 0; } else { System.out.print("How many " + roles[i] + "? "); int temp = -1; while (temp < 0 || temp > numRolesRemaining) { in = s.nextLine(); try { temp = Integer.parseInt(in); if (temp < 0) { System.out.print("Please input a nonnegative integer: "); } } catch (NumberFormatException E) { System.out.print("Please input an integer: "); } } numOfEachRole[i] = temp; numRolesRemaining -= temp; } i++; } System.out.println("numofEachRole:" + Arrays.toString(numOfEachRole)); String[] names = new String[numPlayers]; int index = 0; while (index < names.length) { System.out.println("Player " + (index + 1)); System.out.print("What is your name? "); String name = s.nextLine(); while (Arrays.asList(names).contains(name)) { System.out.print("That name has already been chosen. Please choose another name: "); name = s.nextLine(); } names[index] = name; index++; } ArrayList<String> rolesInGame = new ArrayList<String>(); // all the valid roles in game for (i = 0; i < numOfEachRole.length; i++) { for (int j = numOfEachRole[i]; j > 0; j--) { rolesInGame.add(roles[i]); // j roles of type i } } System.out.println(rolesInGame); for (i = 0; i < names.length; i++) { int rand = (int) (Math.random() * rolesInGame.size()); String roleForI = rolesInGame.get(rand); rolesInGame.remove(rand); System.out.println("roleForI: " + roleForI); System.out.println("rolesInGame: " + rolesInGame); switch (roleForI) { // "mafia","godfathers","framers","cops","doctors","bombs","drunks","vigilantes","grannies","fools","hookers","serialkillers","villager" case "mafia": game.addPlayer(new Mafia(names[i])); break; case "godfathers": game.addPlayer(new Godfather(names[i])); break; case "framers": game.addPlayer(new Framer(names[i])); break; case "cops": game.addPlayer(new Cop(names[i])); /* double rando = Math.random(); if (rando < 0.25){ game.addPlayer(new Cop(names[i])); } else if (rando < 0.5){ game.addPlayer(new NaiveCop(names[i])); } else if (rando < 0.75){ game.addPlayer(new ParanoidCop(names[i])); } else { game.addPlayer(new InsaneCop(names[i])); } break; */ break; case "doctors": game.addPlayer(new Doctor(names[i])); break; case "bombs": game.addPlayer(new Bomb(names[i])); break; case "drunks": game.addPlayer(new Drunk(names[i])); break; case "vigilantes": game.addPlayer(new Vigilante(names[i])); break; case "grannies": game.addPlayer(new Granny(names[i])); break; case "fools": game.addPlayer(new Fool(names[i])); break; case "hookers": game.addPlayer(new Hooker(names[i])); break; case "serialkillers": game.addPlayer(new SerialKiller(names[i])); break; default: game.addPlayer(new Villager(names[i])); break; } } }
public static void addPlayer() { // temporary thing for testing purposes String[] types = { "villager", "mafia", "godfather", "framer", "cop", "doctor", "bomb", "drunk", "vigilante", "granny", "fool", "hooker", "serialkiller" }; Scanner s = new Scanner(System.in); System.out.println("What kind of player would you like to be? "); for (String str : types) { System.out.print( str + " " // + "[" + str.substring(0,1) + "]" + " "); } System.out.println(); String in = s.nextLine(); in = in.toLowerCase(); System.out.print("What is your name? "); String name = s.nextLine(); while (!(validateName(name))) { System.out.print("That name has already been chosen. Please choose another name: "); name = s.nextLine(); } Player temp = null; if (in.equals("villager") || in.equals("v")) { temp = new Villager(name); } else if (in.equals("mafia") || in.equals("m")) { temp = new Mafia(name); } else if (in.equals("godfather")) { temp = new Godfather(name); } else if (in.equals("cop") || in.equals("c")) { double rand = Math.random(); if (rand < 0.25) { temp = new Cop(name); } else if (rand < 0.5) { temp = new NaiveCop(name); } else if (rand < 0.75) { temp = new ParanoidCop(name); } else { temp = new InsaneCop(name); } } else if (in.equals("doctor") || in.equals("d")) { temp = new Doctor(name); } else if (in.equals("bomb")) { temp = new Bomb(name); } else if (in.equals("drunk")) { temp = new Drunk(name); } else if (in.equals("vigilante")) { temp = new Vigilante(name); } else if (in.equals("granny")) { temp = new Granny(name); } else if (in.equals("fool")) { temp = new Fool(name); } else if (in.equals("hooker")) { temp = new Hooker(name); } else if (in.equals("SerialKiller")) { temp = new SerialKiller(name); } else { temp = new Villager(name); } // temporary final assignment // there's gotta be a better way to do this game.addPlayer(temp); }
public void addPlayer(String name) { game.addPlayer(name); playersView.setPlayerList(game.getPlayers()); playersAdapter.notifyDataSetChanged(); }