Skip to content

Commit 1b99d40

Browse files
committed
First commit.
0 parents  commit 1b99d40

File tree

7 files changed

+464
-0
lines changed

7 files changed

+464
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.pyc

board.py

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import re
2+
from pieces import Rook, Pawn, King, Knight, Bishop, Queen
3+
from point import Point
4+
5+
class Board(object):
6+
def __init__(self, state=None):
7+
self.width = 8
8+
self.height = 8
9+
10+
self.board = []
11+
12+
self.pieces = []
13+
self.white_pieces = []
14+
self.black_pieces = []
15+
16+
self.whites_turn = True
17+
18+
if not state:
19+
self.loadState('''
20+
r n b q k b n r
21+
p p p p p p p p
22+
. . . . . . . .
23+
. . . . . . . .
24+
. . . . . . . .
25+
. . . . . . . .
26+
P P P P P P P P
27+
R N B Q K B N R
28+
''')
29+
else:
30+
self.loadState(state)
31+
32+
def copy(self):
33+
return Board(str(self))
34+
35+
def __getitem__(self, key):
36+
if type(key) == int:
37+
return self.board[key]
38+
elif type(key) == tuple:
39+
return self.board[key[1]][key[0]]
40+
elif type(key) == Point:
41+
return self.board[key.y][key.x]
42+
else:
43+
raise ValueError('Invalid board coordinate')
44+
45+
46+
def __setitem__(self, key, value):
47+
if type(key) == int:
48+
self.board[key] = value
49+
elif type(key) == tuple:
50+
self.board[key[1]][key[0]] = value
51+
elif type(key) == Point:
52+
self.board[key.y][key.x] = value
53+
else:
54+
raise ValueError('Invalid board coordinate')
55+
56+
57+
def loadState(self, state):
58+
state = re.split('\n+', re.sub(r'[^prnbqkPRNBQK\.\n]', '', state.strip()))
59+
self.state = state
60+
61+
board = []
62+
63+
for y, row in enumerate(state):
64+
new_row = []
65+
66+
for x, square in enumerate(row):
67+
if square == '.':
68+
new_row += [None]
69+
else:
70+
for piece in [Rook, Pawn, King, Knight, Bishop, Queen]:
71+
if piece.character == square.lower():
72+
obj = piece(self, not square.islower(), Point(x, y))
73+
74+
new_row += [obj]
75+
self.pieces += [obj]
76+
77+
if not square.islower():
78+
self.white_pieces += [obj]
79+
else:
80+
self.black_pieces += [obj]
81+
82+
break
83+
84+
board += [new_row]
85+
86+
self.board = board
87+
88+
89+
def __str__(self):
90+
result = []
91+
92+
for row in self.board:
93+
temp_row = []
94+
95+
for cell in row:
96+
if cell == None:
97+
temp_row += ['.']
98+
elif type(cell) == str:
99+
temp_row += [cell]
100+
else:
101+
temp_row += [cell.letter()]
102+
103+
result += [' '.join(temp_row)]
104+
105+
return '\n'.join(result)

direction.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Direction(object):
2+
def __init__(self, x=None, y=None):
3+
self.x = x
4+
self.y = y
5+
6+
def copy(self):
7+
return Direction(self.x, self.y)
8+
9+
def flip_y(self):
10+
return Direction(self.x, -self.y)
11+
12+
def flip_x(self):
13+
return Direction(-self.x, self.y)
14+
15+
def __add__(self, other):
16+
return Direction(self.x + other.x, self.y + other.y)
17+
18+
def __sub__(self, other):
19+
return Direction(self.x - other.x, self.y - other.y)
20+
21+
def __eq__(self, other):
22+
return self.x == other.x and self.y == other.y
23+
24+
def __ne__(self, other):
25+
return self.x != other.x or self.y != other.y
26+
27+
def __str__(self):
28+
return 'Direction <{x}, {y}>'.format(
29+
x=self.x,
30+
y=self.y
31+
)

