日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

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

發(fā)布時(shí)間:2023/12/1 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python掷骰子_用于掷骰子的Python程序(2人骰子游戏) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

python擲骰子

Here, we will be going to design a very simple and easy game and implement it using abstract 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.

在這里,我們將設(shè)計(jì)一個(gè)非常簡單的游戲,并使用抽象數(shù)據(jù)類實(shí)現(xiàn)它。 該代碼包含兩個(gè)不同的類(整個(gè)程序的基礎(chǔ))。 一個(gè)將是玩家的職業(yè),其他將是游戲的職業(yè)。

The formal code structure is as:

正式的代碼結(jié)構(gòu)如下:

  • 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()類 :此玩家類將存儲(chǔ)玩家名稱,年齡和游戲的顏色代碼。 有一種稱為得分的方法,該方法存儲(chǔ)與玩家關(guān)聯(lián)的屬性得分。 另一個(gè)方法getscore()用來調(diào)用存儲(chǔ)的分?jǐn)?shù)值。

  • 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()類 :此類表示游戲,并接受輸入作為玩家(類類型)和步數(shù)。 方法__init __()定義與類類型游戲關(guān)聯(lián)的屬性。 game ()方法由整個(gè)游戲組成。

  • 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()函數(shù) :函數(shù)dice只是將輸出值設(shè)為[1,2,3,4,5,6]中的一個(gè)隨機(jī)值。 這使用random.choice()函數(shù)執(zhí)行此任務(wù)。

Game Rules:

游戲規(guī)則:

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.

玩家將擲出一個(gè)骰子,輸出將被添加到該玩家的當(dāng)前分?jǐn)?shù)中(最初等于零)。 如果骰子的輸出為6,則將其再次拋出(一個(gè)骰子:6,再轉(zhuǎn)一圈:4。則總數(shù)為6 + 4 = 10)。 總數(shù)的總和等于具有特定次數(shù)的嘗試的玩家總得分。

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-------------- 485 489 491 525 NAME: zack COLOUR: green SCORE: 485 NAME: kina COLOUR: red SCORE: 491 NAME: johny COLOUR: yellow SCORE: 489 NAME: usher COLOUR: blue SCORE: 525

Practice more python experiences here: python programs

在這里練習(xí)更多python經(jīng)驗(yàn): python程序

翻譯自: https://www.includehelp.com/python/program-for-rolling-the-dice-2-player-dice-game.aspx

python擲骰子

總結(jié)

以上是生活随笔為你收集整理的python掷骰子_用于掷骰子的Python程序(2人骰子游戏)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。