def main(): run = True board = ttt.grid() i = 0 while(run): pygame.time.delay(100) grid(win) if(ttt.checkGameOver(board)): if(ttt.stalemate(board)): gameOver(win, "Now, nobody wins") if(i % 2 == 0): gameOver(win, "Blue Wins!!!") else: gameOver(win, "Red Wins!!!") for event in pygame.event.get(): if(event.type == pygame.QUIT): run = False if(event.type == pygame.MOUSEBUTTONDOWN): coords = getBlockCoords(pygame.mouse.get_pos()[1],pygame.mouse.get_pos()[0]) block = getBlock(pygame.mouse.get_pos()[1],pygame.mouse.get_pos()[0]) if(board[block[0]][block[1]] == [' ']): if(i % 2 == 0): circle(win, coords[1], coords[0]) board[block[0]][block[1]] = ['O'] else: cross(win, coords[1], coords[0]) board[block[0]][block[1]] = ['X'] i += 1 pygame.display.update() pygame.quit()
def test_checkStatus(self): newTest = ttt.grid() newTest.makeMark((1, 1), 'O') newTest.makeMark((0, 0), 'O') newTest.makeMark((-1, -1), 'O') self.assertEqual(newTest.checkStatus(), -1) newTest = ttt.grid() newTest.makeMark((1, 1), 'O') newTest.makeMark((0, 0), 'X') newTest.makeMark((-1, -1), 'O') newTest.makeMark((-1, 0), 'X') newTest.makeMark((1, 0), 'O') newTest.makeMark((1, -1), 'X') newTest.makeMark((-1, 1), 'O') newTest.makeMark((0, 1), 'X') newTest.makeMark((0, -1), 'O') self.assertEqual(newTest.checkStatus(), 0)
def test_findPotential(self): self.assertEqual(self.test_grid.findPotential(), (0, 0)) self.test_grid.makeMark((1, -1), 'X') self.test_grid.makeMark((0, 0), 'O') self.test_grid.makeMark((-1, 1), 'X') potentialPos = self.test_grid.findPotential() self.assertTrue(potentialPos in [(1, 0), (-1, 0), (0, -1), (0, 1)]) newTest = ttt.grid() newTest.makeMark((0, 0), 'X') newTest.makeMark((1, 1), 'O') newTest.makeMark((-1, -1), 'X') potentialPos = newTest.findPotential() self.assertTrue(potentialPos in [(-1, 1), (1, -1)])
def test_computerMove(self): self.test_grid.makeMark((1, -1), 'X') self.test_grid.makeMark((0, 0), 'O') self.test_grid.makeMark((-1, 1), 'X') self.test_grid.computerMove() lastCompMove = self.test_grid.marked[-1] self.assertTrue(lastCompMove in [(1, 0), (-1, 0), (0, -1), (0, 1)]) newTest = ttt.grid() newTest.makeMark((0, 0), 'X') newTest.makeMark((1, 1), 'O') newTest.makeMark((-1, 0), 'X') newTest.computerMove() lastCompMove = newTest.marked[-1] self.assertEqual(lastCompMove, (1, 0))
def setUp(self): self.test_grid = ttt.grid()
import ttt, collections """ AI Algorithm has 3 cases: 1. Computer has 2/3 in a row. 2. Player has 2/3 in a row. 3. None of the above. """ if __name__ == '__main__': TicTacToe = ttt.grid() print "Do you want to go first?" start = raw_input("[N/y]: ") print "The computer's mark will be \'O\'." status = None if start == 'y': last_move = 'computer' else: last_move = 'player' while status is None: if last_move == 'player': last_move = 'computer' else: last_move = 'player' TicTacToe.step(move=last_move) status = TicTacToe.checkStatus() if status == -1: TicTacToe.showGrid() print 'You lose.' break elif status == 1: