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.mzph.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…