-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.py
187 lines (167 loc) · 5.06 KB
/
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
board_Count = [[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]]
board_Player = [['0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0']]
Display_Box = [['0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0']]
# mapping 1d elements to 2d elements
def mapper(num):
if num == 0:
return 0, 0
elif num == 1:
return 0, 1
elif num == 2:
return 0, 2
elif num == 3:
return 0, 3
elif num == 4:
return 0, 4
elif num == 5:
return 1, 0
elif num == 6:
return 1, 1
elif num == 7:
return 1, 2
elif num == 8:
return 1, 3
elif num == 9:
return 1, 4
elif num == 10:
return 2, 0
elif num == 11:
return 2, 1
elif num == 12:
return 2, 2
elif num == 13:
return 2, 3
elif num == 14:
return 2, 4
elif num == 15:
return 3, 0
elif num == 16:
return 3, 1
elif num == 17:
return 3, 2
elif num == 18:
return 3, 3
elif num == 19:
return 3, 4
elif num == 20:
return 4, 0
elif num == 21:
return 4, 1
elif num == 22:
return 4, 2
elif num == 23:
return 4, 3
else:
return 4, 4
def red_play_first(row, col):
board_Player[row][col] = 'R'
board_Count[row][col] += 1
return 'green'
def red_play(row, col):
if board_Player[row][col] == 'R' or board_Player[row][col] == '0':
board_Player[row][col] = 'R'
board_Count[row][col] += 1
split_Check(row, col)
check_val = win_check()
if check_val == 'red-win':
return 'Red'
return 'green'
else:
return 'red'
def green_play(row, col):
if board_Player[row][col] == 'G' or board_Player[row][col] == '0':
board_Player[row][col] = 'G'
board_Count[row][col] += 1
split_Check(row, col)
check_val = win_check()
if check_val == 'green-win':
return 'Green'
return 'red'
else:
return 'green'
# function to check the four edges
def edge_check():
if board_Count[0][0] == 2:
Split(0, 0, board_Player[0][0])
elif board_Count[0][4] == 2:
Split(0, 4, board_Player[0][4])
elif board_Count[4][0] == 2:
Split(4, 0, board_Player[4][0])
elif board_Count[4][4] == 2:
Split(4, 4, board_Player[4][4])
# function to check the splitting possibility
def split_Check(row, col):
edge_check()
if (row == 0 and col != 0) or (row == 4 and col != 0) or (row != 0 and col == 0) or (row != 0 and col == 4):
if board_Count[row][col] == 3:
Split(row, col, board_Player[row][col])
elif row != 0 and col != 0 and row != 4 and col != 4:
if board_Count[row][col] == 4:
Split(row, col, board_Player[row][col])
# function to split the balls (chaining)
def Split(row, col, player):
board_Player[row][col] = '0'
board_Count[row][col] = 0
if row - 1 >= 0:
board_Player[row - 1][col] = player
board_Count[row - 1][col] += 1
split_Check(row - 1, col)
if row + 1 <= 4:
board_Player[row + 1][col] = player
board_Count[row + 1][col] += 1
split_Check(row + 1, col)
if col - 1 >= 0:
board_Player[row][col - 1] = player
board_Count[row][col - 1] += 1
split_Check(row, col - 1)
if col + 1 <= 4:
board_Player[row][col + 1] = player
board_Count[row][col + 1] += 1
split_Check(row, col + 1)
# function to check the winning possibility of player
def win_check():
row = 0
red_player_ctr = 0
green_player_ctr = 0
for i in range(5):
col = 0
for j in range(5):
# Condition to the check the red winning
if (board_Player[row][col] == 'R') or (board_Player[row][col] == '0'):
red_player_ctr += 1
if red_player_ctr == 25:
print("red win")
return 'red-win'
# Condition to check the Green winning
if (board_Player[row][col] == 'G') or (board_Player[row][col] == '0'):
green_player_ctr += 1
if green_player_ctr == 25:
print("green-win")
return 'green-win'
col += 1
row += 1
def Star_Converter():
for i in range(5):
for j in range(5):
if board_Count[i][j] == 1:
Display_Box[i][j] = '*'
elif board_Count[i][j] == 2:
Display_Box[i][j] = '**'
elif board_Count[i][j] == 3:
Display_Box[i][j] = '***'
elif board_Count[i][j] == 4:
Display_Box[i][j] = '****'
else:
Display_Box[i][j] = '0'