[44] | 1 | package games.league; |
---|
| 2 | |
---|
| 3 | import ec.simple.SimpleFitness; |
---|
| 4 | import ec.vector.DoubleVectorIndividual; |
---|
| 5 | import games.Player; |
---|
| 6 | import games.scenarios.GameScenario; |
---|
| 7 | import games.scenarios.SimpleTwoPlayersGameScenario; |
---|
| 8 | |
---|
| 9 | import java.io.FileReader; |
---|
| 10 | import java.io.IOException; |
---|
| 11 | import java.io.LineNumberReader; |
---|
| 12 | import java.util.ArrayList; |
---|
| 13 | import java.util.List; |
---|
| 14 | |
---|
| 15 | import cecj.app.othello.OthelloBoard; |
---|
| 16 | import cecj.app.othello.OthelloGame; |
---|
| 17 | import cecj.app.othello.OthelloPlayer; |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | public class League { |
---|
| 21 | |
---|
| 22 | private List<String> names; |
---|
| 23 | private List<List<Player>> teams; |
---|
| 24 | |
---|
| 25 | private int[] score; |
---|
| 26 | private int[] wins; |
---|
| 27 | private int[] draws; |
---|
| 28 | private int[] losts; |
---|
| 29 | |
---|
| 30 | private Player bestPlayer; |
---|
| 31 | private int bestPlayerScore; |
---|
| 32 | private String bestPlayerTeam; |
---|
| 33 | |
---|
| 34 | public League() { |
---|
| 35 | teams = new ArrayList<List<Player>>(); |
---|
| 36 | names = new ArrayList<String>(); |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | public void play() { |
---|
| 40 | score = new int[teams.size()]; |
---|
| 41 | wins = new int[teams.size()]; |
---|
| 42 | draws = new int[teams.size()]; |
---|
| 43 | losts = new int[teams.size()]; |
---|
| 44 | GameScenario scenario; |
---|
| 45 | OthelloGame game = new OthelloGame(new OthelloBoard()); |
---|
| 46 | |
---|
| 47 | bestPlayer = null; |
---|
| 48 | bestPlayerTeam = null; |
---|
| 49 | bestPlayerScore = 0; |
---|
| 50 | |
---|
| 51 | for (int team = 0; team < teams.size(); team++) { |
---|
| 52 | for (Player player : teams.get(team)) { |
---|
| 53 | int playerScore = 0; |
---|
| 54 | |
---|
| 55 | for (int opponentTeam = 0; opponentTeam < teams.size(); opponentTeam++) { |
---|
| 56 | if (team == opponentTeam) continue; |
---|
| 57 | |
---|
| 58 | for (Player opponent : teams.get(opponentTeam)) { |
---|
| 59 | scenario = new SimpleTwoPlayersGameScenario(new Player[] { player, opponent }); |
---|
| 60 | game.reset(); |
---|
| 61 | int result = scenario.play(game); |
---|
| 62 | score[team] += points(result); |
---|
| 63 | playerScore += points(result); |
---|
| 64 | if (result > 0) wins[team]++; |
---|
| 65 | if (result == 0) draws[team]++; |
---|
| 66 | if (result < 0) losts[team]++; |
---|
| 67 | |
---|
| 68 | scenario = new SimpleTwoPlayersGameScenario(new Player[] { opponent, player }); |
---|
| 69 | game.reset(); |
---|
| 70 | result = -scenario.play(game); |
---|
| 71 | score[team] += points(result); |
---|
| 72 | playerScore += points(result); |
---|
| 73 | if (result > 0) wins[team]++; |
---|
| 74 | if (result == 0) draws[team]++; |
---|
| 75 | if (result < 0) losts[team]++; |
---|
| 76 | } |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | if (playerScore > bestPlayerScore) { |
---|
| 80 | bestPlayerScore = playerScore; |
---|
| 81 | bestPlayer = player; |
---|
| 82 | bestPlayerTeam = names.get(team); |
---|
| 83 | } |
---|
| 84 | } |
---|
| 85 | } |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | private static int points(int result) { |
---|
| 89 | if (result > 0) { |
---|
| 90 | return 3; |
---|
| 91 | } else if (result == 0){ |
---|
| 92 | return 1; |
---|
| 93 | } else { |
---|
| 94 | return 0; |
---|
| 95 | } |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | public void printTable() { |
---|
| 99 | for (int team = 0; team < teams.size(); team++) { |
---|
| 100 | System.out.println(names.get(team) + " : " + score[team] + "\t\t wins = " + wins[team] + " draws = " + draws[team] + " losts = " + losts[team]); |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | private void addTeam(String name, String filename) { |
---|
| 105 | DoubleVectorIndividual ind = new DoubleVectorIndividual(); |
---|
| 106 | ind.fitness = new SimpleFitness(); |
---|
| 107 | ind.genome = new double[64]; |
---|
| 108 | |
---|
| 109 | try { |
---|
| 110 | List<Player> team = new ArrayList<Player>(); |
---|
| 111 | LineNumberReader reader = new LineNumberReader(new FileReader(filename)); |
---|
| 112 | Integer.parseInt(reader.readLine()); |
---|
| 113 | for (int i = 0; i < 20; i++) { |
---|
| 114 | ind.readIndividual(null, reader); |
---|
| 115 | team.add(new OthelloPlayer(ind.genome)); |
---|
| 116 | } |
---|
| 117 | teams.add(team); |
---|
| 118 | names.add(name); |
---|
| 119 | } catch (IOException e) { |
---|
| 120 | e.printStackTrace(); |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | private void printBestPlayer() { |
---|
| 125 | System.out.println("\n Best player comes from team " + bestPlayerTeam + " and its score = " + bestPlayerScore); |
---|
| 126 | |
---|
| 127 | double[] wpc = ((OthelloPlayer)bestPlayer).getWPC(); |
---|
| 128 | for (int i = 0; i < 64; i++) { |
---|
| 129 | if (i % 8 == 0) { |
---|
| 130 | System.out.println(""); |
---|
| 131 | } else { |
---|
| 132 | System.out.print(" "); |
---|
| 133 | } |
---|
| 134 | System.out.print(Math.round(wpc[i] * 100) / 100.); |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | public static void main(String[] args) { |
---|
| 139 | League league = new League(); |
---|
| 140 | league.addTeam("CEL", "EXP_CEL_best_players"); |
---|
| 141 | league.addTeam("CEL + HoF", "EXP5v0_best_players"); |
---|
| 142 | league.addTeam("TDL", "EXP3_best_players"); |
---|
| 143 | league.addTeam("CEL + TDL", "EXP4_best_players"); |
---|
| 144 | league.addTeam("CEL + TDL + HoF", "EXP5_best_players"); |
---|
| 145 | //league.addTeam("CEL + TDL(0) + HoF", "EXP5v0_best_players"); |
---|
| 146 | //league.addTeam("CEL + TDL(1)(rand) + HoF", "EXP5v1rand_best_players"); |
---|
| 147 | //league.addTeam("CEL + TDL(2) + HoF", "EXP5v2_best_players"); |
---|
| 148 | //league.addTeam("CEL + TDL(5) + HoF", "EXP5v5_best_players"); |
---|
| 149 | //league.addTeam("CEL + TDL(10) + HoF", "EXP5v10_best_players"); |
---|
| 150 | //league.addTeam("CEL + TDL(50) + HoF", "EXP5v50_best_players"); |
---|
| 151 | |
---|
| 152 | league.play(); |
---|
| 153 | league.printTable(); |
---|
| 154 | league.printBestPlayer(); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | } |
---|