-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchess.py
96 lines (84 loc) · 2.56 KB
/
chess.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
def pe_allowed(cell_from,cell_to,white):
x1, y1 = cell_from
x2, y2 = cell_to
if (x1, y1) == (x2, y2):
return False
if white:
return y1+1 == y2 and abs(x1-x2) == 1
else:
return y1-1 == y2 and abs(x1-x2) == 1
def out_of_range(cell_from, cell_to):
x1, y1 = cell_from
x2, y2 = cell_to
return not (
1 <= y1 <= 8 and
1 <= x1 <= 8 and
1 <= y2 <= 8 and
1 <= x2 <= 8
)
def p_allowed(cell_from,cell_to,white):
x1, y1 = cell_from
x2, y2 = cell_to
if (x1, y1) == (x2, y2):
return False
if not(
1 <= y1 <= 8 and
1 <= x1 <= 8 and
1 <= y2 <= 8 and
1 <= x2 <= 8
):
return False
if x1 != x2:
return False
if white:
if y1 == 2 and y2 == 4:
return True
return y1+1 == y2
else:
if y1 == 7 and y2 == 5:
return True
return y1-1 == y2
def k_allowed(cell_from,cell_to,white=False):
x1, y1 = cell_from
x2, y2 = cell_to
if (x1, y1) == (x2, y2):
return False
if (1 <= x1 <= 8) and (1 <= x2 <= 8) and (1 <= y1 <= 8) and (1 <= y2 <= 8) and \
((abs(x1-x2) == 1 and abs(y1-y2) == 1) or (x1 == x2 and abs(y1-y2) == 1)
or (y1 == y2 and abs(x1-x2) == 1)):
return True
else:
return False
def r_allowed(cell_from,cell_to,white=False):
x1, y1 = cell_from
x2, y2 = cell_to
if (x1, y1) == (x2, y2):
return False
if (1 <= x1 <= 8) and (1 <= x2 <= 8) and (1 <= y1 <= 8) and (1 <= y2 <= 8) and \
((y1 == y2 and x1 != x2 and x1 in range(1, 9) and x2 in range(1, 9)) or\
(x1 == x2 and y1 != y2 and y1 in range(1, 9) and y2 in range(1, 9))):
return True
else:
return False
def b_allowed(cell_from,cell_to,white=False):
x1, y1 = cell_from
x2, y2 = cell_to
if (x1, y1) == (x2, y2):
return False
if (1 <= x1 <= 8) and (1 <= x2 <= 8) and (1 <= y1 <= 8) and (1 <= y2 <= 8) and \
(abs(x1 - x2) == abs(y1 - y2)):
return True
else:
return False
def n_allowed(cell_from,cell_to,white=False):
x1, y1 = cell_from
x2, y2 = cell_to
if (x1, y1) == (x2, y2):
return False
if (1 <= x1 <= 8) and (1 <= x2 <= 8) and (1 <= y1 <= 8) and (1 <= y2 <= 8) and \
((abs(x1 - x2) == 2 and abs(y1 - y2) == 1) or (abs(x1 - x2) == 1 and abs(y1 - y2) == 2)):
return True
else:
return False
def q_allowed(cell_from,cell_to,white=False):
return b_allowed(cell_from, cell_to) or r_allowed(cell_from, cell_to)