-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
87 lines (68 loc) · 2.48 KB
/
main.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
"""
Mancala move calculator
Input: stones in each pocket of the board
Output: list of moves that will provide player with most amount of stones.
"""
def main():
""" get input from user for stones at each pocket """
boxes = list()
for i in range(12):
user_input = "-1"
# make sure user input is a number > 0
while not user_input.isnumeric():
print("Number of pebbles in", i, "th box: ", end='')
user_input = input()
boxes.append(int(user_input))
boxes.append(0)
# output order of moves to make
moves = rotations(boxes)
print("To get", moves[0], "pebbles, make moves in this order: ", end='')
print(moves[1:])
def rotations(boxes):
""" test every move from index 6-11 """
boxes_copied = boxes.copy()
saved_moves = [0]
for i in range(6, 12):
if boxes_copied[i] > 0:
dict_of_moves = one_rotation(boxes_copied, i)
if dict_of_moves[0] > saved_moves[0]:
saved_moves = dict_of_moves
return saved_moves
def one_rotation(boxes, index):
""" test one rotation of stones from chosen index """
boxes_copy = boxes.copy()
pebbles = boxes_copy[index]
dict_moves = [index]
boxes_copy[index] = 0
while pebbles >= 1:
# move to next pocket of the board
if index == 12:
index = 0
else:
index += 1
new_pebbles = boxes_copy[index]
# adjust pebble amount in next box of the board
if pebbles == 1 and index != 12 and new_pebbles > 0:
# grab stones from this new pocket
pebbles += new_pebbles
boxes_copy[index] = 0
elif pebbles == 1 and index == 12:
# if ending pebble is placed into mancala store
pebbles -= 1
boxes_copy[index] += 1
# start new set of rotations
pairing = rotations(boxes_copy)
boxes_copy[12] = pairing[0]
pairing.pop(0)
dict_moves.extend(pairing)
else:
pebbles -= 1
boxes_copy[index] += 1
# insert the number of gained stones into the first index of the list
dict_moves.insert(0, boxes_copy[12])
boxes_copy.clear()
# returns a list with gained stones in first index.
# The following numbers are respective pockets for players to move
return dict_moves
if __name__ == '__main__':
main()