[英]Logic Error in a matrix roll dice program in Python
我有一個簡單游戲的下面的代碼,讓player1和player2輪流滾動兩個六面骰子(死)。 在達到49(矩陣中的最后一個數字)時,玩家將獲勝。 玩家從1開始,然后向上移動滾動的數字(兩者都死亡)。
我的代碼中有三個錯誤( 邏輯錯誤 ),我需要一些幫助。
1) 獲勝者功能不起作用。 如果玩家達到49或接近(見最后的輸出),它在邏輯上不允許他們獲勝。 (只需按Enter鍵運行測試,看下面粘貼的特殊輸出)。 玩家1和玩家2可以達到49,但它不會將他們帶到“贏家()”模塊。 為什么? 我正確地使用了“> =”或者我沒有?
話雖如此,在一次測試中:玩家2達到了49,並沒有進入獲勝者()模塊:
Press Enter to continue
Player 2, it's your turn to roll the dice: Press r to roll:>>>
Player2: You rolled a: 4 and a 6 which gives you a: 10
You have moved 10 spaces to position:....... 49
2)在完全退出程序並重新開始時, player1position和player2position的值被重置為0並且它可以工作。 但是如果我只是去main()來重放游戲,那么這些值就不會被重置 。 我不完全理解全局變量和初始化的用法和聲明。 任何人都可以解釋一下,解釋/建議會有所幫助
3) 負數不能准確處理。 如果在任何一個回合中玩家擲出兩個相同的號碼,那么他們就應該回到滾動的數字。 例如,如果2和2滾動,則玩家返回4.但是,如果玩家在5,10個空格時回到-5,則下一回合不會將玩家1位置或玩家2位置的總數加起來並且計數是搞砸了 。 我嘗試了各種修復,我對任何評論,建議和改進都感興趣。
錯誤的輸出如下:
首先,這是矩陣:
*************LOADING GAME******************
Welcome: player1 and player2
[43, 44, 45, 46, 47, 48, 49]
[42, 41, 40, 39, 38, 37, 36]
[29, 30, 31, 32, 33, 34, 35]
[28, 27, 26, 25, 24, 23, 22]
[15, 16, 17, 18, 19, 20, 21]
[14, 13, 12, 11, 10, 9, 8]
[1, 2, 3, 4, 5, 6, 7]
相關的代碼如下:
import sys
import csv
import random
def main():
startscreen()
def loadstartgame(player1,player2):
with open("messages.txt","r") as f:
idnumber="1"
fReader=csv.reader(f)
for row in fReader:
for field in row:
if field==idnumber:
print(row[2])
start=input("Start?:>>")
if start=="s" or "S":
callmatrix(player1,player2,7)
def startscreen():
print("******************************Welcome to CONQUER THE MATRIX****************************")
print("You can take on a friend in this game")
player1=input("Enter name of Player 1: ")
player2=input("Enter name of Player 2: ")
print("Welcome ...", player1,"and, ", player2, "...Are you ready?")
response=input("Yes or No? Player 1 - perhaps you could confirm please: ")
if response=="yes" or "Yes" or "yEs" or "YeS":
loadstartgame(player1, player2)
else:
sys.exit()
def matrix(n):
grid = [[1 + i + n * j for i in range(n)] for j in range(n)]
for row in grid[1::2]:
row.reverse()
return grid[::-1][:]
def callmatrix(player1,player2, n):
print("*************LOADING GAME******************")
print("Welcome:", player1,"and", player2)
for i in matrix(n):
print(i)
playing = True
playerturns(player1,player2,playing)
def playerturns(player1,player2,playing):
print(" - - - - - - - - - - ")
print("Press Enter to continue")
while(playing):
roll=input()
if roll=="":
RollTwoDiceP1(player1,player2)
else:
break
RollTwoDiceP2(player1,player2)
global player1position #declare a local variable called player1position
player1position=0 #set the global variable before the start of this sub to 0
def RollTwoDiceP1(player1,player2):
global player1position
print("player1position in PLAYER1ROLL", player1position)
#print("player1position at stat of RollTwoDicep1", player1position) #a test that prints the current value of player 1 position
turn=input("Player 1, it's your turn to roll the dice: Press r to roll:>>>")
die1=random.randint(1,6)
die2=random.randint(1,6)
#die1=5 #for testing purposes
#die2=5 #for testing purposes
roll=die1+die2
print("Player1: You rolled a:", die1, "and a", die2, "which gives you a:", roll)
playing = False
matrixlist=matrix(7)
if die1==die2:
gobackspacesP1(roll,player1,player2)
else:
print("You have moved", roll, "spaces to position:.......",matrixlist[6][0]+(roll+player1position))
#print("playerposition plus roll:", player1position+roll) #a test that prints the playerposition+roll - this always comes up to 1 more than it should do on the location, so below substract 1, to make it produce an accurate location on the matrix for a particular roll
player1position=matrixlist[6][0]+(roll+player1position)
if player1position>=49:
winner()
else:
playerturns(player1,player2,playing)
#global player1position
#global player2position
def gobackspacesP1(roll,player1,player2):
global player1position
if player1position<=1:
player1position=0
print("Oh dear - sorry you are now back to square 1 - literally!")
else:
print("Oh dear -you are now back on the matrix to position:", (1+player1position)-roll)
matrixlist=matrix(7)
#new player1position=the first playerposition minus the roll (rather than add it) and + 1 (as the initial position is always 1)
player1position=1+(player1position-roll)
#player1position=matrixlist[6][0]+(roll+player1position)-roll
playing=False
print("player1position in goback", player1position)
playerturns(player1,player2,playing)
def gobackspacesP2(roll,player1,player2):
global player2position
if player2position<=1:
player2position=0
print("Oh dear - sorry you are now back to square 1 - literally!")
else:
print("Oh dear -you are now back on the matrix to position:", (1+player2position)-roll)
matrixlist=matrix(7)
#new player1position=the first playerposition minus the roll (rather than add it) and + 1 (as the initial position is always 1)
player2position=1+(player2position-roll)
#player1position=matrixlist[6][0]+(roll+player1position)-roll
playing=True
print("player2position in goback", player2position)
playerturns(player1,player2,playing)
global player2position
player2position=0
def RollTwoDiceP2(player1,player2):
global player2position
#Remember you need to add the functioanlity for a double identical dice roll to this subroutine for Player2 as well.
turn=input("Player 2, it's your turn to roll the dice: Press r to roll:>>>")
die1=random.randint(1,6)
die2=random.randint(1,6)
roll=die1+die2
print("Player2: You rolled a:", die1, "and a", die2, "which gives you a:", roll)
playing = True
matrixlist=matrix(7)
if die1==die2:
gobackspacesP2(roll,player1,player2)
else:
print("You have moved", roll, "spaces to position:.......",matrixlist[6][0]+(roll+player2position))
#print("playerposition plus roll:", player1position+roll) #a test that prints the playerposition+roll - this always comes up to 1 more than it should do on the location, so below substract 1, to make it produce an accurate location on the matrix for a particular roll
player2position=matrixlist[6][0]+(roll+player2position)
if player2position>=49:
winner()
else:
playerturns(player1,player2,playing)
def winner():
print("Ahh, it appears you have won! Congratulations!")
sys.exit()
def easygrid():
grid=[[43,44,45,46,47,48,49],[42,41,40,39,38,37,36],[29,30,31,32,33,34,35],[28, 27, 26, 25,24,23,22], [15,16,17, 18, 19, 20, 21], [14, 13, 12, 11, 10, 9, 8], [1, 2, 3,4,5,6,7]]
for i in grid:
print(i)
main()
我也對任何關於我使用全局變量的評論以及如何在這種情況下最好地使用/不使用它們感興趣。
我不想要基於類的解決方案,所以答案必須是解決這個問題。
ERRONEOUS OUTPUT 1(只有玩家2獲勝,即使玩家1應該獲勝)最后幾次贏得勝利:
player1position in PLAYER1ROLL 38
Player 1, it's your turn to roll the dice: Press r to roll:>>>
Player1: You rolled a: 3 and a 5 which gives you a: 8
You have moved 8 spaces to position:....... 47
- - - - - - - - - -
Press Enter to continue
Player 2, it's your turn to roll the dice: Press r to roll:>>>
Player2: You rolled a: 6 and a 5 which gives you a: 11
You have moved 11 spaces to position:....... 43
- - - - - - - - - -
Press Enter to continue
player1position in PLAYER1ROLL 46
Player 1, it's your turn to roll the dice: Press r to roll:>>>
Player1: You rolled a: 3 and a 5 which gives you a: 8
You have moved 8 spaces to position:....... 55
- - - - - - - - - -
Press Enter to continue
Player 2, it's your turn to roll the dice: Press r to roll:>>>
Player2: You rolled a: 6 and a 1 which gives you a: 7
You have moved 7 spaces to position:....... 50
You've won! Congratulations!
該錯誤出現在這一行:(在RollTwoDice1和RollTwoDice2子系統中)
player1position=matrixlist[6][0]+(roll+player1position)
矩陣列表[6] [0]添加了一個錯誤的+1,當刪除時排序邏輯錯誤。
另外:(在gobacktospaces subs)
player2position=1+(player2position-roll)
它有一個額外的-1,它不斷扭曲勝利者功能和加起來的結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.