-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.py
More file actions
148 lines (113 loc) · 3.83 KB
/
project.py
File metadata and controls
148 lines (113 loc) · 3.83 KB
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
#!/usr/bin/python
from __future__ import unicode_literals
import re
#from collections import namedtuple
class Piece(object):
def __init__(self, color):
self._algebraic = None
self.color = color
def __repr__(self):
return self.__class__.__name__.lower()
def algebraic(self):
return self._algebraic
class Pawn(Piece):
def __init__(self, color):
self._algebraic = '(P)'
self.color = color
class Rook(Piece):
def __init__(self, color):
self._algebraic = 'R'
self.color = color
class Knight(Piece):
def __init__(self, color):
self._algebraic = 'N'
self.color = color
class Bishop(Piece):
def __init__(self, color):
self._algebraic = 'B'
self.color = color
class Queen(Rook, Bishop):
def __init__(self, color):
self._algebraic = 'Q'
self.color = color
class King(Piece):
def __init__(self, color):
self._algebraic = 'K'
self.color = color
class Square(object):
def __init__(self, x, y, current_piece=None):
self.x = x
self.y = y
self.current_piece = current_piece
def __repr__(self):
return self.get_algebraic()
def get_algebraic(self):
if self.current_piece:
return '%s%s%s' % (self.current_piece.algebraic(),
self.x, self.y)
else:
return '%s%s' % (self.x, self.y)
class Board(object):
def __init__(self):
x_axis = [chr(i) for i in xrange(97, 105)]
y_axis = [i for i in xrange(1, 9)]
self.squares = [Square(x, y)
for x in x_axis
for y in y_axis]
self._white_back_row_order = [Rook,
Knight,
Bishop,
Queen,
King,
Bishop,
Knight,
Rook,
]
self.reset_game()
def reset_game(self):
self._reset_pawns()
self._reset_white_back_row()
self._reset_black_back_row()
def _reset_pawns(self):
for row, color in [(2, 'white'), (7, 'black')]:
for square in self.get_row(row):
square.current_piece = Pawn(color)
def _reset_white_back_row(self):
back_row_pieces = self._white_back_row_order
for column, square in enumerate(self.get_row(1)):
square.current_piece = back_row_pieces[column]('white')
def _reset_black_back_row(self):
back_row_pieces = self._white_back_row_order
# Fix black's ordering, switch queen and king
back_row_pieces[3:5] = [King, Queen]
for column, square in enumerate(self.get_row(1)):
square.current_piece = back_row_pieces[column]('black')
def get_row(self, row_num):
"""
Returns a list of squares from a particular row on the x-axis.
"""
return [square
for square in self.squares
if square.y == row_num]
def get_all_rows(self, reverse=True):
if reverse:
return [self.get_row(i)
for i in xrange(8, 0, -1)]
else:
return [self.get_row(i)
for i in xrange(1, 9)]
class Game(object):
def __init__(self):
self.board = Board()
def print_layout(self):
for row in self.board.get_all_rows():
print ' | '.join([square.get_algebraic()
for square in row])
def parse_move(self, move_input):
pass
if __name__ == '__main__':
game = Game()
game.print_layout()
while True:
move = raw_input("Your move> ")
game.parse_move(move)