Exemple #1
0
def handle(wc, agent):
    try:
        t = wc.obj['@type']
        if t == MOVE_MSG_TYPE:
            them = wc.obj.get('ill_be', '')
            if them and isinstance(them, str) and len(them) == 1 and them in 'XO':
                them = them.strip().upper()
            else:
                raise Exception('Expected "ill_be" to contain either "X" or "O".')
            moves = wc.obj.get('moves', [])
            if not isinstance(moves, list) or len(moves) > 9:
                raise Exception('Expected "moves" to be a list of at most 9 items.')
            g = game.Game()
            g.load(moves)
            w = g.winner()
            if w:
                agent.trans.send('{"@type": "result", "outcome": "%s won."}')
            me = game.other_player(them)
            if g.whose_turn()

            if them == 'X':
                g.load(wc.obj['moves'])
                choice = ai.next_move(g, me)
                g[choice] = me
            agent.trans.send('{"@type": "%s"}' % MOVE_MSG_TYPE, wc.sender)
    except Exception as e:
        agent.trans.send('{"@type": "problem-report", "explain_ltxt": "%s"}', wc.sender)
    return True
 def test_head_to_head(self):
     # Make the AI play against itself a bunch of times. Randomize which
     # side starts. Every game should take a full 9 moves, and every gamee
     # should end with a draw.
     for i in range(100):
         g = game.Game()
         player = random.choice('XO')
         n = 0
         while True:
             cell = ai.next_move(g, player)
             g[cell] = player
             n += 1
             w = g.winner()
             if w:
                 if w != g.first or n != 9:
                     self.fail('Game won unexpectedly:\n%s.' % str(g))
                 break
             if n == 9:
                 break
             player = game.other_player(player)
Exemple #3
0
if __name__ == '__main__':
    import random
    import sys

    import ai
    try:
        while True:
            print("\nYou be Xs, I'll be Os.")
            g = Game()
            player = random.choice('XO')
            if player == 'O':
                print("I'll go first.")
            w = None
            for i in range(9):
                if player == 'O':
                    choice = ai.next_move(g, player)
                    print('My move: %s' % choice)
                    g[choice] = player
                else:
                    print(str(g))
                    sys.stdout.write('Your move: ')
                    x = input().strip()
                    g[x] = 'X'
                w = g.winner()
                if w:
                    break
                player = other_player(player)
            print(str(g))
            if w == 'X':
                print('You win!')
            elif w == 'O':
 def test_first_move(self):
     g = game.Game()
     self.assertEqual('B2', ai.next_move(g, 'x'))
Exemple #5
0
def main():
    clock = pg.time.Clock()
    now_turn = True

    board = initBoard()
    # mouse_pos = None
    active_pile = None

    finished = False
    winner = None

    string = "这次一定啦啦啦啦啦啦啦啦啦"

    index = 0

    while True:
        clock.tick(30)
        left_click = False
        right_click = False

        for event in pg.event.get():
            if event.type == QUIT:
                pg.quit()
                quit()
                break

            elif event.type == MOUSEBUTTONDOWN:
                mouse_pos = event.pos

                if (event.button == 1):
                    if finished:
                        board = initBoard()
                        finished = True
                        now_turn = True
                    else:
                        left_click = True
                elif (event.button == 3):
                    right_click = True

        if (now_turn and left_click):
            suc = fillTile(string[index], mouse_pos, board)
            index += 1
            if suc:
                now_turn = not now_turn
        elif (not now_turn and right_click):
            suc = fillTile(string[index], mouse_pos, board)
            index += 1
            if suc:
                now_turn = not now_turn

        if ((not now_turn) and AI):
            ai_x, ai_y = ai.next_move(board, False)
            if (ai_x != None):
                board[ai_y][ai_x] = "X"
                now_turn = not now_turn

        winner = checkWin(board)

        if winner:
            finished = True

        WINDOW.fill((0, 0, 0))

        if (finished):
            drawWin(winner)
        else:
            drawBoard(board)

        pg.display.update()
Exemple #6
0
 async def make_ai_move(self, channel, g):
     move = ai.next_move(g)
     await channel.send("!put {}".format(move))
     await self.make_move(channel, g, self.bot.user, move)