-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
321 additions
and
38 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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: | ||
|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.