Created
April 22, 2010 21:17
-
-
Save tcrayford/375821 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.Comparator; | |
public class KalaTournament { | |
private enum WIN_FLAG { | |
PLAYER_0, PLAYER_1, DRAW | |
} | |
private static class AIComparer implements Comparator<KalaPlayer> { | |
private int MAX_TIME; | |
public AIComparer(int maxTime) { | |
this.MAX_TIME = maxTime; | |
} | |
public AIComparer() { | |
this.MAX_TIME = 200; | |
} | |
public int compare(KalaPlayer p1, KalaPlayer p2) { | |
int player1Wins = 0; | |
int player2Wins = 0; | |
System.out.println("comparing " + p1.getClass().getName() + " with " + p2.getClass().getName()); | |
int draws = 0; | |
// We run once for each number of starting stones. | |
for (int game = 1; game <= 10; game++) { | |
// First we run with player 1 getting the first go | |
WIN_FLAG result = winningPlayer(game, p1, p2); | |
if (result == WIN_FLAG.PLAYER_0) { | |
System.out.print("."); | |
player1Wins++; | |
} else if (result == WIN_FLAG.PLAYER_1) { | |
System.out.print("L"); | |
player2Wins++; | |
} else { | |
System.out.print("="); | |
draws++; | |
} | |
} | |
for (int game = 1; game <= 10; game++) { | |
// Then we run with player 2 getting the first go | |
WIN_FLAG result = winningPlayer(game, p2, p1); | |
if (result == WIN_FLAG.PLAYER_0) { | |
System.out.print("L"); | |
player2Wins++; | |
} else if (result == WIN_FLAG.PLAYER_1) { | |
System.out.print("."); | |
player1Wins++; | |
} else { | |
System.out.print("="); | |
draws++; | |
} | |
} | |
// Then we return the results for the winning player | |
if(player1Wins>player2Wins) { | |
System.out.println("\n" + p1.getClass().getName() + " wins"); | |
return 1; | |
} else if(player2Wins>player1Wins) { | |
System.out.println("\n" + p2.getClass().getName() + " wins"); | |
return -1; | |
} else { | |
System.out.println("\ndraw"); | |
return 0; | |
} | |
} | |
public WIN_FLAG winningPlayer(int numStones, KalaPlayer firstPlayer, KalaPlayer secondPlayer) { | |
KalaGameState state = new KalaGameState(numStones); | |
while (!state.gameOver()) { | |
int turn = state.getTurn(); | |
int move = -1; | |
long startTime = System.currentTimeMillis(); | |
try { | |
if (turn == 0) { | |
move = firstPlayer.chooseMove(state.clone()); | |
} else if (turn == 1) { | |
move = secondPlayer.chooseMove(state.clone()); | |
} | |
//if (System.currentTimeMillis() - startTime > MAX_TIME) { | |
//System.out.println("player " + turn + " took too long making a move, so the other player won"); | |
//return getWin(state); | |
//} else { | |
state.makeMove(move); | |
//} | |
} catch (NoMoveAvailableException e) { | |
System.out.println("player " + turn + "failed to make a move"); | |
return getWin(state); | |
} catch (IllegalMoveException e) { | |
System.out.println("player " + turn + "made an illegal move"); | |
return getWin(state); | |
} | |
} | |
return getWin(state); | |
} | |
public WIN_FLAG getWin(KalaGameState gs) { | |
if (gs.getKala(0) > gs.getKala(1)) { | |
return WIN_FLAG.PLAYER_0; | |
} else if (gs.getKala(1) > gs.getKala(0)) { | |
return WIN_FLAG.PLAYER_1; | |
} else { | |
return WIN_FLAG.DRAW; | |
} | |
} | |
} | |
public static void main(String[] args) { | |
// Create a list of all players | |
ArrayList<KalaPlayer> players = new ArrayList<KalaPlayer>(); | |
players.add(new LegendPlayerVariable()); //Lewis | |
players.add(new IterativeWithHashTableOrdering(180)); //Tom | |
players.add(new Vasek()); //Vasek | |
players.add(new ComputerGoodPlayer(6)); //Ian | |
players.add(new AngelaPlayer()); //Angela | |
players.add(new KalaRandomPlayer()); // Erwann | |
// Sort the players using the AI comparer above | |
Collections.sort(players, new AIComparer()); | |
System.out.println("\n\nResults"); | |
System.out.println("================="); | |
// We reverse the collection as sorting puts players who win at the end of the list. | |
Collections.reverse(players); | |
for (int ranking = 0; ranking < players.size(); ranking++) { | |
KalaPlayer player = players.get(ranking); | |
System.out.println(player.getClass().getName() + " is ranked at: " + (ranking + 1)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment