-
Notifications
You must be signed in to change notification settings - Fork 0
/
MoveValidator.py
71 lines (56 loc) · 3.23 KB
/
MoveValidator.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
from lib2to3.pgen2.driver import Driver
import pygame
from MartianField import MartianField
from MartianPiece import MartianPiece, PAWN, DRONE, QUEEN
class MoveValidator:
def IsValidMove(self, selectedPiece: MartianPiece, field: MartianField, pieces: list[MartianPiece]):
if selectedPiece.pieceType == PAWN:
# One space at a time, in any of the diagonal directions
return max(selectedPiece.row, field.row) - min(selectedPiece.row, field.row) == 1 \
and max(selectedPiece.column, field.column) - min(selectedPiece.column, field.column) == 1
if selectedPiece.pieceType == DRONE:
# One or two spaces, on either the horizontal or vertical lines. Jumping is not allowed.
if selectedPiece.row == field.row and max(selectedPiece.column, field.column) - min(selectedPiece.column, field.column) == 1:
return True
if selectedPiece.column == field.column and max(selectedPiece.row, field.row) - min(selectedPiece.row, field.row) == 1:
return True
if selectedPiece.row == field.row \
and max(selectedPiece.column, field.column) - min(selectedPiece.column, field.column) == 2 \
and self.fieldIsEmpty(min(selectedPiece.column, field.column) + 1, selectedPiece.row, pieces):
return True
if selectedPiece.column == field.column \
and max(selectedPiece.row, field.row) - min(selectedPiece.row, field.row) == 2 \
and self.fieldIsEmpty(selectedPiece.column, min(selectedPiece.row, field.row) + 1, pieces):
return True
return False
if selectedPiece.pieceType == QUEEN:
# Any distance, in any straight-line direction: horizontally, vertically, or diagonally. Jumping is not allowed.
if selectedPiece.column == field.column:
for r in range(min(selectedPiece.row, field.row) + 1, max(selectedPiece.row, field.row)):
if not self.fieldIsEmpty(selectedPiece.column, r, pieces):
return False
return True
if selectedPiece.row == field.row:
for c in range(min(selectedPiece.column, field.column) + 1, max(selectedPiece.column, field.column)):
if not self.fieldIsEmpty(c, selectedPiece.row, pieces):
return False
return True
if abs(selectedPiece.row - field.row) == abs(selectedPiece.column - field.column):
if (abs(selectedPiece.row - field.row) == 1):
return True
slope = 1 \
if selectedPiece.column > field.column and selectedPiece.row > field.row \
or selectedPiece.column < field.column and selectedPiece.row < field.row \
else -1
for i in range(1, abs(selectedPiece.row - field.row)):
if not self.fieldIsEmpty(min(selectedPiece.column, field.column) + i * slope, min(selectedPiece.row, field.row) + i, pieces):
return False
return True
return False
def fieldIsEmpty(self, column: int, row: int, pieces: list[MartianPiece]):
for piece in pieces:
if piece.isCapturedByPlayer != None:
return True
if piece.column == column and piece.row == row:
return False
return True