Sticks is a 2-player mathematical strategy game.
It is played with a pile/heap of sticks, where players take turns removing sticks from the pile.
The player to take the last stick from the pile loses.
Each player must take at least one and at most t sticks.
A game of Sticks is parametric over the number of initial sticks in the pile and the maximum number of sticks a player
may take on their turn (called maxTake
).
For demonstration and testing purposes, use a pile of 21 sticks and a maxTake
rule of 3.
That is, a player can take 1, 2, or 3 sticks on their turn.
The game serves as a good introduction to programming concepts such as interfaces1, inheritance2,
and polymorphism (see use of concrete player types as IPlayer
in SticksGame).
Students will use their knowledge of class structure to implement their own custom types with fields, constants,
properties, constructors, and methods.
This also offers reinforcement material for loops, conditional statements, and user interaction via the Console
class.
The following topics are likely to be new material for students:
- Top-down design
- Creating non-static classes
- Interfaces
- Abstract classes, methods, and the
override
keyword - Property usage
- Domain boundaries, e.g. validating a player's move in the
SticksGame
class instead of eachIPlayer
implementation - Polymorphism
- Before coding the program, explain and demonstrate the game with sticks.
- Explain top-down vs bottom-up design.
- Have students create
SticksRunner
as the program entrypoint, and finish the body ofSticksRunner.Main
without implementing its dependencies. - Use the IDE code generation tools (found under "quick fix" or a context menu) to generate the classes and methods
used inside
SticksRunner.Main
.- When implementing the static method
SticksGame.Prompt
, point out that we know it should be static because inMain
it's called on the class, not an instance.
- When implementing the static method
Sticks/Architecture.puml
is a PlantUML diagram source describes the architecture of the project.
- Create the program entrypoint
SticksRunner.Main()
.- Interactively ask the player for the rules of their game using
SticksGame.Prompt(string prompt)
. - Instantiate a
SticksGame
with those rules using theSticksGame(int initialSticks, int maxTake)
constructor. - Run the game using the
Run()
method on theSticksGame
instance.
- Interactively ask the player for the rules of their game using
- Create fields in
SticksGame
:- The class must have a public constant
MinTake
that equals1
. - Use the constructor to initialize private fields
readonly int _maxTake
andint _sticks
.
- The class must have a public constant