Пример #1
0
 def test_legal_plays(self):
     state_history = []
     state = tictactoe.new_game()
     state['board'] = [['X', '', ''], ['', 'O', ''], ['', '', 'X']]
     state_history.append(state)
     expected = [(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)]
     self.assertEqual(tictactoe.legal_plays(state_history), expected)
Пример #2
0
    def test_to_string(self):
        state_history = []
        state = tictactoe.new_game()
        state['board'] = [['X', '', 'X'], ['O', 'O', ''], ['X', '', '']]
        state_history.append(state)

        expected = ' X | 2 | X\n'
        expected += '-----------\n'
        expected += ' O | O | 6\n'
        expected += '-----------\n'
        expected += ' X | 8 | 9'
        self.assertEqual(tictactoe.to_string(state_history), expected)
Пример #3
0
 def test_next_state(self):
     state_history = []
     state = tictactoe.new_game()
     state['board'] = [['X', '', ''], ['', 'O', ''], ['', '', 'X']]
     state['player'] = 1
     state_history.append(state)
     play = (1, 2)
     expected = {
         'board': [['X', '', ''], ['', 'O', 'O'], ['', '', 'X']],
         'player': 0
     }
     self.assertEqual(tictactoe.next_state(state_history, play), expected)
Пример #4
0
    def test_winner(self):
        state_history = []
        state = tictactoe.new_game()
        state_history.append(state)

        # Horizontal win
        state['board'] = [['X', '', 'X'], ['O', 'O', 'O'], ['', '', 'X']]
        self.assertEqual(tictactoe.winner(state_history), 1)

        # Vertical win
        state['board'] = [['X', '', 'O'], ['X', 'O', ''], ['X', '', 'X']]
        self.assertEqual(tictactoe.winner(state_history), 0)

        # Diagonal win
        state['board'] = [['X', 'X', 'O'], ['', 'O', ''], ['O', '', 'X']]
        self.assertEqual(tictactoe.winner(state_history), 1)

        # No win yet
        state['board'] = [['X', '', 'X'], ['', 'O', 'O'], ['', '', 'X']]
        self.assertEqual(tictactoe.winner(state_history), -1)

        # Tied game
        state['board'] = [['X', 'O', 'X'], ['O', 'X', 'O'], ['O', 'X', 'O']]
        self.assertEqual(tictactoe.winner(state_history), -2)
Пример #5
0
class MCTSThread(threading.Thread):
    def __init__(self, game, state):
        threading.Thread.__init__(self)
        self.game = game
        self.state = state
        self.data = None

    def run(self):
        self.data = montecarlo.best_play(self.game, self.state)

    def results(self):
        return self.data


state = tictactoe.new_game()
num_threads = 5
threads = []
wins = []

# Create and start num_threads new threads
for i in range(num_threads):
    thread = MCTSThread(tictactoe, state)
    thread.start()
    threads.append(thread)

# Wait for all threads to complete execution then get results from each
for thread in threads:
    thread.join()
    wins.append(thread.results())
Пример #6
0
 def test_new_game(self):
     expected = {
         'board': [['', '', ''], ['', '', ''], ['', '', '']],
         'player': 0
     }
     self.assertEqual(tictactoe.new_game(), expected)
Пример #7
0
import tictactoe
# user interface
# DDIRections / Welcome
#the format the user should enter , how do you want your user to input

# print updated board
# ask for move  , switch player 
# Print winner
#exception Handling
# 
# 


## make  a new game stae 
_game = tictactoe.new_game()
board =  _game[2]



def print_board(board:[[str]]) -> None:
    '''
    prints TicTacToe board represented as 2D list to the console
    in the format board[column][row]
    '''

    for row in range(0,3):
        row_str = ''
        for col in range(0,3):
            row_str += board[col][row] + ' | '