// This doesn't really "test" the game flow so much as let us // examine it to evaluate it. public static void main(String[] args) throws InterruptedException { String gameName = "tictactoe"; Game theGame = GameRepository.getDefaultRepository().getGame(gameName); GameFlow flow = new GameFlow(theGame.getRules()); System.out.println("Size of flow: " + flow.getNumTurns()); System.out.println("Sentence forms in flow: " + flow.getSentenceForms()); for (int i = 0; i < flow.getNumTurns(); i++) { System.out.println("On turn " + i + ": " + flow.getSentencesTrueOnTurn(i)); } System.out.println("Turn after last: " + flow.getTurnAfterLast()); }
public static void main(String[] args) throws IOException, SymbolFormatException, GdlFormatException, InterruptedException, GoalDefinitionException { // Extract the desired configuration from the command line. String tourneyName = args[0]; String gameKey = args[1]; Game game = GameRepository.getDefaultRepository().getGame(gameKey); int startClock = Integer.valueOf(args[2]); int playClock = Integer.valueOf(args[3]); if ((args.length - 4) % 3 != 0) { throw new RuntimeException("Invalid number of player arguments of the form host/port/name."); } List<String> hostNames = new ArrayList<String>(); List<String> playerNames = new ArrayList<String>(); List<Integer> portNumbers = new ArrayList<Integer>(); String matchName = tourneyName + "." + gameKey + "." + System.currentTimeMillis(); for (int i = 4; i < args.length; i += 3) { String hostname = args[i]; Integer port = Integer.valueOf(args[i + 1]); String name = args[i + 2]; hostNames.add(hostname); portNumbers.add(port); playerNames.add(name); } int expectedRoles = Role.computeRoles(game.getRules()).size(); if (hostNames.size() != expectedRoles) { throw new RuntimeException( "Invalid number of players for game " + gameKey + ": " + hostNames.size() + " vs " + expectedRoles); } Match match = new Match(matchName, -1, startClock, playClock, game); match.setPlayerNamesFromHost(playerNames); // Actually run the match, using the desired configuration. GameServer server = new GameServer(match, hostNames, portNumbers); server.run(); server.join(); // Open up the directory for this tournament. // Create a "scores" file if none exists. File f = new File(tourneyName); if (!f.exists()) { f.mkdir(); f = new File(tourneyName + "/scores"); f.createNewFile(); } // Open up the XML file for this match, and save the match there. f = new File(tourneyName + "/" + matchName + ".xml"); if (f.exists()) f.delete(); BufferedWriter bw = new BufferedWriter(new FileWriter(f)); bw.write(match.toXML()); bw.flush(); bw.close(); // Open up the JSON file for this match, and save the match there. f = new File(tourneyName + "/" + matchName + ".json"); if (f.exists()) f.delete(); bw = new BufferedWriter(new FileWriter(f)); bw.write(match.toJSON()); bw.flush(); bw.close(); // Save the goals in the "/scores" file for the tournament. bw = new BufferedWriter(new FileWriter(tourneyName + "/scores", true)); List<Integer> goals = server.getGoals(); String goalStr = ""; String playerStr = ""; for (int i = 0; i < goals.size(); i++) { Integer goal = server.getGoals().get(i); goalStr += Integer.toString(goal); playerStr += playerNames.get(i); if (i != goals.size() - 1) { playerStr += ","; goalStr += ","; } } bw.write(playerStr + "=" + goalStr); bw.flush(); bw.close(); }
@Override public void actionPerformed(ActionEvent e) { if (e.getSource() == gameSelector.getGameList()) { theGame = gameSelector.getSelectedGame(); for (int i = 0; i < roleLabels.size(); i++) { gamePanel.remove(roleLabels.get(i)); gamePanel.remove(playerFields.get(i)); } roleLabels.clear(); playerFields.clear(); validate(); runButton.setEnabled(false); if (theGame == null) return; StateMachine stateMachine = new ProverStateMachine(); stateMachine.initialize(theGame.getRules()); List<Role> roles = stateMachine.getRoles(); int newRowCount = 11; for (int i = 0; i < roles.size(); i++) { roleLabels.add(new JLabel(roles.get(i).getName().toString() + ":")); playerFields.add(playerSelector.getPlayerSelectorBox()); playerFields.get(i).setSelectedIndex(i % playerFields.get(i).getModel().getSize()); gamePanel.add( roleLabels.get(i), new GridBagConstraints( 0, newRowCount, 1, 1, 1.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(1, 5, 1, 5), 5, 5)); gamePanel.add( playerFields.get(i), new GridBagConstraints( 1, newRowCount++, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(1, 5, 1, 5), 5, 5)); } gamePanel.add( runButton, new GridBagConstraints( 1, newRowCount, 1, 1, 0.0, 1.0, GridBagConstraints.SOUTH, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0)); validate(); runButton.setEnabled(true); } }