wargame package

Submodules

wargame.abstractgameunit module

wargame.abstractgameunit

This module contains the AbstractGameUnit class implementation.

This modue is compatible with Python 3.5.x and later. It contains supporting code for the book, Learning Python Application Development Packt Publishing.

This is my version of the code, it is pretty much similar to the original author version.

copyright

2020, Jean Tardelli

license

The MIT License (MIT). See LICENSE file for further details.

class wargame.abstractgameunit.AbstractGameUnit(name='')[source]

Bases: abc.ABC

Abstract class to represent a game character (or a ‘unit’)

Variables
  • name – Name of the character (set by subclassess)

  • max_hp – Maximum ‘hit points’ or ‘health points’ for the unit. This is set by the subclasses.

  • health_meter – Keeps track of the current health of the unit

  • enemy – Present enemy of this unit. At any time, it can have only one enemy.

  • unit_type – Tells if this is a ‘friend’ or an ‘enemy’

Parameters

name – Accept the name of this game character

See also

Classes Knight and OrcRider

attack(enemy)[source]

The main logic to ‘attack’ the enemy unit.

This method handles combat between the player (Knight instance) and the given enemy (at the moment OrcRider instance). In the combat, one of the units could get injured or both will scape unhurt. The method reduces the ‘health’ oh the injured unit by a randomly selected amount.

Parameters

enemy – The enemy to be attacked (instance of subclass of AbstractGameUnit)

See also

Knight.acquire_hut()

heal(heal_by=2, full_healing=True)[source]

Heal the unit replenishing all the hit points

This method is called when you (the player) enters a friendly hut.

Parameters
  • heal_by`health_meter`will be updated by this amount if full healing is not requested.

  • full_healing – Fully heal this unit by resetting the `heal_meter`to the maximum limit.

See also

Knigth.acquire_hut()

abstract info()[source]

Print information about this game unit.

Abstract method. See subclasses for implementation.

reset_health_meter()[source]

Reset the health_meter (assing default hit points)

show_health(bold=False, end='\n')[source]

Print info on the current health reading of this game unit

The arguments to this method are mainly to customize the message display style.

Parameters
  • bold – Flag to indicate whether information should be printed in bold style or normal style.

  • end – Specify how the message should end i.e wheter a new line character should be appended in the end or you want to add a space or a tab (for message continuation)

wargame.attackoftheorcs module

wargame.attackoftheorcs

This module contains the AttackOfTheOrcs class implementation.

This modue is compatible with Python 3.5.x and later. It contains supporting code for the book, Learning Python Application Development Packt Publishing.

This is my version of the code, it is pretty much similar to the original author version.

copyright

2020, Jean Tardelli

license

The MIT License (MIT). See LICENSE file for further details.

class wargame.attackoftheorcs.AttackOfTheOrcs[source]

Bases: object

Main class with the high level logic to play Attack of The Orcs game

Variables
  • huts – List object to hold instances of Hut class.

  • player – Represents the player playing this game. This is an instance of class Knight in current implementation.

See also

self.play() where the main action happens.

get_occupants()[source]

Return a list of occupant types for all huts.

This is mainly used for printing information on current status of the hut (wheter unoccupied or acquired).

If the occupant is not None the occupant type will be ‘enemy’ or ‘friend’. But if there is no occupant or is already ‘ACQUIRED’ the occupant_type will display that information instead. See Hut.get_occupant_type() for more details.

Return a list that collects this information from all the huts. This is a list comprehension example. More on the list comprehension in a chapter on Performance.

Returns

A list containing occupant types (string)

play()[source]

Workhorse method to play the game.

Controls the high level logic to play the game. this is called from the main program to begin the game execution.

In summary, this method has the high level logic that does the following by calling appropriate functionality:

  • Set up instance variables for the game

  • Accept the user input for hut number to enter

  • Attempt to acquire the hut (Knight.acquire_hut())

  • Determine if the player wins or loses.

See also

Knight.acquire_hut()

show_game_mission()[source]

Print the game mission in the console

wargame.gameuniterror module

wargame.gameuniterror

Shows how to create a custom exception class for the Attack of the Orcs game.

This modue is compatible with Python 3.5.x and later. It contains supporting code for the book, Learning Python Application Development Packt Publishing.

This is my version of the code, it is pretty much similar to the original author’s version.

copyright

2020, Jean Tardelli

license

The MIT License (MIT). See LICENSE file for further details.

exception wargame.gameuniterror.GameUnitError(message='', code=0)[source]

Bases: Exception

Custom exceptions class for the AbstractGameUnit and its subclasses

Inherits built-in Exception class.

Variables
  • error_message – Print the error message with an error code.

  • error_dict – Python dictionary object that stores error number as the key and the detailed error message as its value.

See also

:py:meth: abstractgameunit.AbstractGameUnit.heal for an example usage.

wargame.gameutils module

wargame.gameutils

This module contains some utility function for the game Attack of the Orcs

