如果声明错误(Tic Tac Toe) [英] If Statement Error (Tic Tac Toe)

查看:56
本文介绍了如果声明错误(Tic Tac Toe)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候!我正在尝试在Python中制作一个多人游戏(没有人工智能,2人)游戏。

tic tac toe。到目前为止,它非常简单。我唯一关注的是赢得支票,看看一个人是否赢了。起初它看起来好像在工作,但现在它有时会在某些瓷砖上输入X或O(无关紧要)的时候分配一个奖金(第1行第1列

不会出错,但第2行第3列将是...)。如果你能找到问题,我会非常感激!这是代码:


#TIC TAC TOE

#开始时间:10/31/05

#结束:还在进行中


loop = 1


while while == 1:

print" TIC TAC TOE" ;

打印" 1 - 播放多人游戏>

打印" 2 - 退出"

选项=输入(">" )


如果选项== 2:

#QUIT

loop = 0


if option == 1:

#MAIN GAME LOOP

print"规则:你将轮流转。

打印 ;在你的回合中,你可以把你的信(O = Player 1或

X = Player 2),

print放在任何未占用的广场上。

print"第一个获得3连胜。祝你好运!


gameboard = ['''''''''''''''''''''''''''''''''''' '''''''''''''''

win = 0

turnnumber = 0


而win!= 1:

if turnnumber%2 == 0:

print"

print" Player 1"

print"

打印

" [",gameboard [0],"]"," [",gameboard [1],"] "," [",gameboard [2],"]"

print

" [",gameboard [3],"] "," [",gameboard [4],"]"," [",gameboard [5],"]"

打印

" [",gameboard [6],"]"," [",gameboard [7],""," [",gameboard [8]," ]

打印什么行?"

row =输入(">")

print"什么column?"

column = input(">")

if(row> 3或row< 1)或(column > 3或列< 1):

print" Exceeeded limits。

turnnumber = turnnumber - 1


如果row == 1且column == 1:

如果gameboard [0]!=(''''或''X''):

游戏板[0] =(''O'')

else:

print" This cel l已经填满。

turnnumber = turnnumber - 1


如果行== 2且列== 1:

如果游戏板[3]!=(''''或''X''):

游戏板[3] =('''')


else:

print"这个单元格已经填满。

turnnumber = turnnumber - 1

如果行== 3和列== 1:

如果游戏板[6]!=(''''或''X''):

gameboard [6] =( ''O'')

else:

print"这个单元格已经填满。

turnnumber = turnnumber - 1


如果行== 1且列== 2:

如果gameboard [1]!=(''''或''X''):

gameboard [1] =(''O'')


else:

print"此单元格已填充。"

turnnumber = turnnumber - 1


如果row == 2且column == 2:

if gameboard [ 4]!=(''O''或''X''):

游戏board [4] =(''O'')

else:

print"此单元格已经填充。

turnnumber = turnnumber - 1


如果row == 3且column == 2:

if gameboard [7]!=('''''或' 'X''):

gameboard [7] =('''')


else:

print" ;此单元格已经填充。

turnnumber = turnnumber - 1


如果行== 1且列== 3:

如果游戏板[2]!=(''''或''X''):

游戏板[2] =('''')


else:

print"此单元格已经填充。

turnnumber = turnnumber - 1


如果行== 2且列== 3:

如果游戏板[5]!=(''''或''X''):

gameboard [5] =(''O'')

else:

print"此单元格已经填充。

turnnumber = turnnumber - 1


如果row == 3且column == 3:

如果游戏板[8]!=(''''或''X''):

gameboard [8] =('''''' )


else:

print"这个单元格已经填满。

turnnumber = turnnumber - 1

turnnumber = turnnumber + 1


if(((gameboard [0]和gameboard [1]和gameboard [2])==

''O'')或((游戏板[3]和游戏板[4]和游戏板[5])==''O'')或

((游戏板[ 6]和游戏板[7]和游戏板[8])==''O'')\

或((游戏板[0]和游戏板[3]和游戏板[6])= =

''O'')或((游戏板[1]和游戏板[4]和游戏板[7])==''O'')或

((游戏板[2]和游戏板[5]和游戏板[8])==''O'')\

或((游戏板[0]和游戏板[4]和游戏板[ 8])==

''O'')或((游戏板[2]和游戏板[4]和游戏板[6])==''O'')):

print" Player 1 wins!"

win = 1


if((gameboard [0:9])==(''''或''X''))和(赢) == 0):

print" Tie。"

win = 1


if turnnumber%2 == 1并获胜!= 1:

print"

print" Player 2"

print"

打印

" [",gameboard [0],"]"," [",gameboard [1],"] "," [",gameboard [2],"]"

print

" [",gameboard [3],"] "," [",gameboard [4],"]"," [",gameboard [5],"]"

打印

" [",gameboard [6],"]"," [",gameboard [7],""," [",gameboard [8]," ]

打印什么行?"

row =输入(">")

print"什么column?"

column = input(">")

if(row> 3或row< 1)或(column > 3或列< 1):

print" Exceeeded limits。"

turnnumber = turnnumber - 1

if row = = 1和列== 1:

如果gameboard [0]!=(''''或''X''):

gameboard [0] = (''X'')

else:

print" this cell is al准备好了。

turnnumber = turnnumber - 1


如果行== 2且列== 1:

如果gameboard [3]!=(''''或''X''):

gameboard [3] =(''X'')


else:

print"这个单元格已经填满。

turnnumber = turnnumber - 1


if row == 3和列== 1:

如果游戏板[6]!=(''''或''X''):

游戏板[6] =(''X'')

else:

print"此单元格已经填充。

turnnumber = turnnumber - 1


如果行== 1且列== 2:

如果游戏板[1]!=(''''或''X''' ):

gameboard [1] =(''X'')


else:

print"此单元格是已经填好了。

turnnumber = turnnumber - 1


如果row == 2且column == 2:

if gameboard [4]!=(''''或''X''):

游戏board [4] =(''X'')

else:

print"此单元格已经填充。

turnnumber = turnnumber - 1


如果row == 3且column == 2:

if gameboard [7]!=('''''或' 'X''):

gameboard [7] =(''X'')


else:

print" ;此单元格已经填充。

turnnumber = turnnumber - 1


如果行== 1且列== 3:

如果游戏板[2]!=(''''或''X''):

游戏板[2] =(''X'')


else:

print"此单元格已经填充。

turnnumber = turnnumber - 1


如果行== 2且列== 3:

如果游戏板[5]!=(''''或''X''):

gameboard [5] =(''X'')

else:

print"此单元格已经填充。

turnnumber = turnnumber - 1


如果row == 3且column == 3:

如果游戏板[8]!=(''''或''X''):

gameboard [8] =('''X'' )


else:

print"这个单元格已经填满。

turnnumber = turnnumber - 1

turnnumber = turnnumber + 1


if(((gameboard [0]和gameboard [1]和gameboard [2])==

''X'')或((游戏板[3]和游戏板[4]和游戏板[5])==''X'')或

((游戏板[ 6]和游戏板[7]和游戏板[8])==''X'')\

或((游戏板[0]和游戏板[3]和游戏板[6])= =

''X'')或((游戏板[1]和游戏板[4]和游戏板[7])==''X'')或

((游戏板[2]和游戏板[5]和游戏板[8])==''X'')\

或((游戏板[0]和游戏板[4]和游戏板[ 8])==

''X'')或((游戏板[2]和游戏板[4]和游戏板[6])==''X'')):

print" Player 2 wins!"

win = 1

Greetings! I am trying to make a multiplayer (no AI, 2 person) game of
tic tac toe in Python. So far it has been pretty simple. My only
concern is with the win checking to see if a person has won. At first
it looked like it was working, but now it sometimes assigns a win when
you enter an X or O (doesn''t matter) on certain tiles (row 1, column 1
won''t be an error, but row 2, column 3 will be...). If you can find
the problem, I''d be very thankful! Here''s the code:

# TIC TAC TOE
# Started: 10/31/05
# Ended: still in progress

loop = 1

while loop == 1:
print "TIC TAC TOE"
print "1 - Play Multiplayer"
print "2 - Quit"
option = input("> ")

if option == 2:
# QUIT
loop = 0

if option == 1:
# MAIN GAME LOOP
print "Rules: You will alternate turns."
print "On your turn, you can place your letter (O = Player 1 or
X = Player 2)",
print "in any unoccupied square."
print "The first to get 3 in a row wins. Good luck!"

gameboard = ['' '','' '','' '','' '','' '','' '','' '','' '','' '']

win = 0
turnnumber = 0

while win != 1:
if turnnumber % 2 == 0:
print " "
print "Player 1"
print " "
print
"[",gameboard[0],"]","[",gameboard[1],"]","[",gameboard[2],"]"
print
"[",gameboard[3],"]","[",gameboard[4],"]","[",gameboard[5],"]"
print
"[",gameboard[6],"]","[",gameboard[7],"]","[",gameboard[8],"]"
print "What row?"
row = input("> ")
print "What column?"
column = input("> ")

if (row > 3 or row < 1) or (column > 3 or column < 1):
print "Exceeeded limits."
turnnumber = turnnumber - 1

if row == 1 and column == 1:
if gameboard[0] != (''O'' or ''X''):
gameboard[0] = (''O'')
else:
print "This cell is already filled."
turnnumber = turnnumber - 1

if row == 2 and column == 1:
if gameboard[3] != (''O'' or ''X''):
gameboard[3] = (''O'')

else:
print "This cell is already filled."
turnnumber = turnnumber - 1
if row == 3 and column == 1:
if gameboard[6] != (''O'' or ''X''):
gameboard[6] = (''O'')
else:
print "This cell is already filled."
turnnumber = turnnumber - 1

if row == 1 and column == 2:
if gameboard[1] != (''O'' or ''X''):
gameboard[1] = (''O'')

else:
print "This cell is already filled."
turnnumber = turnnumber - 1

if row == 2 and column == 2:
if gameboard[4] != (''O'' or ''X''):
gameboard[4] = (''O'')
else:
print "This cell is already filled."
turnnumber = turnnumber - 1

if row == 3 and column == 2:
if gameboard[7] != (''O'' or ''X''):
gameboard[7] = (''O'')

else:
print "This cell is already filled."
turnnumber = turnnumber - 1

if row == 1 and column == 3:
if gameboard[2] != (''O'' or ''X''):
gameboard[2] = (''O'')

else:
print "This cell is already filled."
turnnumber = turnnumber - 1

if row == 2 and column == 3:
if gameboard[5] != (''O'' or ''X''):
gameboard[5] = (''O'')
else:
print "This cell is already filled."
turnnumber = turnnumber - 1

if row == 3 and column == 3:
if gameboard[8] != (''O'' or ''X''):
gameboard[8] = (''O'')

else:
print "This cell is already filled."
turnnumber = turnnumber - 1

turnnumber = turnnumber + 1

if (((gameboard[0] and gameboard[1] and gameboard[2]) ==
''O'') or ((gameboard[3] and gameboard[4] and gameboard[5]) == ''O'') or
((gameboard[6] and gameboard[7] and gameboard[8]) == ''O'')\
or ((gameboard[0] and gameboard[3] and gameboard[6]) ==
''O'') or ((gameboard[1] and gameboard[4] and gameboard[7]) == ''O'') or
((gameboard[2] and gameboard[5] and gameboard[8]) == ''O'')\
or ((gameboard[0] and gameboard[4] and gameboard[8]) ==
''O'') or ((gameboard[2] and gameboard[4] and gameboard[6]) == ''O'')):
print "Player 1 wins!"
win = 1

if ((gameboard[0:9]) == (''O'' or ''X'')) and (win == 0):
print "Tie."
win = 1

if turnnumber % 2 == 1 and win != 1:
print " "
print "Player 2"
print " "
print
"[",gameboard[0],"]","[",gameboard[1],"]","[",gameboard[2],"]"
print
"[",gameboard[3],"]","[",gameboard[4],"]","[",gameboard[5],"]"
print
"[",gameboard[6],"]","[",gameboard[7],"]","[",gameboard[8],"]"
print "What row?"
row = input("> ")
print "What column?"
column = input("> ")

if (row > 3 or row < 1) or (column > 3 or column < 1):
print "Exceeeded limits."
turnnumber = turnnumber - 1
if row == 1 and column == 1:
if gameboard[0] != (''O'' or ''X''):
gameboard[0] = (''X'')
else:
print "This cell is already filled."
turnnumber = turnnumber - 1

if row == 2 and column == 1:
if gameboard[3] != (''O'' or ''X''):
gameboard[3] = (''X'')

else:
print "This cell is already filled."
turnnumber = turnnumber - 1

if row == 3 and column == 1:
if gameboard[6] != (''O'' or ''X''):
gameboard[6] = (''X'')
else:
print "This cell is already filled."
turnnumber = turnnumber - 1

if row == 1 and column == 2:
if gameboard[1] != (''O'' or ''X''):
gameboard[1] = (''X'')

else:
print "This cell is already filled."
turnnumber = turnnumber - 1

if row == 2 and column == 2:
if gameboard[4] != (''O'' or ''X''):
gameboard[4] = (''X'')
else:
print "This cell is already filled."
turnnumber = turnnumber - 1

if row == 3 and column == 2:
if gameboard[7] != (''O'' or ''X''):
gameboard[7] = (''X'')

else:
print "This cell is already filled."
turnnumber = turnnumber - 1

if row == 1 and column == 3:
if gameboard[2] != (''O'' or ''X''):
gameboard[2] = (''X'')

else:
print "This cell is already filled."
turnnumber = turnnumber - 1

if row == 2 and column == 3:
if gameboard[5] != (''O'' or ''X''):
gameboard[5] = (''X'')
else:
print "This cell is already filled."
turnnumber = turnnumber - 1

if row == 3 and column == 3:
if gameboard[8] != (''O'' or ''X''):
gameboard[8] = (''X'')

else:
print "This cell is already filled."
turnnumber = turnnumber - 1

turnnumber = turnnumber + 1

if (((gameboard[0] and gameboard[1] and gameboard[2]) ==
''X'') or ((gameboard[3] and gameboard[4] and gameboard[5]) == ''X'') or
((gameboard[6] and gameboard[7] and gameboard[8]) == ''X'')\
or ((gameboard[0] and gameboard[3] and gameboard[6]) ==
''X'') or ((gameboard[1] and gameboard[4] and gameboard[7]) == ''X'') or
((gameboard[2] and gameboard[5] and gameboard[8]) == ''X'')\
or ((gameboard[0] and gameboard[4] and gameboard[8]) ==
''X'') or ((gameboard[2] and gameboard[4] and gameboard[6]) == ''X'')):
print "Player 2 wins!"
win = 1

推荐答案

al *********** @ gmail.com 写道:
al***********@gmail.com writes:
问候!我正在尝试在Python中制作一个多人游戏(没有AI,2人)的游戏。到目前为止,它非常简单。我唯一关心的是胜利检查以确定一个人是否赢了。起初它看起来像是在工作,但现在它有时会在某些瓷砖上输入X或O(无关紧要)时获得一个胜利(第1行,第1列不会是一个错误,但第2行,第3列将是......)。如果你能找到问题,我会非常感激!这是代码:


你有两个问题。其中一个会导致您的问题。另一个

告诉你复制*批次*代码。


第一个问题是逻辑和平等测试不是

以你认为的方式互操作:


(游戏板[0]和游戏板[1]和游戏板[2])==''X'')


确实*不*验证每个游戏板[0],游戏板[1]和

游戏板[2]都等于''X''。它测试值

(gameboard [0]和gamerboard [1]和gameboard [2])是否等于''X''。

前者几乎肯定是gameboard [2]的价值,除非

gameboard [0]或gameboard [1]评估为假值,在这种情况下

,这是你与''X'相比的价值。你将不得不将这个

打破为一组三个测试。


类似的评论适用于像gameboard [0]这样的测试! =(''O''或''X'')"。

rhs是一个常数 - ''X''。这个你可以改写为

" gameboard [0]不在''0X''"。


第二个问题是你''重新使用你应该使用的代码

数据。当你看到很长一段非常相似的代码反复重复

时,你应该考虑用数据替换代码。对于

实例,所有这些看起来像:


如果row == X和column == Y:

if游戏板[V]不在''OX'':

gammeboard [V] =''O''

else:

print" ;此单元格已被填充。

turnnumber - = 1

[重复多次。虽然我在这,但是这个字符串中的第二个和更多的

测试应该是elif,而不是if。]

可以用字典替换(或者列表清单):


boardmap = {(1,1):0,(2,1):3,(3,1):6,

(1,2):1,(2,2):4,(3,2):7,

(1,3):2,(2,3):5, (3,3):8

}


cell = boardmap [row,column]

如果gameboard [cell]没有在''OX'':

gameboard [cell] =''O''

else:

print"此单元格已经填写。

turnnumber - = 1

作为列表列表的表示留作

读者的练习。 />

< mike

#TIC TAC TOE
#Started:10/31/05
#Ended:仍在进行中
loop = 1

while loop == 1:
printTIC TAC TOE
打印1 - 播放多人游戏
打印2 - 退出
选项=输入("&g t;")

如果选项== 2:
#QUIT
loop = 0
如果选项== 1:
#MAIN GAME LOOP
打印"规则:您将轮流巡视。
打印在您的回合中,您可以放置​​您的信件(O = Player 1或
X = Player 2 ),
打印在任何未占用的方格中。
print"第一个获得3连胜。祝你好运!

gameboard = ['''','''','''','''','''','''',''''' ,'''','''']

获胜= 0
转数= 0
赢得!= 1:
如果转数为% 2 == 0:
打印
print播放器1
打印
print
[,gameboard [0],"",[,,gameboard [1],""," [" ,gameboard [2],"]"
print
" [",gameboard [3],"]",[,,gameboard [4]," ],[,,gameboard [5],"]"
打印
" [",gameboard [6],"]"," [" ;,gameboard [7],"]"," [",gameboard [8],"]"
print"什么行?"
row = input(" ;>")
打印什么列?"
列=输入(">")
if(row> 3或row< ; 1)或(列> 3或列< 1):
打印超出限制。
turnnumber = turnnumber - 1

如果行== 1和列== 1:
如果游戏板[0] !=(''O''或''X''):
gameboard [0] =(''O'')
else:
print"此单元格已填充。
turnnumber = turnnumber - 1

如果row == 2且column == 1:
如果gameboard [3]!=('''''或' 'X''):
游戏板[3] =(''O'')

否则:
打印此单元格已填充。
turnnumber = turnnumber - 1
如果row == 3且column == 1:
如果gameboard [6]!=(''''或''X''):
gameboard [6] =('''')
其他:
打印这个单元格已经填满。
turnnumber = turnnumber - 1
如果row == 1且column == 2:
如果gameboard [1]!=(''''或''X''):
gameboard [1] =(' 'O'')

其他:
打印这个单元格已经填满。
turnnumber = turnnumber - 1

如果row = = 2和列== 2:
如果gameboard [4]!=(''''或''X''):
gameboard [4] =(''''')
else:
print"这个单元格已经填满。
turnnumber = turnnumber - 1

如果row == 3 and column == 2:游戏板[7] =('''')

其他:<打印&q uot;这个单元格已经填满了。
turnnumber = turnnumber - 1

如果row == 1且column == 3:
如果gameboard [2]!=( ''O''或''X''):
gameboard [2] =(''O'')

否则:
打印此单元格已经填充。
turnnumber = turnnumber - 1
如果row == 2且column == 3:
如果gameboard [5]!=('''''或者''X''):
gameboard [5] =('''')
否则:
打印这个单元格已经填满。
turnnumber = turnnumber - 1
如果row == 3和column == 3:
如果gameboard [8]!=(''''或''X''):
gam eboard [8] =(''O'')

其他:
打印这个单元格已经填满。
turnnumber = turnnumber - 1

turnnumber = turnnumber + 1

if(((gameboard [0]和gameboard [1]和gameboard [2])==
''O'')或( (游戏板[3]和游戏板[4]和游戏板[5])==''O'')或
((游戏板[6]和游戏板[7]和游戏板[8])=='' O())\
或((游戏板[0]和游戏板[3]和游戏板[6])==
''O'')或((游戏板[1]和游戏板[] 4]和游戏板[7])==''O'')或
((游戏板[2]和游戏板[5]和游戏板[8])==''O'')\
或((游戏板[0]和游戏板[4]和游戏板[8])==
''O'')或((游戏板[2]和游戏板[4]和游戏板[6]) ==''O'')):
打印玩家1胜!
win = 1

if((gameboard [0:9])==(''O''或''X''))和(win == 0):
打印Tie。
win = 1

如果转号为%2 == 1并赢了!= 1:
打印
print播放器2
打印
print
[,gameboard [0],"",[,,gameboard [1],""," [" ,gameboard [2],"]"
print
" [",gameboard [3],"]",[,,gameboard [4]," ],[,,gameboard [5],"]"
打印
" [",gameboard [6],"]"," [" ;,gameboard [7],"]"," [",gameboard [8],"]"
print"什么行?"
row = input(" ;>")
打印什么列?"
列=输入(">")
if(row> 3或row< ; 1)或(列> 3或列< 1):
打印超出限制。
turnnumber = turnnumber - 1
如果row == 1且column == 1:
如果游戏板[0]!=( 'O''或''X''):
游戏板[0] =(''X'')
否则:
打印此单元格已填充。
turnnumber = turnnumber - 1
如果row == 2且column == 1:
如果gameboard [3]!=(''''或''X'' ):
游戏板[3] =(''X'')

其他:
打印此单元格已填充。
turnnumber = turnnumber - 1

如果行== 3和列== 1:
如果游戏板[6]!=(''''或''X''):
gameboard [6] =(''X'')
else:
print"此单元格已经填充。
turnnumber = turnnumber - 1
如果row == 1且column == 2:
如果gameboard [1]!=(''''或''X''):
gameboard [1] =(' 'X'')

其他:
打印此单元格已填充。
turnnumber = turnnumber - 1

如果row = = 2和列== 2:
如果gameboard [4]!=(''''或''X''):
gameboard [4] =('''X'')
else:
print"这个单元格已经填满。
turnnumber = turnnumber - 1

如果row == 3 and column == 2:游戏板[7] =(''X'')

其他:<打印&q uot;这个单元格已经填满了。
turnnumber = turnnumber - 1

如果row == 1且column == 3:
如果gameboard [2]!=( ''O''或''X''):
游戏板[2] =(''X'')

否则:
打印此单元格已经填充。
turnnumber = turnnumber - 1
如果row == 2且column == 3:
如果gameboard [5]!=('''''或者''X''):
游戏板[5] =(''X'')
否则:
打印此单元格已经填充。
turnnumber = turnnumber - 1
如果row == 3和column == 3:
如果gameboard [8]!=(''''或''X''):
gam eboard [8] =(''X'')

其他:
打印此单元格已填充。
turnnumber = turnnumber - 1

turnnumber = turnnumber + 1

if(((gameboard [0]和gameboard [1]和gameboard [2])==
''X'')或( (游戏板[3]和游戏板[4]和游戏板[5])==''X'')或
((游戏板[6]和游戏板[7]和游戏板[8])=='' X'')\
或((gameboard [0]和gameboard [3]和gameboard [6])==
''X'')或((gameboard [1]和gameboard [ 4]和游戏板[7])==''X'')或
((游戏板[2]和游戏板[5]和游戏板[8])==''X'')\
或((游戏板[0]和游戏板[4]和游戏板[8])==
''X'')或((游戏板[2]和游戏板[4]和游戏板[6]) ==''X'')):
打印玩家2获胜!
win = 1
Greetings! I am trying to make a multiplayer (no AI, 2 person) game of
tic tac toe in Python. So far it has been pretty simple. My only
concern is with the win checking to see if a person has won. At first
it looked like it was working, but now it sometimes assigns a win when
you enter an X or O (doesn''t matter) on certain tiles (row 1, column 1
won''t be an error, but row 2, column 3 will be...). If you can find
the problem, I''d be very thankful! Here''s the code:
You''ve got two problems. One of them causes your problem. The other
caues you to duplicate a *lot* of code.

The first problem is that logical and equality tests don''t
interoperate the way you think they do:

(gameboard[0] and gameboard[1] and gameboard[2]) == ''X'')

does *not* verify that each of gameboard[0], gameboard[1] and
gameboard[2] are all equal to ''X''. It tests whether or not the value
(gameboard[0] and gamerboard[1] and gameboard[2]) is equal to ''X''. The
former is almost certainly the value of gameboard[2], unless
gameboard[0] or gameboard[1] evaluates to a false value, in which case
that''s the value you''re comparing to ''X''. You''ll have to break this
one out into a set of three tests.

Similar comments apply to tests like "gameboard[0] != (''O'' or ''X'')". The
rhs of that is a constant - ''X''. This one you can rewrite as
"gameboard[0] not in ''0X''".

The second problem is that you''re using code where you should be using
data. Whenver you see a long stretch of very similar code repeated
over and over, you should consider replacing the code with data. For
instance, all those things that look like:

if row == X and column == Y:
if gameboard[V] not in ''OX'':
gammeboard[V] = ''O''
else:
print "This cell is already filled."
turnnumber -= 1
[repeate multiple times. And while I''m at it, the second and further
tests in this string should be elif, not if.]
Can be replaced by a dictionary (or a list of lists):

boardmap = {(1, 1): 0, (2, 1): 3, (3, 1): 6,
(1, 2): 1, (2, 2): 4, (3, 2): 7,
(1, 3): 2, (2, 3): 5, (3, 3): 8
}

cell = boardmap[row, column]
if gameboard[cell] not in ''OX'':
gameboard[cell] = ''O''
else:
print "This cell is already filled."
turnnumber -= 1
The representation as a list of lists is left as an exercise for the
reader.

<mike
# TIC TAC TOE
# Started: 10/31/05
# Ended: still in progress

loop = 1

while loop == 1:
print "TIC TAC TOE"
print "1 - Play Multiplayer"
print "2 - Quit"
option = input("> ")

if option == 2:
# QUIT
loop = 0

if option == 1:
# MAIN GAME LOOP
print "Rules: You will alternate turns."
print "On your turn, you can place your letter (O = Player 1 or
X = Player 2)",
print "in any unoccupied square."
print "The first to get 3 in a row wins. Good luck!"

gameboard = ['' '','' '','' '','' '','' '','' '','' '','' '','' '']

win = 0
turnnumber = 0

while win != 1:
if turnnumber % 2 == 0:
print " "
print "Player 1"
print " "
print
"[",gameboard[0],"]","[",gameboard[1],"]","[",gameboard[2],"]"
print
"[",gameboard[3],"]","[",gameboard[4],"]","[",gameboard[5],"]"
print
"[",gameboard[6],"]","[",gameboard[7],"]","[",gameboard[8],"]"
print "What row?"
row = input("> ")
print "What column?"
column = input("> ")

if (row > 3 or row < 1) or (column > 3 or column < 1):
print "Exceeeded limits."
turnnumber = turnnumber - 1

if row == 1 and column == 1:
if gameboard[0] != (''O'' or ''X''):
gameboard[0] = (''O'')
else:
print "This cell is already filled."
turnnumber = turnnumber - 1

if row == 2 and column == 1:
if gameboard[3] != (''O'' or ''X''):
gameboard[3] = (''O'')

else:
print "This cell is already filled."
turnnumber = turnnumber - 1
if row == 3 and column == 1:
if gameboard[6] != (''O'' or ''X''):
gameboard[6] = (''O'')
else:
print "This cell is already filled."
turnnumber = turnnumber - 1

if row == 1 and column == 2:
if gameboard[1] != (''O'' or ''X''):
gameboard[1] = (''O'')

else:
print "This cell is already filled."
turnnumber = turnnumber - 1

if row == 2 and column == 2:
if gameboard[4] != (''O'' or ''X''):
gameboard[4] = (''O'')
else:
print "This cell is already filled."
turnnumber = turnnumber - 1

if row == 3 and column == 2:
if gameboard[7] != (''O'' or ''X''):
gameboard[7] = (''O'')

else:
print "This cell is already filled."
turnnumber = turnnumber - 1

if row == 1 and column == 3:
if gameboard[2] != (''O'' or ''X''):
gameboard[2] = (''O'')

else:
print "This cell is already filled."
turnnumber = turnnumber - 1

if row == 2 and column == 3:
if gameboard[5] != (''O'' or ''X''):
gameboard[5] = (''O'')
else:
print "This cell is already filled."
turnnumber = turnnumber - 1

if row == 3 and column == 3:
if gameboard[8] != (''O'' or ''X''):
gameboard[8] = (''O'')

else:
print "This cell is already filled."
turnnumber = turnnumber - 1

turnnumber = turnnumber + 1

if (((gameboard[0] and gameboard[1] and gameboard[2]) ==
''O'') or ((gameboard[3] and gameboard[4] and gameboard[5]) == ''O'') or
((gameboard[6] and gameboard[7] and gameboard[8]) == ''O'')\
or ((gameboard[0] and gameboard[3] and gameboard[6]) ==
''O'') or ((gameboard[1] and gameboard[4] and gameboard[7]) == ''O'') or
((gameboard[2] and gameboard[5] and gameboard[8]) == ''O'')\
or ((gameboard[0] and gameboard[4] and gameboard[8]) ==
''O'') or ((gameboard[2] and gameboard[4] and gameboard[6]) == ''O'')):
print "Player 1 wins!"
win = 1

if ((gameboard[0:9]) == (''O'' or ''X'')) and (win == 0):
print "Tie."
win = 1

if turnnumber % 2 == 1 and win != 1:
print " "
print "Player 2"
print " "
print
"[",gameboard[0],"]","[",gameboard[1],"]","[",gameboard[2],"]"
print
"[",gameboard[3],"]","[",gameboard[4],"]","[",gameboard[5],"]"
print
"[",gameboard[6],"]","[",gameboard[7],"]","[",gameboard[8],"]"
print "What row?"
row = input("> ")
print "What column?"
column = input("> ")

if (row > 3 or row < 1) or (column > 3 or column < 1):
print "Exceeeded limits."
turnnumber = turnnumber - 1
if row == 1 and column == 1:
if gameboard[0] != (''O'' or ''X''):
gameboard[0] = (''X'')
else:
print "This cell is already filled."
turnnumber = turnnumber - 1

if row == 2 and column == 1:
if gameboard[3] != (''O'' or ''X''):
gameboard[3] = (''X'')

else:
print "This cell is already filled."
turnnumber = turnnumber - 1

if row == 3 and column == 1:
if gameboard[6] != (''O'' or ''X''):
gameboard[6] = (''X'')
else:
print "This cell is already filled."
turnnumber = turnnumber - 1

if row == 1 and column == 2:
if gameboard[1] != (''O'' or ''X''):
gameboard[1] = (''X'')

else:
print "This cell is already filled."
turnnumber = turnnumber - 1

if row == 2 and column == 2:
if gameboard[4] != (''O'' or ''X''):
gameboard[4] = (''X'')
else:
print "This cell is already filled."
turnnumber = turnnumber - 1

if row == 3 and column == 2:
if gameboard[7] != (''O'' or ''X''):
gameboard[7] = (''X'')

else:
print "This cell is already filled."
turnnumber = turnnumber - 1

if row == 1 and column == 3:
if gameboard[2] != (''O'' or ''X''):
gameboard[2] = (''X'')

else:
print "This cell is already filled."
turnnumber = turnnumber - 1

if row == 2 and column == 3:
if gameboard[5] != (''O'' or ''X''):
gameboard[5] = (''X'')
else:
print "This cell is already filled."
turnnumber = turnnumber - 1

if row == 3 and column == 3:
if gameboard[8] != (''O'' or ''X''):
gameboard[8] = (''X'')

else:
print "This cell is already filled."
turnnumber = turnnumber - 1

turnnumber = turnnumber + 1

if (((gameboard[0] and gameboard[1] and gameboard[2]) ==
''X'') or ((gameboard[3] and gameboard[4] and gameboard[5]) == ''X'') or
((gameboard[6] and gameboard[7] and gameboard[8]) == ''X'')\
or ((gameboard[0] and gameboard[3] and gameboard[6]) ==
''X'') or ((gameboard[1] and gameboard[4] and gameboard[7]) == ''X'') or
((gameboard[2] and gameboard[5] and gameboard[8]) == ''X'')\
or ((gameboard[0] and gameboard[4] and gameboard[8]) ==
''X'') or ((gameboard[2] and gameboard[4] and gameboard[6]) == ''X'')):
print "Player 2 wins!"
win = 1




-

Mike Meyer< mw*@mired.org> http://www.mired.org/home/mwm/

独立的WWW / Perforce / FreeBSD / Unix顾问,电子邮件以获取更多信息。



--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.


谢谢。我似乎并不理解逻辑陈述,因为我认为我做了这么多!


剩下的一个问题是我处理的问题:


如果游戏板[单元格]不在''OX'':

gameboard [cell] =''O''

else :

print"这个单元格已经填满了。

turnnumber - = 1


(游戏板[单元格]没有在''OX''是游戏板[cell]!='''OX''文本我在我的代码中使用了
,对吗?)


我对OX感到困惑:究竟是什么*是什么?在那之后,我打算

重写所有代码...... :)

Thank you. It seems I didn''t understand logic statements as much as I
thought I did!

The one remaining question I have deals with this:

if gameboard[cell] not in ''OX'':
gameboard[cell] = ''O''
else:
print "This cell is already filled."
turnnumber -= 1

(gameboard[cell] not in ''OX'' is the gameboard[cell] != ''OX'' text I sort
of used in my code, right?)

I am confused about "OX": what exactly *is* it? After that, I plan to
rewrite all the code... :)


al *********** @ gmail.com 写道:
al***********@gmail.com writes:
谢谢。我似乎并不像我想象的那样理解逻辑陈述!

剩下的一个问题是我处理的:

如果游戏板[cell]不在''OX'':
gameboard [cell] =''O''
else:
print"这个单元格已经填满。
turnnumber - = 1

(游戏板[cell]不在''OX''是gameboard [cell]!='''OX''文本我在我的代码中使用了
对吗?)

我对OX感到困惑:究竟是什么*是什么?之后,我计划重写所有代码...... :)
Thank you. It seems I didn''t understand logic statements as much as I
thought I did!

The one remaining question I have deals with this:

if gameboard[cell] not in ''OX'':
gameboard[cell] = ''O''
else:
print "This cell is already filled."
turnnumber -= 1

(gameboard[cell] not in ''OX'' is the gameboard[cell] != ''OX'' text I sort
of used in my code, right?)

I am confused about "OX": what exactly *is* it? After that, I plan to
rewrite all the code... :)




" OX"是一个字符串。说长串短裤是
的一种方式,询问longtring是否包含shorttring。在你的情况下,

短信串是一个字符长,但这是一个字符串

Python。 Python没有明显的字符数据类型。


虽然我很喜欢它,但不要被单引号与双重混淆

报价。与其他语言不同,在Python中它们具有相同的

含义。唯一的区别是你需要做些什么才能在字符串中获得单个

(双)引用。


< mike

-

Mike Meyer< mw*@mired.org> http://www.mired.org/home/mwm/

独立的WWW / Perforce / FreeBSD / Unix顾问,电子邮件以获取更多信息。



"OX" is a string. Saying "shortstring in longstring" is one way of
asking if shortstring is contained in longstring. In your case,
shortstring is one character long, but that''s a string to
Python. Python doesn''t have a distinct character data type.

While I''m at it, don''t be confused by single quotes vs. double
quotes. Unlike other languages, in Python they have the same
meaning. The only difference is what you have to do to get a single
(double) quote in the string.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.


这篇关于如果声明错误(Tic Tac Toe)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