gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 10:01:57 26 4
gpt4 key购买 nike

我写了这个基本的 if-then声明井字游戏,但它 super 重复。我想知道是否有一种更简单的方法可以让它看起来更优雅,运行更高效。我基本上想要制作一个坐标为 1-9 的 3x3 网格。然后我提示玩家一输入一个位置,该位置被替换为 XO .然后我重复直到完成一个完整的电路板。这是我的代码。

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()

关于python-3.x - 基于 if-then 语句的两个玩家 python Tic-Tac-Toe 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49786565/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com