Tic Tac掩护!!无法使程序正常工作

2024-04-16 16:45:50 发布

您现在位置:Python中文网/ 问答频道 /正文

这是一个Tic-Tac-Toe程序,但它有一个特定的转折点…你可以用一个更大的xo来掩盖对手的xo。你们有中号的和大号的。由于某些原因,我无法让我的棋盘显示当前的移动,而且它似乎无法识别获胜的条件。任何帮助都将不胜感激。你知道吗

#Winner checker.

def player_done(playerSym, board):
    return ((board[6] == playerSym and board[7] == playerSym and board[8] == playerSym) or # across the top
    (board[3] == playerSym and board[4] == playerSym and board[5] == playerSym) or # across the middle
    (board[0] == playerSym and board[1] == playerSym and board[2] == playerSym) or # across the bottom
    (board[6] == playerSym and board[3] == playerSym and board[0] == playerSym) or # down the left side
    (board[7] == playerSym and board[4] == playerSym and board[3] == playerSym) or # down the middle
    (board[8] == playerSym and board[5] == playerSym and board[2] == playerSym) or # down the right side
    (board[6] == playerSym and board[4] == playerSym and board[2] == playerSym) or # diagonal
    (board[8] == playerSym and board[4] == playerSym and board[0] == playerSym)) # diagonal

def opponent_done(oppSym, board):
    return ((board[6] == oppSym and board[7] == oppSym and board[8] == oppSym) or # across the top
    (board[3] == oppSym and board[4] == oppSym and board[5] == oppSym) or # across the middle
    (board[0] == oppSym and board[1] == oppSym and board[2] == oppSym) or # across the bottom
    (board[6] == oppSym and board[3] == oppSym and board[0] == oppSym) or # down the left side
    (board[7] == oppSym and board[4] == oppSym and board[1] == oppSym) or # down the middle
    (board[8] == oppSym and board[5] == oppSym and board[2] == oppSym) or # down the right side
    (board[6] == oppSym and board[4] == oppSym and board[2] == oppSym) or # diagonal
    (board[8] == oppSym and board[4] == oppSym and board[0] == oppSym)) # diagonal

# Sets up the Board

def drawBoard(board):
    print('   |   |')
    print(' ' + board[6] + ' | ' + board[7] + ' | ' + board[8])
    print('   |   |')
    print('-----------')
    print('   |   |')
    print(' ' + board[5] + ' | ' + board[4] + ' | ' + board[3])
    print('   |   |')
    print('-----------')
    print('   |   |')
    print(' ' + board[0] + ' | ' + board[1] + ' | ' + board[2])
    print('   |   |')

#Prints the current board

def print_board(board):
    # Make a duplicate of the board list and return it the duplicate.
    dupeBoard = []
    for i in board:
        dupeBoard.append(i)
    return dupeBoard

#The Board

board = ["0", "1", "2", "3", "4", "5", "6", "7", "8"]

done = False

#The Move list for X's and O's

xmovesList = ["*","x","X"]
omovesList = [".","o","O"]

#Greeting message and choosing the initial X and O assignment.
print "Welcome to Tic-Tac-Toe-Cover-Up!"
playerSym = raw_input("Player 1, please select either X or O for your pieces: ")
print

#Player and opponent move assignment
if (playerSym == "X"):
    playerSym = xmovesList
    oppSym = omovesList
    turn = "X"
else:
    playerSym = omovesList
    oppSym = xmovesList
    turn = "O"

#Main game loop.
while not done:
    drawBoard(board)


    print turn,"'s turn"
    if (turn == "X"):
        player = xmovesList
        opponent = omovesList
    else:
        player = omovesList
        opponent = xmovesList

    print "Select the place you want to play (0-8)"
    print_board(board)

    pos = input("Select: ")
    if pos <=8 and pos >=0:
        Y = pos/3
        X = pos%3
        if X != -1:
            X -=1
        else:
            X = 2
            Y -=1

        if board == " ":
            board = player[9]
            moved = True
            done = player()
        elif board != " ":
            if board == opponent[0]:
                board = player[1]
                moved = True
                done = player_done()
                done = opponent_done()
            if board == opponent[1]:
                board = player[2]
                moved = True
                done = player_done()
                done = opponent_done()

        if done == False:
            if turn == "X":
                turn = "O"
            else:
                turn = "X"

