我做了这个代码井字棋在python和迭代后,它的工作正常,但在计算机之间停止输入.我的代码涉及到一个用户与计算机游戏.请帮助我,我知道我的代码不包含注解,但我希望你能理解,我是一个初学者,所以请帮助我!!!
在这段代码中,计算机首先让你选择O
或X
,然后进行抛硬币,你输入1或0,并将其与一个在1和0之间的随机值进行比较。
对于计算机,我使用了randint()
函数,以便它选择放置X
或O
的行和列。在每次迭代中,代码都会检查计算机获得的随机行或列是否与已经填充的行或列不匹配。所以这就是所有关于此代码的内容!!
import random
row1=None
column1=None
row2 = None
column2 = None
data1=[]
data2=[]
data3=[]
data4=[]
count=0
count2=0
print("Welcome to TIC-TAC-TOE!!")
tic=input("Enter O or X as your choice --> ")
if tic=='O':
tac='X'
else:
tac='O'
block=[[' ',' ',' '],[' ',' ',' '],[' ',' ',' ']]
i=0
choice=int(input("Enter 1 for Head and 0 for Tail --> "))
x=random.randint(0,1)
if choice==x:
print("You won the toss you get the first chance : ")
print("## Enter the row and column you want to put your choice in ##")
row1=int(input("Row --> "))
data1.append(row1)
column1=int(input("Column --> "))
data2.append(column1)
block[row1-1][column1-1]=tic
print(block[0][0],"|",block[0][1],"|",block[0][2])
print(block[1][0],"|",block[1][1],"|",block[1][2])
print(block[2][0],"|",block[2][1],"|",block[2][2])
while True:
i+=1
if block[0][0]==block[0][1] and block[0][1]==block[0][2] and block[0][0]!=' ':
if block[0][0]==tic:
print("You Win!!")
else:
print("You Lose!!")
break
elif block[1][0]==block[1][1] and block[1][1]==block[1][2] and block[1][0]!=' ':
if block[1][0]==tic:
print("You Win!!")
else:
print('you Lose!!')
break
elif block[2][0]==block[2][1] and block[2][1]==block[2][2] and block[2][0]!=' ':
if block[2][0]==tic:
print("You Win!!")
else:
print("You Lose!!")
break
elif block[0][0]==block[1][0] and block[1][0]==block[2][0] and block[0][0]!=' ':
if block[0][0]==tic:
print("You Win!!")
else:
print("You Lose!!")
break
elif block[0][1]==block[1][1] and block[1][1]==block[2][1] and block[0][1]!=' ':
if block[0][1]==tic:
print("You Win!!")
else:
print("You Lose!!")
break
elif block[0][2]==block[1][2] and block[1][2]==block[2][2] and block[0][2]!=' ':
if block[0][2]==tic:
print("You Win!!")
else:
print("You Lose!!")
break
elif block[0][0]==block[1][1] and block[1][1]==block[2][2] and block[0][0]!=' ':
if block[0][0]==tic:
print("You Win!!")
else:
print("You Lose!!")
break
elif block[0][2]==block[1][1] and block[1][1]==block[2][0] and block[0][2]!=' ':
if block[0][2]==tic:
print("You Win!!")
else:
print("You Lose!!")
break
elif i%2!=0:
print("## A.I ##")
if row2!=None and column2!=None:
row2=random.randint(1,3)
data3.append(row2)
index1+=1
length1=len(data3)
for j in data1:
if row2==j:
count+=1
for k in range(0,index1):
if row2==data3[k]:
count2+=1
while count>0 or count2>0:
row2=random.randint(1,3)
for j in data1:
if row2 == j:
count += 1
for k in range(0, index1):
if row2 == data3[k]:
count2 += 1
data3[index1]=row2
column2=random.randint(1,3)
data4.append(column2)
index2+=1
length2=len(data4)
count=0
count2=0
for j in data2:
if row2==j:
count+=1
for k in range(0,index2):
if column2==data4[k]:
count2+=1
while count>0 or count2>0:
column2=random.randint(1,3)
for j in data2:
if row2 == j:
count += 1
for k in range(0, index2):
if column2 == data4[k]:
count2 += 1
data4[index2]=column2
block[row2-1][column2-1] = tac
else:
row2=random.randint(1,3)
data3.append(row2)
index1=0
for j in data1:
if row2==j:
count+=1
while count>0:
count=0
row2=random.randint(1,3)
data3[0]=row2
for j in data1:
if row2 == j:
count += 1
column2=random.randint(1,3)
data4.append(column2)
index2=0
count = 0
for j in data2:
if row2 == j:
count += 1
while count>0:
count=0
column2=random.randint(1,3)
data4[0]=column2
for j in data2:
if row2 == j:
count += 1
block[row2 - 1][column2 - 1] = tac
#block[row2-1][column2-1]=tac
print(block[0][0],"|",block[0][1],"|",block[0][2])
print(block[1][0],"|",block[1][1],"|",block[1][2])
print(block[2][0],"|",block[2][1],"|",block[2][2])
elif i%2==0:
print('## Your Turn ##')
row1=int(input("Row --> "))
data1.append(row1)
column1=int(input("Column --> "))
data2.append(column2)
block[row1-1][column1-1]=tic
print(block[0][0],"|",block[0][1],"|",block[0][2])
print(block[1][0],"|",block[1][1],"|",block[1][2])
print(block[2][0],"|",block[2][1],"|",block[2][2])
else:
print("Draw!!")
break
else:
print("## A.I won the toss ##")
print("## A.I ##")
row2=random.randint(1,3)
data3.append(row2)
index1=0
column2=random.randint(1,3)
data4.append(column2)
index2=0
block[row2-1][column2-1]=tac
print(block[0][0],"|",block[0][1],"|",block[0][2])
print(block[1][0],"|",block[1][1],"|",block[1][2])
print(block[2][0],"|",block[2][1],"|",block[2][2])
while True:
i+=1
if block[0][0]==block[0][1] and block[0][1]==block[0][2] and block[0][0]!=' ':
if block[0][0]==tic:
print("You Win!!")
else:
print("You Lose!!")
break
elif block[1][0]==block[1][1] and block[1][1]==block[1][2] and block[1][0]!=' ':
if block[1][0]==tic:
print("You Win!!")
else:
print('you Lose!!')
break
elif block[2][0]==block[2][1] and block[2][1]==block[2][2] and block[2][0]!=' ':
if block[2][0]==tic:
print("You Win!!")
else:
print("You Lose!!")
break
elif block[0][0]==block[1][0] and block[1][0]==block[2][0] and block[0][0]!=' ':
if block[0][0]==tic:
print("You Win!!")
else:
print("You Lose!!")
break
elif block[0][1]==block[1][1] and block[1][1]==block[2][1] and block[0][1]!=' ':
if block[0][1]==tic:
print("You Win!!")
else:
print("You Lose!!")
break
elif block[0][2]==block[1][2] and block[1][2]==block[2][2] and block[0][2]!=' ':
if block[0][2]==tic:
print("You Win!!")
else:
print("You Lose!!")
break
elif block[0][0]==block[1][1] and block[1][1]==block[2][2] and block[0][0]!=' ':
if block[0][0]==tic:
print("You Win!!")
else:
print("You Lose!!")
break
elif block[0][2]==block[1][1] and block[1][1]==block[2][0] and block[0][2]!=' ':
if block[0][2]==tic:
print("You Win!!")
else:
print("You Lose!!")
break
elif i%2==0:
print("## A.I ##")
row2=random.randint(1,3)
data3.append(row2)
index1+=1
length1 = len(data3)
for j in data1:
if row2==j:
count+=1
for k in range(0,index1):
if row2==data3[k]:
count2+=1
while count>0 or count2>0:
count=0
count2=0
row2=random.randint(1,3)
for j in data1:
if row2 == j:
count += 1
for k in range(0,index1):
if row2==data3[k]:
count2+=1
data3[index1]=row2
count=0
count2=0
column2=random.randint(1,3)
data4.append(column2)
index2+=1
length2 = len(data4)
for j in data2:
if column2==j:
count+=1
for k in range(0,index2):
if column2==data4[k]:
count2+=1
while count>0 or count2>0:
count=0
count2=0
column2=random.randint(1,3)
for j in data2:
if column2 == j:
count += 1
for k in range(0,index2):
if column2==data4[k]:
count2+=1
data4[index2]=column2
block[row2-1][column2-1]=tac
print(block[0][0],"|",block[0][1],"|",block[0][2])
print(block[1][0],"|",block[1][1],"|",block[1][2])
print(block[2][0],"|",block[2][1],"|",block[2][2])
elif i%2!=0:
print("## Your Turn ##")
row1=int(input("Row --> "))
data1.append(row1)
column1=int(input("Column --> "))
data2.append(column2)
block[row1-1][column1-1]=tic
print(block[0][0],"|",block[0][1],"|",block[0][2])
print(block[1][0],"|",block[1][1],"|",block[1][2])
print(block[2][0],"|",block[2][1],"|",block[2][2])
else:
print("Draw!!")
break
print("Thanks for playing!!")
2条答案
按热度按时间fykwrbwg1#
我稍微修改了一下代码,去掉了while循环,代之以另一个while循环,它检查该行和列所在的位置是否白色,如果没有,那么列继续随机变化,直到有一个地方有空白。现在对于行,我只是检查整行是否被占用,如果是,那么行继续随机变化。这整个逻辑被改变了,只是轮到计算机。和猜猜我减少了代码从300行到188行一样50%
也改变了绘制的逻辑,它基本上是rekt,它只是现在检查是否所有的地方都不是白色,如果是,那么它给出 * 绘制 *
改进的代码
chhqkbe12#
这是一个模块化版本,增加了在所有条件下正确运行的检查: