我尝试制作一个功能来检查是否有人赢了,如果它上下起伏而不是对角线或横跨。
它有时会起作用,但有时会说它连续3次获胜。
继承我的代码。
#myBoard is a 2d array storing the board. col is the column a player is
#trying to move, and player is the player to move. If it is a valid move,
#the program will go ahead and change myBoard.
def move2(myBoard, col, player):
if player == True:
for i in range(len(myBoard) - 1,-1,-1):
if myBoard[i][col] == 0:
myBoard[i][col] = 1
player = False
break
else:
for i in range(len(myBoard) - 1,-1,-1):
if myBoard[i][col] == 0:
myBoard[i][col] = -1
player = True
break
return myBoard, player
#Returns 1 if player 1 has won, a -1 if player 2 has won, and 0 otherwise.
#lastColPlayed is the last valid move that was made.
def checkWin(myBoard, lastColPlayed):
player1amount = 0
player2amount = 0
for i in range(len(myBoard) - 1):
if myBoard[i][lastColPlayed] == 1:
player2amount = 0
player1amount += 1
if myBoard[i][lastColPlayed] == -1:
playet1amount = 0
player2amount += 1
if player1amount == 3:
return 1
elif player2amount == 3:
return -1
else:
return 0
#prints myBoard to the screen
def printBoard(myBoard):
for row in myBoard:
for item in row:
if item == 0:
print("_", end="")
elif item == -1:
print("0", end="")
elif item == 1:
print("X", end="")
print()
#returns true if it's a draw
def isDraw(myBoard):
return False
def main():
won = 0
draw = False
player1turn = True
print("Welcome to Connect Four!")
rows = input("Please enter a number of rows: ")
check = True
while check == True:
try:
if int(rows) <= 5:
while int(rows) <= 5:
rows = input("Please enter a Valid choice: ")
else:
check = False
except ValueError:
rows = input("Please enter a Valid choice: ")
columns = input("Please enter a number of columns: ")
check2 = True
while check2 == True:
try:
if int(columns) <= 5:
while int(columns) <= 5:
columns = input("Please enter a Valid choice: ")
else:
check2 = False
except ValueError:
columns = input("Please enter a Valid choice: ")
myBoard = []
myBoardTemp = []
for i in range(int(columns)):
myBoardTemp.append(0)
for i in range(int(rows)):
myBoard.append([0] * int(columns))
printBoard(myBoard)
check3 = True
while won == 0 and draw == False:
move = input("Please enter a move: ")
while check3 == True:
try:
if int(move) < 0 or int(move) > len(myBoard[0]):
while int(move) < 0 or int(move) > len(myBoard[0]):
move = input("Please enter a valid choice: ")
else:
check3 = False
except ValueError:
move = input("Please enter a valid choice: ")
myBoard, player1turn = move2(myBoard,int(move) - 1,player1turn)
printBoard(myBoard)
won = checkWin(myBoard,int(move) - 1)
draw = isDraw(myBoard)
if won == 1:
print("Player 1 has won!")
elif won == -1:
print("Player 2 has won!")
elif draw == True:
print("It is a draw!")
main()
这一次有效
____________________
____________________
X___________________
X_0_________________
X_0_________________
X_0_________________
Player 1 has won!
这一次它没有
____________________
____________________
__XX________________
_X0X0_______________
_0XX0_______________
X0X00_______________
Player 1 has won!
有什么问题?
答案 0 :(得分:1)
一些事情:
1)range(n)返回从0到n-1的所有值
因此,当您在checkWin
中输入时:
for i in range(len(myBoard) - 1):
你实际上并没有考虑底线。
2)你只是连续3次检查。第一个例子给出看似正确答案的唯一原因是因为它没有考虑底行。 在你的第二个例子中,你连续有三个X(不包括底行),这就是为什么它错误地断言胜利。
所以答案是:
player1amount
和player2amount
是否为4(或更好:&gt; 3)