一、游戏规则简介:
设有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
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))
player_list = []
winner = 0
Round = 0
initialize(player_list)
while not winner:
score_board = []
Round += 1
print('--------------Round: {}---------------'.format(Round))
for man in player_list:
print('------------************------------')
print('Player_{}:'.format(man.num))
man.addition = 0
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]