【发布时间】:2020-11-01 14:30:37
【问题描述】:
我在制作 TIC TAC TOE 游戏时尝试了我的第一个代码,主要逻辑工作正常。我缺乏用户界面的编码专业知识,任何人都可以帮助我以行和列 o/p 和“X”或“O”的形式从应用程序中获取输入,它们在主逻辑的帮助下相互切换。
这是我的代码:
#below code is for runing python TIC TAC TOE
#Check for result
def check_array(arr):
for i in range(3):
set = True
# print("Checking array value {}".format(arr[i][0]))
for j in range (1,3):
# print("Element {0}{1}= {2}".format(i,j,arr[i][j]))
# print("compared {0} with {1}".format(arr[i][0],arr[i][j]))
if arr[i][j]==" ":
set = False
# print("False")
break
if arr[i][0]!=arr[i][j]:
set = False
# print("False")
break
# print("Value of j = {0} and set = {1}".format(j,set))
if set==True:
break
# print("Checked array horizontaly")
if set==True:
print("Horizontal Strike ")
return 0
# print("Checking Vertical Strike")
for i in range(3):
set = True
for j in range(1,3):
# print(arr[0][i],end=" with ")
# print(arr[j][i])
if arr[j][i]==" ":
set = False
# print("False")
# print("BREAKING")
break
if arr[0][i]!=arr[j][i]:
set = False
# # print("BREAKING")
break
if set==True:
break
if set==True:
print("Vertical Strike")
return 0
if arr[1][1] == " ":
set = False
# print("False")
elif arr[1][1]==arr[0][0] and arr[1][1]==arr[2][2]:
set=True
print("Diagonal Strike")
return 0
elif arr[1][1]==arr[2][0] and arr[1][1]==arr[0][2]:
set=True
print("Diagonal Strike")
return 0
else:
set=False
#get location of mark
def Get_Loc():
try:
i =Get_value()
except ValueError:
print("Value Error please enter a int value")
Get_Loc()
try:
j =Get_value()
except ValueError:
print("Value Error please enter a int value")
Get_Loc()
return i,j
def Get_value():
value=int(input("Enter value"))
return value
#check if array is full
def arr_full(arr):
set=False
for i in range(3):
for j in range(3):
# print("Array[{0},{1}]={2}".format(i,j,arr[i][j]))
if arr[i][j]==' ':
# print("RESET")
set=True
# print(set)
if set==False:
# print(set)
print("Array is Full")
return 0
def board(arr):
print("{0}_|_{1}_|_{2} ".format(arr[0][0],arr[0][1],arr[0][2]))
print("{0}_|_{1}_|_{2} ".format(arr[1][0],arr[1][1],arr[1][2]))
print("{0} | {1} | {2} ".format(arr[2][0],arr[2][1],arr[2][2]))
arr=[[" "," "," "],[" "," "," "],[" "," "," "]]
turn=True
player=['X','O']
while True:
turn = not turn
print("Player {} turn to play ".format(player[turn]))
i,j = Get_Loc()
print("Location value = '{}'".format(arr[i][j]))
if arr[i][j]!=" ":
print("Error: Location Full")
turn = not turn
continue
arr[i][j]=player[turn]
board(arr)
exit = check_array(arr)
if exit==0:# or arr_full(arr)==0:
break
print("Player {} Win".format(player[turn]))
请提及 cmets 以帮助理解您的代码
【问题讨论】:
标签: python user-interface tkinter tic-tac-toe