Skip to content

Commit

Permalink
v1.0.0 major update
Browse files Browse the repository at this point in the history
  • Loading branch information
a2ys committed Oct 5, 2022
1 parent 1585bf9 commit 5bc25a2
Show file tree
Hide file tree
Showing 10 changed files with 321 additions and 38 deletions.
Binary file modified __pycache__/board.cpython-310.pyc
Binary file not shown.
Binary file modified __pycache__/move.cpython-310.pyc
Binary file not shown.
Binary file modified __pycache__/moves.cpython-310.pyc
Binary file not shown.
169 changes: 137 additions & 32 deletions board.py

Large diffs are not rendered by default.

Binary file modified defs/__pycache__/const.cpython-310.pyc
Binary file not shown.
8 changes: 5 additions & 3 deletions defs/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import defs.ChessErrors

initial_board = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
# initial_board = "4k3/8/8/8/8/8/8/3KQRR1 w KQkq - 0 1"
# initial_board = "4k3/8/8/8/8/8/8/4K2R w KQkq - 0 1"


# Pre-defined game constraints
Expand Down Expand Up @@ -48,6 +48,7 @@
"To fix - Check all 'color' arguments of pieces in board.py.fen_to_board() method, or if you've defined the board.py by yourself, check the 'color' argument entered."
no_king_error = "No King was found in the FEN string. If not done by you, report to me at - [email protected]"
king_captured_error = "The King was captured. There must be a bug, please report to me at - [email protected] with full explanation of the moves you made."
invalid_move_identifier_error = "You provided an invalid move identifier."


# Method that returns a 2-D list from a 1-D list
Expand Down Expand Up @@ -75,18 +76,20 @@ def get_move_id(arg_move: list, spl_move_identifier: str = 'n') -> str:
if spl_move_identifier.lower() in ['c', 'e', 'p', 'n']:
m_id += spl_move_identifier.lower()
else:
raise defs.ChessErrors.InvalidMoveIdentifier("You provided an invalid move identifier.")
raise defs.ChessErrors.InvalidMoveIdentifier(invalid_move_identifier_error)

return m_id


# Method to compare two move IDs
def is_move_id_equal(move_id: str, all_move_id: list) -> bool:
for m_id in all_move_id:
if m_id[:-1] == move_id:
return True
return False


# Method to get the move type in various cases.
def get_move_type(move_id: str, all_move_id: list) -> str:
for m_id in all_move_id:
if m_id[:-1] == move_id:
Expand All @@ -109,5 +112,4 @@ def legal_moves(pseudo_legal_moves: list, illegal_moves: list) -> list:
for move in illegal_moves:
if move in pseudo_legal_moves:
lgl_moves.remove(move)

return lgl_moves
3 changes: 2 additions & 1 deletion move.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import piece.Piece


# This class defines a 'Move' object responsible for identification, validation and execution of a move.
class Move:
def __init__(self, start_pos: tuple, end_pos: tuple, board: list) -> None:
Expand Down Expand Up @@ -36,7 +37,7 @@ def set_special_pos(self, pos: tuple) -> None:
def get_extra_piece(self) -> piece.Piece:
return self.extra_piece

def set_extra_piece(self, extra_piece_object: piece.Piece):
def set_extra_piece(self, extra_piece_object: piece.Piece) -> None:
self.extra_piece = extra_piece_object

def print_info(self) -> None:
Expand Down
172 changes: 172 additions & 0 deletions move_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import unittest

import board
import move
import moves
from defs import const


class Tests(unittest.TestCase):
def test_first(self):
gs = board.GameState()
total = 0

for rank in gs.board:
for piece in rank:
if piece.get_alpha() != '--':
if piece.get_color().lower() == 'white':
for _ in moves.legal_moves(piece, gs.board):
total += 1

self.assertEqual(total, 20)

def test_second(self):
gs = board.GameState()
total = 0

for rank in gs.board:
for piece in rank:
if piece.get_alpha() != "--":
if piece.get_color().lower() == 'white':
for legal_move in moves.legal_moves(piece, gs.board):
pos = const.get_move_from_id(legal_move)
mv = move.Move(pos[0], pos[1], gs.board)
gs.make_move(mv, sound=False)

for r in gs.board:
for p in r:
if p.get_alpha() != "--":
if p.get_color().lower() == 'black':
for _ in moves.legal_moves(p, gs.board):
total += 1

gs.undo_move()

self.assertEqual(total, 400)

def test_third(self):
gs = board.GameState()
total = 0

for rank in gs.board:
for piece in rank:
if piece.get_alpha() != "--":
if piece.get_color().lower() == 'white':
for legal_move in moves.legal_moves(piece, gs.board):
pos = const.get_move_from_id(legal_move)
m = move.Move(pos[0], pos[1], gs.board)
gs.make_move(m, sound=False)

