def test11(): print "Launching test11. It may take time. Begin with small number of N (number of copies of the game)" checker = 1 HUMAN, MACHINE = 1, 2 t = time.time() game = Game(5, 5, MACHINE, 4) game.mark(MACHINE, (2, 1)) game.mark(HUMAN, (2, 2)) game.mark(MACHINE, (1, 2)) game.mark(HUMAN, (0, 3)) game.mark(MACHINE, (1, 1)) game.mark(HUMAN, (1, 0)) nextStep = game.decideNextStep(MACHINE, Game.algo1) print "Next step chosen by machine: ", nextStep game.mark(MACHINE, nextStep) game.draw() if nextStep != (3, 1): print "Expected: ", (3, 1) print "Received: ", nextStep print "Please increase the number of copies, or review your implementarion." checker = 0 time_taken = time.time() - t print "Learning with algo 1 took ", time_taken, " seconds." if checker == 1: print "Test 11 OK" else: print "Test 11 not OK" return time_taken
def test9(): X, O = 1, 2 game = Game(5, 5, O, 4) checker = 1 game.mark(O, (2, 1)) game.mark(X, (2, 2)) game.mark(O, (1, 1)) game.mark(X, (0, 3)) game.mark(O, (3, 1)) cell_list = [] for i in range(20): cell = game.getBoard().getARandomEmptyCell() if cell not in cell_list: cell_list.append(cell) if len(cell_list) <= 1: print "getARandomEmptyCell() not OK. It is not random." checker = 0 if (2, 1) in cell_list or (2, 2) in cell_list or (1, 1) in cell_list or (0, 3) in cell_list or (3, 1) in cell_list: print "getARandomEmptyCell() not OK. Some non-empty cell was chosen: (2, 1) or (2, 2) or (1, 1) or (0, 3) or (3, 1)" checker = 0 game.mark(X, (4, 0)) game.mark(O, (4, 1)) if game.evaluateLastStepRandomly(O) != 1: print "evaluateLastStepRandomly() not OK. It should be 1. Received: ", game.evaluateLastStepRandomly(O) game.draw() if checker == 1: print "Test 9 OK" else: print "Test 9 not OK"
def test4(): game = Game(5, 5, 2, 4) checker = 1 if game.getBoard().getWidth() != 5 or game.getBoard().getHeight() != 5: print "getBoard() not OK. Please check" checker = 0 if not game.isActive(): print "isActive() not OK." print "Expected: True" print "Received: ", game.isActive() checker = 0 if game.getTurn() != 2: print "getTurn() not OK." print "Expected: 2" print "Received: ", game.getTurn() checker = 0 if game.getWinner() != 0: print "getWinner() not OK." print "Expected: 0" print "Received: ", game.getWinner() checker = 0 game.deactivate() if game.isActive(): print "isActive() or deactivate() not OK." print "Expected: False" print "Received: ", game.isActive() checker = 0 game.activate() if not game.isActive(): print "isActive() or activate() not OK." print "Expected: True" print "Received: ", game.isActive() checker = 0 game.switchTurn() if game.getTurn() != 1: print "switchTurn() not OK." print "Expected: 1" print "Received: ", game.getTurn() checker = 0 game.declareWinner(2) if game.getWinner() != 2: print "declareWinner() not OK." print "Expected: 2" print "Received: ", game.getWinner() checker = 0 game.draw() if checker == 1: print "Test 4 OK" else: print "Test 4 not OK"
def test7(): game = Game(5, 5, 2, 4) checker = 1 game.getBoard().setBoardStatus([[0, 0, 0, 0, 0], [0, 0, 1, 1, 2], [0, 0, 0, 2, 0], [0, 0, 2, 1, 0], [0, 2, 1, 0, 0]]) if game.isFull(): game.draw() print "isFull() not OK." print "Expected: False" print "Received: ", game.isFull() checker = 0 game.draw() game = Game(5, 5, 2, 4) game.getBoard().setBoardStatus([[1, 2, 1, 2, 2], [2, 1, 1, 1, 2], [2, 1, 1, 2, 1], [2, 2, 2, 1, 1], [1, 2, 1, 1, 2]]) if not game.isFull(): game.draw() print "isFull() not OK." print "Expected: True" print "Received: ", game.isFull() checker = 0 game.draw() if checker == 1: print "Test 7 OK" else: print "Test 7 not OK"
def test13(): print "Launch test11 again for comparison" time_algo1 = test11() print "Launching test13. It may take time." checker = 1 HUMAN, MACHINE = 1, 2 t = time.time() game = Game(5, 5, MACHINE, 4) game.mark(MACHINE, (2, 1)) game.mark(HUMAN, (2, 2)) game.mark(MACHINE, (1, 2)) game.mark(HUMAN, (0, 3)) game.mark(MACHINE, (1, 1)) game.mark(HUMAN, (1, 0)) nextStep = game.decideNextStep(MACHINE, Game.algo3) print "Next step chosen by machine: ", nextStep game.mark(MACHINE, nextStep) game.draw() if nextStep != (3, 1): print "Expected: ", (3, 1) print "Received: ", nextStep print "Please increase the number of copies, or review your implementarion." checker = 0 time_taken = time.time() - t print "Learning with algo 3 took ", time_taken, " seconds." if time_taken > time_algo1: print "Warning: the algo is implemented but something wrong for runtime. It should run in less than time for algo1." else: print "Algo 3 run in ", round(time_taken / time_algo1, 3)*100, "% of time needed of algo 1." if checker == 1: print "Test 13 OK" else: print "Test 13 not OK"
def test10(): X, O = 1, 2 game = Game(5, 5, O, 4) checker = 1 game.mark(O, (2, 1)) game.mark(X, (2, 2)) game.mark(O, (1, 2)) game.mark(X, (0, 3)) game.mark(O, (1, 1)) copy_game = game.generateGameCopy() if game == copy_game: print "generateGameCopy() not OK. Expected: a copy, received: the instance itself." checker = 0 if game.getBoard() == copy_game.getBoard(): print "generateGameCopy() not OK. The copy and the main game are sharing the same board. Please use another board for the copy." checker = 0 if not copy_game.isActive(): print "generateGameCopy() not OK. It is not active while the main game is." checker = 0 if copy_game.getTurn() != game.getTurn(): print "generateGameCopy() not OK. Please review __turn." checker = 0 if copy_game.getWinner() != game.getWinner(): print "generateGameCopy() not OK. Please review __winner." checker = 0 if copy_game.getBoard().getBoardStatus() != game.getBoard().getBoardStatus(): print "generateGameCopy() not OK. The board status are not the same." print "Expected: ", game.getBoard().getBoardStatus() print "Received: ", copy_game.getBoard().getBoardStatus() checker = 0 game.draw() if checker == 1: print "Test 10 OK" else: print "Test 10 not OK"
def test8(): game = Game(5, 5, 2, 4) checker = 1 game.getBoard().setBoardStatus([[0, 0, 0, 0, 0], [0, 0, 1, 1, 2], [0, 0, 0, 2, 0], [0, 0, 0, 1, 0], [0, 2, 1, 0, 0]]) if game.getTurn() != 2: game.switchTurn() game.mark(2, (3, 2)) game.draw() if game.isActive(): print "mark() not OK. You may have forgotten to deactivate the game." print "Expected: Status active = True" print "Received: Status active = ", game.isActive() checker = 0 if game.getWinner() != 2: print "mark() not OK. You may have forgotten to declare the winner." print "Expected: Winner 2" print "Received: Winner ", game.getWinner() game = Game(5, 5, 2, 4) checker = 1 game.getBoard().setBoardStatus([[0, 2, 1, 2, 2], [2, 1, 1, 1, 2], [1, 1, 2, 2, 1], [2, 2, 1, 1, 1], [1, 2, 1, 1, 2]]) if game.getTurn() != 1: game.switchTurn() game.mark(1, (0, 0)) game.draw() if game.isActive(): print "mark() not OK. You may have forgotten to deactivate the game." print "Expected: Status active = True" print "Received: Status active = ", game.isActive() checker = 0 if game.getWinner() != 0: print "mark() not OK. The game should have no winner." print "Expected: Winner 0" print "Received: Winner ", game.getWinner() game = Game(5, 5, 2, 4) checker = 1 game.getBoard().setBoardStatus([[0, 0, 0, 0, 0], [0, 0, 1, 1, 2], [0, 0, 0, 2, 0], [0, 0, 0, 1, 0], [0, 2, 1, 0, 0]]) if game.getTurn() != 2: game.switchTurn() game.mark(2, (3, 4)) game.draw() if not game.isActive(): print "mark() not OK. " print "Expected: Status active = True" print "Received: Status active = ", game.isActive() checker = 0 if game.getWinner() != 0: print "mark() not OK. The game should have no winner." print "Expected: Winner 0" print "Received: Winner ", game.getWinner() if game.getTurn() != 1: print "mark() not OK. You may have forgotten to switch the turn." print "Expected: Turn of 1" print "Received: Turn of ", game.getTurn() if checker == 1: print "Test 8 OK" else: print "Test 8 not OK"
def test6(): game = Game(5, 5, 2, 4) checker = 1 game.getBoard().setBoardStatus([[0, 0, 2, 0, 0], [2, 1, 0, 0, 0], [0, 1, 0, 0, 0], [0, 1, 0, 2, 0], [0, 1, 0, 0, 2]]) if not game.isVictoryCell((3, 1)): print "isVictoryCell() not OK." game.draw() print "(3, 1) should be a victory cell." checker = 0 if game.isVictoryCell((0, 4)): print "isVictoryCell() not OK." game.draw() print "(0, 4) should not be a victory cell." checker = 0 game.draw() game = Game(5, 5, 2, 4) game.getBoard().setBoardStatus([[0, 0, 2, 0, 0], [2, 1, 1, 1, 1], [0, 0, 0, 0, 0], [0, 0, 0, 2, 0], [0, 0, 0, 0, 2]]) if not game.isVictoryCell((1, 4)): print "isVictoryCell() not OK." game.draw() print "(1, 4) should be a victory cell." checker = 0 if game.isVictoryCell((2, 0)): print "isVictoryCell() not OK." game.draw() print "(2, 0) should not be a victory cell." checker = 0 game.draw() game = Game(5, 5, 2, 4) game.getBoard().setBoardStatus([[0, 0, 2, 0, 0], [1, 2, 1, 1, 1], [0, 0, 2, 0, 0], [0, 0, 0, 2, 0], [0, 0, 0, 0, 2]]) if not game.isVictoryCell((3, 3)): print "isVictoryCell() not OK." game.draw() print "(3, 3) should be a victory cell." checker = 0 if game.isVictoryCell((0, 0)): print "isVictoryCell() not OK." game.draw() print "(0, 0) should not be a victory cell." checker = 0 game.draw() game = Game(5, 5, 2, 4) game.getBoard().setBoardStatus([[0, 0, 0, 0, 0], [0, 0, 1, 1, 2], [0, 0, 0, 2, 0], [0, 0, 2, 1, 0], [0, 2, 1, 0, 0]]) if not game.isVictoryCell((4, 1)): print "isVictoryCell() not OK." game.draw() print "(4, 1) should be a victory cell." checker = 0 if game.isVictoryCell((2, 2)): print "isVictoryCell() not OK." game.draw() print "(2, 2) should not be a victory cell." checker = 0 game.draw() if checker == 1: print "Test 6 OK" else: print "Test 6 not OK"
def test5(): print "Creating a game 5x5. Player O go first." game = Game(5, 5, 2, 4) checker = 1 try: print "Player X wants to mark at (0, 0)" game.mark(1, (0, 0)) except: pass if game.getBoard().getCellStatus((0, 0)) == 1: print "mark() not OK." print "Player X should not have right to mark, as it's not his turn." checker = 0 try: print "Player O marks at (1, 1)" game.mark(2, (1, 1)) game.draw() except: print "mark() not OK. Please check your code." return if game.getBoard().getCellStatus((1, 1)) != 2: print "mark() not OK. Please check your code." checker = 0 try: print "Player X wants to mark at (1, 1)" game.mark(1, (1, 1)) except: pass if game.getBoard().getCellStatus((1, 1)) == 1: print "mark() not OK." print "Player X should not have right to mark, as the cell is already occupied." checker = 0 print "Try deactivating the game" game.deactivate() try: print "Player X wants to mark at (1, 2)" game.mark(1, (1, 2)) except: pass if game.getBoard().getCellStatus((1, 2)) == 1: print "mark() not OK." print "Player X should not have right to mark, as the game has finished." checker = 0 print "Reactivating the game" game.activate() if game.getTurn() == 2: game.switchTurn() try: print "Player X wants to mark at (1, 2)" game.mark(1, (1, 2)) game.draw() except: pass if game.getBoard().getCellStatus((1, 2)) != 1: print "mark() not OK. Please check your code." checker = 0 if checker == 1: print "Test 5 OK" else: print "Test 5 not OK"