首页 > 解决方案 > Python 3井字游戏的问题

问题描述

  1. 玩家 xyz 赢得的输出在赢得一次机会后被打印出来。
  2. 即使玩家 2 获胜,输出也会显示玩家 1 获胜 我已经尝试了一些尝试并尝试自己解决它,但直到现在都没有运气。此外,如果您有任何建议可以使代码更高效或更美观,请将其放在下面。我的代码:
def printrow(r1,r2,r3):
    print(r1)
    print(r2)
    print(r3)
def empty(inp):
    if inp!='-':
        print("Please enter a position which is empty")
    else:
        return 'valid'
def user1(inp):
    if inp not in range(1,10):
        print("Please enter a number in the range of 1 to 9")
    else:
        if inp==1:
            if empty(row1[0])=='valid':
                row1[0]='X'
                printrow(row1,row2,row3)
        elif inp==2:
            if empty(row1[1])=='valid':
                row1[1]='X'
                printrow(row1,row2,row3)
        elif inp==3:
            if empty(row1[2])=='valid':
                row1[2]='X'
                printrow(row1,row2,row3)
        elif inp==4:
            if empty(row2[0])=='valid':
                row2[0]='X'
                printrow(row1,row2,row3)
        elif inp==5:
            if empty(row2[1])=='valid':
                row2[1]='X'
                printrow(row1,row2,row3)
        elif inp==6:
            if empty(row2[2])=='valid':
                row2[2]='X'
                printrow(row1,row2,row3)
        elif inp==7:
            if empty(row3[0])=='valid':
                row3[0]='X'
                printrow(row1,row2,row3)
        elif inp==8:
            if empty(row3[1])=='valid':
                row3[1]='X'
                printrow(row1,row2,row3)
        elif inp==9:
            if empty(row3[2])=='valid':
                row3[2]='X'
                printrow(row1,row2,row3)
def user2(inp):
        if inp not in range(1, 10):
            print("Please enter a number in the range of 1 to 9")
        else:
            if inp == 1:
                if empty(row1[0]) == 'valid':
                    row1[0] = '0'
                    printrow(row1,row2,row3)
            elif inp == 2:
                if empty(row1[1]) == 'valid':
                    row1[1] = '0'
                    printrow(row1,row2,row3)
            elif inp == 3:
                if empty(row1[2]) == 'valid':
                    row1[2] = '0'
                    printrow(row1,row2,row3)
            elif inp == 4:
                if empty(row2[0]) == 'valid':
                    row2[0] = '0'
                    printrow(row1,row2,row3)
            elif inp == 5:
                if empty(row2[1]) == 'valid':
                    row2[1] = '0'
                    printrow(row1,row2,row3)
            elif inp == 6:
                if empty(row2[2]) == 'valid':
                    row2[2] = '0'
                    printrow(row1,row2,row3)
            elif inp == 7:
                if empty(row3[0]) == 'valid':
                    row3[0] = '0'
                    printrow(row1,row2,row3)
            elif inp == 8:
                if empty(row3[1]) == 'valid':
                    row3[1] = '0'
                    printrow(row1,row2,row3)
            elif inp == 9:
                if empty(row3[2]) == 'valid':
                    row3[2] = '0'
                    printrow(row1,row2,row3)
def check(r1,r2,r3):
            if r1[0] == 'X' and r1[1] == 'X' and r1[2] == 'X':
                return "Congratulations Player 1! You won."
            elif r1[0] == 'X' and r2[0] == 'X' and r3[0] == 'X':
                return "Congratulations Player 1! You won."
            elif r1[0] == 'X' and r2[1] == 'X' and r3[2] == 'X':
                return "Congratulations Player 1! You won."
            elif r1[1] == 'X' and r2[1] == 'X' and r3[1] == 'X':
                return "Congratulations Player 1! You won."
            elif r1[2] == 'X' and r2[2] == 'X' and r3[2] == 'X':
                return "Congratulations Player 1! You won."
            elif r1[2] == 'X' and r2[1] == 'X' and r3[0] == 'X':
                return "Congratulations Player 1! You won."
            elif r2[0] == 'X' and r2[1] == 'X' and r2[2] == 'X':
                return "Congratulations Player 1! You won."
            elif r3[0] == 'X' and r3[1] == 'X' and r3[2] == 'X':
                return "Congratulations Player 1! You won."
            elif r1[0] == '0' and r1[1] == '0' and r1[2] == '0':
                return "Congratulations Player 2! You won."
            elif r1[0] == '0' and r2[0] == '0' and r3[0] == '0':
                return "Congratulations Player 2! You won."
            elif r1[0] == '0' and r2[1] == '0' and r3[2] == '0':
                return "Congratulations Player 2! You won."
            elif r1[1] == '0' and r2[1] == '0' and r3[1] == '0':
                return "Congratulations Player 2! You won."
            elif r1[2] == '0' and r2[2] == '0' and r3[2] == '0':
                return "Congratulations Player 2! You won."
            elif r1[2] == '0' and r2[1] == '0' and r3[0] == '0':
                return "Congratulations Player 2! You won."
            elif r2[0] == '0' and r2[1] == '0' and r2[2] == '0':
                return "Congratulations Player 2! You won."
            elif r3[0] == '0' and r3[1] == '0' and r3[2] == '0':
                return "Congratulations Player 2! You won."
print("[1,2,3]\n[4,5,6]\n[7,8,9]")
count=0; winner=False
row1=['-','-','-']
row2= ['-','-','-']
row3=['-','-','-']
print("Welcome to Tic Tac Toe")
print("Player 1 will be X and will start the game, and player 2 will be 0 and play second.")
while winner == False:
    if count<9:
        count+=1
        if type(check(row1,row2,row3))==str:
            print(check(row1,row2,row3))
        else:
            inp=int(input("Player 1:  Where do you want to insert the next X?    "))
            user1(inp)
            inp=int(input("Player 2:  Where do you want to insert the next 0?    "))
            user2(inp)

标签: pythonpython-3.xtic-tac-toe

解决方案


问题#1:调用后循环不会停止check(),所以游戏会继续。您可以调用break退出循环并结束游戏。

问题 2:我尝试运行您的代码,它说玩家 2 赢了。

Player 1:  Where do you want to insert the next X?    4
['X', 'X', '0']
['X', '0', '-']
['-', '-', '-']
Player 2:  Where do you want to insert the next 0?    7
['X', 'X', '0']
['X', '0', '-']
['0', '-', '-']
Congratulations Player 2! You won.
Congratulations Player 2! You won.
Congratulations Player 2! You won.
Congratulations Player 2! You won.
Congratulations Player 2! You won.
Congratulations Player 2! You won.

一般反馈:当代码重复多次时,这是一个令人担忧的迹象。编写起来需要更多的努力,更难理解,而且更容易出现错误。一个好的经验法则是,当您发现自己多次复制/粘贴代码时,请改用循环!在这种情况下,您可能不需要所有这些ifs。而且您可能不需要复制播放器 1 和播放器 2 的代码 - 您能想出一种方法来为他们两个重用相同的代码吗?也许是一个功能?


推荐阅读