-
Notifications
You must be signed in to change notification settings - Fork 0
/
juegoDamas.py
298 lines (286 loc) · 14.2 KB
/
juegoDamas.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
ef printBoard(board):
x = len(board)
y = len(board[0])
print(" "*6, end = "")
for i in range(0, x):
print(i+1, end = " ")
print()
print(" "+"-"*26)
for i in range(0, x):
print(" "+str(i+1)+" |", end = "")
for j in range(0, y):
if board[i][j] == "R":
print(" \U0001F534 ", end = "")
elif board[i][j] == "RD":
print(" \U0001F536 ", end = "")
elif board[i][j] == "B":
print(" \U0001F535 ", end = "")
elif board[i][j] == "BD":
print(" \U0001F537 ", end = "")
else:
print(" ", end="")
print("|", end="")
print()
print(" " + "-" * 26)
def canEatAgainBlue(row, column, board):
itCan = False
# Check if is possible give 2 steps forward
if row > 2:
# Check left
if column > 2:
if (board[row - 2][column - 2] == "R" or board[row - 2][column - 2] == "RD") and (
board[row - 3][column - 3] == ""):
itCan = True
# Check right
if column < 4:
if (board[row - 2][column] == "R" or board[row - 2][column] == "RD") and (
board[row - 3][column + 1] == ""):
itCan = True
return itCan
def canEatAgainBlueD(row, column, board):
itCan = False
# Check if is possible give 2 steps forward
if row > 2:
# Check left
if column > 2:
if (board[row - 2][column - 2] == "R" or board[row - 2][column - 2] == "RD") and (
board[row - 3][column - 3] == ""):
itCan = True
# Check right
if column < 4:
if (board[row - 2][column] == "R" or board[row - 2][column] == "RD") and (
board[row - 3][column + 1] == ""):
itCan = True
if row < 4:
# Check left
if column > 2:
if (board[row][column - 2] == "R" or board[row][column - 2] == "RD") and (
board[row + 1][column - 3] == ""):
itCan = True
# Check right
if column < 4:
if (board[row][column] == "R" or board[row][column] == "RD") and (
board[row + 1][column + 1] == ""):
itCan = True
return itCan
def canEatAgainRed(row, column, board):
itCan = False
# Check if is possible give 2 steps forward
if row < 4:
# Check left
if column > 2:
if (board[row][column - 2] == "B" or board[row][column - 2] == "BD") and (
board[row +1][column - 3] == ""):
itCan = True
# Check right
if column < 4:
if (board[row][column] == "B" or board[row][column] == "BD") and (
board[row + 1][column + 1] == ""):
itCan = True
return itCan
def canEatAgainRedD(row, column, board):
itCan = False
# Check if is possible give 2 steps forward
if row < 4:
# Check left
if column > 2:
if (board[row][column - 2] == "B" or board[row][column - 2] == "BD") and (
board[row +1][column - 3] == ""):
itCan = True
# Check right
if column < 4:
if (board[row][column] == "B" or board[row][column] == "BD") and (
board[row + 1][column + 1] == ""):
itCan = True
if row > 2:
# Check left
if column > 2:
if (board[row - 2][column - 2] == "B" or board[row - 2][column - 2] == "BD") and (
board[row - 3][column - 3] == ""):
itCan = True
# Check right
if column < 4:
if (board[row - 2][column] == "B" or board[row - 2][column] == "BD") and (
board[row - 3][column + 1] == ""):
itCan = True
return itCan
def redMove(rowOrigin, columnOrigin, rowDestiny, columnDestiny, board, redRowLocked, redColLocked):
# Normal move case
turn = "Red"
if redRowLocked == -1 and redColLocked == -1 or redRowLocked == rowOrigin and redColLocked == columnOrigin:
if rowDestiny == rowOrigin + 1 and ((columnDestiny >= 0 and columnDestiny == columnOrigin - 1) or (
columnDestiny <= len(board) and columnDestiny == columnOrigin + 1)) and board[rowDestiny - 1][
columnDestiny - 1] == "":
board[rowOrigin - 1][columnOrigin - 1] = ""
board[rowDestiny - 1][columnDestiny - 1] = "RD" if rowDestiny == len(board) else "R"
turn = "Blue"
print("Valid movement")
# Eat move case
elif rowDestiny == rowOrigin + 2 and ((columnDestiny >= 0 and columnDestiny == columnOrigin - 2) or (
columnDestiny <= len(board) and columnDestiny == columnOrigin + 2)) and board[rowDestiny - 1][
columnDestiny - 1] == "" and (
board[rowOrigin][columnOrigin - 1 + (-1 if columnDestiny < columnOrigin else 1)] == "B" or
board[rowOrigin][columnOrigin - 1 + (-1 if columnDestiny < columnOrigin else 1)] == "BD"):
board[rowOrigin - 1][columnOrigin - 1] = ""
board[rowOrigin][columnOrigin - 1 + (-1 if columnDestiny < columnOrigin else 1)] = ""
board[rowDestiny - 1][columnDestiny - 1] = "RD" if rowDestiny == len(board) else "R"
print("Valid movement")
turn = "Red" if canEatAgainRed(rowDestiny, columnDestiny, board) else "Blue"
else:
print("Invalid movement")
if turn == "Red":
return turn, rowDestiny, columnDestiny
else:
return turn, -1, -1
else:
print("Invalid movement, move the one that ate")
return turn, redRowLocked, redColLocked
def redDMove(rowOrigin, columnOrigin, rowDestiny, columnDestiny, board, redRowLocked, redColLocked):
turn = "Red"
if redRowLocked == -1 and redColLocked == -1 or redRowLocked == rowOrigin and redColLocked == columnOrigin:
if (1 <= rowDestiny <= len(board) and rowDestiny != rowOrigin and 1 <= columnDestiny <= len(
board) and columnDestiny != columnOrigin
and abs(rowDestiny - rowOrigin) == 1 and abs(columnDestiny - columnOrigin) == 1 and
board[rowDestiny - 1][columnDestiny - 1] == ""):
board[rowOrigin - 1][columnOrigin - 1] = ""
board[rowDestiny - 1][columnDestiny - 1] = "RD"
print("Valid movement")
turn = "Blue"
elif (1 <= rowDestiny <= len(board) and rowDestiny != rowOrigin and 1 <= columnDestiny <= len(board) and columnDestiny != columnOrigin
and abs(rowDestiny-rowOrigin) == 2 and abs(columnDestiny-columnOrigin) == 2 and board[rowDestiny-1][columnDestiny-1] == ""
and (board[rowOrigin if rowDestiny > rowOrigin else rowOrigin-2][columnOrigin if columnDestiny > columnOrigin else columnOrigin-2] == "B"
or board[rowOrigin if rowDestiny > rowOrigin else rowOrigin-2][columnOrigin if columnDestiny > columnOrigin else columnOrigin-2] == "BD")):
board[rowOrigin-1][columnOrigin-1] = ""
board[rowOrigin if rowDestiny > rowOrigin else rowOrigin - 2][
columnOrigin if columnDestiny > columnOrigin else columnOrigin - 2] = ""
board[rowDestiny-1][columnDestiny-1] = "RD"
print("Valid movement")
turn = "Red" if canEatAgainRedD(rowDestiny, columnDestiny, board) else "Blue"
if turn == "Red":
return turn, rowDestiny, columnDestiny
else:
return turn, -1, -1
else:
print("Invalid movement, move the one that ate")
return turn, redRowLocked, redColLocked
def blueMove(rowOrigin, columnOrigin, rowDestiny, columnDestiny, board, blueRowLocked, blueColLocked):
# Normal move case
turn = "Blue"
if blueRowLocked == -1 and blueColLocked == -1 or blueRowLocked == rowOrigin and blueColLocked == columnOrigin:
if rowDestiny == rowOrigin -1 and ((columnDestiny >= 0 and columnDestiny == columnOrigin - 1) or (
columnDestiny <= len(board) and columnDestiny == columnOrigin + 1)) and board[rowDestiny - 1][
columnDestiny - 1] == "":
board[rowOrigin - 1][columnOrigin - 1] = ""
board[rowDestiny - 1][columnDestiny - 1] = "BD" if rowDestiny == 1 else "B"
print("Valid movement")
turn = "Red"
# Eat move case
elif rowDestiny == rowOrigin - 2 and ((columnDestiny >= 0 and columnDestiny == columnOrigin - 2) or (
columnDestiny <= len(board) and columnDestiny == columnOrigin + 2)) and board[rowDestiny - 1][
columnDestiny - 1] == "" and (
board[rowDestiny][columnOrigin - 1 + (-1 if columnDestiny < columnOrigin else 1)] == "R" or
board[rowDestiny][columnOrigin - 1 + (-1 if columnDestiny < columnOrigin else 1)] == "RD"):
board[rowOrigin - 1][columnOrigin - 1] = ""
board[rowDestiny][columnOrigin - 1 + (-1 if columnDestiny < columnOrigin else 1)] = ""
board[rowDestiny - 1][columnDestiny - 1] = "BD" if rowDestiny == 1 else "B"
print("Valid movement")
turn = "Blue" if canEatAgainBlue(rowDestiny, columnDestiny, board) else "Red"
else:
print("Invalid movement")
if turn == "Blue":
return turn, rowDestiny, columnDestiny
else:
return turn, -1, -1
else:
print("Invalid movement, move the one that ate")
return turn, blueRowLocked, blueColLocked
def blueDMove(rowOrigin, columnOrigin, rowDestiny, columnDestiny, board, blueRowLocked, blueColLocked):
turn = "Blue"
if blueRowLocked == -1 and blueColLocked == -1 or blueRowLocked == rowOrigin and blueColLocked == columnOrigin:
if(1 <= rowDestiny <= len(board) and rowDestiny != rowOrigin and 1 <= columnDestiny <= len(board) and columnDestiny != columnOrigin
and abs(rowDestiny-rowOrigin) == 1 and abs(columnDestiny-columnOrigin) == 1 and board[rowDestiny-1][columnDestiny-1] == ""):
board[rowOrigin-1][columnOrigin-1] = ""
board[rowDestiny-1][columnDestiny-1] = "BD"
print("Valid movement")
turn = "Red"
elif (1 <= rowDestiny <= len(board) and rowDestiny != rowOrigin and 1 <= columnDestiny <= len(board) and columnDestiny != columnOrigin
and abs(rowDestiny-rowOrigin) == 2 and abs(columnDestiny-columnOrigin) == 2 and board[rowDestiny-1][columnDestiny-1] == ""
and (board[rowOrigin if rowDestiny > rowOrigin else rowOrigin-2][columnOrigin if columnDestiny > columnOrigin else columnOrigin-2] == "R"
or board[rowOrigin if rowDestiny > rowOrigin else rowOrigin-2][columnOrigin if columnDestiny > columnOrigin else columnOrigin-2] == "RD")):
board[rowOrigin-1][columnOrigin-1] = ""
board[rowOrigin if rowDestiny > rowOrigin else rowOrigin - 2][
columnOrigin if columnDestiny > columnOrigin else columnOrigin - 2] = ""
board[rowDestiny-1][columnDestiny-1] = "BD"
print("Valid movement")
turn = "Blue" if canEatAgainBlueD(rowDestiny, columnDestiny, board) else "Red"
if turn == "Blue":
return turn, rowDestiny, columnDestiny
else:
return turn, -1, -1
else:
print("Invalid movement, move the one that ate")
return turn, blueRowLocked, blueColLocked
def move(rowOrigin, columnOrigin, rowDestiny, columnDestiny, board, turn, blueRowLocked, blueColLocked, redRowLocked, redColLocked):
if board[rowOrigin-1][columnOrigin-1] == "R" and turn =="Red":
#print("R")
return redMove(rowOrigin, columnOrigin, rowDestiny, columnDestiny, board, redRowLocked, redColLocked)
elif board[rowOrigin-1][columnOrigin-1] == "RD" and turn =="Red":
#print("RD")
return redDMove(rowOrigin, columnOrigin, rowDestiny, columnDestiny, board, redRowLocked, redColLocked)
elif board[rowOrigin-1][columnOrigin-1] == "B" and turn =="Blue":
#print("B")
return blueMove(rowOrigin, columnOrigin, rowDestiny, columnDestiny, board, blueRowLocked, blueColLocked)
elif board[rowOrigin-1][columnOrigin-1] == "BD" and turn =="Blue":
#print("BD")
return blueDMove(rowOrigin, columnOrigin, rowDestiny, columnDestiny, board, blueRowLocked, blueColLocked)
else:
print("Error: Invalid movement")
print(turn)
return turn, -1, -1
def winner(board):
countBlue = 0
countRed = 0
for i in range(0, len(board[0])):
for j in range(0, len(board[0])):
if(board[i][j] == "B" or board[i][j] == "BD"):
countBlue += 1
if (board[i][j] == "R" or board[i][j] == "RD"):
countRed += 1
if countBlue == 0:
printBoard(board)
print("Red player wins")
return True
if countRed == 0:
printBoard(board)
print("Blue player wins")
return True
return False
def main(board, turn):
currentTurn = turn
blueRowLocked = -1
blueColLocked = -1
redRowLocked = -1
redColLocked = -1
while not winner(board):
printBoard(board)
if(currentTurn == "Red"):
print("Red player turn")
rowOrigin = int(input("Enter origin row: "))
columnOrigin = int(input("Enter origin column: "))
rowDestiny = int(input("Enter destiny row: "))
columnDestiny = int(input("Enter destiny column: "))
currentTurn, redRowLocked, redColLocked = move(rowOrigin, columnOrigin, rowDestiny, columnDestiny, board, "Red", blueRowLocked, blueColLocked, redRowLocked, redColLocked)
else:
print("Blue player turn")
rowOrigin = int(input("Enter origin row: "))
columnOrigin = int(input("Enter origin column: "))
rowDestiny = int(input("Enter destiny row: "))
columnDestiny = int(input("Enter destiny column: "))
currentTurn, blueRowLocked,blueColLocked = move(rowOrigin, columnOrigin, rowDestiny, columnDestiny, board, "Blue", blueRowLocked, blueColLocked, redRowLocked, redColLocked)
board = [["R", "", "R", "","R"],
["", "R", "", "R",""],
["", "", "", "",""],
["", "B", "", "B",""],
["B", "", "B", "","B"]]
turn = "Red"
main(board, turn)