python擲骰子_用于擲骰子的Python程序(2人骰子游戲)

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.

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

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 random
def roll():
return random.choice([1,2,3,4,5,6])
class player(object):
def __init__(self, name, age, colour):
self.name = name
self.age = age
self.colour = colour
def score(self, score):
self.score = score    
def getscore(self):
return self.score
def getname(self):
return self.name
def __str__(self):
return 'NAME: ' + self.name + '\nCOLOUR: ' + self.colour + '\nSCORE: ' + str(self.score)
class game(object):
def __init__(self, playr, trails):
self.trails = trails
self.playr = playr
def gaming(self):
throw = 0
score = 0
for i in range(self.trails):
throw = roll()
if throw == 6:
throw = throw + roll()
score = throw + score    
return score
def __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

在這里練習更多python經驗: python程序

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

python擲骰子

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/379873.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/379873.shtml
英文地址,請注明出處:http://en.pswp.cn/news/379873.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

ForeignKey和ManyToManyField的限制關系

authorsmodels.ManyToManyField(Author,limit_choice_to{name__endswith:Smith}這樣可以更方便的查詢。轉載于:https://www.cnblogs.com/chenjianhong/archive/2012/03/22/4145158.html

linux 目錄命令_Linux目錄命令能力問題和解答

linux 目錄命令This section contains Aptitude Questions and Answers on Linux Directory Commands. 本節包含有關Linux目錄命令的 Aptitude問答。 1) There are the following statements that are given which of them are correct about Linux commands? In the Linux o…

終于在HP2133上成功安裝xp

今天拿到一臺HP2133迷你筆記本,原裝vista home basic,由于本人是在不喜歡vista,于是決定將使用xp換之。 很久沒有研究裝系統了,HP2133沒有光驅,以前也沒啥這方面經驗,搞這個玩意安裝完軟件折騰了大半天&…

Java——GUI(圖形用戶界面設計)

事件處理:事件:用戶的一個操作(例如:點擊一下鼠標,或者敲擊一下鍵盤)事件源:被操作的組件(例如:在一個窗體中的一個按鈕,那個按鈕就屬于被操作的組件,按鈕就是事件源)監聽器&#xf…

python安全攻防---信息收集---IP查詢

IP查詢是通過當前所獲得的URL去查詢對應IP地址的過程,可應用Socket庫函數中的gethostbyname()獲取域名所對用的IP值 程序如下: # -*- coding:utf-8 -*- IP查詢import socket ip socket.gethostbyname(www.baidu.com) print(ip)運行結果: …

智能課程表Android版-學年學期星期的實現

上次我們實現了日期和時間的動態顯示,這次我們來實現學年,學期,周次的顯示,如圖: 首先是學年學期的顯示: Calendar cCalendar.getInstance(); int yearc.get(Calendar.YEAR); int monthc.get(Calendar.MONTH)1;//Calen…

感染linux腳本程序技術

前言 ---- 本文來源于29A病毒雜志,其上對linux shell病毒技術有了一個綜合的闡述,我不想翻譯它,我以它的那篇為模板 寫了這篇中文的文章,里面的代碼我都做了調試. 對于shell編程的程序員來說所謂的shell病毒技術其實根本就是小牛一毛,這點在大家看完本文后就會有所體會 但,簡單…

Java——設計模式(簡單工廠模式)

* A:簡單工廠模式概述* 簡單工廠模式又叫靜態工廠方法模式,它定義了一個具體的工廠類負責創建一些類的實例* B:優點* 客戶端不需要再負責對象的創建,從而明確了各個類的職責* 簡單來說,客戶端你只需要用就可以了,就…

Java ObjectOutputStream writeFloat()方法與示例

ObjectOutputStream類writeFloat()方法 (ObjectOutputStream Class writeFloat() method) writeFloat() method is available in java.io package. 在java.io包中提供了writeFloat()方法 。 writeFloat() method is used to write the given 4 bytes of a float value. writeFl…

python安全攻防---信息收集---whois查詢

whois是用來查詢域名的IP以及所有者信息的傳輸協議。簡單地說,whois就是一個數據庫,用來查詢域名是否以及被注冊,以及注冊域名的詳細信息(如域名所有人、域名注冊商等)。 使用whois查詢,首先通過pip安裝py…

百度面試題:從輸入url到顯示網頁,后臺發生了什么?

參考http://igoro.com/archive/what-really-happens-when-you-navigate-to-a-url/ http://www.cnblogs.com/wenanry/archive/2010/02/25/1673368.html 原文:http://igoro.com/archive/what-really-happens-when-you-navigate-to-a-url/ 作為一個軟件開發者,你一定會…

VS2005無法啟動修復辦法

c:\Program Files\Microsoft Visual Studio 8\Common7\IDE>devenv /ResetSkipPkgs 轉載于:https://www.cnblogs.com/doc/archive/2008/10/10/1307887.html

Java——設計模式(工廠方法模式)

* A:工廠方法模式概述* 工廠方法模式中抽象工廠類負責定義創建對象的接口,具體對象的創建工作由繼承抽象工廠的具體類實現。* 簡單來說:先定義一個工廠,工廠里面有些方法,這些方法就是用來創建動物的,然后有很多子工…

python安全攻防---爬蟲基礎---get和post提交數據

get提交數據1 get提交的數據就附在提交給服務器的url之后,以?開頭參數之間以&隔開,例如/admin/user/123456.aspx?name123&id123 案例:寫個腳本,在sogou自動搜索周杰倫,并將搜索頁面的數據獲取 程…

JavaMail中解決中文附件名亂碼的問題

網上有很多類似的解決方案,很多是使用 if ((fileName ! null) && (fileName.toLowerCase().indexOf("gb2312") ! -1)){ fileName MimeUtility.decodeText(fileName); } 來解決,但對應gbk編碼的附件名,這里仍不能正確處…

Java ObjectOutputStream writeBytes()方法與示例

ObjectOutputStream類writeBytes()方法 (ObjectOutputStream Class writeBytes() method) writeBytes() method is available in java.io package. writeBytes()方法在java.io包中可用。 writeBytes() method is used to write the given string as a sequence of bytes. write…

如何集中注意力(3)

如何集中注意力(3) [ 2008-10-20 10:15 ]by Glen Stansberry of LifeDev (feed). 集中注意力并不是一件很容易的事情,尤其是處在當前這個充滿誘惑和紛擾的世界里。下面是繼續告訴大家一些有幫助的方法: 7. Plan your day to the T…

Java——網絡編程三要素

* A:計算機網絡* 是指將地理位置不同的具有獨立功能的多臺計算機及其外部設備,通過通信線路連接起來,在網絡操作系統、網絡管理軟件及網絡通信協議的管理和協調下,實現資源共享和信息傳遞的計算機系統。* 其實我們這些網,之所以能…

python安全攻防---爬蟲基礎--re解析數據

0x01 re基礎 使用re模塊,必須先導入re模塊 import refindall():匹配所有符合正則的內容,返回的是一個列表 import restr "我的電話:10086,女朋友電話:11011" list re.findall(\d,str) print…

BSP for good 3d engine

1.open sourcehttp://irrlicht.sourceforge.net/downloads/ 2.不錯的小材質http://www.permadi.com/tutorial/webgraph/index.html 3.java bsp display(demo):http://www.symbolcraft.com/graphics/bsp/index.php http://www.faqs.org/faqs/graphics/bsptree-faq/ 4.a bps ill…