def test_check_win(self): self.game = tictactoe.TicTacToe() self.game.playing_field = ["X", "X", "X", 4, 5, 6, 7, 8, 9] self.game.check_win() self.assertTrue(self.game.win[0]) self.game = tictactoe.TicTacToe() self.game.playing_field = ["X", "0", "X", "0", "X", "X", "0", "X", "0"] self.game.check_win() self.assertFalse(self.game.win[0])
def test_check_draw(self): self.game = tictactoe.TicTacToe() self.game.playing_field = ["X", "0", "X", "0", "X", "X", "0", "X", "0"] self.game.check_draw() self.assertTrue(self.game.draw) self.game = tictactoe.TicTacToe() self.game.playingField = ["X", "X", "X", "0", "X", "X", "0", 8, "0"] self.game.check_draw() self.assertFalse(self.game.draw)
def test_taketurnandverifyturnchange(): tic = tictactoe.TicTacToe() tic.new_game("player1", "player2") tic.take_turn(1, 0) assert tic.board[0] == 1 assert tic.player_turn == 2
def test_game_tree(): """Tests for the game tree.""" result = True game = ttt.TicTacToe(3, 3) model = TestModel() root = gt.State({}, game, model, po.first_policy, None) root.rollout(9, 0, []) result = result and expect_equal( len(root.states), 8, 'visited states after first rollout') for i in range(9): result = result and expect_equal(root.actions[i].visit_count, 1 if i == 0 else 0, 'visit count for action {}'.format(i)) root.rollout(9, 0, []) result = result and expect_equal( len(root.states), 8, 'visited states after second rollout') for i in range(9): result = result and expect_equal(root.actions[i].visit_count, 2 if i == 0 else 0, 'visit count for action {}'.format(i)) result = result and expect_equal(root.prior, 2.0, 'prior') for i in range(9): result = result and expect_equal(root.actions[i].prior, 3.0, 'prior for action {}'.format(i)) result = result and expect_equal(root.actions[0].average_value, 1.0, 'average value') test_result(result, 'Game Tree Test')
def test_wins_and_finish(self): game = tictactoe.TicTacToe() self.assertFalse(game.is_win()) game.make_turn("X", 0) game.make_turn("O", 4) game.make_turn("X", 1) is_finished, is_win = game.make_turn("O", 8) # X X _ # _ O _ # _ _ O self.assertFalse(is_finished) self.assertFalse(is_win) is_finished, is_win = game.make_turn("X", 2) # X X X # _ O _ # _ _ O self.assertTrue(is_finished) self.assertTrue(is_win) with self.assertRaises(tictactoe.IncorrectTurn) as ar: game.make_turn("O", 3) self.assertEqual(ar.exception.message, "The game is over")
def setUp(self): self.marker = 'o' self.g = tictactoe.TicTacToe() e, x, o = tictactoe.TicTacToe.EMPTY_CELL, 'x', 'o' self.g.board = [[x, e, o], [e, e, e], [o, e, x]] self.opo = tictactoe.TicTacToeOponent(self.marker)
def testMove(self): t = ttt.TicTacToe(3) self.assertEqual(t.board, [0, 0, 0, 0, 0, 0, 0, 0, 0], "Board state is unexpected.") t.move(0) self.assertEqual(t.board, [1, 0, 0, 0, 0, 0, 0, 0, 0], "Board state is unexpected.") t.move(1) self.assertEqual(t.board, [1, -1, 0, 0, 0, 0, 0, 0, 0], "Board state is unexpected.") state = [0, 0, 0, 1, 0, 0, -1, 0, 0] t.reset(state) t.move(0) self.assertEqual(t.board, [1, 0, 0, 1, 0, 0, -1, 0, 0], "Board state is unexpected.")
def testTicTacToeBuildAndReset(self): t = ttt.TicTacToe(3) self.assertEqual(t.board, [0, 0, 0, 0, 0, 0, 0, 0, 0], "Board state is unexpected.") self.assertEqual(t.turn, 1, "Turn has unexpected state at start of game.") state = [0, 0, 0, 1, 0, 0, -1, 0, 0] t.reset(state) self.assertEqual(t.board, state, "Board state is unexpected after reset.") self.assertEqual(t.turn, 1, "Turn has unexpected state after reset to midgame.") state = [0, 0, 1, 1, 0, 0, -1, 0, 0] t.reset(state) self.assertEqual(t.board, state, "Board state is unexpected after reset.") self.assertEqual(t.turn, -1, "Turn has unexpected state after reset to midgame.")
def playTicTacToe(self, instring): response = input("Do you want to play TicTacToe with me?" + "\n") if response in self.Affirmations: num = int( input( "What size do you want the board to be? Enter a number.")) board = tictactoe.TicTacToe(num) board.playOneGame(tictactoe.HumanPlayer(num), tictactoeminimax.MinimaxPlayer(num, 3), True) reply = "That was a good game!" else: reply = "Good, that you backed out. I believe that we're all equal. Games just create animosity." return reply
def test_game_tree_with_opponent(): """Tests for the game tree with an opponent.""" result = True game = ttt.TicTacToe(3, 3) model = TestModel() opponent = TestOpponent(game) root = gt.State({}, game, model, po.first_policy, opponent.policy) root.rollout(9, 0, []) result = result and expect_equal( len(root.states), 4, 'visited states after first rollout') result = result and expect_equal(root.actions[0].average_value, 1.0, 'average value') test_result(result, 'Game Tree With Opponent Test')
def reset_game_action(self): # reinitalize game to reset self.game = tictactoe.TicTacToe() # making label text empty self.label.setText("") # traverse push list for buttons in self.push_list: for button in buttons: # making all the button enabled button.setEnabled(True) # removing text of all the buttons button.setText("")
def Play(run_count): players = {} players[1] = randplayer.Player() players[2] = optimalplayer.Player() wins = {} last_wins = {} wins[0] = 0 wins[1] = 0 wins[2] = 0 last_wins[0] = 0 last_wins[1] = 0 last_wins[2] = 0 current_turn = 2 once = False for i in range(run_count): game = tictactoe.TicTacToe(3, 3) while not game.Terminated(): current_turn = 3 - current_turn current_turn_player = players[current_turn] while not game.PlaceTurn( current_turn, current_turn_player.GetMove(game.CurrentBoard(), (3, 3))): # keep trying if illegal move pass for id, player in players.iteritems(): player.Inform(id, game.Winner()) if i > 2000000 and not once and game.Winner() == 1: game.PrintGame() once = True wins[game.Winner()] += 1 if i % 100000 == 0: print 'After %d games' % (i + 1) print 'Draws = %d (d=%d)' % (wins[0], wins[0] - last_wins[0]) print 'Player 1 wins = %d (d=%d)' % (wins[1], wins[1] - last_wins[1]) print 'Player 2 wins = %d (d=%d)' % (wins[2], (wins[2] - last_wins[2])) players[2].PrintCounts() last_wins[0] = wins[0] last_wins[1] = wins[1] last_wins[2] = wins[2] print 'Final Tally:' print 'Draws= %d' % wins[0] print 'Player 1 wins = %d' % wins[1] print 'Player 2 wins = %d' % wins[2]
def test_fully_connected_model(): """Tests for the fully connected model.""" result = True game = ttt.TicTacToe(3, 3) model = mo.FullyConnectedModel(3, [9, 9], batch_size=2) result = result and expect_equal(len(model.layers), 3, 'number of layers') s1 = game.state() game.take_action(0) s2 = game.state() game.take_action(4) s3 = game.state() result = result and expect_equal(model.value(s1), 0.0, 'value') model.train([(s1, 0, np.arange(9, dtype=np.float32)), (s2, 1.0, np.zeros(9, dtype=np.float32)), (s3, 0.5, np.ones(9, dtype=np.float32))]) test_result(result, 'Fully Connnected Model Test')
async def on_message(message): # called when a message is sent if message.author == client.user: # if message is sent by the bot return if message.content.startswith("!help"): await bot_help.bot_help(message) if message.content.startswith("!hello"): await message.channel.send("Hello there.") if message.content.startswith("!coinflip"): await coin_flip.coinflip(message) if message.content.startswith("!tictactoe"): state.tictactoe.append(tictactoe.TicTacToe(message)) await state.tictactoe[-1].create_board()
def __init__(self): self.tictactoe = tictactoe.TicTacToe() self.game_status = "no game" self.player_turn = "no game" self.player1 = "no game" self.player2 = "no game" self.player1id = 0 self.player2id = 0 self.board_message = 0 # This will hold the message that displays the board # and players will react to, to be able to play. # python has objc style where you manually call the init method on the parent # super.__init() doesn't seem to work, it insists on an argument #super.__init__() discord.Client.__init__(self)
def test_convolutional_model(): """Tests for the convolutional model.""" result = True game = ttt.TicTacToe(3, 3) model = mo.ConvolutionalModel(3, [10, 9], batch_size=2) result = result and expect_equal(len(model.layers), 2, 'number of layers') s1 = game.state() game.take_action(0) s2 = game.state() game.take_action(4) s3 = game.state() result = result and expect_equal(model.value(s1), 0.0, 'value') np.testing.assert_almost_equal(model.policy(s1), np.ones(9) / 9) model.train([(s1, 0, np.arange(9, dtype=np.float32)), (s2, 1.0, np.zeros(9, dtype=np.float32)), (s3, 0.5, np.ones(9, dtype=np.float32))]) test_result(result, 'Convolutional Model Test')
def test_table_model(): """Tests for the table model.""" result = True game = ttt.TicTacToe(3, 3) model = mo.TableModel(3) model.insert_or_update(game.state(), (1.0, np.ones(9), 1.0)) result = result and expect_equal(len(model.table), 1, 'table size') stats, rotation_amt, flipped, key = model.lookup(game.state()) result = result and expect_equal(rotation_amt, 0, 'rotation amount') result = result and expect_equal(flipped, False, 'flipped') result = result and expect_equal(key, game.state_key(), 'table key') game.take_action(0) game.take_action(1) result = result and expect_equal( model.lookup(game.state()), None, 'non-existent key') original_key = game.state_key() model.insert_or_update(game.state(), (1.0, np.arange(9), 1.0)) stats, rotation_amt, flipped, key = model.lookup(game.state()) result = result and expect_equal(rotation_amt, 0, 'rotation amount') result = result and expect_equal(flipped, False, 'flipped') result = result and expect_equal(key, original_key, 'table key') game.undo_action() game.undo_action() game.take_action(8) game.take_action(5) stats, rotation_amt, flipped, key = model.lookup(game.state()) result = result and expect_equal(rotation_amt, 1, 'rotation amount') result = result and expect_equal(flipped, True, 'flipped') result = result and expect_equal(key, original_key, 'table key') model.insert_or_update(game.state(), (3.0, np.arange(9) * 3, 1.0)) stats, _, _, _ = model.lookup(game.state()) value, policy, weight = stats result = result and expect_equal(value, 2.0, 'value') result = result and expect_equal( np.array_equal(policy, np.array([4, 4, 4, 8, 8, 8, 12, 12, 12])), True, 'policy') result = result and expect_equal(weight, 2.0, 'weight') result = result and expect_equal( np.array_equal( model.policy(game.state()), np.array([4, 4, 4, 8, 8, 8, 12, 12, 12])), True, 'model policy') result = result and expect_equal( model.value(game.state()), 2.0, 'model value') test_result(result, 'Table Model Test')
def __init__(self): super().__init__() # setting title self.setWindowTitle("Tic Tac Toe by Jason Carter") # setting geometry self.setGeometry(100, 100, 300, 550) # calliing method self.UiComponents() # Showing all the widgets self.show() # Initalize game self.game = tictactoe.TicTacToe() self.is_ai = False
def test_turn_restrictions(self): game = tictactoe.TicTacToe() # unknown player with self.assertRaises(TypeError): game.make_turn("Z", 3) # correct turn game.make_turn("X", 3) self.assertEqual(game.board[3], "X") # out of turn with self.assertRaises(tictactoe.IncorrectTurn) as ar: game.make_turn("X", 3) self.assertEqual(ar.exception.message, "Current player O") # filled cell with self.assertRaises(tictactoe.IncorrectTurn) as ar: game.make_turn("O", 3) self.assertEqual(ar.exception.message, "The cell 3 is already filled")
def testEndGame(self): t = ttt.TicTacToe(3) self.assertFalse(t.is_win()) state = [1, 1, 1, 0, -1, -1, 0, 0, 0] t.reset(state) self.assertEqual(t.is_win(), (ttt.TicTacToe.Row, 0, 1), "Row win is not detected.") state = [-1, 1, 0, 1, 1, 1, 0, -1, -1] t.reset(state) self.assertEqual(t.is_win(), (ttt.TicTacToe.Row, 1, 1), "Row win is not detected.") state = [-1, 0, 0, -1, 1, 1, -1, 0, 1] t.reset(state) self.assertEqual(t.is_win(), (ttt.TicTacToe.Column, 0, -1), "Column win is not detected.") state = [-1, 0, 0, 1, -1, 1, 0, 1, -1] t.reset(state) self.assertEqual(t.is_win(), (ttt.TicTacToe.Diagonal, 0, -1), "Diagonal win is not detected.") state = [-1, 0, 1, 1, 1, -1, 1, 0, -1] t.reset(state) self.assertEqual(t.is_win(), (ttt.TicTacToe.Diagonal, 1, 1), "Diagonal win not detected") state = [-1, 1, 1, 1, 1, -1, -1, -1, 1] t.reset(state) self.assertEqual(t.is_win(), (ttt.TicTacToe.StaleMate, 0, 0), "Stalemate is not detected.")
def test_agents(): """Tests for the stock agents.""" result = True game = ttt.TicTacToe(3, 3) random = ag.RandomAgent('Random', game) dumb = ag.DumbAgent('Dumb', game) optimal = ag.MinimaxAgent('Minimax', game) # Smoke test for the random agent. game.take_action(random.select_move()) game.undo_action() result = result and expect_equal(dumb.select_move(), 0, 'dumb first move') game.take_action(0) result = result and expect_equal(dumb.select_move(), 1, 'dumb second move') game.take_action(4) game.take_action(3) result = result and expect_equal(optimal.select_move(), 6, 'minimax move') result = result and expect_equal(optimal.optimal_moves(), [6], 'minimax optimal moves') game.reset() # result = result and expect_equal(optimal.optimal_moves(), range(9), 'minimax all moves optimal') test_result(result, 'Agents Test')
def __init__(self): self.new_clients = [] #list of new sockets of which the user id is not known self.logged_name2sock = {} #dictionary mapping username to socket self.logged_sock2name = {} # dict mapping socket to user name self.all_sockets = [] self.group = grp.Group() #tictactoe incorporation self.ttt = t.TicTacToe() #start server self.server=socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.server.bind(SERVER) self.server.listen(5) self.all_sockets.append(self.server) #initialize past chat indices self.indices={} # sonnet # self.sonnet_f = open('AllSonnets.txt.idx', 'rb') # self.sonnet = pkl.load(self.sonnet_f) # self.sonnet_f.close() self.sonnet = indexer.PIndex("AllSonnets.txt") self.public_key1 = 71031 self.public_key2 = 20573
def menu(): while True: print("\n1. start\n2. exit") what_user_want = input("Enter your choice: ") if what_user_want not in ("start", "exit", "1", "2"): print("Bad parameters!") continue if what_user_want in ("exit", "2"): sys.exit() else: while True: players = input("Choose player: ") players = players.split(" ") if len(players) > 2: print("Bad parameters!") continue if not set(players).issubset( {"user", "easy", "medium", "hard"}): print("Bad parameters!") continue game = tictactoe.TicTacToe(players[0], players[1]) print(game.play()) break
def main(): game = ttt.TicTacToe(8, 4) random = ag.RandomAgent('Random', game) config = rl.Config( training_epochs=2, games_per_epoch=10, rollouts_per_move=20, rollout_depth=4, rollout_policy=functools.partial(po.alpha_zero_mcts_policy, c_puct=10.0), play_policy=functools.partial(po.alpha_zero_play_policy, tau=1.5), inference_policy=po.greedy_prior_policy, opponent_rollout_policy=None, opponent_play_policy=None, policy_target=functools.partial(po.alpha_zero_visit_counts_to_target, action_space=game.action_space(), tau=1.0), inference_rollouts_per_move=40, inference_rollout_depth=4) model = mo.KerasModel(game, [128], [64, 32], [16, 4], data_passes=100) agent = rl.RLAgent('RL Agent', game, model, config, [random], 100) agent.train(print_progress=True) gl.play_match(g=game, agent_a=agent, agent_b=random, num_games=4) gl.interactive_play(game, agent)
def test_rl_agent(): """Tests for the RL Agent.""" result = True game = ttt.TicTacToe(3, 3) model = mo.TableModel(3) config = rl.Config( training_epochs=1, games_per_epoch=1, rollouts_per_move=1, rollout_depth=9, rollout_policy=functools.partial(po.alpha_zero_mcts_policy, c_puct=10.0), play_policy=functools.partial(po.alpha_zero_play_policy, tau=1.0), inference_policy=po.greedy_prior_policy, opponent_rollout_policy=None, opponent_play_policy=None, policy_target=functools.partial( po.alpha_zero_visit_counts_to_target, action_space=game.action_space(), tau=1.0), inference_rollouts_per_move=0, inference_rollout_depth=0) agent = rl.RLAgent('RL Agent', game, model, config, [], 0, None) agent.train() test_result(result, 'RL Agent Test')
def test_basics(self): game = tictactoe.TicTacToe() self.assertEqual(len(game.board), 9)
import socket import tictactoe listen_socket = socket.socket() listen_socket.bind(('127.0.0.1', 3456)) listen_socket.listen() game_socket, addr = listen_socket.accept() game = tictactoe.TicTacToe() while True: # Display current Tic-Tac-Toe board print(game.render_board()) # Check if client player won if game.get_winner() is not None: print('Opponent wins!') print() break # Check if the board is full if game.is_full(): print('Stalemate') print() break # Prompt for move from server player move = -1 while move != 0 and not game.is_valid_move(move): move = int(input('Server moves ' + '(0 to quit): ')) print()
import tictactoe import json import os.path """Main tic tac toe program run""" if __name__ == '__main__': tictactoeMatch = tictactoe.TicTacToe() print('* * * Welcome to Tic Tac Toe * * *\n') while True: gameBoard = [' '] * 10 playerLetter, computerLetter = 'O', 'X' turn = tictactoeMatch.chooseFirstPlayer() print('The ' + turn + ' will go first.') gamePlaying = True # Reads computerlosses.txt file at start of game to add losing boards if the file exists if os.path.isfile('computerlosses.txt'): with open('computerlosses.txt', 'r') as f: file_losses = json.load(f) for i, item in enumerate(file_losses): # Appends bad moves from computerlosses.txt file to bad moves list if not currently in list if item not in tictactoeMatch.getCaptureComputerLoss(): tictactoeMatch.captureComputerLoss(file_losses[i]) tictactoeMatch.setComputerWinStatus(False) f.closed while gamePlaying:
import json, random, os import tensorflow as tf import models, tictactoe if __name__ == "__main__": while True: model = models.NeuralNetworkModel() checkpoint_directory = "./training_checkpoints" checkpoint_prefix = os.path.join(checkpoint_directory, "ckpt") checkpoint = tf.train.Checkpoint(model=model) status = checkpoint.restore( tf.train.latest_checkpoint(checkpoint_directory)).expect_partial() board = tictactoe.TicTacToe() history = models.compare_models(model, model, board) directory_name = "./data/{}/".format(checkpoint.save_counter.numpy()) os.makedirs(directory_name, exist_ok=True) run_id = random.randint(0, 2**20 - 1) filename = os.path.join(directory_name, "gamedata-{:05x}.json".format(run_id)) with open(filename, 'w') as file: json.dump([(x.tolist(), p.tolist(), v) for x, p, v in history], file)
def test_tictactoe(): tic = tictactoe.TicTacToe() assert tic.board[random.randint(0, 8)] is None assert tic.player_turn == 0 assert type(tic.player_names) == list assert type(tic.board) == list