gpt4 book ai didi

python - 玩家轮流后我如何保持积分更新?

转载 作者:太空宇宙 更新时间:2023-11-04 04:11:47 25 4
gpt4 key购买 nike

我不能让我的 self.points 改变我的 points 变量。每次玩家再次轮到他/她时,它只会重新启动用户点数回到 0。

该程序最少支持 2 名玩家,最多支持 4 名玩家。

您有 2 个随机掷出的六面骰子。如果玩家获得 double ,则将两个骰子加在一起,然后乘以 2。答案将加到分数中。如果第一个骰子是 1,则将两个骰子相加,然后从玩家点数中减去,结束回合。

如果玩家没有得到双倍或第一个骰子没有得到 1,则将两个骰子相加,然后添加到玩家的分数中。这一直持续到玩家达到 100 分,结束游戏。

import random, sys

class game():

def __init__(self, name):
self.name = name
self.stop = False


def mainloop(self):
print("Would you like to roll the dice {}?: ".format(self.name))
return input().lower().startswith('y')


def playing(self, points, score):
self.points = points
self.score = score
while True:
if self.mainloop() == True:
print("it's {} go at the game with {} points".format(self.name, self.points))
self.rule()
if self.points >= 100:
print("the winners is {} with {} many points".format(self.name, self.points))
sys.exit()
print("{} your points is: {}".format(self.name, self.points))
if self.stop == True:
print("###############################################################")
print("##################### NEXT PLAYERS TURN #####################")
print("###############################################################")
self.score.append(self.points)
return (self.score)
else:
self.score.append(self.points)
return (self.score)


def rule(self):
question = 0
answer = 0
dice_1 = random.randint(1, 6)
dice_2 = random.randint(1, 6)
print("dice 1 is: {}".format(dice_1))
print("dice 2 is: {}".format(dice_2))


if dice_1 == dice_2:
if dice_1 != 1:
question = "({} + {}) * 2".format(dice_1, dice_2)
answer = eval(question)
print("add {} POINTS!!!".format(answer))
question = ("{} + {}".format(self.points, answer))
self.points = eval(question)
print(self.points)



elif dice_1 == 1:
print("add 25 POINTS!!!")
question = ("{} + 25".format(self.points))
self.points = eval(question)
print(self.points)



elif dice_1 == 1 or dice_2 == 1:
print("unluckly! minus the points")
question = "{} + {}".format(dice_1, dice_2)
answer = eval(question)
print("{} - {}".format(self.points, answer))
question = ("{} - {}".format(self.points, answer))
self.points = eval(question)
print(self.points)
self.stop = True


elif dice_1 != 1 or dice_2 != 1:
print("adding points!!!")
question = "{} + {}".format(dice_1, dice_2)
answer = eval(question)
print("{} + {}".format(self.points, answer))
question = "{} + {}".format(self.points, answer)
self.points = eval(question)
print(self.points)
return True





def check_letter(question):
if question.isalpha():
return False
else:
print("please input a letter")
return True




def main():
number = [2, 3, 4]
players = 0
while players not in number:
players = int(input("how many players are there? please input a number between 2 and 4: "))

score = []
username = []
for x in range(int(players)):
name = input("what is your name?: ")
while check_letter(name) == True:
name = input("what is your name? please input a letter: ")
username == username.append(name)

for z in range(int(players)):
score.append(0)

while True:
for user in username:
for points in score:
while True:
gamer = game(user)
gamer.playing(points, score)
break
break





main()

最佳答案

可能需要进行此调整:

    while True:
for i, user in enumerate(username):
for points in score:
while True: # I'm not sure why you have this, if you break in the first iteration in any case
gamer = game(user, points)
new_points = gamer.playing()
if new_points is not None: # I was not sure if you return always the new points
score[i] = new_points
break
break

另一件事,你应该改变:

    def __init__(self, name, points):
self.name = name
self.points = points
self.stop_turn = False #instead of own method self.false

(删除方法 self.false 并重命名变量;))

一次完整的代码(还有很多需要重构):

import random, sys


class game():

def __init__(self, name, points, score):
self.name = name
self.stop = False
self.points = points
self.score = score

def mainloop(self):
print("Would you like to roll the dice {}?: ".format(self.name))
return input().lower().startswith('y')

def playing(self):
while True:
if self.mainloop():
print("it's {} go at the game with {} points".format(self.name, self.points))
self.rule()
if self.points >= 100:
print("the winners is {} with {} many points".format(self.name, self.points))
sys.exit()
print("{} your points is: {}".format(self.name, self.points))
if self.stop:
print("###############################################################")
print("##################### NEXT PLAYERS TURN #####################")
print("###############################################################")
# self.score.append(self.points)
return self.points
else:
# self.score.append(self.points)
return self.points

def rule(self):
question = 0
answer = 0
dice_1 = random.randint(1, 6)
dice_2 = random.randint(1, 6)

print("dice 1 is: {}".format(dice_1))
print("dice 2 is: {}".format(dice_2))

if dice_1 == dice_2:
if dice_1 != 1:
question = "({} + {}) * 2".format(dice_1, dice_2)
answer = dice_1 + dice_2
print("add {} POINTS!!!".format(answer))
question = ("{} + {}".format(self.points, answer))
self.points += answer
print(self.points)



elif dice_1 == 1:
print("add 25 POINTS!!!")
question = ("{} + 25".format(self.points))
self.points += 25
print(self.points)



elif dice_1 == 1 or dice_2 == 1:
print("unluckly! minus the points")
question = "{} + {}".format(dice_1, dice_2)
answer = dice_1 + dice_2
print("{} - {}".format(self.points, answer))
question = ("{} - {}".format(self.points, answer))
self.points -= answer
print(self.points)
self.stop = True


elif dice_1 != 1 or dice_2 != 1:
print("adding points!!!")
question = "{} + {}".format(dice_1, dice_2)
answer = dice_1 + dice_2
print("{} + {}".format(self.points, answer))
question = "{} + {}".format(self.points, answer)
self.points += answer
print(self.points)
return True


def check_letter(question):
if question.isalpha():
return False
else:
print("please input a letter")
return True


def main():
number = [2, 3, 4]
players = 0
while players not in number:
players = int(input("how many players are there? please input a number between 2 and 4: "))

score = []
username = []
for x in range(int(players)):
name = input("what is your name?: ")
while check_letter(name) == True:
name = input("what is your name? please input a letter: ")
username.append(name)

for z in range(int(players)):
score.append(0)

while True:
for i, user in enumerate(username):
gamer = game(user, score[i], score)
print("??", score, i)
score[i] = gamer.playing()


main()

关于python - 玩家轮流后我如何保持积分更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56167746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com