-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
168 lines (145 loc) · 5.12 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
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
import p1, p2
import disjoint
# Main.py - main function of this project, all functions to be defined here.
ds = disjoint.DisjointSet()
ds.initialize()
# Board (13 * 13 matrix) initialized with 'U' --13x13 for virtual nodes
board = [['U' for j in range(13)] for i in range(13)]
# Connected cells
rvals = [0, 1, 0, -1, -1, 1]
cvals = [-1, -1, 1, 1, 0, 0]
# Function to fill in values for board
def initialize_board():
global board
for i in range(1, 12):
board[i][0] = 'R'
board[i][12] = 'R'
board[0][1:12] = ['B'] * 11
board[12][0:11] = ['B'] * 11
board[0][0] = board[0][12] = board[12][0] = board[12][12] = 'O' # virtual corners not required
# This function checks whether a cell in board is occupied or empty
is_cell_empty = lambda r, c : board[r+1][c+1] == 'U'
# This function checks whether a new piece can be placed or not.
is_valid_move = lambda r, c : 0 <= r < 11 and 0 <= c < 11 and board[r + 1][c + 1] == 'U'
# This function checks whether a cell is inside board or not
check_in_range = lambda x, y : 0 <= x < 13 and 0 <= y < 13
# This function returns a list of Connected cells with same color stone
def get_connected_components(x, y):
global board, rvals, cvals
ret, color = [], board[x][y]
for k in range(6):
a, b = x + cvals[k], y + rvals[k]
if check_in_range(a, b):
if board[a][b] == color:
ret.append((a, b))
return ret
# This functions first checks for validity of new move and then update Board with new move.
def move(r, c, color):
global board
if is_valid_move(r, c):
r, c = r + 1, c + 1
board[r][c] = color
conn_comps = get_connected_components(r, c)
id0 = ds.calc_id(r, c)
for cell in conn_comps:
x, y = cell
id1 = ds.calc_id(x, y)
ds.merge(id0, id1)
# This function checks if any player is winning or not.
def check_winning():
if ds.find_set(1, 156):
return 'B'
elif ds.find_set(13, 25):
return 'R'
return None
get_player = lambda color : {'R':'P1', 'B':'P2'}[color]
get_other = lambda color : {'R':'P2', 'B':'P1'}[color]
# This function drives the program and plays the game.
def main():
initialize_board()
swapped = False
# P1's first turn.
player, color, lang = p1, 'R', lang1
x, y = player.move([i[1:-1] for i in board[1:-1]], lang, True) # For giving 11X11 matrix
if (x, y) == ('x', 'x'):
print(get_other(color), "wins!")
return
x, y = int(x), int(y)
if not is_valid_move(x, y):
print("Invalid move")
print(get_other(color), "wins!")
return
else:
move(x, y, color)
print(get_player(color), color, (x, y))
# P2's first turn
player, color, lang = p2, 'B', lang2
x, y = player.move([i[1:-1] for i in board[1:-1]], lang, True) # For giving 11X11 matrix
if (x, y) == ('x', 'x'):
print(get_other(color), "wins!")
return
x, y = int(x), int(y)
if not 0 <= x < 11 or not 0 <= y < 11:
print("Invalid move")
print(get_other(color), "wins!")
return
if is_cell_empty(x, y):
move(x, y, color)
print(get_player(color), color, (x, y))
else: # player 2 will return coordinates of red stone to swap i.e. board[x][y] == 'R'
if swapped == False:
board[x+1][y+1] = 'U'
move(x, y, color)
print(get_player(color), color, (x, y), 'Swap!')
swapped = True
else:
print('Already Swapped Once!')
print("Invalid move")
print(get_other(color), "wins!")
return
# Normal gameplay
while not check_winning():
player, color, lang = (p2, 'B', lang2) if (player == p1) else (p1, 'R', lang1)
x, y = player.move([i[1:-1] for i in board[1:-1]], lang, False) # For giving 11X11 matrix
if (x, y) == ('x', 'x'):
print(get_other(color), "wins!")
return
x, y = int(x), int(y)
if player == p2 and board[x+1][y+1]=='R':
print("Already Swapped Once!")
print("Invalid move")
print(get_player(color),color, (x,y))
print(get_other(color), "wins!")
return
if not is_valid_move(x, y):
print("Invalid move")
print(get_other(color), "wins!")
return
else:
move(x, y, color)
print(get_player(color), color, (x, y))
# The game has ended. Declare the winner
print(get_player(check_winning()), ' wins!')
# This is for selecting language.
langs = {
1: "C",
2: "C++",
3: "Java",
4: "Python2",
5: "Python3",
}
def print_menu():
for i in range(1, 6):
print("%d."%(i), langs[i])
print_menu()
lang1 = int(input("Select language for player 1(1-5): "))
lang2 = int(input("Select language for player 2(1-5): "))
if 1 <= lang1 <= 5 and 1 <= lang2 <= 5:
print("You have selected the following: ")
print("P1:", langs[lang1])
print("P2:", langs[lang2])
main()
p1.delete_classfiles(lang1)
p2.delete_classfiles(lang2)
else:
print("Invalid language selection.")