Skip to content

Commit 86e78b9

Browse files
committed
Slight refactor and small bugfix
1 parent 3f62408 commit 86e78b9

File tree

5 files changed

+55
-29
lines changed

5 files changed

+55
-29
lines changed

chessington/engine/board.py

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,31 @@
66
from collections import namedtuple
77
from enum import Enum, auto
88

9+
from chessington.engine.data import Player, Square
910
from chessington.engine.pieces import Pawn, Knight, Bishop, Rook, Queen, King
1011

1112
BOARD_SIZE = 8
1213

13-
class Player(Enum):
14+
class Board:
1415
"""
15-
The two players in a game of chess.
16+
A representation of the chess board, and the pieces on it.
1617
"""
17-
WHITE = auto()
18-
BLACK = auto()
1918

20-
21-
class Square(namedtuple('Square', 'row col')):
22-
"""
23-
An immutable pair (row, col) representing the coordinates of a square.
24-
"""
19+
def __init__(self, player, board_state):
20+
self.current_player = Player.WHITE
21+
self.board = board_state
2522

2623
@staticmethod
27-
def at(row, col):
28-
"""
29-
Creates a square at the given row and column.
30-
"""
31-
return Square(row=row, col=col)
24+
def empty():
25+
return Board(Player.WHITE, Board._create_empty_board())
3226

27+
@staticmethod
28+
def at_starting_position():
29+
return Board(Player.WHITE, Board._create_starting_board())
3330

34-
class Board:
35-
"""
36-
A representation of the chess board, and the pieces on it.
37-
"""
38-
39-
def __init__(self):
40-
self.current_player = Player.WHITE
41-
self.board = Board._create_starting_board()
31+
@staticmethod
32+
def _create_empty_board():
33+
return [[None] * BOARD_SIZE for _ in range(BOARD_SIZE)]
4234

4335
@staticmethod
4436
def _create_starting_board():
@@ -87,4 +79,4 @@ def move_piece(self, from_square, to_square):
8779
if moving_piece is not None and moving_piece.player == self.current_player:
8880
self.set_piece(to_square, moving_piece)
8981
self.set_piece(from_square, None)
90-
self.current_player = Player.WHITE if self.current_player == Player.BLACK else Player.WHITE
82+
self.current_player = self.current_player.opponent()

chessington/engine/data.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
Data classes for easy representation of concepts such as a square on the board or a player.
3+
"""
4+
5+
from collections import namedtuple
6+
from enum import Enum, auto
7+
8+
class Player(Enum):
9+
"""
10+
The two players in a game of chess.
11+
"""
12+
WHITE = auto()
13+
BLACK = auto()
14+
15+
def opponent(self):
16+
if self == Player.WHITE: return Player.BLACK
17+
else: return Player.WHITE
18+
19+
20+
class Square(namedtuple('Square', 'row col')):
21+
"""
22+
An immutable pair (row, col) representing the coordinates of a square.
23+
"""
24+
25+
@staticmethod
26+
def at(row, col):
27+
"""
28+
Creates a square at the given row and column.
29+
"""
30+
return Square(row=row, col=col)

chessington/engine/pieces.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
from abc import ABC, abstractmethod
66

7+
from chessington.engine.data import Player, Square
8+
79
class Piece(ABC):
810
"""
911
An abstract base class from which all pieces inherit.

chessington/ui/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
import PySimpleGUI as psg
88

9-
from chessington.engine.board import Player, Board, Square, BOARD_SIZE
9+
from chessington.engine.board import Board, BOARD_SIZE
10+
from chessington.engine.data import Player, Square
1011
from chessington.engine.pieces import Pawn, Knight, Bishop, Rook, Queen, King
1112

1213
IMAGES_BASE_DIRECTORY = 'images'
@@ -68,7 +69,7 @@ def highlight_squares(window, from_square, to_squares):
6869
def play_game():
6970
psg.ChangeLookAndFeel('GreenTan')
7071

71-
board = Board()
72+
board = Board.at_starting_position()
7273
board_layout = render_board(board)
7374
window = psg.Window('Chessington', default_button_element_size=(12, 1), auto_size_buttons=False).Layout(board_layout)
7475

tests/test_board.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
from chessington.engine.board import Board, Square, Player
1+
from chessington.engine.board import Board
2+
from chessington.engine.data import Player, Square
23

34
def test_new_board_has_white_pieces_at_bottom():
45

56
# Arrange
6-
board = Board()
7+
board = Board.at_starting_position()
78

89
# Act
910
piece = board.get_piece(Square.at(0, 0))
@@ -14,7 +15,7 @@ def test_new_board_has_white_pieces_at_bottom():
1415
def test_new_board_has_black_pieces_at_top():
1516

1617
# Arrange
17-
board = Board()
18+
board = Board.at_starting_position()
1819

1920
# Act
2021
piece = board.get_piece(Square.at(7, 0))
@@ -25,7 +26,7 @@ def test_new_board_has_black_pieces_at_top():
2526
def test_pieces_can_be_moved_on_the_board():
2627

2728
# Arrange
28-
board = Board()
29+
board = Board.at_starting_position()
2930
from_square = Square.at(1, 0)
3031
piece = board.get_piece(from_square)
3132

0 commit comments

Comments
 (0)