Ship of Fools 小游戏的Python实现

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()

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值