示例#1
0
 def test_a_game_can_be_over_once_one_player_has_won(self):
     game = TicTac()
     game.board.body = [
         ['y', 0, 0],
         ['y', 0, 0],
         ['y', 'x', 0],
     ]
     assert game.is_over()
示例#2
0
 def test_a_game_with_empty_tiles_cannot_be_over(self):
     """
     A game in which there are still empty tiles cannot be considered
     over.
     """
     game = TicTac()
     game.player_one.make_move(1, 1)
     assert not game.is_over()
示例#3
0
 def test_board_reflects_the_movement_made_by_player_one(self):
     """
     The board must reflect the movements made by each player
     after is done.
     """
     game = TicTac()
     game.player_one.make_move(1, 1)
     expected = [[0,0,0], [0, 'x', 0], [0, 0, 0]]
     assert game.show_game_board() == expected
示例#4
0
 def test_board_can_be_visualized_as_a_list_at_game_start(self):
     """
     The game board can be visualized as a list of coordinates
     at game start.
     """
     game = TicTac()
     row = [0,0,0]
     expected = [row, row, row]
     assert game.show_game_board() == expected
示例#5
0
 def test_a_game_without_a_clear_victor_is_a_draw(self):
     """
     A game without victor is a draw.
     """
     game = TicTac()
     game.board.body = [
         ['y', 'x', 'y'],
         ['y', 'x', 'y'],
         ['x', 'y', 'x'],
     ]
     assert game.is_a_draw()
示例#6
0
 def test_a_player_can_win_by_filling_a_second_diagonal(self):
     """
     A player can win by filling the second diagonal.
     """
     game = TicTac()
     game.board.body = [
         ['x', 'x', 'y'],
         ['x', 'y', 'x'],
         ['y', 'x', 'y'],
     ]
     assert game.who_won() == game.player_two
示例#7
0
 def test_a_player_can_win_by_filling_a_diagonal(self):
     """
     A player can win by filling one of the two possible
     diagonals available.
     """
     game = TicTac()
     game.board.body = [
         ['x', 'x', 'y'],
         ['y', 'x', 'y'],
         ['y', 'y', 'x'],
     ]
     assert game.who_won() == game.player_one
示例#8
0
 def test_a_player_can_win_by_having_a_column_filled(self):
     """
     The player can win by having a complete column with
     its mark.
     """
     game = TicTac()
     game.board.body = [
         ['x', 'x', 'y'],
         ['y', 'x', 'y'],
         ['x', 'y', 'y'],
     ]
     assert game.who_won() == game.player_two
示例#9
0
 def test_a_player_wons_by_having_any_row_with_its_mark(self):
     """
     The first player can win by having any row complete
     with its mark.
     """
     game = TicTac()
     game.board.body = [
         ['x', 'x', 'x'],
         ['x', 'y', 'y'],
         ['y', 'y', 'x'],
     ]
     assert game.who_won() == game.player_one
示例#10
0
 def test_a_without_empty_tiles_can_be_considered_over(self):
     """
     A game in which therer are no more empty tiles can
     be considered over.
     """
     game = TicTac()
     game.board.body = [
         ['x', 'x', 'y'],
         ['y', 'x', 'x'],
         ['x', 'y', 'x'],
     ]
     assert game.is_over()
def play_games():
    won_games = []
    with tf.name_scope('play_game'):
        for _ in range(ITTERCOUNT):
            #generate new game
            game = TicTac(net=True, rand=True)

            # Get board state as flat vector
            inputs = list(game.boards[0])
            inputs.extend(game.boards[1])

            res = sess.run(results, feed_dict={x: [inputs]})

            while game.winner == False:
                game.visual()
                game.doturn(netvals=res[0])
                game.winner = game.check_win()
            if game.history[0] == 'X':
                #learn the last two moves
                won_games.append(game.history[1:])
        #take winning games and build training data
        print(len(won_games))
        for game in won_games:
            for move in game:
                inputvals.append(move[0])
                targetvals.append(vote_for(move[1]))
        print(len(won_games) / float(ITTERCOUNT))
示例#12
0
文件: main.py 项目: CMLL/tdd_tic_tac
def play():
    """
    Simple interaction with a game of tic tac toe.
    """
    print "Welcome to the game of Tic Tac Toe."
    game = TicTac()
    while not game.is_over():
        for row in game.show_game_board():
            pprint(row)
        print "Make a move: "
        try:
            input_x = int(raw_input('Position X: '))
            input_y = int(raw_input('Position Y: '))
            try:
                game.player_one.make_move(input_x, input_y)
            except NotInTurnError:
                game.player_two.make_move(input_x, input_y)
        except InvalidMoveError as error:
            print error.message
    if game.is_a_draw():
        print "Ended in a draw."
    else:
        for row in game.show_game_board():
            pprint(row)
        print 'Player {} won.'.format(game.who_won().player_mark)
示例#13
0
        t1.bclick(int(data))

    # example of an action
    # action: notify
    async def notify_users(self):
        '''notify the number of current connected clients'''
        if self.USERS:  # asyncio.wait doesn't accept an empty list
            message = json.dumps({'type': 'users', 'count': len(self.USERS)})
            await asyncio.wait([user.send(message) for user in self.USERS])

    # action: action
    async def action(self, cnt):
        '''this is an action which will be executed when user presses on button'''
        print(cnt)
        if self.USERS:  # asyncio.wait doesn't accept an empty list
            message = str(cnt)
            await asyncio.wait([user.send(message) for user in self.USERS])

    # expose action
    def do_activate(self, cnt):
        '''this method is exposed to outside, not an async coroutine'''
        # use asyncio to run action
        # must call self.action(), not use self.action, because it must be a async coroutine
        asyncio.get_event_loop().run_until_complete(self.action(cnt))


threadWebSocket = WebSocketThread("websocket_server")
threadWebSocket.start()
t1 = TicTac(threadob=threadWebSocket)
t1.root.mainloop()
示例#14
0
 def test_a_newly_started_game_cannot_be_over(self):
     """
     A new game cannot be considered over.
     """
     game = TicTac()
     assert not game.is_over()
示例#15
0
def main():
    print("Welcome to Tic Tac Toe")
    game = TicTac()
    game.print()
    while True:
        game.player_turn()
        game.cats()
        game.find_win()
        if game.cats() or game.find_win():
            break
        game.computer_turn()
        game.cats()
        game.find_win()
        if game.cats() or game.find_win():
            break