示例#1
0
def play_tictactoe():
    """
    Executes TicTacToe between two players
    """
    print "Size of Tic Tac Toe board: 3"
    size = 3

    marker = raw_input("Choose the marker u want to use from %s: " % MARKER)
    if marker not in MARKER:
        print "wrong marker chossen"
        return

    t = TicTacToe(size)
    print "Tic Tac Toe board"
    t.display()

    turn = MARKER.index(marker)
    player = 0
    inpt = ''

    while (inpt not in QUIT):
        try:
            if player != 0:
                make_move_cpu(t, MARKER[turn], inpt)
            else:
                inpt = raw_input("Player %s make your move: " % (player + 1))
                t.make_move(inpt, MARKER[turn])
            t.display()
            if t.is_winner(MARKER[turn]):
                print "Player %s wins" % (
                    (player + 1) if player == 0 else 'CPU')
                print "Game End"
                return
            turn = turn ^ 1
            player = player ^ 1
        except TieGameException as e:
            print(e)
            return
        except InvalidMoveException as e:
            print(e)
        except Exception as e:
            print "Error: Exiting game"
            return
示例#2
0
    'rounds': 100000,
}

bot = Bot(name=1, q_table=Tools.load_source(bot_desc))
# bot starts first, but alternatively afterwards
start_turn = bot.name
while True:
    game = TicTacToe()
    game.restart(start=start_turn)
    whose_turn = start_turn
    start_turn = start_turn % 2 + 1
    print('game start : ')
    while True:
        if whose_turn is bot.name:
            # bot action
            action = bot.take_turn(game.table)
        else:
            # user action
            action = (int(input('row:')),
                      int(input('col:')))
        game.turn(action)
        game.display()
        whose_turn = whose_turn % 2 + 1
        winner = game.winner()
        if winner is not None:
            if winner is bot.name:
                print('bot won')
            else:
                print('you won')
            break
 def test_display(self):
     game = TicTacToe()
     for i in range(3):
         game.turn((0, i))
         game.turn((1, i))
     game.display()
示例#4
0
from tictactoe import TicTacToe
from agent import GameBot

game = TicTacToe()

bot_turn = 2
player_turn = 1 if bot_turn is 2 else 2

bot = GameBot(player=bot_turn)

turn = 1
game.display()

while game.get_winner() == -1:
    print("----------")
    if turn == bot_turn:
        game = TicTacToe.next_state(game, bot_turn, bot.get_move(game))
    else:
        row = int(input("Row: "))
        col = int(input("Column: "))
        print("----------")

        game = TicTacToe.next_state(game, player_turn, row * 3 + col)

    turn = 1 if turn is 2 else 2
    game.display()

if game.get_winner() is 0:
    print("The game was a draw!")
else:
    print("Player {} wins!".format(game.get_winner()))