Files
cis163/chess/test.py
cutsweettea cb8e6e9b5a more cool stuff
chess, woah
2025-10-15 12:46:55 -04:00

104 lines
3.4 KiB
Python

from chess_piece import ChessPiece
from pytest import fixture, mark
from player import Player
from move import StaticMoveSet, Move, DynamicMoveSet, valid_range
from random import randint, choice
# chess piece tests
@fixture
def valid_piece():
return ChessPiece(Player.WHITE)
def test_update_player(valid_piece: ChessPiece):
valid_piece.player = Player.BLACK
assert valid_piece.player == Player.BLACK
valid_piece.player = Player.WHITE
assert valid_piece.player == Player.WHITE
def test_repr_str(valid_piece: ChessPiece):
rep = str(valid_piece)
assert 'player='
# move set testing (kinda separate from main project)
_init_val = 4
# static move sets
_static_move_sets = [(1, 0), (-3, 0), (0, 2), (0, -1), (4, 4), (-2, -2), (1, -3), (-3, 4)]
@fixture
def valid_static_move_set():
return StaticMoveSet(*_static_move_sets)
# test valid
def test_valid_static_moves(valid_static_move_set: StaticMoveSet):
for ms in _static_move_sets:
assert valid_static_move_set.is_valid_move(Move(_init_val, _init_val, _init_val+ms[0], _init_val+ms[1]))
# test invalid
def test_invalid_static_moves(valid_static_move_set: StaticMoveSet):
for ms in _static_move_sets:
assert not valid_static_move_set.is_valid_move(Move(_init_val, _init_val, _init_val+ms[0]+(-1 if ms[0] < 0 else 1), _init_val+ms[1]+(-1 if ms[1] < 0 else 1)))
# dynamic move sets
_dynamic_move_sets = [(4, 0), (-2, 0), (0, 8), (0, -6), (4, 4), (-4, -4), (2, -5), (-3, 4)]
@fixture
def valid_dynamic_move_set():
return DynamicMoveSet(*_dynamic_move_sets)
def test_valid_dynamic_moves(valid_dynamic_move_set: DynamicMoveSet):
for ms in _dynamic_move_sets:
row = ms[0]
col = ms[1]
# find a valid range on numbers to select from using the row and column
valid_range_row = valid_range(row)
valid_range_col = valid_range(col)
# check if the ranges for rows and columns are empty individually, if so;
# set random value to 0, if not, set it to a random element from it's respective list
if len(valid_range_row) == 0:
rnd_row = 0
else:
rnd_row = choice(valid_range_row)
if len(valid_range_col) == 0:
rnd_col = 0
else:
rnd_col = choice(valid_range_col)
# test dat thing
assert valid_dynamic_move_set.is_valid_move(Move(_init_val, _init_val, _init_val+rnd_row, _init_val+rnd_col))
_RND_MIN = 10
_RND_MAX = 20
def test_invalid_dynamic_moves(valid_dynamic_move_set: DynamicMoveSet):
for ms in _dynamic_move_sets:
row = ms[0]
col = ms[1]
# check if the row and column ranges are equal to zero, if so;
# set random value to 0, if not, create random number between _rnd_min and _rnd_max
# then, add the random number, making it negative if the column or row in that instance > 0
if row == 0:
row_rnd_add = 0
else:
rnd = randint(_RND_MIN, _RND_MAX)
row_rnd_add = row + (rnd if row > 0 else -rnd)
if col == 0:
col_rnd_add = 0
else:
rnd = randint(_RND_MIN, _RND_MAX)
col_rnd_add = col + (rnd if col > 0 else -rnd)
#print(f'{ms}, ({_init_val}+{row_rnd_add}, {_init_val}+{col_rnd_add}) = ({_init_val+row_rnd_add}, {_init_val+col_rnd_add})')
assert not valid_dynamic_move_set.is_valid_move(Move(_init_val, _init_val, _init_val+row_rnd_add, _init_val+col_rnd_add))