From 3dba52f7812839fe0607aa612ff4c2ec4e95329c Mon Sep 17 00:00:00 2001 From: Tibor Bizjak Date: Sun, 1 Sep 2019 16:52:51 +0200 Subject: [PATCH] Created Game object --- chess.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 chess.py diff --git a/chess.py b/chess.py new file mode 100644 index 0000000..bb75cd7 --- /dev/null +++ b/chess.py @@ -0,0 +1,39 @@ +def cross(A, B): + return tuple(a+b for a in A for b in B) + +ranks = "12345678" +files = "abcdefgh" +squares = cross(files, ranks) + +pawn_ranks = "27" +home_ranks = "18" +init_positions = {"pawn" : cross(files, pawn_ranks), + "rook" : cross("ah", home_ranks), + "knight" : cross("bg", home_ranks), + "bishop" : cross("cf", home_ranks), + "queen" : cross("d", home_ranks), + "king" : cross("e", home_ranks) + } +class Game: + def __init__(self): + self.make_board() + + def make_board(self): + self.board = dict((sq, None) for sq in squares) + # Add pieces + for piece, positions in init_positions.items(): + for sq in positions: + self.board[sq] = piece + # Add colors + for sq in cross(files, "12"): + self.board[sq] = ("white", self.board[sq]) + for sq in cross(files, "78"): + self.board[sq] = ("black", self.board[sq]) + +def test(): + game = Game() + assert len(squares) == 8**2 + assert sum(map(len, init_positions.values())) == 8*4 + +test() +