Python中矩阵掷骰子程序的逻辑错误
下面是一个简单游戏的代码,让玩家1和玩家2轮流掷两个六面骰子(骰子)。当达到49(矩阵中的最后一个数字)时,玩家将获胜。玩家从1开始,然后向上移动掷骰数(两个骰子的总数) 我的代码中有三个错误(逻辑错误),我需要一些帮助 1) 赢家功能不起作用。如果玩家达到49岁或接近49岁(请参见结尾的输出),则从逻辑上讲,他们不允许赢家。(只需继续按enter键运行测试,并查看下面粘贴的特殊输出)。玩家1和玩家2可以达到49,但不能进入“winner()”模块。为什么?我是否正确使用了“>=” 话虽如此,在一次测试中:玩家2达到49,但没有进入winner()模块: 2) 完全退出程序并重新启动后,player1position和player2position的值将重置为0,并正常工作。但是如果我只是转到main()重播游戏,这些值不会重置。我不完全理解全局变量和初始化的用法和声明。有人能解释一下吗?一个解释/建议会有帮助吗 3) 负数处理不准确。如果在任何回合中,一名玩家投掷两个相同号码的球,他们将返回掷骰号码。e、 g.如果2和2掷骰,玩家返回4。但是,如果玩家在5,10格时返回到-5,下一回合将不会将玩家1位置或玩家2位置的总数相加,并且计数会混乱。我尝试过各种修复,我对任何评论、建议和改进都感兴趣 错误输出如下: 首先,这是矩阵:Python中矩阵掷骰子程序的逻辑错误,python,matrix,logic,dice,Python,Matrix,Logic,Dice,下面是一个简单游戏的代码,让玩家1和玩家2轮流掷两个六面骰子(骰子)。当达到49(矩阵中的最后一个数字)时,玩家将获胜。玩家从1开始,然后向上移动掷骰数(两个骰子的总数) 我的代码中有三个错误(逻辑错误),我需要一些帮助 1) 赢家功能不起作用。如果玩家达到49岁或接近49岁(请参见结尾的输出),则从逻辑上讲,他们不允许赢家。(只需继续按enter键运行测试,并查看下面粘贴的特殊输出)。玩家1和玩家2可以达到49,但不能进入“winner()”模块。为什么?我是否正确使用了“>=” 话虽如此,在
*************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()
错误就在这一行:(在RollTwoDice1和RollTwoDice2子系统中)
matrixlist[6][0]添加了一个错误的+1,删除该+1后对逻辑错误进行排序
另外:(在gobacktospaces subs中)
它有一个额外的-1,这使得赢家功能和加总的结果都有偏差 错误就在这一行:(在RollTwoDice1和RollTwoDice2子系统中)
matrixlist[6][0]添加了一个错误的+1,删除该+1后对逻辑错误进行排序
另外:(在gobacktospaces subs中)
它有一个额外的-1,这使得赢家功能和加总的结果都有偏差 我没有用矩阵来存储玩家的位置?它们都是作为全局变量存储的player1position和player2position…..你说的使用矩阵是什么意思???@PythonFish这个问题问得太广泛了,你没有给我们足够的信息来解决所有问题(值重置)。你是否使用过传递给每个函数的
player1,player2
?我强烈建议使用一个包含你的方法的类,并将玩家位置作为属性而不是全局变量。我在帖子中提到过,我不希望使用类……但是,是的,注意到。如果我不得不猜测这个问题,您不会在第一次声明时声明全局变量,只在声明之后才声明,因此在您第一次声明player1position
..的地方,它不应该有global
标记@PythonFish你可以突出显示所有代码并点击{}
按钮我没有使用矩阵来存储玩家的位置?它们都是作为全局变量存储的player1position和player2position…..你说的使用矩阵是什么意思???@PythonFish这个问题问得太广泛了,你没有给我们足够的信息来解决所有问题(值重置)。你是否使用过传递给每个函数的player1,player2
?我强烈建议使用一个包含你的方法的类,并将玩家位置作为属性而不是全局变量。我在帖子中提到过,我不希望使用类……但是,是的,注意到。如果我不得不猜测这个问题,您不会在第一次声明时声明全局变量,只在声明之后才声明,因此在您第一次声明player1position
..的地方,它不应该有global
标记@PythonFish您可以突出显示所有代码并点击{}
按钮
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()
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!
player1position=matrixlist[6][0]+(roll+player1position)
player2position=1+(player2position-roll)