import random
class Die:
def roll(self):
self._value=random.randint(1,6)
def get_value(self):
return self._value
class DiceCup:
def __init__(self):
self._dice=[]
for x in range(5):
self._dice.append([Die(),0])
def value(self,index):
return self._dice[index][0].get_value()
def bank(self,index):
self._dice[index][1]=1
def is_banked(self,index):
if self._dice[index][1]==1:
return True
else:
return False
def release(self,index):
self._dice[index][1]=0
def release_all(self):
for i in range(5):
self._dice[i][1]=0
def roll(self):
for i in range(5):
if self._dice[i][1]==False:
self._dice[i][0].roll()
class PlayRoom:
def __init__(self):
self._players=[]
def set_game(self,game):
self._game=game
def add_player(self,player):
self._players.append(player)
def reset_scores(self):
for p in self._players:
p.reset_score()
def play_round(self,game):
for p in self._players:
p.play_round(game)
def game_finished(self):
for i in self._players:
if i.current_score()>=self._game.get_winscore():
return True
def print_score(self):
for i in self._players:
print('\n'+i.get_name(),'has',i.current_score(),'score\n')
def print_winner(self):
maxi=self._players[0].current_score()
for i in self._players:
if maxi<i.current_score():
maxi=i.current_score()
winner=[]
for i in self._players:
if maxi==i.current_score():
winner.append(i.get_name())
print('Winner is',winner)
class ShipofFoolsGame:
def __init__(self,score):
self.winning_score=score
self._cup=DiceCup()
def round(self):
has_ship = False
has_captain = False
has_crew = False
# the sum of the remaining dice, i.e., the score.
crew = 0
# Repeat three times
Round=0
while Round<3:
Round+=1
self._cup.roll()
print(self._cup.value(0),self._cup.value(1),self._cup.value(2),self._cup.value(3),self._cup.value(4))
if not has_ship:
for i in range(5):
if self._cup.value(i)==6:
self._cup.bank(i)
has_ship = True
break
if has_ship and not has_captain:
# A ship but not a captain is banked
for i in range(5):
if self._cup.value(i)==5:
self._cup.bank(i)
has_captain = True
break
if has_ship and has_captain and not has_crew:
# A ship and captain but not a crew is banked
for i in range(5):
if self._cup.value(i)==4:
self._cup.bank(i)
has_crew = True
break
if has_ship and has_captain and has_crew:
# Now we got all needed dice, and can bank the ones we like to save.
#bank all unbanked dice > 3
if Round<3:
num=int(input('How many dice do you want to reroll:'))
if num==1:
index=int(input('What dice do you want to bank:'))-1
while self._cup.is_banked(index)==True:
print('This has been banked')
index=int(input('What dice do you want to bank:'))
self._cup.bank(i)
elif num==0:
for i in range(5):
self._cup.bank(i)
self._cup.release_all()
if has_ship and has_captain and has_crew:
for i in range(5):
crew+=self._cup.value(i)
crew_=15
else:
crew=0
return crew
def get_winscore(self):
return self.winning_score
class Player:
def __init__(self,name):
self._name=name
self._score=0
def current_score(self):
return self._score
def reset_score(self):
self.score=0
def play_round(self,game):
print('\n'+self._name+'\'s turn:\n')
self._score=game.round()
def get_name(self):
return self._name
if __name__=='__main__':
room=PlayRoom()
s1=ShipofFoolsGame(21)
room.set_game(s1)
room.add_player(Player('Ginger'))
room.add_player(Player('Gily'))
room.reset_scores()
while not room.game_finished():
room.play_round(s1)
room.print_score()
room.print_winner()
Ship of Fools 小游戏的Python实现
最新推荐文章于 2024-08-28 16:14:15 发布