Assignment 4 CS0008 Fall 2013

Introduction

This assignment will focus on manipulation of nested lists, while focusing on the flow of a simple (yet complete) game.

Tower of Hanoi

The Tower of Hanoi is a puzzle game that deals with the movement of large structures ("towers") from one location to another. The problem goes as follows: you are given a single tower, which is made up of, say, three blocks. Each block is a different size, and they are stacked on top of one another so that the largest is on the bottom and the smallest is on top. Including the location where this original tower is built, you have three locations which are also conducive to building towers. The goal of the puzzle is to fully move the tower from one location ("peg") to another location. The catch, however: you may only move one block at a time, and that block must be from the top of the peg (ie, you cannot move a block that has another block on top of it). Additionally, when you move a peg, you may only move it to a location where there are no blocks, or to a location where the blocks there are larger (ie, every location must follow the largest-to-smallest rule as well). If you've never played before, try beating the game here, or at least test it to cement your understanding (a good thing to keep in mind is that is is possible; just frustrating at first!).

For assignment 4, you will be programming the Tower of Hanoi game. Begin by downloading hanoi.py.txt. The flow and structure of the program have been provided for you, but the code that makes the game function has not been — that's where you come in.

Do take note, however, that you have been given a function printBoard. This function can be used (at any time) to print out the game board in a simple (yet stylized) manner. You are welcome to look at the code, but you are not required to understand it; additionally, modifying the code within printBoard is not allowed (your code needs to adhere to the rules of the program).

The game board

As you might have guessed, the game board is the nested list that you will be working extensively with. The 'main' list (the list that holds the sublists) will be referred to as the "board," while each nested list (sublist) within the board will be referred to as a "peg." It is your job to write the code that creates and manipulates this, but you will need to follow the following convention:

  1. There should be only three pegs within any board.
  2. Integers will be used to represent the different blocks.
  3. Blocks start with size 1 and increment without skipping any integers (ie, if there are four blocks, they should be sizes 1, 2, 3, and 4).
  4. Pegs must have their blocks sorted in the proper order at all times, with the smallest block in the front and the largest block in the back (ie, a peg must always be [1, 2, 3], never [2, 1, 3] or [2, 3, 1] or so on).
  5. Pegs should only have blocks; they may not have "filler" values (more on this a moment).

The first four points likely make more sense than the fifth convention. Allow us to explain. Consider the following two examples of valid boards:

[ [1, 2, 3], [], [] ]
[ [2, 3], [], [1] ]

Notice how, when moving the smallest block from the first peg to the last peg, the list that represents the first peg "shrank" from [1, 2, 3] to [2, 3]. By "filler" value, we mean that the "empty locations" within each peg are not filled with 0, None, or some other piece of data that we interpret as being semantically empty. This way of representing the game board cannot be changed for the assignment.

Also, the number of blocks that are created can be variable, but there will always only be three pegs.

Your game (or, what to do)

Within hanoi.py.txt, you are given the headers of a number of functions, as well as docstrings. These functions are your responsibility to write, so that they perform the actions they are intended to. main has been provided as-is, to demonstrate how the different pieces work together. There are seven functions, then, that you are required to write for this assignment:

  1. createBoard(size)
  2. getNumBlocks(board)
  3. isTowerComplete(board, pegNo)
  4. isGameComplete(board)
  5. isValidMove(board, fromPeg, toPeg)
  6. makeMove(board, fromPeg, toPeg)
  7. inputInt(prompt)

The output of a complete program will look like this. Each function that you will write will have its own, unique purpose (two functions won't be doing the same thing), so make sure to adhere to the docstrings! You are allowed to create additional functions if you want to, though they aren't required (all the necessary functions are provided for you); if you do create additional functions, write your own docstrings so that we know what your function does!

As a final note, the output from printBoard includes integers above each peg. These numbers are the indeces of each peg within the board (ie, board[0] = the first peg), and are the numbers requested when prompted "From/To which peg?" Keep in mind that inputInt can return the numbers -1 and 3, but that while these are integers, they aren't valid peg positions; make sure that the (hint!) appropriate function detects that.

Grading

These are the aspects of your work that we will focus on in the grading:

What to Hand In

Hand in the following file: