我在用python制作的一个ticTacToe游戏中遇到了一个错误,无法正确解决这个错误

2024-04-19 03:48:06 发布

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

我正在为我所在的康普西班做一个tic-tac-toe游戏,我已经花了大约一个小时来研究这个问题了,完全没有运气,也没有头绪,如果有人能看一下,看看我在哪里傻了,我会非常感激的。 因此,每当我运行下面的tic-tac-toe程序时,我都会得到以下错误:

Traceback (most recent call last):
  File     "C:\Users\kolton\Documents\Python\Josh\compSci\projects\games\ticTacToe\ticTacTo    ev3.py", line 125, in <module>
    move = playerMove(playBoard)
  File     "C:\Users\kolton\Documents\Python\Josh\compSci\projects\games\ticTacToe\ticTacTo    ev3.py", line 68, in playerMove
    move = input("Which space would you like to place your ", pLetter, " in?        (1-9)\n Answer:")
TypeError: input expected at most 1 arguments, got 3

以下是实际程序:

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

def playerLetter():
    letter=""
    while not (letter=="X" or letter=="O"):
        print("Would you like to be X's or O's?(x/o)\nAnswer: ")
        letter=input().upper()
    if letter=="X":
        return["X", "O"]
    else:
        return["O", "X"]
def whoGoesFirst():
    firstN=random.randrange(1, 2)+1
    if firstN ==1:
        return "computer"
    else:
        return "player"

def playAgain():
    keepPlaying = input("Would you like to play again?(y/n)")
    keepPlaying = keepPlaying.lower()
    if keepPlaying == "y":
        print("Cool!")
        play = "y"
    elif keepPlaying == "n":
        print("Darn, well thanks for playing.")
        play = "n"
    else:
        print("You can only answer with an 'n' or an 'a'")

def move(board, pLetter, move):
    board[move]=pLetter

def won(board, pLetter):
    return ((board[1]==letter and board[2]==letter and board[3]==letter) or
            (board[4]==letter and board[5]==letter and board[6]==letter) or
            (board[7]==letter and board[8]==letter and board[9]==letter) or
            (board[1]==letter and board[4]==letter and board[7]==letter) or
            (board[2]==letter and board[5]==letter and board[8]==letter) or
            (board[3]==letter and board[6]==letter and board[9]==letter) or
            (board[1]==letter and board[5]==letter and board[9]==letter) or
            (board[7]==letter and board[5]==letter and board[3]==letter))

def copyBoard(board):
    boardCopy=[]
    for m in board:
        boardCopy.append(m)
    return boardCopy

def isSpaceFree(board, move):
    return board[move]==" "

def playerMove(board):
    move=""
    while move not in "1 2 3 4 5 6 7 8 9".split() or not isSpaceFree(board, int(move)):
        move = input("Which space would you like to place your ", pLetter,  " in?(1-9)\n Answer:")
    return int(move)

def doRandomMove(board, moveList):
    possibleMoves=[]
    for m in moveList:
        if isSpaceFree(board, i):
            possibleMoves.append(m)
    if len(possibleMoves)!=0:
        return random.choice(possibleMoves)
    else:
        return None

def getCMove(board, cLetter):
    for n in range(1, 10):
        copy = boardCopy(board)
        if isSpaceFree(copy, m):
            makeMove(copy, cLetter, m)
            if won(copy, cLetter):
                return m
    for m in range(1, 10):
        copy = boardCopy(board)
        if isSpaceFree(copy, m):
            makeMove(copy, pLetter, m)
            if won(copy, pLetter):
                return m
    move = chooseRandomMoveFromList(board, [1, 3, 7, 9])
    if move != None:
        return move
    if ifSpaceFree(board, 5):
        return 5
    return chooseRandomMoveFromList(board, [2, 4, 6, 8])

def fullBoard (board):
    for s in range(1, 10):
        if isSpaceFree(board, s):
            return False
    return True

print ("""\n
`7MMF'     A     `7MF'     `7MM                                           
  `MA     ,MA     ,V         MM                                           
   VM:   ,VVM:   ,V .gP"Ya   MM  ,p6"bo   ,pW"Wq.`7MMpMMMb.pMMMb.  .gP"Ya 
    MM.  M' MM.  M',M'   Yb  MM 6M'  OO  6W'   `Wb MM    MM    MM ,M'   Yb
    `MM A'  `MM A' 8M8M8M8M  MM 8M       8M     M8 MM    MM    MM 8M8M8M8M
     :MM;    :MM;  YM.    ,  MM YM.    , YA.   ,A9 MM    MM    MM YM.    ,
      VF      VF    `Mbmmd'.JMML.YMbmd'   `Ybmd9'.JMML  JMML  JMML.`Mbmmd' """)

while True:
    playBoard = [" "] * 10
    pLetter, cLetter = playerLetter()
    turn = whoGoesFirst()
    print("The ", turn, " will go first.")
    gameIsPlaying=True
    while gameIsPlaying == True:
        if turn == "player":
            drawBoard(playBoard)
            move = playerMove(playBoard)
            makeMove(playBoard, pLetter, move)
            if isWinner(playBoard, pLetter):
                drawBoard(playBoard)
                print("Congrats!!!")
                gameIsPlaying = False
            else:
                if fullBoard(playBoard):
                    drawBoard(playBoard)
                    print("The game is a tie.")
                    break
                else:
                    turn = "computer"
        else:
            move = getCMove(theBoard, cLetter)
            move(playBoard, cLetter, move)
            if won(playBoard, cLetter, move):
                drawBoard(playBoard)
                print("The computer won the game.")
                gameIsPlaying = False
            else:
                if fullBoard(playBoard):
                    drawBoard(playBoard)
                    print("It's a tie!")
                    break
                else:
                    turn = "player"

        if not playAgain():
            break    

Tags: orandinboardmovereturnifdef
1条回答
网友
1楼 · 发布于 2024-04-19 03:48:06

这个错误说得很清楚

TypeError: input expected at most 1 arguments, got 3

在文档中,hereinputraw_input只接受一个参数(prompt)。您应该在使用之前创建一个字符串,或者最好使用字符串解释。Check-here它有很好的例子。你知道吗

相关问题 更多 >