玩家转身后如何保持积分更新?

时间:2019-05-16 11:40:23

标签: python

我无法使自己变分。每次玩家再次轮到他/她时,它只会重新将用户得分重新设置为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()

1 个答案:

答案 0 :(得分: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()