Tags: orandtheboardifturndownacross
1条回答
网友
1楼 · 发布于 2024-04-16 16:45:50

它有很多问题。 对于空白,您将其与“”进行比较,因为您正在用1-9初始化板 还有一个主要的错误:

board = player[9]

这是你需要的

board[pos] = player[0]

您还需要修正检查逻辑:

board[6] in playerSym  # and not board[6] == playerSym 

这是一个有一些修正的程序。你知道吗

#Winner checker.

def check_done(playerSym,board):
    return ((board[6] in playerSym and board[7] in playerSym and board[8] in playerSym) or # across the top
    (board[3] in playerSym and board[4] in playerSym and board[5] in playerSym) or # across the middle
    (board[0] in playerSym and board[1] in playerSym and board[2] in playerSym) or # across the bottom
    (board[6] in playerSym and board[3] in playerSym and board[0] in playerSym) or # down the left side
    (board[7] in playerSym and board[4] in playerSym and board[3] in playerSym) or # down the middle
    (board[8] in playerSym and board[5] in playerSym and board[2] in playerSym) or # down the right side
    (board[6] in playerSym and board[4] in playerSym and board[2] in playerSym) or # diagonal
    (board[8] in playerSym and board[4] in playerSym and board[0] in playerSym)) # diagonal


# Sets up the Board

def drawBoard(board):
    print('   |   |')
    print(' ' + board[6] + ' | ' + board[7] + ' | ' + board[8])
    print('   |   |')
    print('     -')
    print('   |   |')
    print(' ' + board[5] + ' | ' + board[4] + ' | ' + board[3])
    print('   |   |')
    print('     -')
    print('   |   |')
    print(' ' + board[0] + ' | ' + board[1] + ' | ' + board[2])
    print('   |   |')

#Verifies if board is not full with all largest signs and no mpre moves are possible.
def movesPossible(board):
    #TODO: implement this
    return True

#Prints the current board

def print_board(board):
    # Make a duplicate of the board list and return it the duplicate.
    dupeBoard = []
    for i in board:
        dupeBoard.append(i)
    return dupeBoard

#The Board

board = ["0", "1", "2", "3", "4", "5", "6", "7", "8"]

done = False

#The Move list for X's and O's

xmovesList = ["*","x","X"]
omovesList = [".","o","O"]

#Greeting message and choosing the initial X and O assignment.
print "Welcome to Tic-Tac-Toe-Cover-Up!"
playerSym = raw_input("Player 1, please select either X or O for your pieces: ")
print

#Player and opponent move assignment
if (playerSym == "X"):
    player = xmovesList
    opponent = omovesList
else:
    player = omovesList
    opponent = xmovesList

drawBoard(board)

#Main game loop.
while movesPossible(board):
    print player[2],"'s turn"
    print "Select the place you want to play (0-8)"
    print_board(board)
    moved = False
    pos = input("Select: ")
    if pos <=8 and pos >=0:
        Y = pos/3
        X = pos%3
        if X != -1:
            X -=1
        else:
            X = 2
            Y -=1

        if board[pos] == str(pos):
            board[pos] = player[0]
            moved = True
            done = check_done(player,board)
        else :
            if board[pos] == opponent[0]:
                board[pos] = player[1]
                moved = True
                done = check_done(player,board)
            if board[pos] == opponent[1]:
                board[pos] = player[2]
                moved = True
                done = check_done(player,board)
    drawBoard(board)
    if done:
        break;
    if moved :
        player,opponent = opponent,player

if(done):
    print player[2], "wins "
else:
    print "No more moves possible. It's a draw."

相关问题 更多 >