This modue is compatible with Python 3.5.x and later. It contains supporting code for the book, Learning Python Application Development Packt Publishing.

This is my version of the code, it is pretty much similar to the original author version.

copyright

2020, Jean Tardelli

license

The MIT License (MIT). See LICENSE file for further details.

wargame.gameutils.print_bold(msg, end='\n')[source]

Convinience function to print a message in bold style

Optionally you can also specify how the bold text should end. By default it ends with a new line character.

Parameters
  • msg – Message to be converted to bold style

  • end – Tell how the printed string should end (newline, space etc)

wargame.gameutils.weighted_random_selection(obj1, obj2)[source]

Randomly return one of the following, obj1 or obj2

Parameters
  • obj1 – An instance of class AbstractGameUnit. It can be any object. The calling code should ensure the correct object is passed to this function.

  • obj2 – Another instance of class AbstractGameUnit

Returns

obj1 or obj2

See also

weighted_random_selection_alternate() which is an alternative implementation that is used to demonstrate the importance of unit testing.

wargame.hut module

wargame.hut

This module contains the Hut class implementation.

This modue is compatible with Python 3.5.x and later. It contains supporting code for the book, Learning Python Application Development Packt Publishing.

This is my version of the code, it is pretty much similar to the original author version.

copyright

2020, Jean Tardelli

license

The MIT License (MIT). See LICENSE file for further details.

class wargame.hut.Hut(number, occupant)[source]

Bases: object

Class to create hut object(s) in the game Attack of the Orcs

Parameters
  • number (int) – Hut number to be assigned

  • occupant (AbstractGameUnit) – The new occupant of the Hut

Variables
  • number (int) – A number assigned to this hut

  • occupant (AbstractGameUnit) – The occupant of this hut. Needs to be an instance of the subclass of AbstractGameUnit.

  • is_acquired (boolean) – A boolean flag to indicate if the hut is acquired. In the current implementation this is viewed from the player’s perspective.

See also

where it is used – :py:meth: attackoftheorcs.AttackOfTheOrcs.setup_game_scenario

acquired(new_occupant)[source]

Update the occupant of this hut and set is_acquired flag.

Update the occupant instance variable with the parameter new_occupant and set the is_acquired flag to True.

Parameters

new_occupant – self.occupant will be updated with this parameter

Todo

In the current implementation this is supposed to be called only bt the Knight instance (everything from the player context). A generalization is to allow anyone to ‘acquire’ the hut! In that case, the client code should properly interpret meaning of is_acquired flag! Otherwise it will be a bug! As an exercise, write a unit test to catch this and/or make the calling code robust.

get_occupant_type()[source]

Return a string giving info on the hut occupant type.

Used only for the printing information on who is present in the hut. The information it will return depends on the occupant and can be one of these strings: ‘enemy’, ‘friend’, ‘ACQUIRED’, ‘unoccupied’

The logic is as follows: If the hut.occupant is one of the game characters, it will simply retrieve this info from that instance. Otherwise determine whether it is acquired or unoccupied.

Returns

A string representing the occupant type

wargame.knight module

wargame.knight

This modue is compatible with Python 3.5.x and later. It contains supporting code for the book, Learning Python Application Development Packt Publishing.

This is my version of the code, it is pretty much similar to the original author version.

copyright

2020, Jean Tardelli

license

The MIT License (MIT). See LICENSE file for further details.

class wargame.knight.Knight(name='Sir Foo')[source]

Bases: abstractgameunit.AbstractGameUnit

Class that represents the game character ‘Knight’

The player instance in the game is a Knight instance. Other Knight instances are considered as ‘friends’ of the player and is indicated by the attribute self.unit_type.

Parameters

name (str) – Name of this game character (optional)

Variables
  • max_hp (int) – Maximum number of hit points (health points)

  • health_meter (int) – The actual number of hit points or health points

  • unit_type (str) – Stores id this character (unit) a friend or an enemy

  • enemy (AbstractGameUnit) – Stores who is the enemy (not implemented)

acquire_hut(hut)[source]

Fight the combat (command line) to acquire the hut

Parameters

hut (Hut) – The hut that needs to be acquired.

info()[source]

Print basic information about this character

Overrides AbstractGameUnit.info

run_away()[source]

Abandon the combat and run away from the hut

If the player is losing the combat, there is an option to leave the hut. A strategy to rejuvenate and restart the combat for a better change of winning.

See also

self.acquire_hut()

wargame.orcrider module

wargame.orcrider

This module contains the OrcRider class implementation.

This modue is compatible with Python 3.5.x and later. It contains supporting code for the book, Learning Python Application Development Packt Publishing.

This is my version of the code, it is pretty much similar to the original author version.

copyright

2020, Jean Tardelli

license

The MIT License (MIT). See LICENSE file for further details.

class wargame.orcrider.OrcRider(name='')[source]

Bases: abstractgameunit.AbstractGameUnit

Class that represents the game character Orc Rider

See also

The class Knight and the superclass AbstractGameUnit

info()[source]

Print basic information about this character

Module contents