def restart(self): """ Handles game restart and reconstruction. """ self.game = TicTacToe() self.win_lose_label.destroy() self.canvas.destroy() self.draw_board()
def testCurrentPlayerIsO(self): game = TicTacToe() game.changePlayer() if game.currentPlayer() == 'O': result = True else: result = False self.assertEqual(result, True)
def test_4_by_4_diagonal_3(self): game = TicTacToe(4, 4, 3) game.playX(1, 1) self.assertFalse(game.isFinished()) game.playX(2, 2) self.assertFalse(game.isFinished()) game.playX(3, 3) self.assertTrue(game.isFinished())
def test_3_by_3_horizontal(self): game = TicTacToe() game.playX(0, 0) self.assertFalse(game.isFinished()) game.playX(1, 0) self.assertFalse(game.isFinished()) game.playX(2, 0) self.assertTrue(game.isFinished())
def test_4_by_4_vertical(self): game = TicTacToe(4, 4, 3) game.playX(2, 1) self.assertFalse(game.isFinished()) game.playX(2, 2) self.assertFalse(game.isFinished()) game.playX(2, 3) self.assertTrue(game.isFinished())
def test_3_by_3_diagonal_2(self): game = TicTacToe() game.playX(0, 2) self.assertFalse(game.isFinished()) game.playX(1, 1) self.assertFalse(game.isFinished()) game.playX(2, 0) self.assertTrue(game.isFinished())
def test_3_by_3_vertical_right(self): game = TicTacToe() game.playX(2, 0) self.assertFalse(game.isFinished()) game.playX(2, 1) self.assertFalse(game.isFinished()) game.playX(2, 2) self.assertTrue(game.isFinished())
def main(): # Instantiate a root window root = tkinter.Tk() # Instantiate a Game object gen_game = GUI(root, TicTacToe()) # Enter the main event loop root.mainloop()
async def start_game(client, channel, user): game = TicTacToe() # user will play the game through a message and its discord-reacts. empty_board = str(game) game_message = await channel.send(empty_board) for square in game.board: await game_message.add_reaction(square) # start game loop await get_user_reaction(client, channel, user, game, game_message)
def test_5_by_5_bug(self): game = TicTacToe(5, 5, 4) game.playX(0, 0) game.playO(2, 1) game.playX(1, 1) game.playO(2, 3) game.playX(2, 2) print(game.get_pretty_board) self.assertFalse(game.isFinished())
def test_game1(self): game = TicTacToe() game.put_choice(1, 1) game.put_choice(2, 2) game.put_choice(1, 5) game.put_choice(2, 7) game.put_choice(1, 9) game.put_choice(2, 4) self.assertEqual(game.status_of_the_match(), 1)
def test_naught_win(): game = TicTacToe() game.place_marker(0, 0) game.place_marker(0, 2) game.place_marker(0, 1) game.place_marker(1, 1) game.place_marker(1, 0) game.place_marker(2, 0) assert game.state == STATES.NAUGHT_WON
def test_4_by_4_bug(self): game = TicTacToe(4, 4, 3) game.playX(3, 2) game.playO(1, 2) game.playX(0, 1) game.playO(2, 0) game.playX(2, 3) print(game.get_pretty_board) self.assertFalse(game.isFinished())
def test_look_other_player(self): self.learner = Learner() self.other_player = RandomPlayer() tictactoe = TicTacToe() action = tictactoe.available_moves()[0] tictactoe.play(action[0], action[1], self.other_player) self.learner.look(tictactoe) self.assertEqual(1, len(self.learner.history[self.other_player.name])) self.assertEqual(action, self.learner.history[self.other_player.name][0])
def game_request(self, player): concurrent_print("Received game request from %s" % (player)) player1, player2 = self.game_requests.pushpop2(player) if player1 and player2 and not player1.in_game( ) and not player2.in_game(): game = TicTacToe(N) player2.start_game('o', player1, game) player1.start_game('x', player2, game) concurrent_print("Started game between %s and %s" % (player1, player2))
def test_restart(): game = TicTacToe() game.place_marker(0, 0) game.place_marker(0, 2) game.place_marker(0, 1) game.place_marker(1, 1) game.place_marker(1, 0) game.place_marker(2, 0) game.restart() assert game.board == [[' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' ']]
def play_game(player_token): agent = Agent(switch_token(player_token)) game = TicTacToe([' '] * 9) while not game.over(): play_turn(agent, game) print(game) if game.won(): winner = game.winner() print(f"Better luck next time! {winner} wins!\n") else: print("Game ends in a draw!\n")
def main(): V = View() Tic = TicTacToe() isQuit = False isPlayerturn = True while (isQuit == False): Tic.resetBoard() V.resetBoardView() #gameturnLoop while not (Tic.isBoardFull()): move = -1 if (isPlayerturn): if not (Tic.isWinner('O')): # if comp has not won while (move == -1): mouse_pos = V.getPlayerResponse() box_index = V.getClickedBox(mouse_pos) if V.isBoxClicked( box_index): # if the player has clicked a box move = Tic.playerMove( box_index) # check if that box is available V.drawImage(move, V.player_img) isPlayerturn = not isPlayerturn else: V.set_status_bar_text_and_countdown( "Sorry, Computer's won this time!", 'red') break if not (Tic.isWinner('X')): # if player has not won move = Tic.computeMove() if move == -1: V.set_status_bar_text_and_countdown("Tie Game! Try Again.") break else: V.pauseComp(1) Tic.insertMoves('O', move) V.drawImage(move, V.comp_img) isPlayerturn = not isPlayerturn else: V.set_status_bar_text_and_countdown( "You won this time! Good Job!", 'DarkGreen') break isQuit = V.askToQuit() V.closeWin()
def run(self): while (True): self.View = MenuView(3, self.players) buttons1, buttons2, PlayButton, GRID3, GRID10 = self.View.draw_Menu( self.players) flag = 0 p1 = None p2 = None p3 = None while (flag == 0): for event in pygame.event.get(): if (event.type == pygame.QUIT): sys.exit() if (event.type == pygame.MOUSEBUTTONDOWN): if (p1 != None and p2 != None and p3 != None): if (PlayButton.isClicked(event) == 1): flag = 1 p3 = 2 if (GRID3.isClicked(event) == 1 and p3 != 2): GRID3.Toggle() GRID10.UnToggle() GRID_SIZE = 3 p3 = 1 if (GRID10.isClicked(event) == 1 and p3 != 2): GRID10.Toggle() GRID3.UnToggle() GRID_SIZE = 10 p3 = 1 for i, b in enumerate(buttons1): if (b.isClicked(event) > 0): p1 = deepcopy(self.players[i]) for bb in buttons1: bb.UnToggle() b.Toggle() for i, b in enumerate(buttons2): if (b.isClicked(event) > 0): p2 = deepcopy(self.players[i]) for bb in buttons2: bb.UnToggle() b.Toggle() print("!!!") # TicTacToe(needToWin, GRID_SIZE,player1,player2) A_TicTacToe = TicTacToe(needToWinDict[GRID_SIZE], GRID_SIZE, p1, p2, False) A_TicTacToe.run() pygame.time.wait(30000)
def __init__(self): game_number = self.chosen_game_mode(self.args_parser()) if game_number == 1: print("Welcome to The TicTacToe game.") ttt = TicTacToe("Tic Tac Toe Game", "190x210", self.board_size_tic_tac_toe_game) ttt.run_game() elif game_number == 2: print("Welcome to The Checkers game.") print("In the building process.")
def test_draw(): game = TicTacToe() game.place_marker(0, 0) game.place_marker(0, 1) game.place_marker(0, 2) game.place_marker(1, 0) game.place_marker(1, 2) game.place_marker(1, 1) game.place_marker(2, 0) game.place_marker(2, 2) game.place_marker(2, 1) assert game.state == STATES.DRAW
def test_game4(self): game = TicTacToe() game.put_choice(2, 2) game.put_choice(2, 6) game.put_choice(2, 9) game.put_choice(2, 7) game.put_choice(1, 8) game.put_choice(1, 5) game.put_choice(1, 4) game.put_choice(1, 3) game.put_choice(1, 1) self.assertEqual(game.status_of_the_match(), 3)
def test_copying(self): gameA = TicTacToe(5, 5, 4) gameA.playX(0, 0) gameA.playO(2, 1) gameA.playX(1, 1) gameA.playO(2, 3) gameA.playX(2, 2) gameB = copy.deepcopy(gameA) self.assertEqual(gameA.get_pretty_board, gameB.get_pretty_board) gameB.playO(3, 3) self.assertNotEqual(gameA.get_pretty_board, gameB.get_pretty_board)
def main(): """ There should only be 255168 inspections for the 1st move """ # Global vars global log, move_inspections # Vars game_over = False maxi_player = True # Set up a logger log = create_console_logger('DEBUG', '%(levelname)s: %(message)s') t0 = time() board = TicTacToe() t1 = time() print("It took {} usecs to initialize my board".format(t1 - t0)) # Create a starting board starting_board = [1, 5, 3, 2, 8, 4] #starting_board = [1, 5, 9] #starting_board = [] #starting_board = [] setup_board(board, starting_board) """ Starting board X | O | X ----------- O | O | ----------- | X | """ board.draw() while not game_over: if maxi_player: # computer player # Reset inspection count move_inspections = 0 board.update_board(best_move(board, maxi_player)) print(f"{move_inspections} moves were inspected") else: board.update_board(get_player_move(board)) board.draw() maxi_player = not maxi_player # end or switch players game_over = board.winner() or board.is_tie() if board.winner(): print(f"{board.winner()} is the winner!") else: print("Tie game!")
def terminal(s): for wc in TicTacToe().winning_cases: if s[wc[0]] != '_' and \ s[wc[0]] == s[wc[1]] and \ s[wc[1]] == s[wc[2]]: if s[wc[0]] == 'X': return 1 else: return 2 if '_' not in s: return 3 else: return 0
def test_first_case(self): game = TicTacToe() game.playX(0, 0) game.playX(1, 0) game.playX(2, 0) board_as_x = game.board_for_learning_as_X() expected_as_x = numpy.array( [1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]) self.assertTrue((board_as_x == expected_as_x).all()) board_as_o = game.board_for_learning_as_O() expected_as_o = numpy.array( [2.0, 2.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]) self.assertTrue((board_as_o == expected_as_o).all())
def train(games): # Set timer startTime = clock() # Train computer for i in range(1, games + 1): if i % 1000 == 0: print(i) game = TicTacToe(p1, p2) game.play_game() deltaTime = clock() - startTime m, s = divmod(deltaTime, 60) print("It took " + str(int(m)) + " minutes and " + str(s) + " seconds to play " + str(games) + " games")
def test_move_1(self): game = TicTacToe() class fake_session: def run(self, pred, feed_dict): # move for player X is more attractive then move for player O predictions = numpy.array( [[0.9, 0.2, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]]) return predictions sess = fake_session() x, y = next_move(game, sess, '', '') self.assertEqual(int(x), 0) self.assertEqual(int(y), 0)
def Main(): humanChoice = '' aiChoice = '' turn = '' while 1 : humanChoice = input('Select symbol X or O: ').lower() if humanChoice == 'x': aiChoice = 'o' break elif humanChoice == 'o': aiChoice = 'x' break print('') print('wrong choice') print('') while 1: turn = input('Would you like to play first (y/n): ').lower() if turn == 'y': turn = 'human' break elif turn == 'n': turn = 'ai' break print('') print('wrong choice') print('') game = TicTacToe(humanChoice,aiChoice) while not game.CheckFinalState() and len(game.GetEmptyCells()) > 0: if turn == 'human': game.HumanTime() turn = 'ai' else: game.AiTime() turn = 'human' game.Clear() game.Render() if game.Winner(-1): print('The human wins') elif game.Winner(1): print('The AI wins') else: print('tie')
def main(): game = TicTacToe() currentInput = ' ' if random.randint(0, 1) == 0: game.nextAIMove() game.printField() while (currentInput != 'q' and game.gameIsRunning): currentInput = input() game.performMoves(currentInput) game.printField() print()