-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_game.py
260 lines (215 loc) · 6.64 KB
/
update_game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
from PIL import Image
a=Image.open("istockphoto-455302535-612x612.jpg")
a.show()
a.close()
class GamePlayer:
"""
Encapsulates a player properties
"""
def __init__(self, _id):
self._id = _id
# initial dummy rank -1
self.rank = -1
# starting position for every player is 1
self.position = 1
def set_position(self, pos):
self.position = pos
def set_rank(self, rank):
self.rank = rank
def get_pos(self):
return self.position
def get_rank(self):
return self.rank
class MovingEntity:
"""
You can create any moving entity , like snake or ladder or
wormhole by extending this
"""
def __init__(self, end_pos=None):
# end pos where player would be send on board
self.end_pos = end_pos
# description of moving entity
self.desc = None
def set_description(self, desc):
self.desc = None
def get_end_pos(self):
if self.end_pos is None:
raise Exception("no_end_position_defined")
return self.end_pos
class Snake(MovingEntity):
"""Snake entity"""
def __init__(self, end_pos=None):
super(Snake, self).__init__(end_pos)
self.desc = "Bit by Snake"
class Ladder(MovingEntity):
"""Ladder entity"""
def __init__(self, end_pos=None):
super(Ladder, self).__init__(end_pos)
self.desc = "Climbed Ladder"
class Board:
"""
define board with size and moving entities
"""
def __init__(self, size):
self.size = size
# instead of using list, we can use map of
# {pos:moving_entity} to save space
self.board = {}
def get_size(self):
return self.size
def set_moving_entity(self, pos, moving_entity):
# set moving entity to pos
self.board[pos] = moving_entity
def get_next_pos(self, player_pos):
# get next pos given a specific position player is on
if player_pos > self.size:
return player_pos
if player_pos not in self.board:
return player_pos
print(f'{self.board[player_pos].desc} at {player_pos}')
return self.board[player_pos].get_end_pos()
def at_last_pos(self, pos):
if pos == self.size:
return True
return False
class Dice:
def __init__(self, sides):
# no of sides in the dice
self.sides = sides
def roll(self):
# return random number between 1 to sides
import random
ans = random.randrange(1, self.sides + 1)
return ans
class Game:
def __init__(self):
# game board object
self.board = None
# game dice object
self.dice = None
# list of game player objects
self.players = []
# curr turn
self.turn = 0
self.winner = None
# last rank achieved
self.last_rank = 0
# no of consecutive six in one turn, resets every turn
self.consecutive_six = 0
def initialize_game(self, board: Board, dice_sides, players):
"""
Initialize game using board, dice and players
"""
self.board = board
self.dice = Dice(dice_sides)
self.players = [GamePlayer(i) for i in range(players)]
def can_play(self):
if self.last_rank != len(self.players):
return True
return False
def get_next_player(self):
"""
Return curr_turn player but if it has already won/completed game , return
next player which is still active
"""
while True:
# if rank is -1 , player is still active so return
if self.players[self.turn].get_rank() == -1:
return self.players[self.turn]
# check next player
self.turn = (self.turn + 1) % len(self.players)
def move_player(self, curr_player, next_pos):
# Move player to next_pos
curr_player.set_position(next_pos)
if self.board.at_last_pos(curr_player.get_pos()):
# if at last position set rank
curr_player.set_rank(self.last_rank + 1)
self.last_rank += 1
def can_move(self, curr_player, to_move_pos):
# check if player can move or not ie. between board bound
if to_move_pos <= self.board.get_size() and curr_player.get_rank() == -1:
return True
return False
def change_turn(self, dice_result):
# change player turn basis dice result.
# if it's six, do not change .
# if it's three consecutive sixes or not a six, change
self.consecutive_six = 0 if dice_result != 6 else self.consecutive_six + 1
if dice_result != 6 or self.consecutive_six == 3:
if self.consecutive_six == 3:
print("Changing turn due to 3 consecutive sixes")
self.turn = (self.turn + 1) % len(self.players)
else:
print(f"One more turn for player {self.turn+1} after rolling 6")
def play(self):
"""
starting point of game
game will be player until all players have not been assigned a rank
# get curr player to play
# roll dice
# get next pos of player
# see if pos is valid to move
# move player to next pos
# change turn
Note: Currently everything is automated, ie. dice roll input is not taken from user.
if required , we can change that to give some control to user.
"""
while self.can_play():
curr_player = self.get_next_player()
player_input = input(
f"Player {self.turn+1}, Press enter to roll the dice")
dice_result = self.dice.roll()
print(f'dice_result: {dice_result}')
_next_pos = self.board.get_next_pos(
curr_player.get_pos() + dice_result)
if self.can_move(curr_player, _next_pos):
self.move_player(curr_player, _next_pos)
self.change_turn(dice_result)
self.print_game_state()
self.print_game_result()
def print_game_state(self):
# Print state of game after every turn
print('-------------game state-------------')
for ix, _p in enumerate(self.players):
print(f'Player: {ix+1} is at pos {_p.get_pos()}')
print('-------------game state-------------\n\n')
def print_game_result(self):
# Print final game result with ranks of each player
print('-------------final result-------------')
for _p in sorted(self.players, key=lambda x: x.get_rank()):
print(f'Player: {_p._id+1} , Rank: {_p.get_rank()}')
def sample_run():
# Create a board of size 100 and a dice with 6 sides
board = Board(100)
# Define snakes and add them to the board
snakes = {
17: 7,
62: 19,
87: 24,
54: 34,
64: 60,
93: 73,
95: 75,
98: 79
}
for start, end in snakes.items():
board.set_moving_entity(start, Snake(end))
# Define ladders and add them to the board
ladders = {
1: 38,
4: 14,
9: 31,
21: 42,
28: 84,
51: 67,
71: 91,
80: 100
}
for start, end in ladders.items():
board.set_moving_entity(start, Ladder(end))
# Initialize the game with the board, dice, and 2 players
game = Game()
game.initialize_game(board, 6, 2)
# Start playing the game
game.play()
sample_run()