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

时间:2013-10-19 23:08:11

标签: python python-2.7

这是一个Tic Tac Toe计划,但它有一定的扭曲......你能够用更大的x掩盖对手的oxo。你有中等件和大件。出于某种原因,我无法让我的电路板显示当前的动作,而且似乎无法识别获胜条件。任何帮助将不胜感激。

#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"

1 个答案:

答案 0 :(得分:0)

它有很多问题。 对于空虚你用“”来比较你在哪里用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."