for r in gs.board:
for p in r:
if p.get_alpha() != "--":
if p.get_color().lower() == 'black':
for lgl_move in moves.legal_moves(p, gs.board):
pos = const.get_move_from_id(lgl_move)
mv = move.Move(pos[0], pos[1], gs.board)
gs.make_move(mv, sound=False)

for i in gs.board:
for j in i:
if j.get_alpha() != "--":
if j.get_color().lower() == 'white':
for _ in moves.legal_moves(j, gs.board):
total += 1
gs.undo_move()
gs.undo_move()
self.assertEqual(total, 8902)

def test_fourth(self):
gs = board.GameState()
total = 0

for rank in gs.board:
for piece in rank:
if piece.get_alpha() != "--":
if piece.get_color().lower() == 'white':
for legal_move in moves.legal_moves(piece, gs.board):
pos = const.get_move_from_id(legal_move)
m = move.Move(pos[0], pos[1], gs.board)
gs.make_move(m, sound=False)

for r in gs.board:
for p in r:
if p.get_alpha() != "--":
if p.get_color().lower() == 'black':
for lgl_move in moves.legal_moves(p, gs.board):
pos = const.get_move_from_id(lgl_move)
mv = move.Move(pos[0], pos[1], gs.board)
gs.make_move(mv, sound=False)

for i in gs.board:
for j in i:
if j.get_alpha() != "--":
if j.get_color().lower() == 'white':
for lgl in moves.legal_moves(j, gs.board):
pos = const.get_move_from_id(lgl)
mv = move.Move(pos[0], pos[1], gs.board)
gs.make_move(mv, sound=False)

for a in gs.board:
for b in a:
if b.get_alpha() != "--":
if b.get_color().lower() == 'black':
for _ in moves.legal_moves(b, gs.board):
total += 1
gs.undo_move()
gs.undo_move()
gs.undo_move()
self.assertEqual(total, 197281)

def test_fifth(self):
gs = board.GameState()
total = 0

for rank in gs.board:
for piece in rank:
if piece.get_alpha() != "--":
if piece.get_color().lower() == 'white':
for legal_move in moves.legal_moves(piece, gs.board):
pos = const.get_move_from_id(legal_move)
m = move.Move(pos[0], pos[1], gs.board)
gs.make_move(m, sound=False)

for r in gs.board:
for p in r:
if p.get_alpha() != "--":
if p.get_color().lower() == 'black':
for lgl_move in moves.legal_moves(p, gs.board):
pos = const.get_move_from_id(lgl_move)
mv = move.Move(pos[0], pos[1], gs.board)
gs.make_move(mv, sound=False)

for i in gs.board:
for j in i:
if j.get_alpha() != "--":
if j.get_color().lower() == 'white':
for lgl in moves.legal_moves(j, gs.board):
pos = const.get_move_from_id(lgl)
mv = move.Move(pos[0], pos[1], gs.board)
gs.make_move(mv, sound=False)

for a in gs.board:
for b in a:
if b.get_alpha() != "--":
if b.get_color().lower() == 'black':
pos = const.get_move_from_id(lgl)
mv = move.Move(pos[0], pos[1], gs.board)
gs.make_move(mv, sound=False)

for c in gs.board:
for d in c:
for _ in moves.legal_moves(d, gs.board):
total += 1
gs.undo_move()
gs.undo_move()
gs.undo_move()
gs.undo_move()
self.assertEqual(total, 4865609)


if __name__ == '__main__':
unittest.main()
7 changes: 5 additions & 2 deletions moves.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import board
from defs import const
from main import Main
from piece import Piece, King, Queen, Bishop, Knight, Rook, Pawn, WhiteSpace


Expand Down Expand Up @@ -260,6 +258,11 @@ def legal_moves(piece: Piece, arg_board: list) -> list:
elif isinstance(piece, Pawn.Pawn):
moves = []
for move in pawn_moves(piece.get_rank(), piece.get_file(), arg_board):
# if piece.get_color().lower() == 'white' and piece.get_rank() == 1:
# moves.append(const.get_move_id([(piece.get_rank(), piece.get_file()), (move[0], move[1])], 'p'))
# elif piece.get_color().lower() == 'black' and piece.get_rank() == 6:
# moves.append(const.get_move_id([(piece.get_rank(), piece.get_file()), (move[0], move[1])], 'p'))
# else:
moves.append(const.get_move_id([(piece.get_rank(), piece.get_file()), (move[0], move[1])]))
return moves
else:
Expand Down
Binary file modified piece/__pycache__/King.cpython-310.pyc
Binary file not shown.

0 comments on commit 5bc25a2

Please sign in to comment.