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 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 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 __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_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 test_diag_accessor(self): b = Board(self.valid) self.assertEqual(list('xo.'), b.diag(0)) self.assertEqual(list('.ox'), b.diag(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 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))
def board(): board = Board() return board
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()
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 test_print_board(self): b = Board(3) b.make_new_board() b.print_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")
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")