def playgame(mode):

    currgrid = [[' ' for i in range(gridsize)] for i in range(gridsize)]

    firstmove = True

    grid = []
    flags = []
    mines = []
    starttime = 0
    newgame = False

    showgrid(currgrid)
    print(helpmessage + " Type 'help' to show this message again.\n")

    while True:
        minesleft = numberofmines - len(flags)
        if mode == HUMAN:
            prompt = raw_input('Enter the cell ({} mines left): '.format(minesleft))
            results = [parseinput(prompt, gridsize, helpmessage + '\n')]
        else:
            if firstmove:
                results = ai.firstMove(currgrid)
                firstmove = False
            else:
                results = ai.attemptFlagMine(currgrid, minesleft, flags)
            if not results:
                results = ai.attemptMove(currgrid, flags)

        for result in results:
            (currgrid, grid, flags, starttime, mines, newgame) = respond_to_move(currgrid, grid, flags, starttime, mines, result) 
            if newgame:
                break
        if newgame:
            break
    if playagain():
        playgame(mode)      
Example #2
0
winner = None
first_move = True

legend = "123456789"
rules.printBoard(legend)
print "\nComputer goes first\n"

while not winner and not rules.isFull(board):
    # On the first turn, skip the human player to let the computer go first.
    if not first_move:
        # Player's turn
        board[ai.humanPlayer(board)] = player_char
        winner = rules.getWinner(board)

    # Computer's turn
    if not winner and not rules.isFull(board):
        if first_move:
            board[ai.firstMove()] = comp_char
        else:
            # Don't care what the score was, just take the best move.
            _, move = ai.negamax(board, comp_char)
            assert move is not None
            board[move] = comp_char
        winner = rules.getWinner(board)

    # Either the game just ended or we're on to the next round.
    first_move = False
    rules.printBoard(board)

rules.printEndGame(winner)
Example #3
0
# Pass a 2 on the command-line to let the smart algorithm go first.
if len(sys.argv) > 1 and sys.argv[1] == "2":
    naive_char, smart_char = smart_char, naive_char
    naive_goes_first = False

board = [" "] * 9
winner = None
first_move = True

while not winner and not rules.isFull(board):
    # Skip the naive player's first turn if the smart player goes first.
    if not first_move or naive_goes_first:
        # Naive player's turn.
        if first_move:
            board[ai.firstMove()] = naive_char
        else:
            board[ai.tryToBeSmart(board, naive_char)] = naive_char
        winner = rules.getWinner(board)
        first_move = False

    if not winner and not rules.isFull(board):
        # Smart player's turn
        if first_move:
            board[ai.firstMove()] = smart_char
        else:
            # Don't care what the score was, just take the best move.
            _, move = ai.negamax(board, smart_char)
            assert move is not None
            board[move] = smart_char
        winner = rules.getWinner(board)