python-3.x – if-then语句基于两个玩家python Tic-Tac-Toe程序

我写了这个基本的if-then语句tic tac toe游戏,但是它超级重复.我想知道是否有一种更简单的方法可以让它看起来更优雅,运行效率更高.我基本上想要制作一个坐标为1-9的3×3网格.然后我提示玩家1进入一个位置,并用X或O替换该位置.然后我重复,直到完成一个完成的板.这是我的代码.

import sys


def game():
    global z1, z2, z3, z4, z5, z6, z7, z8, z9
    print("Player one is X player two is O \n")

    X1 = int(input("Player one, where would you like to put your first X?: "))
    if X1 == 1:
        z1 = "X"
    elif X1 == 2:
        z2 = "X"
    elif X1 == 3:
        z3 = "X"
    elif X1 == 4:
        z4 = "X"
    elif X1 == 5:
        z5 = "X"
    elif X1 == 6:
        z6 = "X"
    elif X1 == 7:
        z7 = "X"
    elif X1 == 8:
        z8 = "X"
    elif X1 == 9:
        z9 = "X"
    print(z1, "|", z2, "|", z3)
    print(z4, "|", z5, "|", z6)
    print(z7, "|", z8, "|", z9)
    O1 = int(input("Player one, where would you like to put your first O?: "))
    if O1 == 1:
        z1 = "O"
    elif O1 == 2:
        z2 = "O"
    elif O1 == 3:
        z3 = "O"
    elif O1 == 4:
        z4 = "O"
    elif O1 == 5:
        z5 = "O"
    elif O1 == 6:
        z6 = "O"
    elif O1 == 7:
        z7 = "O"
    elif O1 == 8:
        z8 = "O"
    elif O1 == 9:
        z9 = "O"
    print(z1, "|", z2, "|", z3)
    print(z4, "|", z5, "|", z6)
    print(z7, "|", z8, "|", z9)
    X2 = int(input("Player one, where would you like to put your next X?: "))
    if X2 == 1:
        z1 = "X"
    elif X2 == 2:
        z2 = "X"
    elif X2 == 3:
        z3 = "X"
    elif X2 == 4:
        z4 = "X"
    elif X2 == 5:
        z5 = "X"
    elif X2 == 6:
        z6 = "X"
    elif X2 == 7:
        z7 = "X"
    elif X2 == 8:
        z8 = "X"
    elif X2 == 9:
        z9 = "X"
    print(z1, "|", z2, "|", z3)
    print(z4, "|", z5, "|", z6)
    print(z7, "|", z8, "|", z9)
    O2 = int(input("Player one, where would you like to put your next O?: "))
    if O2 == 1:
        z1 = "O"
    elif O2 == 2:
        z2 = "O"
    elif O2 == 3:
        z3 = "O"
    elif O2 == 4:
        z4 = "O"
    elif O2 == 5:
        z5 = "O"
    elif O2 == 6:
        z6 = "O"
    elif O2 == 7:
        z7 = "O"
    elif O2 == 8:
        z8 = "O"
    elif O2 == 9:
        z9 = "O"
    print(z1, "|", z2, "|", z3)
    print(z4, "|", z5, "|", z6)
    print(z7, "|", z8, "|", z9)
    X3 = int(input("Player one, where would you like to put your next X?: "))
    if X3 == 1:
        z1 = "X"
    elif X3 == 2:
        z2 = "X"
    elif X3 == 3:
        z3 = "X"
    elif X3 == 4:
        z4 = "X"
    elif X3 == 5:
        z5 = "X"
    elif X3 == 6:
        z6 = "X"
    elif X3 == 7:
        z7 = "X"
    elif X3 == 8:
        z8 = "X"
    elif X3 == 9:
        z9 = "X"
    board()
    print(z1, "|", z2, "|", z3)
    print(z4, "|", z5, "|", z6)
    print(z7, "|", z8, "|", z9)
    O3 = int(input("Player one, where would you like to put your next O?: "))
    if O3 == 1:
        z1 = "O"
    elif O3 == 2:
        z2 = "O"
    elif O3 == 3:
        z3 = "O"
    elif O3 == 4:
        z4 = "O"
    elif O3 == 5:
        z5 = "O"
    elif O3 == 6:
        z6 = "O"
    elif O3 == 7:
        z7 = "O"
    elif O3 == 8:
        z8 = "O"
    elif O3 == 9:
        z9 = "O"
    board()
    print(z1, "|", z2, "|", z3)
    print(z4, "|", z5, "|", z6)
    print(z7, "|", z8, "|", z9)
    X4 = int(input("Player one, where would you like to put your next X?: "))
    if X4 == 1:
        z1 = "X"
    elif X4 == 2:
        z2 = "X"
    elif X4 == 3:
        z3 = "X"
    elif X4 == 4:
        z4 = "X"
    elif X4 == 5:
        z5 = "X"
    elif X4 == 6:
        z6 = "X"
    elif X4 == 7:
        z7 = "X"
    elif X4 == 8:
        z8 = "X"
    elif X4 == 9:
        z9 = "X"
    board()
    print(z1, "|", z2, "|", z3)
    print(z4, "|", z5, "|", z6)
    print(z7, "|", z8, "|", z9)
    O4 = int(input("Player one, where would you like to put your next O?: "))
    if O4 == 1:
        z1 = "O"
    elif O4 == 2:
        z2 = "O"
    elif O4 == 3:
        z3 = "O"
    elif O4 == 4:
        z4 = "O"
    elif O4 == 5:
        z5 = "O"
    elif O4 == 6:
        z6 = "O"
    elif O4 == 7:
        z7 = "O"
    elif O4 == 8:
        z8 = "O"
    elif O4 == 9:
        z9 = "O"
    board()
    print(z1, "|", z2, "|", z3)
    print(z4, "|", z5, "|", z6)
    print(z7, "|", z8, "|", z9)
    X5 = int(input("Player one, where would you like to put your next X?: "))
    if X5 == 1:
        z1 = "X"
    elif X5 == 2:
        z2 = "X"
    elif X5 == 3:
        z3 = "X"
    elif X5 == 4:
        z4 = "X"
    elif X5 == 5:
        z5 = "X"
    elif X5 == 6:
        z6 = "X"
    elif X5 == 7:
        z7 = "X"
    elif X5 == 8:
        z8 = "X"
    elif X5 == 9:
        z9 = "X"
    board()
    print(z1, "|", z2, "|", z3)
    print(z4, "|", z5, "|", z6)
    print(z7, "|", z8, "|", z9)

