2-player rolling the bonus game in Python: Here, we will figure out how to execute a python program for rolling the bones (2-player dice game) utilizing theoretical information class?
Here, we will be going to plan an exceptionally basic and simple game and actualize it utilizing dynamic information class. The code comprises of two distinct classes (The base of the entire program).
The one will be the class for the player and others will be for the game.
The proper code structure is as:
- player() class: This player class will store player name, its age and its shading code for the game. There is a method called score which stores the trait score related to the player.
- Another method getscore() for calling the estimation of score put away.
- game() class: This class speaks to the game and accept contribution as the player (class type) and the number of trails.
- The method init() characterizes the trait related to the class type game. The method gaming() is comprising of the entire game.
- dice() work: The capacity dice simply give yield as an irregular incentive from the number set [1,2,3,4,5,6]. This uses random.choice() work for playing out this assignment.
Game Rules:
Player will toss bones and the yield will be added to the present scores of the player (at first equivalent to zero). On the off chance that the bones had yield 6, at that
point it would be tossed once more (one shaker: 6, one more turn: 4. At that point the all out would be 6+4 = 10). The entirety of absolute will toss id the all-out score of the player with a specific number of preliminaries.
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