def test_player_validate_invalid(): game_valid = GameValidation() sm = StateManager() new_state = sm.create({"game_id": "debug_testing"}) result = game_valid.validate_player(new_state, new_state.player_ids[1]) assert result.success == False assert result.errors == ["It is not Player 2's turn to play."]
def test_new_state(): sm = StateManager() new_state = sm.create({"game_id": "debug_testing"}) assert new_state.board == {'size': 3, 'status': [[0, 0, 0], [0, 0, 0], [0, 0, 0]]} assert new_state.player_turn == 1 assert new_state.player_count == 2 assert new_state.history == []
def test_player_validate_non_existent(): game_valid = GameValidation() sm = StateManager() new_state = sm.create({"game_id": "debug_testing"}) result = game_valid.validate_player(new_state, "LOGIC") assert result.success == False assert result.errors == ["There is no player with that ID in this game."]
def test_player_validate_valid(): game_valid = GameValidation() sm = StateManager() new_state = sm.create({"game_id": "debug_testing"}) result = game_valid.validate_player(new_state, new_state.player_ids[0]) assert result.success == True assert result.errors == []
def test_update_state_three_times(): sm = StateManager() new_state = sm.create({"game_id": "debug_testing"}) updated_state = sm.update(new_state, {"coordinates": [1, 1]}) assert updated_state.board == {'size': 3, 'status': [[0, 0, 0], [0, 1, 0], [0, 0, 0]]} assert updated_state.player_turn == 2 updated_state = sm.update(updated_state, {"coordinates": [0, 1]}) assert updated_state.board == {'size': 3, 'status': [[0, 2, 0], [0, 1, 0], [0, 0, 0]]} assert updated_state.player_turn == 1
def test_cats_game(): sm = StateManager() class MockState: def __init__(self): self.board = { "status": [[1, 2, 1, 2], [2, 1, 2, 1], [2, 1, 2, 1], [1, 2, 1, 2]], "size": 4 } assert sm.is_game_complete(MockState()) == (True, True)
def test_incomplete_left_column_one_move(): sm = StateManager() class MockState: def __init__(self): self.board = { "status": [[1, 0, 0], [0, 0, 0], [0, 0, 0]], "size": 3 } assert sm.is_game_complete(MockState()) == (False, False)
def test_diagonal_left_to_right(): sm = StateManager() class MockState: def __init__(self): self.board = { "status": [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]], "size": 4 } assert sm.is_game_complete(MockState()) == (True, False)
def test_win_middle_column(): sm = StateManager() class MockState: def __init__(self): self.board = { "status": [[0, 1, 0], [0, 1, 0], [0, 1, 0]], "size": 3 } assert sm.is_game_complete(MockState()) == (True, False)
def test_position_validation_invalid(): game_valid = GameValidation() sm = StateManager() new_state = sm.create({ "board_size": 3, "board_status": [[0, 0, 0], [0, 0, 0], [0, 1, 0]] }) result = game_valid.validate_position(new_state, [2, 1]) assert result.success == False assert result.errors == ["This position is already populated..."]
def test_validation_big_board_one_off_cases(): cli_valid = CLIValidation() sm = StateManager() new_state = sm.create({"board_size": 8, "game_id": "debug_testing"}) validation_errors = cli_valid.validate_move_text(new_state, "I9") assert validation_errors.success == False assert validation_errors.errors[0] == str( "\"I\" is not one of the valid selctions in position 1... (A - H)") assert validation_errors.errors[1] == str( "\"9\" is not one of the valid selctions in position 2... (1 - 8)")
def test_read_state(): sr = StateRepo() sm = StateManager() new_state = sm.create({"game_id": "debug_testing"}) sr.write_state(new_state) read_data = sr.read_state("debug_testing") assert read_data.board == { "size": 3, "status": [[0, 0, 0], [0, 0, 0], [0, 0, 0]] }
def test_position_validation_valid(): game_valid = GameValidation() sm = StateManager() new_state = sm.create({ "board_size": 3, "board_status": [[1, 1, 1], [1, 1, 1], [1, 0, 1]] }) result = game_valid.validate_position(new_state, [2, 1]) assert result.success == True assert result.errors == []
def test_input_invalid_second_character(): cli_valid = CLIValidation() sm = StateManager() new_state = sm.create({"game_id": "debug_testing"}) text_input = "A4" text_input = list(text_input.upper()) board_size = new_state.board["size"] validation_errors = cli_valid.has_invalid_character_second_position( board_size, text_input) assert validation_errors[0] == str( "\"4\" is not one of the valid selctions in position 2... (1 - 3)")
def test_validation_multi_error(): cli_valid = CLIValidation() sm = StateManager() new_state = sm.create({"game_id": "debug_testing"}) validation_errors = cli_valid.validate_move_text(new_state, "RESPECT") assert validation_errors.success == False assert validation_errors.errors[0] == str( "Input is 5 characters too long...") assert validation_errors.errors[1] == str( "\"R\" is not one of the valid selctions in position 1... (A - C)") assert validation_errors.errors[2] == str( "\"E\" is not one of the valid selctions in position 2... (1 - 3)")
def test_delete_state(): sr = StateRepo() sm = StateManager() current_dir = os.getcwd() data_folder_path = os.path.join(current_dir, r'data') new_state = sm.create({"game_id": "debug_testing"}) state_file_location = os.path.join(data_folder_path, str(new_state.game_id) + ".dat") sr.write_state(new_state) sr.delete_state(new_state.game_id) assert os.path.exists(state_file_location) == False
def __init__(self, is_server=False): self.running = True self.is_server = is_server self.state_manager = StateManager(is_server) if is_server: self.state_manager.change_state(self.state_manager.server_state) else: self.state_manager = StateManager() pygame.init() self.window = pygame.display.set_mode((1024, 768)) pygame.display.set_caption("Snaky Snake") self.state_manager.change_state(self.state_manager.menu_state) self.mainloop()
def test_write_state(): sr = StateRepo() sm = StateManager() new_state = sm.create({ "game_id": "debug_testing", "board_size": 5, "board_status": [[1, 2, 3, 4, 5], [2, 3, 4, 5, 1], [3, 4, 5, 1, 2], [4, 5, 1, 2, 3], [5, 1, 2, 3, 4]] }) sr.write_state(new_state) read_data = sr.read_state("debug_testing") assert read_data.board == { "size": 5, "status": [[1, 2, 3, 4, 5], [2, 3, 4, 5, 1], [3, 4, 5, 1, 2], [4, 5, 1, 2, 3], [5, 1, 2, 3, 4]] }
def test_overwrite_state(): sr = StateRepo() sm = StateManager() new_state = sm.create({ "game_id": "debug_testing", "board_size": 5, "board_status": [[1, 2, 3, 4, 5], [2, 3, 4, 5, 1], [3, 4, 5, 1, 2], [4, 5, 1, 2, 3], [5, 1, 2, 3, 4]] }) sr.write_state(new_state) read_data = sr.read_state("debug_testing") assert read_data.board == { "size": 5, "status": [[1, 2, 3, 4, 5], [2, 3, 4, 5, 1], [3, 4, 5, 1, 2], [4, 5, 1, 2, 3], [5, 1, 2, 3, 4]] } overwrite_state = sm.update(read_data, { "coordinates": [2, 2], "player_ids": ["debug", "testing"] }) sr.write_state(overwrite_state) overwritten_read_data = sr.read_state("debug_testing") assert overwritten_read_data.board == { "size": 5, "status": [[1, 2, 3, 4, 5], [2, 3, 4, 5, 1], [3, 4, 1, 1, 2], [4, 5, 1, 2, 3], [5, 1, 2, 3, 4]] } print(overwritten_read_data.history) assert overwritten_read_data.history[0].board == { "size": 5, "status": [[1, 2, 3, 4, 5], [2, 3, 4, 5, 1], [3, 4, 5, 1, 2], [4, 5, 1, 2, 3], [5, 1, 2, 3, 4]] }
class Game: window = None running = False state_manager = None def __init__(self, is_server=False): self.running = True self.is_server = is_server self.state_manager = StateManager(is_server) if is_server: self.state_manager.change_state(self.state_manager.server_state) else: self.state_manager = StateManager() pygame.init() self.window = pygame.display.set_mode((1024, 768)) pygame.display.set_caption("Snaky Snake") self.state_manager.change_state(self.state_manager.menu_state) self.mainloop() def mainloop(self): while self.running: if not self.is_server: events = pygame.event.get() for event in events: if event.type == pygame.QUIT: self.running = False self.state_manager.events = events self.update() self.render(self.window) pygame.display.update() pygame.time.delay(10) self.window.fill((0, 0, 0)) def update(self): self.state_manager.update() def render(self, window): self.state_manager.render(window)
def test_board_size(): sm = StateManager() assert sm.regulate_board_size(5) == 5
from game.view import View from game.state_manager import StateManager from game.in_cli import InCLI from game.game_input import GameInput from game.state_repo import StateRepo DEFAULT_DEPENDENCIES = { "View": View(), "StateManager": StateManager(), "InCLI": InCLI(), "GameInput": GameInput(), "StateRepo": StateRepo() } class TicTacToe: def __init__(self, injected_dependencies=None, game_id=None): if injected_dependencies == None: injected_dependencies = {} dependencies = dict(DEFAULT_DEPENDENCIES, **injected_dependencies) self.view = dependencies["View"] self.state_manager = dependencies["StateManager"] self.user_input = dependencies["InCLI"] self.game_input = dependencies.get("GameInput") self.state_repo = dependencies.get("StateRepo") self.game_id = self.setup_game_id(game_id) def play(self, game_id=None): if game_id == None: game_id = self.game_id state = self.state_repo.read_state(game_id)
def test_board_size_too_big(): sm = StateManager() assert sm.regulate_board_size(10) == 9
def test_check_identical_values_all_ones(): sm = StateManager() test_array = [1, 1, 1, 1] assert sm.check_identical_values(test_array) == True
def test_check_identical_values_all_zeros(): sm = StateManager() test_array = [0, 0, 0, 0] assert sm.check_identical_values(test_array) == False
def test_dynamic_board(): sm = StateManager() new_state = sm.create({"board_size": 5, "game_id": "debug_testing"}) assert new_state.board["status"] == [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
def test_check_identical_values_all_twos(): sm = StateManager() test_array = [2, 2, 2, 2] assert sm.check_identical_values(test_array) == True
def test_update_board(): sm = StateManager() new_state = sm.create({"game_id": "debug_testing"}) updated_board = sm.update_board(new_state, [1,1]) assert updated_board["board"] == {'size': 3, 'status': [[0, 0, 0], [0, 1, 0], [0, 0, 0]]}