def board():

    if z1 == 'X' and z2 == "X" and z3 == "X":
        print(z1, "|", z2, "|", z3)
        print(z4, "|", z5, "|", z6)
        print(z7, "|", z8, "|", z9)
        print("X wins")
        sys.exit()
    elif z4 == 'X' and z5 == "X" and z6 == "X":
        print(z1, "|", z2, "|", z3)
        print(z4, "|", z5, "|", z6)
        print(z7, "|", z8, "|", z9)
        print("X wins")
        sys.exit()
    elif z7 == 'X' and z8 == "X" and z9 == "X":
        print(z1, "|", z2, "|", z3)
        print(z4, "|", z5, "|", z6)
        print(z7, "|", z8, "|", z9)
        print("X wins")
        sys.exit()
    elif z1 == 'X' and z4 == "X" and z7 == "X":
        print(z1, "|", z2, "|", z3)
        print(z4, "|", z5, "|", z6)
        print(z7, "|", z8, "|", z9)
        print("X wins")
        sys.exit()
    elif z2 == 'X' and z5 == "X" and z8 == "X":
        print(z1, "|", z2, "|", z3)
        print(z4, "|", z5, "|", z6)
        print(z7, "|", z8, "|", z9)
        print("X wins")
        sys.exit()
    elif z3 == 'X' and z6 == "X" and z9 == "X":
        print(z1, "|", z2, "|", z3)
        print(z4, "|", z5, "|", z6)
        print(z7, "|", z8, "|", z9)
        print("X wins")
        sys.exit()
    elif z1 == 'X' and z5 == "X" and z9 == "X":
        print(z1, "|", z2, "|", z3)
        print(z4, "|", z5, "|", z6)
        print(z7, "|", z8, "|", z9)
        print("X wins")
        sys.exit()
    elif z3 == 'X' and z5 == "X" and z7 == "X":
        print("X wins")
        sys.exit()
    elif z1 == 'O' and z2 == "O" and z3 == "O":
        print(z1, "|", z2, "|", z3)
        print(z4, "|", z5, "|", z6)
        print(z7, "|", z8, "|", z9)
        print("O wins")
        sys.exit()
    elif z4 == 'O' and z5 == "O" and z6 == "O":
        print(z1, "|", z2, "|", z3)
        print(z4, "|", z5, "|", z6)
        print(z7, "|", z8, "|", z9)
        print("O wins")
        sys.exit()
    elif z7 == 'O' and z8 == "O" and z9 == "O":
        print(z1, "|", z2, "|", z3)
        print(z4, "|", z5, "|", z6)
        print(z7, "|", z8, "|", z9)
        print("O wins")
        sys.exit()
    elif z1 == 'O' and z4 == "O" and z7 == "O":
        print(z1, "|", z2, "|", z3)
        print(z4, "|", z5, "|", z6)
        print(z7, "|", z8, "|", z9)
        print("O wins")
        sys.exit()
    elif z2 == 'O' and z5 == "O" and z8 == "O":
        print(z1, "|", z2, "|", z3)
        print(z4, "|", z5, "|", z6)
        print(z7, "|", z8, "|", z9)
        print("O wins")
        sys.exit()
    elif z3 == 'O' and z6 == "O" and z9 == "O":
        print(z1, "|", z2, "|", z3)
        print(z4, "|", z5, "|", z6)
        print(z7, "|", z8, "|", z9)
        print("O wins")
        sys.exit()
    elif z1 == 'O' and z5 == "O" and z9 == "O":
        print(z1, "|", z2, "|", z3)
        print(z4, "|", z5, "|", z6)
        print(z7, "|", z8, "|", z9)
        print("O wins")
        sys.exit()
    elif z3 == 'O' and z5 == "O" and z7 == "O":
        print(z1, "|", z2, "|", z3)
        print(z4, "|", z5, "|", z6)
        print(z7, "|", z8, "|", z9)
        print("O wins")
        sys.exit()

