[英]win function for connect four isn't working correctly
我試着做一個函數來檢查是否有人贏了它是上下贏了,而不是對角線贏了。
它有時可以工作,但有時卻說連續三局獲勝。
這是我的代碼。
#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!
怎么了?
一些東西:
1)range(n)返回0到n-1之間的所有值,因此在checkWin
鍵入:
for i in range(len(myBoard) - 1):
您實際上沒有考慮底行。
2)您只連續檢查3個。 第一個示例給出看似正確答案的唯一原因是因為它不考慮最底行。 在第二個示例中,您連續有三個X(不包括底行),因此這就是它錯誤地宣稱勝利的原因。
所以答案是:
player1amount
和player2amount
是否為4(或更佳:> 3)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.