所以这是我的Tic.Tac.Toe游戏,它运作正常。但是我有一个功能来检查电路板是否满,这是不行的。我可以让它去玩家1,然后是玩家2,但是在它停止之后。有人可以帮忙吗?
# Tic Tac Toe Game
import time
import sys
who = 'p1'
finished = 'no'
board = [0,1,2,
3,4,5,
6,7,8]
def show():
print (board[0], '|', board [1], '|', board[2])
print ('---------')
print (board[3], '|', board [4], '|', board[5])
print ('---------')
print (board[6], '|', board [7], '|', board[8])
def checkWin():
if board[0] == 'x' and board[3] == 'x' and board[6] == 'x':
finished = 'yes'
return True
elif board[0] == 'y' and board[3] == 'y' and board[6] == 'y':
finished = 'yes'
return True
elif board[1] == 'x' and board[4] == 'x' and board[7] == 'x':
finished = 'yes'
return True
elif board[1] == 'y' and board[4] == 'y' and board[7] == 'y':
finished = 'yes'
return True
elif board[2] == 'y' and board[5] == 'y' and board[8] == 'y':
finished = 'yes'
return True
elif board[2] == 'x' and board[5] == 'x' and board[8] == 'x':
finished = 'yes'
return True
elif board[0] == 'y' and board[1] == 'y' and board[2] == 'y':
finished = 'yes'
return True
elif board[0] == 'x' and board[1] == 'x' and board[2] == 'x':
finished = 'yes'
return True
elif board[3] == 'x' and board[4] == 'x' and board[5] == 'x':
finished = 'yes'
return True
elif board[3] == 'y' and board[4] == 'y' and board[5] == 'y':
finished = 'yes'
return True
elif board[6] == 'y' and board[7] == 'y' and board[8] == 'y':
finished = 'yes'
return True
elif board[6] == 'x' and board[7] == 'x' and board[8] == 'x':
finished = 'yes'
return True
elif board[0] == 'x' and board[4] == 'x' and board[8] == 'x':
finished = 'yes'
return True
elif board[0] == 'y' and board[4] == 'y' and board[8] == 'y':
finished = 'yes'
return True
elif board[2] == 'y' and board[4] == 'y' and board[6] == 'y':
finished = 'yes'
return True
elif board[2] == 'x' and board[4] == 'x' and board[6] == 'x':
finished = 'yes'
return True
else:
return False
def boardFull():
if board[0] == 'x' or board[0] == 'y' and board[1] == 'x' or board[1] == 'y' and board[2] == 'x' or board[2] == 'y' and board[3] == 'x' or board[3] == 'y' and board[4] == 'x' or board[4] == 'y' and board[5] == 'x' or board[5] == 'y' and board[6] == 'x' or board[6] == 'y' and board[6] == 'x' or board[6] == 'y' and board[7] == 'x' or board[7] == 'y' and board[8] == 'x' or board[8] == 'y':
finished = 'yes'
return True
else:
finished = 'no'
return False
print ('Welcome to a two-player Tic.Tac.Toe Game!')
time.sleep(2)
print ("Just pick a spot to put your mark!")
time.sleep(1.5)
print ('Now, start!')
time.sleep(0.2)
print (show())
while checkWin() == False and boardFull() == False:
while who == 'p1':
try:
p1 = int(input('Player 1, select a spot: '))
if board[p1] != 'x' and board[p1] != 'o':
board[p1] = 'x'
who = 'p2'
break
else:
print ('This spot is taken.')
pass
except (ValueError, IndexError):
print ('That is not available. Please try again.')
print (show())
while who == 'p2':
try:
p2 = int(input('Player 2, select a spot: '))
if board[p2] != 'x' and board[p2] != 'o':
board[p2] = 'o'
who = 'p1'
break
else:
print ('This spot is taken.')
pass
except (ValueError, IndexError):
print ('That is not available. Please try again.')
print (show())
while checkWin() == True:
if boardFull() == False:
# x
if board[0] == 'x' and board[3] == 'x' and board[6] == 'x':
print ('Player 1 won!')
sys.exit()
elif board[1] == 'x' and board[4] == 'x' and board[7] == 'x':
print ('Player 1 won!')
sys.exit()
elif board[2] == 'x' and board[5] == 'x' and board[8] == 'x':
print ('Player 1 won!')
sys.exit()
elif board[0] == 'x' and board[1] == 'x' and board[2] == 'x':
print ('Player 1 won!')
sys.exit()
elif board[3] == 'x' and board[4] == 'x' and board[5] == 'x':
print ('Player 1 won!')
sys.exit()
elif board[6] == 'x' and board[7] == 'x' and board[8] == 'x':
print ('Player 1 won!')
sys.exit()
elif board[0] == 'x' and board[4] == 'x' and board[8] == 'x':
print ('Player 1 won!')
sys.exit()
elif board[2] == 'x' and board[4] == 'x' and board[6] == 'x':
print ('Player 1 won!')
sys.exit()
# y
else:
print ('Player 2 won!')
sys.exit()
else:
print ("It's a tie.")
答案 0 :(得分:3)
很明显,问题在于boardFull
的实施,正如101所说。我没有记忆的优先规则(这就是为什么在复杂的表达式中使用括号通常是个好主意,即使它们在技术上不是必需的)但我认为“或”的优先级低于“和”,意思是{{如果设置了boardFull
,则会返回True
,而不管董事会其余部分的状态如何。
我首先修改board[0]
接受boardFull
作为参数,而不是检查全局变量的状态。这使得该功能更容易推理,更容易测试,并提高了可重用性。我还编写了一个简单的测试套件来测试它的行为:
board
目前,import unittest
def create_board():
return list(range(0, 9))
def board_full(board):
if board[0] == 'x' or board[0] == 'y' and board[1] == 'x' or board[1] == 'y' and board[2] == 'x' or board[2] == 'y' and board[3] == 'x' or board[3] == 'y' and board[4] == 'x' or board[4] == 'y' and board[5] == 'x' or board[5] == 'y' and board[6] == 'x' or board[6] == 'y' and board[6] == 'x' or board[6] == 'y' and board[7] == 'x' or board[7] == 'y' and board[8] == 'x' or board[8] == 'y':
finished = 'yes'
return True
else:
finished = 'no'
return False
class TestBoardFull(unittest.TestCase):
def setUp(self):
self.board = create_board()
def test_default_board_is_not_full(self):
default_board = self.board
self.assertFalse(board_full(default_board))
def test_board_after_two_moves_is_not_full(self):
board = self.board
board[0] = 'x'
board[1] = 'y'
self.assertFalse(board_full(board))
def test_board_is_full_after_all_tiles_marked(self):
board = self.board
board[0] = 'x'
board[1] = 'y'
board[2] = 'x'
board[3] = 'y'
board[4] = 'x'
board[5] = 'y'
board[6] = 'x'
board[7] = 'y'
board[8] = 'x'
self.assertTrue(board_full(board))
if __name__ == '__main__':
unittest.main()
使用您原来的实现,我们可能会显示错误:
board_full(board)
然后我尝试纠正实施:
F..
======================================================================
FAIL: test_board_after_two_moves_is_not_full (__main__.TestBoardFull)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:/Users/<<my_name>>/.PyCharmCE2016.3/config/scratches/scratch.py", line 26, in test_board_after_two_moves_is_not_full
self.assertFalse(board_full(board))
AssertionError: True is not false
----------------------------------------------------------------------
Ran 3 tests in 0.000s
FAILED (failures=1)
并测试这是否有效,至少根据我目前定义的测试:
def board_full(board):
return all(tile == 'x' or tile == 'y' for tile in board)
答案 1 :(得分:2)
你有很多问题。一个是您设置了x
或o
,但您选中了x
或y
。接下来是boardFull()
两次移动后返回True
。但你可以计算正确的动作而不是检查所有细胞。经过9次正确移动后,电路板将满了。
您的代码可能更短
show()
打印电路板,因此您可以在不print()
- print(show())
checkWin()
可以返回False
或获胜者x
或o
,其被视为True
,最后您不必检查再次谁是胜利者。
在while
循环中,你不会检查玩家1和2之间的棋盘是否已满,这样游戏就会挂起,因为玩家1移动后全板已满,玩家2无法制作移动。
checkWin()
仅检查当前玩家是否在最后一次移动后获胜。
我计算正确的动作来检查电路板是否已满 - 在9次移动之后电路板应该已满。
# Tic Tac Toe Game
import time
# --- fucntions ---
def show(board):
print('''-----------
{} | {} | {}
-----------
{} | {} | {}
-----------
{} | {} | {}
-----------'''.format(*board))
def check_winner(board, q):
if board[0] == q and board[3] == q and board[6] == q:
return True
elif board[1] == q and board[4] == q and board[7] == q:
return True
elif board[2] == q and board[5] == q and board[8] == q:
return True
elif board[0] == q and board[1] == q and board[2] == q:
return True
elif board[3] == q and board[4] == q and board[5] == q:
return True
elif board[6] == q and board[7] == q and board[8] == q:
return True
elif board[0] == q and board[4] == q and board[8] == q:
return True
elif board[2] == q and board[4] == q and board[6] == q:
return True
return False
# --- start ---
board = list(range(9))
player = 'x'
number = '1'
moves = 0
print('Welcome to a two-player Tic.Tac.Toe Game!')
time.sleep(2)
print("Just pick a spot to put your mark!")
time.sleep(1.5)
print('Now, start!')
time.sleep(0.2)
show(board)
while True:
while True:
try:
idx = int(input('Player {}, select a spot: '.format(number)))
if board[idx] not in ('x', 'o'):
board[idx] = player
moves += 1
break
else:
print('This spot is taken.')
except (ValueError, IndexError):
print('That is not available. Please try again.')
show(board)
winner = check_winner(board, player)
if winner or moves == 9:
break
# change player after checking who win
if player == 'x':
player = 'o'
number = '2'
else:
player = 'x'
number = '1'
if winner:
print('Player {} won!'.format(number))
else:
print("It's a tie.")