def selectInitialO(self, board): game_rules.printBoard(board) pt = (0, 0, 0) # obviously not a valid location on a 2-D board validMoves = game_rules.getFirstMovesForO(board) while pt not in validMoves: pt = self._promptForPoint("Enter a valid starting location for player O (in the format 'row column'): ") return pt
def play(self): while self.state is not X_VICTORY and self.state is not O_VICTORY: if self.moves: move = self.moves.pop(0) print('playing scripted move', move) self._takeTurn(move) else: self._takeTurn() if self.verbose: game_rules.printBoard(self.board) self.log.close()
def test5(self): p1 = 'a' p2 = 'd' depth = 6 gm = self.makeGame(8, p1, p2, depth) signal.alarm(300) gm.play() game_rules.printBoard(gm.board) print(gm.GetWinner(), "WINS") self.assertEqual(gm.board, [['x', ' ', 'x', ' ', 'x', ' ', ' ', 'o'], [' ', ' ', ' ', 'x', ' ', ' ', ' ', 'x'], [' ', ' ', 'x', ' ', 'x', ' ', ' ', 'o'], ['o', ' ', ' ', ' ', ' ', 'x', ' ', 'x'], [' ', 'o', ' ', 'o', ' ', ' ', ' ', 'o'], ['o', ' ', 'o', ' ', ' ', ' ', 'o', 'x'], ['x', ' ', ' ', 'o', ' ', 'o', 'x', 'o'], ['o', 'x', 'o', 'x', 'o', 'x', 'o', 'x']])
def test1(self): p1 = 'm' p2 = 'd' depth = 2 gm = self.makeGame(4, p1, p2, depth) # set timeout signal.alarm(2) gm.play() game_rules.printBoard(gm.board) print(gm.GetWinner(), "WINS") self.assertEqual(gm.board, [['x', ' ', ' ', 'o'], [' ', 'x', ' ', 'x'], [' ', 'o', ' ', 'o'], ['o', 'x', 'o', 'x']])
def test4(self): # Uses script to have alternative start moves p1 = 'm' p2 = 'd' depth = 6 gm = self.makeGame(4, p1, p2, depth, 'game4.log') # set timeout signal.alarm(2) gm.play() game_rules.printBoard(gm.board) print(gm.GetWinner(), "WINS") self.assertEqual(gm.board, [['x', ' ', ' ', 'o'], ['o', ' ', ' ', 'x'], ['x', 'o', 'x', 'o'], ['o', ' ', ' ', 'x']])
def test1s(self): # Identical to test1 but totally run by the game1.log script # Minimax player never gets called. # Truncate game1.log to script the early game play and Minimax for rest p1 = 'm' p2 = 'd' depth = 2 gm = self.makeGame(4, p1, p2, depth, 'game1.log') # set timeout signal.alarm(2) gm.play() game_rules.printBoard(gm.board) print(gm.GetWinner(), "WINS") self.assertEqual(gm.board, [['x', ' ', ' ', 'o'], [' ', 'x', ' ', 'x'], [' ', 'o', ' ', 'o'], ['o', 'x', 'o', 'x']])
def test1(self): p1 = 'm' p2 = 'd' depth = 2 gm = self.makeGame(4, p1, p2, depth) pool = ThreadPool(processes=1) async_result = pool.apply_async(run_test, [gm]) result = None try: returned_value = async_result.get(300) print("Supposed to look like:") game_rules.printBoard([['x', ' ', ' ', 'o'], [' ', 'x', ' ', 'x'], [' ', 'o', ' ', 'o'], ['o', 'x', 'o', 'x']]) self.assertEqual(gm.board, [['x', ' ', ' ', 'o'], [' ', 'x', ' ', 'x'], [' ', 'o', ' ', 'o'], ['o', 'x', 'o', 'x']]) except Exception as e: self.fail('Timed out: {}'.format(e))
def test6s(self): # Identical to test6 but completely script driven p1 = 'a' p2 = 'd' depth = 6 gm = self.makeGame(8, p1, p2, depth, 'game6.log') signal.alarm(300) gm.play() game_rules.printBoard(gm.board) print(gm.GetWinner(), "WINS") self.assertEqual(gm.board, [['x', ' ', 'x', ' ', 'x', ' ', ' ', 'o'], [' ', ' ', ' ', 'x', ' ', ' ', ' ', 'x'], [' ', ' ', 'x', ' ', 'x', ' ', ' ', 'o'], ['o', ' ', ' ', ' ', ' ', 'x', ' ', 'x'], [' ', 'o', ' ', 'o', ' ', ' ', ' ', 'o'], ['o', ' ', 'o', ' ', ' ', ' ', 'o', 'x'], ['x', ' ', ' ', 'o', ' ', 'o', 'x', 'o'], ['o', 'x', 'o', 'x', 'o', 'x', 'o', 'x']])
def play(self): while self.state is not X_VICTORY and self.state is not O_VICTORY: self._takeTurn() if self.verbose: game_rules.printBoard(self.board)
def getMove(self, board): game_rules.printBoard(board) origin = self._promptForPoint("Choose a piece to move for %s (in the format 'row column'): " % self.symbol.capitalize()) destination = self._promptForPoint("Choose a destination for %s (%s, %s) -> " % (self.symbol.capitalize(), origin[0], origin[1])) return (origin, destination)
def run_test(gm): gm.play() game_rules.printBoard(gm.board) print(gm.GetWinner(), "WINS") return gm.board