【问题标题】:How do I keep the points updated after the players turn?玩家回合后如何保持积分更新?
【发布时间】:2019-05-16 11:40:22
【问题描述】:

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

该程序最少 2 人,最多 4 人。

您有 2 个随机掷出的六面骰子。如果玩家获得双打,则将两个骰子相加,然后乘以 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()

【问题讨论】:

  • 在您与我们共享的代码中,您从未向self.points 写过任何内容。所以他们会一直保持在0。
  • 另一点你的方法/属性(不确定你想要什么)self.false 是空的。你创建一个方法,然后分配false(不管那是因为它不是pythons False)。
  • 你需要将game.playing的返回值存储在某处。
  • 与其在评论部分发布您的帖子,不如编辑您的帖子。在 cmets 中无法正确格式化
  • 您发布的代码对我有用(它不会停留在 100 分,但可能您需要将==100>= 100 交换)

标签: python


【解决方案1】:

可能需要这种调整:

    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()

【讨论】:

  • 很抱歉,但是...我如何使用 self.stop_turn。我觉得有点傻,但想问一下以防万一
  • 只需将所有出现的self.false 替换为self.stop_turn
  • 我为迟到的回复尝试了这个抱歉,但是......它仍然无法正常工作。我想我可以通过学习(如果不是没有的话)来解决它并列举但仍然不明白。我确实尝试过你所说的但仍然无法跟踪特定玩家的积分? :(
猜你喜欢
  • 2022-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-31
  • 1970-01-01
  • 1970-01-01
  • 2021-07-28
相关资源
最近更新 更多