1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > python掷骰子_用于掷骰子的Python程序(2人骰子游戏)

python掷骰子_用于掷骰子的Python程序(2人骰子游戏)

时间:2024-03-09 23:30:54

相关推荐

python掷骰子_用于掷骰子的Python程序(2人骰子游戏)

python掷骰子

Here, we will be going to design a very simple and easy game and implement it usingabstract data class. The code consists of two different classes (The base of the whole program). The one will be the class for the player and others will be for the game.

在这里,我们将设计一个非常简单的游戏,并使用抽象数据类实现它。 该代码包含两个不同的类(整个程序的基础)。 一个将是玩家的职业,其他将是游戏的职业。

The formal code structure is as:

正式的代码结构如下:

player() class: This player class will be storing player name, its age and its color code for the game. There is a method called score which stores the attribute score associated with the player. Another method getscore() for calling the value of score stored.

player()类:此玩家类将存储玩家名称,年龄和游戏的颜色代码。 有一种称为得分的方法,该方法存储与玩家关联的属性得分。 另一个方法getscore()用来调用存储的分数值。

game() class: This class represents the game and take input as the player (class type) and the number of trails. The method __init__() defines the attribute associated with the class type game. The method gaming() is consisting of the whole game.

game()类:此类表示游戏,并接受输入作为玩家(类类型)和步数。 方法__init __()定义与类类型游戏关联的属性。 game ()方法由整个游戏组成。

dice() function: The function dice just give output as a random value from the number set [1,2,3,4,5,6]. This uses random.choice() function for performing this task.

dice()函数:函数dice只是将输出值设为[1,2,3,4,5,6]中的一个随机值。 这使用random.choice()函数执行此任务。

Game Rules:

游戏规则:

Player will throw a dice and the output will be added to the current scores of the player (initially equal to zero). If the dice had output 6 then it would be thrown again (one dice: 6, one more turn: 4. Then the total would be 6+4 = 10). The sum of total will be throwing id the total score of the player with a particular number of trials.

玩家将掷出一个骰子,输出将被添加到该玩家的当前分数中(最初等于零)。 如果骰子的输出为6,则将其再次抛出(一个骰子:6,再转一圈:4。则总数为6 + 4 = 10)。 总数的总和等于具有特定次数的尝试的玩家总得分。

So let us get to the code:

因此,让我们看一下代码:

import randomdef roll():return random.choice([1,2,3,4,5,6])class player(object):def __init__(self, name, age, colour):self.name = nameself.age = ageself.colour = colourdef score(self, score):self.score = score def getscore(self):return self.scoredef getname(self):return self.namedef __str__(self):return 'NAME: ' + self.name + '\nCOLOUR: ' + self.colour + '\nSCORE: ' + str(self.score)class game(object):def __init__(self, playr, trails):self.trails = trailsself.playr = playrdef gaming(self):throw = 0score = 0for i in range(self.trails):throw = roll()if throw == 6:throw = throw + roll()score = throw + score return scoredef __str__(self):return self.playr.getname() + str(self.score)tri = 123 zack = player('zack', 24, 'green')johny = player('johny', 25, 'yellow')kina = player('kina', 14, 'red')usher = player('usher', 13, 'blue')print("-----------LETs PLAy THIs GAMe--------------\n" )#zack.score(88)#print(zack)zackscr = game(zack, tri)johnyscr = game(johny, tri)kinascr = game(kina, tri)usherscr = game(usher, tri)scr = []scr.append(zackscr.gaming())scr.append(johnyscr.gaming())scr.append(kinascr.gaming())scr.append(usherscr.gaming())scrsort = sorted(scr)for el in scrsort:print(el)zack.score(scr[0])usher.score(scr[3])kina.score(scr[2])johny.score(scr[1])#players = []#players.append(zack.getscore())#players.append(usher.getscore())#players.append(kina.getscore())#players.append(johny.getscore())# =============================================================================# =============================================================================# =============================================================================# # # = = = = = = == = = = == = = == = = = == = = == = = == = = == == = == == =#for el in players:# print('--', el)#print(scr[0]) print(zack, '\n')print(kina, '\n')print(johny, '\n')print(usher, '\n')

Output

输出量

-----------LETs PLAy THIs GAMe-------------- 485489491525NAME: zack COLOUR: greenSCORE: 485 NAME: kina COLOUR: redSCORE: 491 NAME: johnyCOLOUR: yellow SCORE: 489 NAME: usherCOLOUR: blue SCORE: 525

Practice more python experiences here: python programs

在这里练习更多python经验: python程序

翻译自: /python/program-for-rolling-the-dice-2-player-dice-game.aspx

python掷骰子

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。