GeminiAi commited on
Commit
8148851
·
verified ·
1 Parent(s): c5181a6

Create engine.py

Browse files
Files changed (1) hide show
  1. engine.py +173 -0
engine.py ADDED
@@ -0,0 +1,173 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # importing stuff
2
+ import numpy
3
+ from Scripts.Variables import *
4
+ import pygame
5
+
6
+
7
+
8
+ class Board:
9
+
10
+ @staticmethod
11
+ def draw_board(display):
12
+ block = unit_size
13
+ cnt = 0
14
+ for i in range(8):
15
+ for j in range(8):
16
+ axis = (i * block, j * block, block, block)
17
+ if cnt % 2 == 0:
18
+ pygame.draw.rect(display, light_block, axis)
19
+ else:
20
+ pygame.draw.rect(display, dark_block, axis)
21
+ cnt += 1
22
+ cnt -= 1
23
+
24
+ @staticmethod
25
+ def draw_pieces(board,display):
26
+ for rows in range(units):
27
+ for block in range(units):
28
+ if board[rows][block] == 1:
29
+
30
+ pygame.draw.circle(display, light_piece, ((block * unit_size) + (unit_size//2), (rows * unit_size) + (unit_size//2)), (unit_size//2)-5)
31
+
32
+ elif board[rows][block] == 2:
33
+
34
+ pygame.draw.circle(display, dark_piece, ((block * unit_size) + (unit_size//2), (rows * unit_size) + (unit_size//2)), (unit_size//2)-5)
35
+
36
+ elif board[rows][block] == 3:
37
+
38
+ pygame.draw.circle(display, light_king, ((block * unit_size) + (unit_size // 2), (rows * unit_size) + (unit_size // 2)), (unit_size // 2) - 5)
39
+
40
+ elif board[rows][block] == 4:
41
+
42
+ pygame.draw.circle(display, dark_king, ((block * unit_size) + (unit_size // 2), (rows * unit_size) + (unit_size // 2)), (unit_size // 2) - 5)
43
+
44
+ @staticmethod
45
+ def draw_hovered_piece(display, piece, x, y):
46
+ if piece == 1:
47
+ pygame.draw.circle(display, light_piece, (x, y), (unit_size//2)-5)
48
+ elif piece == 2:
49
+ pygame.draw.circle(display, dark_piece, (x, y), (unit_size // 2) - 5)
50
+ elif piece == 3:
51
+ pygame.draw.circle(display, light_king, (x, y), (unit_size // 2) - 5)
52
+ elif piece == 4:
53
+ pygame.draw.circle(display, dark_king, (x, y), (unit_size // 2) - 5)
54
+
55
+
56
+ @staticmethod
57
+ def newBoard():
58
+ return [
59
+ [0, 2, 0, 2, 0, 2, 0, 2],
60
+ [2, 0, 2, 0, 2, 0, 2, 0],
61
+ [0, 0, 0, 0, 0, 0, 0, 0],
62
+ [0, 0, 0, 0, 0, 0, 0, 0],
63
+ [0, 0, 0, 0, 0, 0, 0, 0],
64
+ [0, 0, 0, 0, 0, 0, 0, 0],
65
+ [0, 1, 0, 1, 0, 1, 0, 1],
66
+ [1, 0, 1, 0, 1, 0, 1, 0]
67
+
68
+ ]
69
+
70
+ @staticmethod
71
+ def check_promotion(board):
72
+
73
+
74
+ for i in range(8):
75
+ if board[0][i] == 1:
76
+ board[0][i] = 3
77
+ if board[7][i] == 2:
78
+ board[7][i] = 4
79
+
80
+
81
+
82
+
83
+ class Available:
84
+
85
+ @staticmethod
86
+ def check_down(board, row, column, opp):
87
+ available = []
88
+
89
+ if column != 0:
90
+ if row != 0:
91
+ if board[column-1][row-1] == 0:
92
+ available.append(helper.fromIndex(column-1, row-1))
93
+ if row != 7:
94
+ if board[column-1][row+1] == 0:
95
+ available.append(helper.fromIndex(column - 1, row + 1))
96
+
97
+
98
+ if column != 1:
99
+ if row != 0:
100
+ if row != 1:
101
+ if board[column - 2][row - 2] == 0 and board[column - 1][row - 1] in opp:
102
+ available.append(helper.fromIndex(column - 2, row - 2))
103
+ if row != 7:
104
+ if row != 6:
105
+ if board[column - 2][row + 2] == 0 and board[column -1][row + 1] in opp:
106
+ available.append(helper.fromIndex(column - 2, row + 2))
107
+
108
+
109
+ return available
110
+
111
+ @staticmethod
112
+ def check_up(board, row, column, opp):
113
+ available = []
114
+
115
+ if column != 7:
116
+ if row != 0:
117
+ if board[column + 1][row - 1] == 0:
118
+ available.append(helper.fromIndex(column + 1, row - 1))
119
+ if row != 7:
120
+ if board[column + 1][row + 1] == 0:
121
+ available.append(helper.fromIndex(column + 1, row + 1))
122
+
123
+ if column != 6:
124
+ if row != 0:
125
+ if row != 1:
126
+ if board[column + 2][row - 2] == 0 and board[column +1][row -1] in opp:
127
+ available.append(helper.fromIndex(column + 2, row - 2))
128
+ if row != 7:
129
+ if row != 6:
130
+ if board[column + 2][row + 2] == 0 and board[column + 1][row + 1] in opp:
131
+ available.append(helper.fromIndex(column + 2, row + 2))
132
+
133
+ return available
134
+
135
+ @staticmethod
136
+ def check_king(board, row, column, opp):
137
+
138
+ available = Available.check_down(board, row, column, opp)
139
+ for i in Available.check_up(board, row, column, opp):
140
+ available.append(i)
141
+ return available
142
+
143
+ class helper:
144
+ # returns board indexes starting from 0
145
+ @staticmethod
146
+ def fromAxis(x, y):
147
+ return (x // unit_size), (y // unit_size)
148
+
149
+ # returns serial value from 1 to 64
150
+ @staticmethod
151
+ def fromIndex(x, y):
152
+ return (x) * 8 + y + 1 # added one at the end because we have to start from 1
153
+
154
+ # returns x and y index value
155
+ @staticmethod
156
+ def toIndex(serial):
157
+ if serial % 8 != 0:
158
+ return serial // 8, (serial % 8) - 1
159
+ else:
160
+ return (serial // 8) - 1, 7
161
+
162
+ # returns the true x and y values
163
+ @staticmethod
164
+ def toAxis(x, y):
165
+ return x * unit_size, y * unit_size
166
+
167
+ @staticmethod
168
+ def sub_list(list,list2):
169
+ return [list[0] - list2[0] , list[1] - list2[1]]
170
+
171
+ @staticmethod
172
+ def dev_list_by2(list):
173
+ return [list[0] // 2, list[1] // 2]