Python3需要学校项目的帮助

2024-09-30 01:25:31 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我在高中修了一门编程课程,现在我正在制作一个游戏的程序,老师分配给我们所有人制作。这个游戏被称为“棍棒游戏”(如果你想更好地了解游戏的工作原理,请跳过这段视频的一半)。基本上我们一张桌子上有15根棍子,你可以拿走1根、2根或3根棍子。我有一个代码,工作的游戏,但它说,我做了一个非法的举动,为5杆剩余的任何发挥。我找不到问题,希望其他人能找到。你知道吗

pl1 = input("Player 1, what is your username?") #player 1
pl2 = input("Player 2, what is your username?") #player 2
turnsa = 0 #player1 turns
turnsb = 0 #player2 turns
x = 15 #number of sticks
whichplayer = 1 
while(x != 1): 
 while(whichplayer == 1):
   P1 = int(input(pl1 + ', choose an amount of sticks from 1-3 ' + str(x) + 
' sticks remaining')) 
   if P1 < x and P1 < 4: # check for legal move
     x = x - P1
     turnsa = turnsa + 1
     whichplayer = 2 #ending loop to start player 2 turn
   if P1 > 3 or P1 > x: #check for illegal move
     print('illegal move')
     continue #restarting player 1 loop
 while(whichplayer == 2):
   P2 = int(input(pl2 + ', choose an amount of sticks from 1-3 ' + str(x) + 
' sticks remaining'))
   if P2 < x and P2 < 4:
     x = x - P2
     turnsb = turnsb + 1
     whichplayer = 1
   if P2 > 3 or P2 > x:
     print('illegal move')
     continue 
if turnsa > turnsb:
  print('congrats ' + pl1 + ' you win')
if turnsb > turnsa:
  print('congrats ' + pl2 + ' you win')

Tags: of游戏inputmoveifplayerprintp2
1条回答
网友
1楼 · 发布于 2024-09-30 01:25:31

在第一个if语句中减少x,然后在第二个if检查P1 > x时,这是真的,因为您将x5设置为2,而P13。您可以在这里使用if/else,因为如果移动无效,您已经知道它将是无效的移动。此外,您还有一些边缘情况需要使用><!=比较进行检查(尝试每回合使用3根棍子,看看会发生什么)。我会把剩下的留给你锻炼,因为这是学校的工作,将是一个很好的学习经验。如果需要帮助,稍后将进行编辑。你知道吗

if P1 < x and P1 < 4: # check for legal move
    x = x - P1 # <========= decrementing here then checking it in the next if
    turnsa = turnsa + 1
    whichplayer = 2 #ending loop to start player 2 turn
if P1 > 3 or P1 > x: #check for illegal move
    print('illegal move')
    continue #restarting player 1 loop

相关问题 更多 >

    热门问题