Ejemplo n.º 1
0
    def move(self, r, c, game_id):
        reversi = self._conn.reversi
        games = reversi.games
        game = games.find_one({"game": game_id})

        if game['state'] == 'game_over':
            return True, 'game_over'

        board = Board(game[u'board'])
        board = board.move(r, c, 1)
        games.update({'_id': game['_id']}, {"$set": {'board': board._internal_state}, '$push': {'moves': (r, c, 1)}},
                     upsert=False)

        if not board.has_moves(2):
            self.game_over(game_id)
            return True, 'game_over'

        ai = ReversiSimpleAI()

        x, y = ai.get_best_move(board, 2)
        board = board.move(x, y, 2)

        games.update({'_id': game['_id']}, {"$set": {'board': board._internal_state}, '$push': {'moves': (x, y, 2)}},
                     upsert=False)

        if not board.has_moves(1):
            self.game_over(game_id)
            return True, 'game_over'

        return True, 'next_step'
Ejemplo n.º 2
0
from board import Board
from AI import ReversiSimpleAI

game_server = ServerProxy("http://127.0.0.1:8006")
game_id = game_server.start_game('*****@*****.**')

ai = ReversiSimpleAI()

step = 0
while True:
    print '==============================='
    board = Board(game_server.get_board_state(game_id))
    board.pprint()

    x, y = ai.get_best_move(board, 1)
    
    state = game_server.move(x, y, game_id)

    step += 1

    board = Board(game_server.get_board_state(game_id))
    print '_______________________________'
    board.pprint()
    print state
    if state[1] == 'game_over':
        if board.score(1) > board.score(2):
            print "YOU'VE WON!!!", 'in ', step, 'steps'
        if board.score(1) == board.score(2):
            print 'EQUAL SCORE', 'in ', step, 'steps'
        if board.score(1) < board.score(2):