def test_single_minimax_minimax(): player0 = game.MinimaxPlayer(name="hans") player1 = game.MinimaxPlayer(name="robert") ttt = game.TicTacToe(player0, player1) ttt.play()
def test_single_human_alphabeta(): player0 = game.HumanPlayer(name="hans") player1 = game.AlphabetaPlayer(name="robert") ttt = game.TicTacToe(player0, player1) ttt.play()
def main(): board = game.TicTacToe() board.current_player = random.choice((0, 1)) if board.current_player is 0: board.current_player = board.players[0] print("A.I plays first.\nThinking...") minimax.bestMove(board) else: board.current_player = board.players[1] print("Human plays first. Please select your move.") board.window.mainloop()
def run(): """ Runs the game loop for the GUI version """ root = Tk() root.title("TicTacToe") root.config(bg='black') root.resizable(width=False, height=False) # Get player selection inputDialog = PlayerSelect(root) root.wait_window(inputDialog) playerpick = inputDialog.selection tttgame = game.TicTacToe(pick=playerpick) TicTacToeUI(root, tttgame) root.mainloop()
def console(): """ Game loop interface for console. """ print(title()) print('~' * 40) print('select your symbol: (x/o)') while True: s = input(':>') if s == 'x' or s == 'o': break ttt = game.TicTacToe(size=3, pick=s) print('Player {} goes first'.format(ttt.player)) print('~' * 40) # Game loop while ttt.playing: if ttt.player == game.HUMAN: print('Go human...') print(ttt.b) while True: move = input('>') if ttt.b.play(int(move), ttt.PLAYERS[game.HUMAN]): break else: print('invalid move, try again') # If it is the computer's turn, let them compute the best move elif ttt.player == game.CPU: # Run computer turn print('CPU is thinking...') cpu_move = ttt.cpu_turn() print('The CPU played on spot {}'.format(cpu_move)) else: print('Player selection error! Aborting!!!!!') exit() state = ttt.check_state() if state: print(state) input() ttt.next_player()
def test_genius_vs_genius(self): # No one should ever win tie = 0 num_of_repetitions = 300 for i in range(num_of_repetitions): ttc = game.TicTacToe() genius_player1 = game.GeniusComputerPlayer('X', False) genius_player2 = game.GeniusComputerPlayer('O', False) # Get the winner letter winner = game.play(ttc, genius_player1, genius_player2, False) # Update variables, or fail if necessary if winner is None: tie += 1 else: self.fail('Someone has won.') self.assertEqual(num_of_repetitions, tie, f'{num_of_repetitions} games should have been played. Instead, {tie} were played.')
def run(self): game = Game.TicTacToe() alg = Algorithm.Algorithm() print('Welcome to TicTacToe') while True: game.printGame() breaker = False while not breaker: moveStr = input( '\nMake your move! ex) "1 3" where 1 is row and 3 is column.\n' ) move = (int(moveStr[0]), int(moveStr[2])) print('You chose ({0}, {1})!'.format(move[0], move[1])) if game.makeMove(move): breaker = True else: print('Invalid move!') game.printGame() if game.gameOver(): break print('\nAI is thinking...') bestMove = alg.findBestMove(game.getGameState(), game.getIndex(), -1) print('AI chose ({0}, {1})!'.format(bestMove[0], bestMove[1])) game.makeMove(bestMove) if game.gameOver(): break if game.getWinner() == 1: print("You've won!") elif game.getWinner() == -1: print("You've lost!") else: print('Game tied!')
def test_genius_vs_random(self): # Random should never win genius_win = 0 tie = 0 num_of_repetitions = 3000 for i in range(num_of_repetitions): ttc = game.TicTacToe() genius_player = game.GeniusComputerPlayer('X', False) random_player = game.RandomComputerPlayer('O', False) # Get the winner letter winner = game.play(ttc, genius_player, random_player, False) # Update variables, or fail if necessary if winner == 'X': genius_win += 1 elif winner == 'O': self.fail('Random player won.') elif winner is None: tie += 1 self.assertEqual(num_of_repetitions, genius_win + tie, f'{num_of_repetitions} games should have been played. Instead, {genius_win + tie} were played.')
"""Module with showcase of tictactoe game""" import game as g game = g.TicTacToe() game.show_menu()
import pytest import game game_instance = game.TicTacToe() game_instance.board[4] = 'X' def test_available_moves(): assert 4 not in game_instance.available_moves() assert 3 in game_instance.available_moves() def test_make_move(): assert game_instance.make_move(square=4, letter='X') is False assert game_instance.make_move(square=5, letter='X') is True assert (game_instance.board[5] != " ") is True if __name__ == "__main__": test_available_moves() test_make_move()