Coverage for zombie_nomnom/engine/__init__.py: 100%

4 statements  

« prev     ^ index     » next       coverage.py v7.6.8, created at 2024-12-05 01:14 +0000

1"""Module that contains the code to run the core game engine. 

2You can create an instance directly by using the `ZombieDieGame`. 

3 

4```python 

5from zombie_nomnom import ZombieDieGame, DrawDice, Score 

6 

7score = Score() 

8draw_dice = DrawDice(3) 

9 

10game = ZombieDieGame(players=["Mellow"]) 

11 

12game.process_command(draw_dice) 

13game.process_command(score) 

14 

15``` 

16 

17For the core of the engine it doesn't actually know how to play but only  

18how to manage a turn based on tracking the changes in a round with a `RoundState` object. 

19 

20This allows you to extend the game by creating your own custom actions with the `Command` class. 

21This class defines the `execute` method and then allows you to specify what that action does to the game state. 

22You will need to return an new instance of `RoundState` objects that represents the effect of game state after the round is over. 

23 

24```python 

25from zombie_nomnom import Command, ZombieDieGame 

26 

27class CustomCommand(Command): 

28 def execute(self, round: RoundState) -> RoundState: 

29 # do meaningful work to define what you want this command to do to the round. 

30 return round # return either a new command or the exact same command unchanged. 

31 

32custom_command = CustomCommand() 

33game = ZombieDieGame(players=["Meelow"]) 

34 

35game.process_command(custom_command) # now it just works in the game!! 

36 

37``` 

38 

39This only allows you to modify the state of the current player or the current turn that is active in the engine. 

40That being said it should provide a nice way to extend the app with custom actions for a players. 

41 

42The core objects that we use in our engine are three: 

43- `Player` 

44- `RoundState` 

45 

46```python 

47from zombie_nomnom.models import DieBag, Die, Face 

48from zombie_nomnom.engine import Player, RoundState 

49 

50player = Player( 

51 name="Mega Man" 

52) 

53 

54# you can add dice to their hand. 

55player_with_die_added = player.add_dice( 

56 Die( 

57 faces=[Face.BRAIN] * 6 

58 ) 

59) 

60player_with_die_added.hand # dice is now updated with new die. 

61 

62player.rerolls # pulls the dice that are re-rollable in hand 

63player.brains # pulls all the scoring dice in hand. 

64player.shots # pulls all the damaging dice in hand. 

65 

66# counts the shots in the hand and see if you have been shot 3 or more times. 

67player.is_player_dead() # player is not dead 

68 

69# new player with nothing in their hand. 

70new_player = player.clear_hand() 

71new_player.hand # is empty list 

72 

73# resets the players game stats 

74new_player = player.reset()  

75new_player.total_brains # now 0 

76new_player.hand # empty list 

77 

78# counts the dice in the hand and adds to score. 

79new_player = player.calculate_score() 

80new_player.total_brains # player had 1 brain so it will now be 1 

81new_player.hand # is not empty list 

82 

83# no methods just holds value to package it in a single object. 

84round = RoundState( 

85 bag=DieBag.standard_bag(), 

86 player=player, 

87) 

88``` 

89""" 

90 

91from .models import Player, RoundState, uuid_str 

92from .commands import Command, Score, DrawDice 

93from .game import ZombieDieGame 

94 

95 

96__all__ = [ 

97 "Player", 

98 "RoundState", 

99 "uuid_str", 

100 "Command", 

101 "Score", 

102 "DrawDice", 

103 "ZombieDieGame", 

104 "commands", 

105 "game", 

106 "models", 

107 "serialization", 

108]