Lab 02

CSCI 1913 – Introduction to Algorithms, Data Structures, and Program Development

Nim

  • Simple two-player game
  • Players take turns removing tokens from the board by choosing a row from which to remove tokens
  • Players can remove 1 to 3 tokens (neither more less)
  • The player that takes the last token looses

Initial game state – 5 rows, 3 maximum tokens:

====================
1 #
2 ##
3 ###
4 ###
5 ###
====================

Nim – game play

Game with 4 rows, 3 tokens max:

====================
1 #
2 ##
3 ###
4 ###
====================
Choose a row 1 - 4 

Nim – game play

====================
1 #
2 ##
3 ###
4 ###
====================
Choose a row 1 - 4 2
How many tokens to take, 1 to 3 (no more than are available)

Nim – game play

====================
1 #
2 ##
3 ###
4 ###
====================
Choose a row 1 - 4 2
How many tokens to take, 1 to 3 (no more than are available) 2
====================
1 #
2 
3 ###
4 ###
====================
Choose a row 1 - 4

Nim – game play

====================
1 #
2 
3 ###
4 ###
====================
Choose a row 1 - 4 3
How many tokens to take, 1 to 3 (no more than are available)

Nim – game play

====================
1 #
2 
3 ###
4 ###
====================
Choose a row 1 - 4 3
How many tokens to take, 1 to 3 (no more than are available)  2
====================
1 #
2 
3 #
4 ###
====================
Choose a row 1 - 4

Nim – game play

Choose a row 1 - 4 4
How many tokens to take, 1 to 3 (no more than are available) 3
====================
1 #
2 
3 #
4 
====================
Choose a row 1 - 4

Nim – game play

====================
1 #
2 
3 #
4 
====================
Choose a row 1 - 4 1
How many tokens to take, 1 to 3 (no more than are available) 1
====================
1 
2 
3 #
4 
====================
Choose a row 1 - 4 

Nim – game play

====================
1 #
2 
3 #
4 
====================
Choose a row 1 - 4 

Nim – game play

Choose a row 1 - 4 1
How many tokens to take, 1 to 3 (no more than are available) 1
====================
1 
2 
3 #
4 
====================
Choose a row 1 - 4 3
How many tokens to take, 1 to 3 (no more than are available) 1
Game over, the last player lost!

Game State Representation

====================
1 ####
2 ###
3
4 #
====================

Represented with the list [4,3,0,1]

Set up

  • Download tester code
  • Download code to play game
  • Open VS Code (or another editor of your choosing), open the folder where you have the previous two python files and create a nim.py file

Functions you need to write

  • create_game_state(size, token_max)
  • is_valid_move(game_state, row, takes)
  • update(game_state, row, takes)
  • draw_game_state(game_state)
  • is_over(game_state)

Add at the top of nim.py your name, and the name of any lab partners you worked with, as well as docstrings for each function.

Documentation

Remember to document your code!

Three ways to document your code:

  • Use good variable names - python prefers snake_case naming
  • in-line comments #
  • Docstrings - strings included as first lines of programs and functions (careful with indentation)