001 /* 002 * Created on Feb 15, 2005 003 * 004 */ 005 package aima.test.gametest; 006 007 import java.util.ArrayList; 008 009 import junit.framework.TestCase; 010 import aima.games.AlphaBeta; 011 import aima.games.GameState; 012 import aima.games.TicTacToe; 013 import aima.games.TicTacToeBoard; 014 015 /** 016 * @author Ravi Mohan 017 * 018 */ 019 public class TicTacToeTest extends TestCase { 020 021 private TicTacToeBoard tb; 022 023 public TicTacToeTest(String name) { 024 super(name); 025 } 026 027 @Override 028 public void setUp() { 029 tb = new TicTacToeBoard(); 030 } 031 032 public void testCreation() { 033 TicTacToe t3 = new TicTacToe(); 034 assertEquals(9, t3.getMoves(t3.getState()).size()); 035 assertEquals("X", t3.getPlayerToMove(t3.getState())); 036 } 037 038 public void testOnCreationBoardIsEmpty() { 039 040 assertEquals(true, tb.isEmpty(0, 0)); 041 assertEquals(true, tb.isEmpty(0, 2)); 042 assertEquals(true, tb.isEmpty(2, 0)); 043 assertEquals(true, tb.isEmpty(2, 2)); 044 } 045 046 public void testMovingBumpsLevelByOne() { 047 TicTacToe t1 = new TicTacToe(); 048 int level = t1.getLevel(t1.getState()); 049 assertEquals(0, level); 050 t1.makeMove(0, 0); 051 level = t1.getLevel(t1.getState()); 052 assertEquals(1, level); 053 } 054 055 public void testMarkingAsSquareMakesItNonEmpty() { 056 tb.markX(0, 0); 057 assertEquals(false, tb.isEmpty(0, 0)); 058 } 059 060 public void testAllCombinationsOfSuccessLines() { 061 tb.markX(0, 0); 062 tb.markX(0, 1); 063 tb.markX(0, 2); 064 assertEquals(true, tb.isAnyRowComplete()); 065 tb.markO(0, 2); 066 assertEquals(false, tb.isAnyRowComplete()); 067 tb.markO(1, 2); 068 tb.markO(2, 2); 069 assertEquals(true, tb.isAnyColumnComplete()); 070 tb.markX(2, 2); 071 tb.markX(1, 1); 072 assertEquals(true, tb.isAnyDiagonalComplete()); 073 tb.markO(1, 1); 074 tb.markO(2, 0); 075 assertEquals(true, tb.isAnyDiagonalComplete()); 076 // tb.print(); 077 } 078 079 public void testGivenABoardNUmberOfPossibleMovesCalculatedCorrectly() { 080 TicTacToe t3 = new TicTacToe(); 081 assertEquals(9, t3.getMoves(t3.getState()).size()); 082 083 assertEquals("X", t3.getPlayerToMove(t3.getState())); 084 t3.makeMove(t3.getState(), 0, 0); 085 086 assertEquals(8, t3.getMoves(t3.getState()).size()); 087 assertEquals("O", t3.getPlayerToMove(t3.getState())); 088 089 // t3.getBoard().print(); 090 // try illegal move 091 t3.makeMove(t3.getState(), 0, 0); 092 assertEquals(8, t3.getMoves(t3.getState()).size()); 093 assertEquals("O", t3.getPlayerToMove(t3.getState())); 094 // t3.printPossibleMoves(); 095 096 t3.makeMove(t3.getState(), 1, 1); 097 assertEquals(7, t3.getMoves(t3.getState()).size()); 098 assertEquals("X", t3.getPlayerToMove(t3.getState())); 099 100 } 101 102 public void testCalculateUtilityOfABoard() { 103 104 // game 1 105 TicTacToe t3 = new TicTacToe(); 106 t3.makeMove(t3.getState(), 0, 0); 107 assertEquals(0, t3.getUtility(t3.getState())); 108 t3.makeMove(t3.getState(), 2, 2); 109 assertEquals(0, t3.getUtility(t3.getState())); 110 t3.makeMove(t3.getState(), 2, 0); 111 assertEquals(0, t3.getUtility(t3.getState())); 112 t3.makeMove(t3.getState(), 1, 0); 113 assertEquals(0, t3.getUtility(t3.getState())); 114 t3.makeMove(t3.getState(), 0, 2); 115 assertEquals(0, t3.getUtility(t3.getState())); 116 t3.makeMove(t3.getState(), 0, 1); 117 assertEquals(0, t3.getUtility(t3.getState())); 118 t3.makeMove(t3.getState(), 1, 1); 119 assertEquals(1, t3.computeUtility((t3.getState()))); 120 121 // t3.getBoard().print(); 122 123 // game 2 124 TicTacToe t4 = new TicTacToe(); 125 t4.makeMove(t4.getState(), 0, 0); 126 assertEquals(0, t4.getUtility(t4.getState())); 127 t4.makeMove(t4.getState(), 0, 1); 128 assertEquals(0, t4.getUtility(t4.getState())); 129 t4.makeMove(t4.getState(), 0, 2); 130 assertEquals(0, t4.getUtility(t4.getState())); 131 t4.makeMove(t4.getState(), 1, 0); 132 assertEquals(0, t4.getUtility(t4.getState())); 133 t4.makeMove(t4.getState(), 1, 1); 134 assertEquals(0, t4.getUtility(t4.getState())); 135 t4.makeMove(t4.getState(), 1, 2); 136 assertEquals(0, t4.getUtility(t4.getState())); 137 t4.makeMove(t4.getState(), 2, 0); 138 // t4.getBoard().print(); 139 assertEquals(1, t4.computeUtility(t4.getState())); 140 } 141 142 /* 143 * public void testMinimumValue(){ TicTacToe t3 = new TicTacToe(); 144 * t3.makeMove(2,2); int i = t3.minValue(t3.getState()); 145 * System.out.println("i = " + i); 146 * 147 * TicTacToe t4 = new TicTacToe(); t4.makeMove(2,1); int j = 148 * t4.minValue(t4.getState()); System.out.println("j = " + j); 149 * assertEquals(-1,i); assertEquals(-1,j); } 150 */ 151 public void testGenerateSuccessors() { 152 TicTacToe t3 = new TicTacToe(); 153 ArrayList successors = t3.getSuccessorStates(t3.getState()); 154 assertEquals(9, successors.size()); 155 checkSuccessorList(successors, "O", 8); 156 ArrayList successors2 = t3.getSuccessorStates((GameState) successors 157 .get(0)); 158 checkSuccessorList(successors2, "X", 7); 159 ArrayList successors3 = t3.getSuccessorStates((GameState) successors2 160 .get(0)); 161 checkSuccessorList(successors3, "O", 6); 162 163 // System.out.println("done"); 164 } 165 166 public void testGameStateEquality() { 167 TicTacToeBoard tb1 = new TicTacToeBoard(); 168 TicTacToeBoard tb2 = new TicTacToeBoard(); 169 GameState gs1 = new GameState(); 170 GameState gs2 = new GameState(); 171 gs1.put("board", tb1); 172 gs2.put("board", tb2); 173 assertEquals(gs1, gs2); 174 gs1.put("minimaxValue", new Integer(3)); 175 assertTrue(!(gs1.equals(gs2))); 176 177 } 178 179 public void testMiniMax() { 180 TicTacToe t3 = new TicTacToe(); 181 t3.makeMove(0, 0); 182 t3.makeMove(2, 2); 183 t3.makeMove(2, 0); 184 t3.makeMove(1, 1); 185 assertEquals(1, t3.getMiniMaxValue(t3.getState())); 186 187 } 188 189 public void testMiniMax2() { 190 TicTacToe t3 = new TicTacToe(); 191 t3.makeMove(0, 0); 192 t3.makeMove(2, 2); 193 t3.makeMove(2, 0); 194 195 assertEquals(1, t3.getMiniMaxValue(t3.getState())); 196 197 } 198 199 public void testMiniMax3() { 200 TicTacToe t3 = new TicTacToe(); 201 t3.makeMove(0, 0); 202 t3.makeMove(1, 1); 203 204 assertEquals(0, t3.getMiniMaxValue(t3.getState())); 205 206 } 207 208 public void testMiniMax7() { 209 TicTacToe t3 = new TicTacToe(); 210 t3.makeMove(0, 0); 211 t3.makeMove(0, 1); 212 213 assertEquals(1, t3.getMiniMaxValue(t3.getState())); 214 215 } 216 217 public void tesMiniMax4() { 218 TicTacToe t3 = new TicTacToe(); 219 220 assertEquals(0, t3.getMiniMaxValue(t3.getState())); 221 222 } 223 224 public void testTerminalStateDetection() { 225 TicTacToe t3 = new TicTacToe(); 226 t3.makeMove(0, 0); 227 t3.makeMove(0, 1); 228 t3.makeMove(0, 2); 229 t3.makeMove(1, 0); 230 t3.makeMove(1, 1); 231 t3.makeMove(1, 2); 232 // assertEquals(true,t3.terminalTest(t3.getState())); 233 assertEquals(1, t3.getMiniMaxValue(t3.getState())); 234 } 235 236 public void testMiniMax15() { 237 238 TicTacToe t1 = new TicTacToe(); 239 t1.makeMove(0, 0); 240 241 int minimax1 = t1.getMiniMaxValue(t1.getState()); 242 TicTacToe t2 = new TicTacToe(); 243 t2.makeMove(0, 2); 244 int minimax2 = t2.getMiniMaxValue(t2.getState()); 245 TicTacToe t3 = new TicTacToe(); 246 t3.makeMove(2, 0); 247 int minimax3 = t3.getMiniMaxValue(t3.getState()); 248 TicTacToe t4 = new TicTacToe(); 249 int minimax4 = t4.getMiniMaxValue(t4.getState()); 250 t4.makeMove(2, 2); 251 // System.out.println(minimax1 + " " +minimax2+ " " +minimax3 + " "+ 252 // minimax4+ " "); 253 assertEquals(minimax1, minimax2); 254 255 } 256 257 public void testMiniMax6() { 258 TicTacToe t3 = new TicTacToe(); 259 t3.makeMove(0, 0); 260 t3.makeMove(0, 1); 261 t3.makeMove(0, 2); 262 263 t3.makeMove(2, 2); 264 t3.makeMove(1, 2); 265 266 t3.makeMove(2, 1); 267 t3.makeMove(1, 1); 268 assertEquals(-1, t3.getMiniMaxValue(t3.getState())); 269 270 } 271 272 public void testAlphaBeta() { 273 TicTacToe t1 = new TicTacToe(); 274 int alphabeta0 = t1.getAlphaBetaValue(t1.getState()); 275 assertEquals(0, alphabeta0); 276 t1.makeMove(0, 0); 277 278 // System.out.println(t1.getLevel(t1.getState())); 279 int alphabeta1 = t1.getAlphaBetaValue(t1.getState()); 280 assertEquals(0, alphabeta1); 281 TicTacToe t2 = new TicTacToe(); 282 t2.makeMove(0, 2); 283 int alphabeta2 = t2.getAlphaBetaValue(t2.getState()); 284 assertEquals(0, alphabeta2); 285 286 TicTacToe t3 = new TicTacToe(); 287 t3.makeMove(2, 0); 288 int alphabeta3 = t3.getAlphaBetaValue(t3.getState()); 289 assertEquals(0, alphabeta3); 290 291 TicTacToe t4 = new TicTacToe(); 292 int alphabeta4 = t4.getAlphaBetaValue(t4.getState()); 293 t4.makeMove(2, 2); 294 // System.out.println(alphabeta1 + " " +alphabeta2+ " " +alphabeta3 + " 295 // "+ alphabeta4+ " "); 296 assertEquals(0, alphabeta4); 297 298 } 299 300 private void checkSuccessorList(ArrayList successorList, 301 String playerToMove, int sizeOfSuccessors) { 302 for (int i = 0; i < successorList.size(); i++) { 303 GameState h = (GameState) successorList.get(i); 304 305 ArrayList successors2 = new TicTacToe().getSuccessorStates(h); 306 assertEquals(sizeOfSuccessors, successors2.size()); 307 assertEquals(playerToMove, new TicTacToe().getPlayerToMove(h)); 308 } 309 } 310 311 public void testAlphaBetaMinValueCalculation() { 312 // board x o x , o o x,- x - 313 TicTacToe t = new TicTacToe(); 314 315 t.makeMove(0, 0); // x 316 t.makeMove(0, 1); // o 317 t.makeMove(0, 2); // x 318 319 t.makeMove(1, 0); // o 320 t.makeMove(1, 2); // x 321 t.makeMove(1, 1); // o 322 323 t.makeMove(2, 1); // x 324 325 int minValue = t.minValue(t.getState(), new AlphaBeta( 326 Integer.MIN_VALUE, Integer.MAX_VALUE)); 327 assertEquals(0, minValue); 328 329 } 330 331 }