print("Here is your Tic-Tac-Toe board")

z1 = ' '
z2 = ' '
z3 = ' '
z4 = ' '
z5 = ' '
z6 = ' '
z7 = ' '
z8 = ' '
z9 = ' '
print("(1) | (2) | (3)")
print("(4) | (5) | (6)")
print("(7) | (8) | (9)")

game()

最佳答案 如果我在这里给你一个完整的脚本,我不认为我会帮助你,但我会给你一些我想出的东西,可能会让你想到一些新的方法.我(有意)没有添加任何赢/输逻辑,也不会要求每个玩家输入多个输入;但是,这应该可以帮助你扩展思路.

考虑将面向对象的方法与您已经采用的程序方法相结合.下面,游戏板被显示为一个对象(面向对象的范例),每个游戏通过一个函数(程序范例)与之交互;虽然每个玩家本身也可以是一个对象,但每个功能可以是他们各自类的方法.

class board():
    # make the entire board a dictionary that can be accessed via keys
    board = {"z1": "-", "z2": "-", "z3": "-",
             "z4": "-", "z5": "-", "z6": "-",
             "z7": "-", "z8": "-", "z9": "-"}

    # method that uses passed in variables to change board values by key
    def set_z(self, choice, player_symbol):
        self.board[choice] = player_symbol

    # method to print the current state of the board using dictionary 
    # keys
    def show_board(self):
        print(self.board["z1"], "|", self.board["z2"], "|", 
              self.board["z3"])
        print(self.board["z4"], "|", self.board["z5"], "|", 
              self.board["z6"])
        print(self.board["z7"], "|", self.board["z8"], "|",  
              elf.board["z9"])

# instantiate the board class above as an object called 'new_board'            
new_board = board()

# access the instantiated object via its setter method 'set_z'
# and print the current state of the board object
def x_player_input():
    X1 = input("Player one, where would you like to put your first X?: ")
    new_board.set_z(X1, 'X')
    new_board.show_board()

def o_player_input():
    O1 = input("Player two, where would you like to put your first o?: ")
    new_board.set_z(O1, 'O')
    new_board.show_board()

# this just calls each player input method once; you would need to think
# of a way to call these until some win/loss logic declares the game's 
# outcome
x_player_input()
o_player_input()
点赞