-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMancala.py
267 lines (216 loc) · 8.52 KB
/
Mancala.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
261
262
263
264
265
266
267
class Player:
"""Class that represents a player in a mancala game"""
def __init__(self, name):
"""
Constructor for player class, initializes required data members.
All data members are private.
Args:
name (str): name of a player
"""
self._name = name
def get_name(self):
"""
Method that returns the name of a player. Takes no parameters.
Returns:
self._name (str): name of the player
"""
return self._name
class Mancala:
"""Class that represents a mancala game"""
def __init__(self):
"""
Constructor for mancala class, initializes required data members.
All data members are private. Takes no arguments.
"""
self._player1 = None
self._player2 = None
self._board = {
1 : 4, 2 : 4, 3 : 4, 4 : 4, 5 : 4, 6 : 4, 7 : 0,
8 : 4, 9 : 4, 10 : 4, 11 : 4, 12 : 4, 13 : 4, 14 : 0
}
def create_player(self, player_name):
"""
Method that creates a player and prints a warning if
more than two players want to play a game.
Args:
player_name (str): name of the player
Returns:
print('Only two players can play this game at the same time') (str):
message if more than two players want to play the game.
Player(player_name) (obj): registers the player name
in the Player class.
"""
if self._player1 is None:
self._player1 = player_name
elif self._player1 is not None and self._player2 is None:
self._player2 = player_name
elif self._player1 is not None and self._player2 is not None:
return print('Only two players can play this game at the same time')
return Player(player_name)
def check_for_opposite_pit(self, player, pit):
"""
Method that checks the pit opposite to the one
the player ends in and transfers all the seeds
in the opposite pit to the player's pit.
Args:
player (int): player that moved
pit (int): pit the player ends in
"""
opposite_dict = {
1 : 13, 2 : 12, 3 : 11, 4 : 10, 5 : 9, 6 : 8,
13 : 1, 12 : 2, 11 : 3, 10 : 4, 9 : 5, 8 : 6
}
opposite_pit = opposite_dict[pit]
self._board[pit] += self._board[opposite_pit]
self._board[opposite_pit] = 0
if player == 1:
self._board[7] += self._board[pit]
self._board[pit] = 0
if player == 2:
self._board[14] += self._board[pit]
self._board[pit] = 0
def play_game(self, player, pit):
"""
Method that represents a player turn. Allows the
player to choose a pit.
Args:
player (int): number assignated to a player,
either 1 or 2
pit (int): pit number the player chooses to
play
Returns:
list(self._board.values()) (list): shows the
state of the board after the player's move.
"""
if pit > 6 or pit <= 0:
return 'Invalid number for pit index'
if self.game_ended() == True:
return 'Game is ended'
if player == 2:
pit += 7
if self._board[pit] == 0:
return 'That pit has no seeds, please pick another one'
seed_num = self._board[pit]
self._board[pit] = 0
self.move_seed(player, pit, seed_num)
if self.game_ended() == True:
self.end_clear_board()
return list(self._board.values())
def move_seed(self, player, start_pit, seeds):
"""
Method that represents the seed movement on the board.
Args:
player (int): number assignated to a player,
either 1 or 2
start_pit (int): pit player chose initially.
seeds (int): number of seeds to move.
"""
next_pit_dict = {
1 : 2, 2 : 3, 3 : 4, 4 : 5, 5 : 6, 6 : 7, 7 : 8,
8 : 9, 9 : 10, 10 : 11, 11 : 12, 12 : 13, 13 : 14, 14 : 1
}
new_pit = next_pit_dict[start_pit]
if player == 1 and new_pit == 14:
new_pit = next_pit_dict[14]
if player == 2 and new_pit == 7:
new_pit = next_pit_dict[7]
if seeds > 0:
self._board[new_pit] += 1
self.move_seed(player, new_pit, seeds - 1)
if seeds == 0 and self._board[start_pit] == 1:
if start_pit != 7 and start_pit != 14:
if player == 1 and 1 <= start_pit <= 6:
self.check_for_opposite_pit(player, start_pit)
if player == 2 and 8 <= start_pit <= 13:
self.check_for_opposite_pit(player, start_pit)
if seeds == 0:
if player == 1 and start_pit == 7:
return 'player 1 take another turn'
if player == 2 and start_pit == 14:
return 'player 2 take another turn'
def p1_pits(self):
"""
Creates a list with the seeds in each pit for player 1.
Returns:
p1_pits (list): list showing the amount of seeds in
each pit for player 1. The list shows pits
in ascending order.
"""
p1_pits = [self._board[1], self._board[2], self._board[3], self._board[4], self._board[5], self._board[6]]
return p1_pits
def p2_pits(self):
"""
Creates a list with the seeds in each pit for player 2.
Returns:
p2_pits (list): list showing the amount of seeds in
each pit for player 2. The list shows pits
in ascending order.
"""
p2_pits = [self._board[8], self._board[9], self._board[10], self._board[11], self._board[12], self._board[13]]
return p2_pits
def print_board(self):
"""
Method that prints the state of the board in the
following format:
player1:
store: number of seeds in player 1's store
player 1 seeds number from pit 1 to 6 in a list
player2:
store: number of seeds in player 2's store
player 2 seeds number from pit 1 to 6 in a list
Takes no parameters.
"""
p1_store_seeds = self._board[7]
p2_store_seeds = self._board[14]
print(
f'player1:\nstore: {p1_store_seeds}\n{self.p1_pits()}\nplayer2:\nstore: {p2_store_seeds}\n{self.p2_pits()}'
)
def game_ended(self):
"""
Method that checks if the game has ended by counting
seed number in each player's side of the board.
Takes no parameters.
Returns:
True (bool): game has ended.
"""
if sum(self.p1_pits()) == 0:
return True
elif sum(self.p2_pits()) == 0:
return True
def end_clear_board(self):
"""
Method that, if the game has ended, finishes the game
by moving all remaining seeds to their side's store.
"""
if sum(self.p1_pits()) != 0 or sum(self.p2_pits()) != 0:
if sum(self.p1_pits()) == 0:
self._board[14] += sum(self.p2_pits())
for numbers in range(8,14):
self._board[numbers] = 0
elif sum(self.p2_pits()) == 0:
self._board[7] += sum(self.p1_pits())
for numbers in range(1,7):
self._board[numbers] = 0
def return_winner(self):
"""
Method that calculates who the winner is by comparing
seed number in both player's stores.
If the game has not ended prints a warning.
Returns:
print(f'Winner is player 1: {self._player1}') (str):
player 1 wins.
print("It's a tie") (str): game ends in a tie.
print(f'Winner is player 2: {self._player2}') (str):
player 2 wins.
print('Game has not ended') (str): end condition not met.
"""
if self.game_ended() == True:
result = self._board[7]/self._board[14]
if result > 1:
return f'Winner is player 1: {self._player1}'
if result == 1:
return "It's a tie"
if result < 1:
return f'Winner is player 2: {self._player2}'
if self.game_ended() != True:
return 'Game has not ended'