from chess_piece import ChessPiece from pytest import fixture from player import Player from move import StaticMoveSet, Move, DynamicMoveSet, valid_range, pawn_valid_move_sets from random import randint, choice from pawn import Pawn from chess_model import DEFAULT_BOARD # chess piece tests # general piece @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=' in rep # pawn piece _INIT_PAWN_ORIGINAL_VAL = (4, 1) @fixture def valid_board(): # ask how to define the board pass @fixture def valid_black_pawn(): return Pawn(Player.BLACK) def test_valid_move_black_pawn_1(valid_black_pawn: Pawn): # try all valid pawn moves for ms in pawn_valid_move_sets: assert valid_black_pawn.is_valid_move(Move(_INIT_PAWN_ORIGINAL_VAL[0], _INIT_PAWN_ORIGINAL_VAL[1], _INIT_PAWN_ORIGINAL_VAL[0]+ms[1], _INIT_PAWN_ORIGINAL_VAL[1]+ms[0]), DEFAULT_BOARD) # move set testing (kinda separate from main project) _INIT_MOVE_ORIGIN_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: print(ms) assert valid_static_move_set.is_valid_move(Move(_INIT_MOVE_ORIGIN_VAL, _INIT_MOVE_ORIGIN_VAL, _INIT_MOVE_ORIGIN_VAL+ms[0], _INIT_MOVE_ORIGIN_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_MOVE_ORIGIN_VAL, _INIT_MOVE_ORIGIN_VAL, _INIT_MOVE_ORIGIN_VAL+ms[0]+(-1 if ms[0] < 0 else 1), _INIT_MOVE_ORIGIN_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_MOVE_ORIGIN_VAL, _INIT_MOVE_ORIGIN_VAL, _INIT_MOVE_ORIGIN_VAL+rnd_row, _INIT_MOVE_ORIGIN_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_MOVE_ORIGIN_VAL, _INIT_MOVE_ORIGIN_VAL, _INIT_MOVE_ORIGIN_VAL+row_rnd_add, _INIT_MOVE_ORIGIN_VAL+col_rnd_add))