main.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import re, sys, random
2+
3+
from board import Board
4+
5+
from colorama import init, Style, Fore
6+
init()
7+
8+
9+
if __name__ == '__main__':
10+
board = Board('''
11+
r n b q k b n r
12+
p p p p p p p p
13+
. . . . . . . .
14+
. . . . . . . .
15+
. . . . . . . .
16+
. . . . . . . .
17+
P P P P P P P P
18+
R N B Q K B N R
19+
''')
20+
21+
print random.choice(board.pieces).draw_moves()

piece.py

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
from point import Point
2+
3+
from colorama import init, Style, Fore
4+
init()
5+
6+
class Piece(object):
7+
def __init__(self, board=None, color=True, position=Point()):
8+
self.color = color
9+
self.color_int = -1 if self.color else 1
10+
11+
self.has_moved = False
12+
self.is_pinned = False
13+
self.board = board
14+
15+
self.position = position
16+
17+
def moves(self):
18+
return self.get_moves(self.move_directions, self.length)
19+
20+
def attacks(self):
21+
return self.get_attacks(self.move_directions, self.length)
22+
23+
def __str__(self):
24+
return '{color} {piece} at ({x}, {y})'.format(
25+
color='white' if self.color else 'black',
26+
piece=self.name,
27+
x=self.position.x,
28+
y=self.position.y
29+
)
30+
31+
def get_moves(self, move_directions, length):
32+
moves = []
33+
34+
for move in move_directions:
35+
position = self.position.copy()
36+
total_length = 0
37+
38+
while total_length < length:
39+
position += move
40+
total_length += 1
41+
42+
if position.in_bounds():
43+
square = self.board[position]
44+
45+
if square:
46+
break
47+
else:
48+
moves.append(position)
49+
else:
50+
break
51+
52+
return moves
53+
54+
def get_attacks(self, move_directions, length):
55+
attacks = []
56+
57+
for move in move_directions:
58+
position = self.position.copy()
59+
total_length = 0
60+
61+
while total_length < length:
62+
position += move
63+
total_length += 1
64+
65+
if position.in_bounds():
66+
square = self.board[position]
67+
68+
if square:
69+
if square.color != self.color:
70+
attacks.append(position)
71+
72+
break
73+
else:
74+
break
75+
76+
return attacks
77+
78+
def draw_moves(self):
79+
temp_board = self.board.copy()
80+
81+
temp_board[self.position] = Fore.GREEN + self.letter() + Style.RESET_ALL
82+
83+
for move in self.moves():
84+
temp_board[move] = Fore.YELLOW + '*' + Style.RESET_ALL
85+
86+
for attack in self.attacks():
87+
temp_board[attack] = Fore.RED + temp_board[attack].letter() + Style.RESET_ALL
88+
89+
return str(temp_board)
90+
91+
def letter(self):
92+
character = self.character.upper() if self.color else self.character
93+
94+
if self.color:
95+
character = Style.BRIGHT + character + Style.RESET_ALL
96+
97+
return character
98+
99+
def can_move(self, x, y):
100+
if self.x == x and self.y == y:
101+
return False
102+
103+
if not self.is_valid((x, y)):
104+
return False
105+
106+
target = self.board[x, y]
107+
108+
if target and target.color == self.color:
109+
return False
110+
111+
if (x, y) in self.attacks() and self.board[x, y]:
112+
return True
113+
elif (x, y) in self.moves() and not self.board[x, y]:
114+
return True
115+
else:
116+
return False
117+
118+
def move(self, x, y):
119+
if self.can_move(x, y):
120+
self.board[x, y] = self
121+
self.board[self.x, self.y] = None
122+
123+
self.x = x
124+
self.y = y
125+
126+
return True
127+
else:
128+
return False
129+
130+
def __eq__(self, other):
131+
return self.letter().lower() == other.letter().lower() if other else False
132+
133+
def __ne__(self, other):
134+
return self.letter().lower() != other.letter().lower() if other else False

0 commit comments

Comments
 (0)