Python实现掷骰子小游戏 The Game of Pig

一、游戏规则简介:

设有n个玩家(2~5个人比较合适),所有玩家过一遍叫一个 round,每个玩家的机会叫一个 turn 每个turn: 1,只要扔到 1 ,本轮所有得分清零,骰子给下一个玩家; 2,如果扔到其他的数字,可以选择继续扔,只要不扔到 1 ,结果就可以累加;也可以选择结束自己的 turn ,把骰子给下一个玩家 玩家中,积分先达到 100 分的为赢家。

二、代码实现

import random


class Player:
    def __init__(self, num, score, iswin=False, addition=0):
        self.num = num  # 玩家编号
        self.score = score  # 玩家得分
        self.addition = addition  # 玩家一个turn的加分

    def one_roll(self):  # 投一次骰子
        return random.randint(1, 6)  # 用随机数来模拟投骰子

    def continue_or_not(self):
        will = ' '  # 玩家的意愿
        while will is not 'y' or 'n':
            will = input('want to continue?(y/n) ')
            if will is 'y':
                return True
            elif will is 'n':
                return False


def initialize(player_list):  # 用一个列表来存储玩家对象
    num_players = int(input('please input number of players(2~5): '))
    for i in range(num_players):
        player_list.append(Player(i + 1, 0))


# Initialize
player_list = []  # 玩家列表
winner = 0  # 记录赢家编号
Round = 0  # 记录round数
initialize(player_list)  # 对玩家列表初始化

# Begin the game

while not winner:
    score_board = []
    Round += 1
    print('--------------Round: {}---------------'.format(Round))  # 显示round数
    for man in player_list:
        print('------------************------------')
        print('Player_{}:'.format(man.num))  # 显示当前玩家
        man.addition = 0  # 对本turn的加分预先清零
        go_on = True  # 是否继续
        while go_on:
            result = man.one_roll()
            print(result)
            man.addition = man.addition + result
            if result is 1:
                man.addition = 0
                break
            go_on = man.continue_or_not()
        man.score = man.score + man.addition
        score_board.append(man.score)
        if man.score >= 100:
            winner = man.num
            print('Winner: player_{}'.format(winner))
            break
    print("score_board: {}".format(score_board))


三、运行结果

please input number of players(2~5): 2
--------------Round: 1---------------
------------************------------
Player_1:
5
want to continue?(y/n) y
4
want to continue?(y/n) y
3
want to continue?(y/n) y
4
want to continue?(y/n) y
4
want to continue?(y/n) y
5
want to continue?(y/n) y
6
want to continue?(y/n) y
2
want to continue?(y/n) n
------------************------------
Player_2:
4
want to continue?(y/n) y
4
want to continue?(y/n) y
5
want to continue?(y/n) y
5
want to continue?(y/n) n
score_board: [33, 18]
--------------Round: 2---------------
------------************------------
Player_1:
1
------------************------------
Player_2:
4
want to continue?(y/n) y
5
want to continue?(y/n) y
4
want to continue?(y/n) y
3
want to continue?(y/n) n
score_board: [33, 34]
--------------Round: 3---------------
------------************------------
Player_1:
1
------------************------------
Player_2:
2
want to continue?(y/n) y
4
want to continue?(y/n) y
4
want to continue?(y/n) y
6
want to continue?(y/n) y
1
score_board: [33, 34]
--------------Round: 4---------------
------------************------------
Player_1:
2
want to continue?(y/n) y
2
want to continue?(y/n) y
5
want to continue?(y/n) y
4
want to continue?(y/n) y
3
want to continue?(y/n) y
3
want to continue?(y/n) y
6
want to continue?(y/n) n
------------************------------
Player_2:
5
want to continue?(y/n) y
1
score_board: [58, 34]
--------------Round: 5---------------
------------************------------
Player_1:
1
------------************------------
Player_2:
5
want to continue?(y/n) y
5
want to continue?(y/n) y
1
score_board: [58, 34]
--------------Round: 6---------------
------------************------------
Player_1:
2
want to continue?(y/n) y
6
want to continue?(y/n) y
5
want to continue?(y/n) y
3
want to continue?(y/n) y
5
want to continue?(y/n) y
6
want to continue?(y/n) n
------------************------------
Player_2:
4
want to continue?(y/n) y
3
want to continue?(y/n) y
2
want to continue?(y/n) n
score_board: [85, 43]
--------------Round: 7---------------
------------************------------
Player_1:
4
want to continue?(y/n) n
------------************------------
Player_2:
3
want to continue?(y/n) y
4
want to continue?(y/n) y
2
want to continue?(y/n) y
5
want to continue?(y/n) n
score_board: [89, 57]
--------------Round: 8---------------
------------************------------
Player_1:
4
want to continue?(y/n) n
------------************------------
Player_2:
4
want to continue?(y/n) y
1
score_board: [93, 57]
--------------Round: 9---------------
------------************------------
Player_1:
2
want to continue?(y/n) n
------------************------------
Player_2:
6
want to continue?(y/n) y
1
score_board: [95, 57]
--------------Round: 10---------------
------------************------------
Player_1:
3
want to continue?(y/n) n
------------************------------
Player_2:
4
want to continue?(y/n) y
3
want to continue?(y/n) y
5
want to continue?(y/n) y
4
want to continue?(y/n) n
score_board: [98, 73]
--------------Round: 11---------------
------------************------------
Player_1:
1
------------************------------
Player_2:
3
want to continue?(y/n) y
2
want to continue?(y/n) y
3
want to continue?(y/n) y
6
want to continue?(y/n) y
6
want to continue?(y/n) y
1
score_board: [98, 73]
--------------Round: 12---------------
------------************------------
Player_1:
2
want to continue?(y/n) n
Winner: player_1
score_board: [100]

你可能感兴趣的:(python,python)