def test_make_new_board(self): b = Board(3) b.make_new_board() self.assertEqual(len(b.board), b.bd * b.bd, "length of board is set to board dimentions") self.assertEqual(b.board[0], b.empty_mark, "empty cell set at board 0 0")
def board_load(board_json): data = json.loads(board_json) if 'size' not in data or 'squares' not in data: raise BoardSerializerException("Missing required properties") if type(data['size']) is not int: raise BoardSerializerException("Invalid size property: %s" % type(data['size'])) if type(data['squares']) is not list: raise BoardSerializerException("Invalid squares property") if len(data['squares']) != data['size']: raise BoardSerializerException("Length of squares doesn't match size property") for idx, square in enumerate(data['squares']): if square not in [None, u'O', u'X']: raise BoardSerializerException("Invalid item in squares property") if square is not None: data['squares'][idx] = str(square) board = Board(data['size']) board.squares = data['squares'][:] return board
def __init__(self): # TTT related self.ttt = Board(ply=9) self.human_first = True # UI related self.root = Tk() self.root.resizable(0, 0) self.root.title("3D TTT") # TTT frames self.ttt_frames = [Frame(self.root) for x in range(3)] for i in range(3): self.ttt_frames[i].grid(row=0, column=i) self.button_pos = dict() self._init_board() # control frame self.control_frame = Frame(self.root, padx=5, pady=5) self.control_frame.grid(row=1, column=1) self.new_game_btn = Button(self.control_frame, text='New Game', \ command=lambda: self.reset()) self.new_game_btn.pack(side=LEFT, fill=BOTH, expand=True) self.toggle_human_first_btn = Button(self.control_frame, \ text='Human First', command=lambda: self.toggle_human_first()) self.toggle_human_first_btn.pack(side=RIGHT, fill=BOTH, expand=True) self.ply_box = Spinbox(self.control_frame, from_=1, to=20, \ textvariable=self.ttt.difficulty, command=lambda: self.reset()) self.ply_box.pack(side=RIGHT, fill=BOTH, expand=True) # start UI self.update_pieces() self.start() self.root.mainloop()
def __init__(self): self.action_space = spaces.Discrete(9) self.observation_space = spaces.Box(low=0, high=1, shape=(2, 3, 3), dtype=np.uint8) self.rand_bot = Player() self.board = Board() self.done = False self.reset()
def test_is_board_empty(self): b = Board(3) b.make_new_board() self.assertTrue(b.is_board_empty(), "board is empty") m = Mark(0) b.make_move(0, m) self.assertFalse(b.is_board_empty(), "board is not empty")
def __init__(self): # TTT related self.ttt = Board(ply=9) self.human_first = True # UI related self.root = Tk() self.root.resizable(0, 0) self.root.title("3D TTT") # TTT frames self.ttt_frames = [Frame(self.root) for x in range(3)] for i in range(3): self.ttt_frames[i].grid(row=0, column=i) self.button_pos = dict() self._init_board() # control frame self.control_frame = Frame(self.root, padx=5, pady=5) self.control_frame.grid(row=1, column=1) self.new_game_btn = Button(self.control_frame, text='New Game', \ command=lambda: self.reset()) self.new_game_btn.pack(side=LEFT, fill=BOTH, expand=True) self.toggle_human_first_btn = Button(self.control_frame, \ text='Human First', command=lambda: self.toggle_human_first()) self.toggle_human_first_btn.pack(side=RIGHT, fill=BOTH, expand=True) self.ply_box = Spinbox(self.control_frame, from_=1, to=20, \ textvariable=self.ttt.difficulty, command=lambda: self.reset()) self.ply_box.pack(side=RIGHT, fill=BOTH, expand=True) # start UI self.update_pieces() self.start() self.root.mainloop()
def _start_game(self, data, message): markers = ['X', 'O'] game = self._storage.get_game(data['person']) reply = '\n' if game is not None: reply += "Game already in progress\n" return self._show_board(data, message, reply) if len(data['args']) > 0: if data['args'][0] not in markers: reply += "Invalid marker choice, try again" return self._send_reply(message, reply) else: human = data['args'][0] else: human = random.choice(markers) cpu = 'X' if human == 'O' else 'O' board = Board(9) computer = ComputerPlayer(cpu) if cpu == "X": move = computer.get_square(board, None, None) board.place(move, cpu) game = { 'twitter_id': data['person'], 'marker': human, 'board': serializers.board_store(board), 'computer_player': serializers.player_store(computer), 'date_started': arrow.utcnow().datetime, 'date_updated': arrow.utcnow().datetime } self._storage.put_game(game) reply += "You're %s" % game["marker"] reply += "\n" + self._pretty_board(board) self._send_reply(message, reply)
def test_make_move(self): b = Board(3) b.make_new_board() m1 = Mark(0) b.make_move(0, m1) self.assertEqual(len(b.board), b.bd * b.bd, "length of board is set to board dimentions") self.assertEqual(b.board[0], m1, "cell marked") #TODO: check exception using UT with self.assertRaises(Exception) as context: b.make_move(0, m1) print "exception message is " + str(context.exception) self.assertTrue("Invalid move" in str(context.exception))
def ttt(): playerX = Player('X') playerO = Player('O') board = Board() ttt = TicTacToe(board, playerX, playerO) return ttt
return 0 if board.check_win() != 0: return 1 for pos in board.availablePositions(): if board.movesMade % 2 == 0: board.make_move(pos, 1) else: board.make_move(pos, -1) total += allGameStates(board) test = board.undo_move(pos) get_symmetry(board) #print (board.movesMade) if board.movesMade == 1: print(total) board.showBoard() return total board = Board() # board.make_move((0, 0), 1) # board.make_move((1, 2), -1) # get_symmetry(board) # print([b for b in all_states]) print(allGameStates(board)) print(len(all_states))
def setUp(self): self.Board = Board()
class BoardTest(unittest.TestCase): def setUp(self): self.Board = Board() def test_board(self): self.assertEqual(self.Board.Board.all(), np.matrix('-1 -1 -1; -1 -1 -1; -1 -1 -1').all(), 'incorrect default board setup') def test_available_spaces(self): self.assertEqual(self.Board.available_spaces(), [0, 1, 2, 3, 4, 5, 6, 7, 8], 'incorrect available_spaces') def test_open_space(self): self.assertEqual(self.Board.open_space(3), True, 'incorrect available_spaces') def test_take_space(self): self.Board.take_space(0, 0) self.assertEqual(self.Board.Board.all(), np.matrix('0 -1 -1; -1 -1 -1; -1 -1 -1').all(), 'incorrect default board setup') def test_reset_game(self): self.Board.reset_game() self.assertEqual(self.Board.Board.all(), np.matrix('-1 -1 -1; -1 -1 -1; -1 -1 -1').all(), 'incorrect default board setup') def test_game_status(self): self.Board.Board = np.matrix('0 0 0; -1 -1 -1; -1 -1 -1') status = self.Board.game_status() self.assertEqual(0, status) self.Board.Board = np.matrix('-1 -1 0; -1 -1 0; -1 -1 0') status = self.Board.game_status() self.assertEqual(0, status) self.Board.Board = np.matrix('1 1 1; -1 -1 -1; -1 -1 -1') status = self.Board.game_status() self.assertEqual(1, status) self.Board.Board = np.matrix('0 -1 -1; -1 0 -1; -1 -1 0') status = self.Board.game_status() self.assertEqual(0, status) self.Board.Board = np.matrix('-1 -1 0; -1 0 0; -1 -1 0') status = self.Board.game_status() self.assertEqual(0, status) self.Board.Board = np.matrix('-1 -1 -1; -1 -1 -1; -1 -1 -1')
def test_row_accessor(self): b = Board(self.valid) self.assertEqual(list('xo.'), b.row(0)) self.assertEqual(list('xo.'), b.row(1)) self.assertEqual(list('xo.'), b.row(2))
def setUp(self): self.Board = Board()
class TTTUI(object): default_ply = 6 neither_color = '#%02x%02x%02x' % (255, 255, 255) # white player_color = '#%02x%02x%02x' % (62, 188, 0) # green computer_color = '#%02x%02x%02x' % (192, 35, 3) # red win_color = '#%02x%02x%02x' % (25, 111, 254) # blue def __init__(self): # TTT related self.ttt = Board(ply=9) self.human_first = True # UI related self.root = Tk() self.root.resizable(0, 0) self.root.title("3D TTT") # TTT frames self.ttt_frames = [Frame(self.root) for x in range(3)] for i in range(3): self.ttt_frames[i].grid(row=0, column=i) self.button_pos = dict() self._init_board() # control frame self.control_frame = Frame(self.root, padx=5, pady=5) self.control_frame.grid(row=1, column=1) self.new_game_btn = Button(self.control_frame, text='New Game', \ command=lambda: self.reset()) self.new_game_btn.pack(side=LEFT, fill=BOTH, expand=True) self.toggle_human_first_btn = Button(self.control_frame, \ text='Human First', command=lambda: self.toggle_human_first()) self.toggle_human_first_btn.pack(side=RIGHT, fill=BOTH, expand=True) self.ply_box = Spinbox(self.control_frame, from_=1, to=20, \ textvariable=self.ttt.difficulty, command=lambda: self.reset()) self.ply_box.pack(side=RIGHT, fill=BOTH, expand=True) # start UI self.update_pieces() self.start() self.root.mainloop() def toggle_human_first(self): self.human_first = not self.human_first self.toggle_human_first_btn.config(text='Human First' if \ self.human_first else 'Computer First') self.reset() def _find_button(self, frame, r, c): for child in frame.children.values(): info = child.grid_info() if info['row'] == r and info['column'] == c: return child return None def update_pieces(self): player_pieces = self.ttt.get_moves(self.ttt.human) computer_pieces = self.ttt.get_moves(self.ttt.ai) cnt = 0 for b, board in enumerate(self.ttt.board): for r, row in enumerate(board): for c, col in enumerate(row): color = self.neither_color text = '-' occupied = False if cnt in player_pieces: color = self.player_color text = self.ttt.human if cnt in computer_pieces: color = self.computer_color text = self.ttt.ai if self.ttt.complete and cnt in self.ttt.winning_combo: color = self.win_color btn = self.button_pos[cnt] btn.config(text=text, bg=color, state=DISABLED if \ occupied else NORMAL) cnt += 1 def place_human(self, position): if position in self.ttt.allowed_moves and not self.ttt.complete: self.ttt.move(position, self.ttt.human) self.ttt.human_turn = False self.update_pieces() self.place_computer() def place_computer(self): if not self.ttt.complete: self.ttt.computers_move() self.update_pieces() def reset(self): self.ttt.reset() self.ttt.difficulty = self.default_ply if not \ self.ply_box.get().isdigit() else int(self.ply_box.get()) self.ttt.human_turn = self.human_first self.update_pieces() self.start() def _init_board(self): cnt = 0 for b, board in enumerate(self.ttt.board): for x, row in enumerate(board): for y, cell in enumerate(row): padding = (0, 0) if y == 2 and b != 2: padding = (0, 12) btn = Button(self.ttt_frames[b], width=8, height=4, \ command=lambda x=cell: self.place_human(x)) btn.grid(row=x, column=y, padx=padding) self.button_pos[cnt] = btn cnt += 1 def start(self): if not self.ttt.human_turn: self.place_computer()
class TTTUI(object): default_ply = 6 neither_color = '#%02x%02x%02x' % (255, 255, 255) # white player_color = '#%02x%02x%02x' % (62, 188, 0) # green computer_color = '#%02x%02x%02x' % (192, 35, 3) # red win_color = '#%02x%02x%02x' % (25, 111, 254) # blue def __init__(self): # TTT related self.ttt = Board(ply=9) self.human_first = True # UI related self.root = Tk() self.root.resizable(0, 0) self.root.title("3D TTT") # TTT frames self.ttt_frames = [Frame(self.root) for x in range(3)] for i in range(3): self.ttt_frames[i].grid(row=0, column=i) self.button_pos = dict() self._init_board() # control frame self.control_frame = Frame(self.root, padx=5, pady=5) self.control_frame.grid(row=1, column=1) self.new_game_btn = Button(self.control_frame, text='New Game', \ command=lambda: self.reset()) self.new_game_btn.pack(side=LEFT, fill=BOTH, expand=True) self.toggle_human_first_btn = Button(self.control_frame, \ text='Human First', command=lambda: self.toggle_human_first()) self.toggle_human_first_btn.pack(side=RIGHT, fill=BOTH, expand=True) self.ply_box = Spinbox(self.control_frame, from_=1, to=20, \ textvariable=self.ttt.difficulty, command=lambda: self.reset()) self.ply_box.pack(side=RIGHT, fill=BOTH, expand=True) # start UI self.update_pieces() self.start() self.root.mainloop() def toggle_human_first(self): self.human_first = not self.human_first self.toggle_human_first_btn.config(text='Human First' if \ self.human_first else 'Computer First') self.reset() def _find_button(self, frame, r, c): for child in frame.children.values(): info = child.grid_info() if info['row'] == r and info['column'] == c: return child return None def update_pieces(self): player_pieces = self.ttt.get_moves(self.ttt.human) computer_pieces = self.ttt.get_moves(self.ttt.ai) cnt = 0 for b, board in enumerate(self.ttt.board): for r, row in enumerate(board): for c, col in enumerate(row): color = self.neither_color text = '-' occupied = False if cnt in player_pieces: color = self.player_color text = self.ttt.human if cnt in computer_pieces: color = self.computer_color text = self.ttt.ai if self.ttt.complete and cnt in self.ttt.winning_combo: color = self.win_color btn = self.button_pos[cnt] btn.config(text=text, bg=color, state=DISABLED if \ occupied else NORMAL) cnt += 1 def place_human(self, position): if position in self.ttt.allowed_moves and not self.ttt.complete: self.ttt.move(position, self.ttt.human) self.ttt.human_turn = False self.update_pieces() self.place_computer() def place_computer(self): if not self.ttt.complete: self.ttt.computers_move() self.update_pieces() def reset(self): self.ttt.reset() self.ttt.difficulty = self.default_ply if not \ self.ply_box.get().isdigit() else int(self.ply_box.get()) self.ttt.human_turn = self.human_first self.update_pieces() self.start() def _init_board(self): cnt = 0 for b, board in enumerate(self.ttt.board): for x, row in enumerate(board): for y, cell in enumerate(row): padding = (0, 0) if y == 2 and b != 2: padding = (0, 12) btn = Button(self.ttt_frames[b], width=8, height=4, \ command=lambda x=cell: self.place_human(x)) btn.grid(row=x, column=y, padx=padding) self.button_pos[cnt] = btn cnt += 1 def start(self): if not self.ttt.human_turn: self.place_computer()
def test_check_winner(self): b = Board(3) # test row winning b.make_new_board() m1 = Mark(0) m2 = Mark(0) b.make_move(0, m1) b.make_move(1, m2) b.make_move(2, m1) (winning_mark, stat) = b.check_winner() print "mark is " + repr(b.board[0].mark) print "mark is " + repr(b.board[1].mark) print "board is " + repr(b.board) self.assertTrue(b.board[0].mark == b.board[1].mark, "board mark equality passed") print "winning mark is " + str(winning_mark) + "stat is " + str(stat) self.assertEqual(winning_mark, m1, "winning mark correct") self.assertEqual(stat, True, "status returned true correcntly") # test column winning b1 = Board(3) b1.make_new_board() m1 = Mark(1) m2 = Mark(1) b1.make_move(0, m1) b1.make_move(3, m2) b1.make_move(6, m1) (winning_mark, stat) = b1.check_winner() print "mark is " + repr(b1.board[0].mark) print "mark is " + repr(b1.board[1].mark) print "board is " + repr(b1.board) print "winning mark is " + str(winning_mark) + "stat is " + str(stat) self.assertEqual(winning_mark, m1, "winning mark correct") self.assertEqual(stat, True, "status returned true correcntly") # test diagonal winning b2 = Board(3) b2.make_new_board() m1 = Mark(1) m2 = Mark(1) b2.make_move(2, m1) b2.make_move(4, m2) b2.make_move(6, m1) (winning_mark, stat) = b2.check_winner() print "mark is " + repr(b2.board[0].mark) print "mark is " + repr(b2.board[1].mark) print "board is " + repr(b2.board) print "winning mark is " + str(winning_mark) + "stat is " + str(stat) self.assertEqual(winning_mark, m1, "winning mark correct") self.assertEqual(stat, True, "status returned true correcntly") # test no winner b3 = Board(3) b3.make_new_board() m1 = Mark(1) m2 = Mark(1) b3.make_move(1, m1) b3.make_move(4, m2) b3.make_move(6, m1) (winning_mark, stat) = b3.check_winner() print "mark is " + repr(b3.board[0].mark) print "mark is " + repr(b3.board[1].mark) print "board is " + repr(b3.board) print "winning mark is " + str(winning_mark) + "stat is " + str(stat) self.assertEqual(winning_mark, b3.empty_mark, "winning mark correct") self.assertEqual(stat, False, "status returned true correcntly")
def board(): board = Board() return board
def test_make_winnning_combo(self): b = Board(3) b.make_new_board() wc = b.make_winning_combo() self.assertEqual(len(wc), 8, "winning combo len is correct") self.assertTrue(wc[0] == (0, 1, 2), "winnnng combo entry 0 is correct")
class BoardTest(unittest.TestCase): def setUp(self): self.Board = Board() def test_board(self): self.assertEqual(self.Board.Board.all(), np.matrix('-1 -1 -1; -1 -1 -1; -1 -1 -1').all(), 'incorrect default board setup') def test_available_spaces(self): self.assertEqual(self.Board.available_spaces(), [0,1,2,3,4,5,6,7,8], 'incorrect available_spaces') def test_open_space(self): self.assertEqual(self.Board.open_space(3), True, 'incorrect available_spaces') def test_take_space(self): self.Board.take_space(0, 0) self.assertEqual(self.Board.Board.all(), np.matrix('0 -1 -1; -1 -1 -1; -1 -1 -1').all(), 'incorrect default board setup') def test_reset_game(self): self.Board.reset_game() self.assertEqual(self.Board.Board.all(), np.matrix('-1 -1 -1; -1 -1 -1; -1 -1 -1').all(), 'incorrect default board setup') def test_game_status(self): self.Board.Board = np.matrix('0 0 0; -1 -1 -1; -1 -1 -1') status = self.Board.game_status() self.assertEqual(0,status) self.Board.Board = np.matrix('-1 -1 0; -1 -1 0; -1 -1 0') status = self.Board.game_status() self.assertEqual(0,status) self.Board.Board = np.matrix('1 1 1; -1 -1 -1; -1 -1 -1') status = self.Board.game_status() self.assertEqual(1,status) self.Board.Board = np.matrix('0 -1 -1; -1 0 -1; -1 -1 0') status = self.Board.game_status() self.assertEqual(0,status) self.Board.Board = np.matrix('-1 -1 0; -1 0 0; -1 -1 0') status = self.Board.game_status() self.assertEqual(0,status) self.Board.Board = np.matrix('-1 -1 -1; -1 -1 -1; -1 -1 -1')
def test_print_board(self): b = Board(3) b.make_new_board() b.print_board()
def test_col_accessor(self): b = Board(self.valid) self.assertEqual(list('xxx'), b.col(0)) self.assertEqual(list('ooo'), b.col(1)) self.assertEqual(list('...'), b.col(2))
class tttEnv(gym.Env): metadata = {'render.modes': ['human']} def __init__(self): self.action_space = spaces.Discrete(9) self.observation_space = spaces.Box(low=0, high=1, shape=(2, 3, 3), dtype=np.uint8) self.rand_bot = Player() self.board = Board() self.done = False self.reset() def step(self, action=-1, player=1, model=None): reward = 0 if player == 1: if type(action) == int: movePos = (int(action / 3), int(action % 3)) else: movePos = action moveDone = self.board.make_move(movePos, player) if moveDone == 0: reward = -2 self.done = True return self.board.state, reward, self.done, {} win = self.board.check_win() if win != 0: if win == 2: reward = 0 self.done = True else: reward = player self.done = True return self.board.state, reward, self.done, {} positions = self.board.availablePositions() movePos = self.rand_bot.choose_action(positions) moveDone = self.board.make_move(movePos, -1) win = self.board.check_win() if win != 0: if win == 2: reward = 0 self.done = True else: reward = -1 self.done = True return self.board.state, reward, self.done, {} # else: # if action == 'random': # positions = self.board.availablePositions() # movePos = self.rand_bot.choose_action(positions) # moveDone = self.board.make_move(movePos, -1) # else: # if type(action) == int: # movePos = (int(action/3), int(action%3)) # else: # movePos = action # moveDone = self.board.make_move(movePos, -1) # if moveDone == 0: # reward = 0 # self.done = True # return self.board.state, reward, self.done, {} # win = self.board.check_win() # if win != 0: # if win == 2: # reward = 0 # self.done = True # else: # reward = -1 # self.done = True # return self.board.state, reward, self.done, {} return self.board.state, reward, self.done, {} #------------------ # if model == None: # positions = self.board.availablePositions() # movePos = self.rand_bot.choose_action(positions) # moveDone = self.board.make_move(movePos, -1) # else: # s = self.one_hot_encode(self.board.state) # qvals_s = model.predict(s.reshape(1,18)) # a = np.argmax(qvals_s) # movePos = (int(a/3), int(a%3)) # moveDone = self.board.make_move(movePos, -1) # win = self.board.check_win() # if win != 0: # if win == 2: # reward = 0 # self.done = True # else: # reward = -1 # self.done = True # return self.one_hot_encode(self.board.state), reward, self.done, {} #----------------- Monte Carlo's Step ------------------------------ # reward = 0 # if action != -1: # if type(action) == int: # movePos = (int(action/3), int(action%3)) # else: # movePos = action # moveDone = self.board.make_move(movePos, player) # win = self.board.check_win() # if win != 0: # if win == 2: # reward = 0 # self.done = True # else: # reward = 1 # self.done = True # return self.one_hot_encode(self.board.state), reward, self.done, {} # else: # positions = self.board.availablePositions() # movePos = self.rand_bot.choose_action(positions) # moveDone = self.board.make_move(movePos, player) # win = self.board.check_win() # if win != 0: # if win == 2: # reward = 0 # self.done = True # else: # reward = player # self.done = True # return self.board.state, reward, self.done, {} # return self.board.state, reward, self.done, {} def render(self): self.board.showBoard() def reset(self): self.board.reset() self.done = False # if np.random.rand() < 0.5: # positions = self.board.availablePositions() # movePos = self.rand_bot.choose_action(positions) # moveDone = self.board.make_move(movePos, -1) return self.board.state def close(self): pass def one_hot_encode(self, state): one_hot_board = np.zeros((2, 3, 3)) for i in range(3): for j in range(3): if state[i, j] == 1: one_hot_board[0, i, j] = 1 elif state[i, j] == -1: one_hot_board[1, i, j] = -1 return one_hot_board def possible_move_boards(self, player=1): positions = self.board.availablePositions() possible_Boards = [] temp_state = self.board.state for p in positions: temp_state[p] = player possible_Boards.append((list(temp_state.reshape(3 * 3)), p)) temp_state[p] = 0 return possible_Boards def set_state(self, state): self.board.state = state
def test_diag_accessor(self): b = Board(self.valid) self.assertEqual(list('xo.'), b.diag(0)) self.assertEqual(list('.ox'), b.diag(1))
def test_board_init(self): b = Board(3) self.assertEqual(b.empty_mark.mark, -1) self.assertEqual(b.bd, 3) self.assertEqual(len(b.winning_combo), 8, "winning combo length correct")