【发布时间】:2021-04-02 07:58:41
【问题描述】:
我正在开发connect4游戏,现在我正在检查获胜者,但获胜者检查功能无法正常工作。如何解决? 在 pycharm 编辑器中,它说即使使用了变量获胜者也没有使用。在四个相同的数字垂直之后,它没有打印谁是赢家。我不知道如何解决它。 谢谢!
from termcolor import colored
field = [[" ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " "]]
def drawField(field):
for row in range(11):
if row % 2 == 0:
rowIndex = int(row / 2)
for column in range(13):
if column % 2 == 0:
columnIndex = int(column / 2)
if column == 12:
print(field[rowIndex][columnIndex])
else:
print(field[rowIndex][columnIndex], end="")
else:
print("|", end="")
else:
print("-------------")
def reset():
for i in range(6):
for b in range(7):
field[i][b] = " "
drawField(field)
Player = 1
def winnerCheck(characters):
maxSQ = 0
char = False
sq = 0
for i in characters:
if i != char:
char = i
sq = 1
else:
sq += 1
if sq > maxSQ:
maxChar = char
maxSQ = sq
if maxChar == "X" or maxChar == "O":
if maxSQ == 4:
winner = 0
if maxChar == "X":
winner = "1"
else:
winner = "2"
print("--------------------------------------")
print("| The winner is player", winner, end="")
print(" |")
print("--------------------------------------")
while True:
rowIndex = False
currentChoice = False
print("Player turn:", Player)
column = int(input("Please enter a column: ")) - 1
if column <= 6:
print(True)
else:
print("You can choose numbers only between 1 and 7 included!")
continue
if Player == 1:
for i in range(5, -1, -1):
if field[0][column] != " ":
print("This column is already filled up! You can't put here anymore!")
full = 0
for b in range(7):
if field[0][b] != " ":
full += 1
if full == 7:
print("There is no winner!")
reset()
break
else:
if field[i][column] != " ":
continue
else:
field[i][column] = colored("X", "red")
drawField(field)
Player = 2
currentChoice = field[i][column]
rowIndex = i
break
else:
for i in range(5, -1, -1):
if field[0][column] != " ":
print("This column is already filled up! You can't put here anymore!")
full = 0
for b in range(7):
if field[0][b] != " ":
full += 1
if full == 7:
print("There is no winner!")
reset()
break
else:
if field[i][column] != " ":
continue
else:
field[i][column] = colored("O", "green")
drawField(field)
currentChoice = field[i][column]
Player = 1
rowIndex = i
break
characters = []
for i in range(6):
print(i)
characters.append(field[i][column])
print(characters)
winnerCheck(characters)
【问题讨论】:
-
您收到
winnernot used 的 lint 错误,因为您将值分配给winner,然后立即分配另一个值,语句winner = 0除了执行不必要的任务外没有任何作用。
标签: python algorithm function logic game-development