× Home (CS 211) Notes Projects Waxman's Website

Purpose

The goal is to move all of the disk from one tower to another tower, with the following rules:

This project will be using vectors. This project must work for both even and odd numbers of disk. One way of doing this is either using an if else condition or have two separate functions, one for even rings and other for odd.

Data Representation

Use an array of integer vectors, each vector is representing one of the towers, and the last element in the vector is the top of the tower.
The numbers in the vectors represent the disks. The larger the number the larger the disk. The number on the bottom of the vector or tower is NOT representing any disks; that number is there as a padding so when we call the back function on an “empty” tower it will not crash the program.
This program will use 4 variables:

Algorithm

Move the disk on the top of the from tower to the top of the to tower

Calculate the new "from", "to", and "candidate" for the next move:

from
Compare the disk from the top of the two towers that is not the "to" tower using (to+1)%3 or (to+2)%3. These two disks are the disks that have not just been moved. Check which one is smaller and the tower with the smaller disk is the new "from" tower.
candidate
The disc on top of the "from" tower
to
The new "to" tower is the closest tower on which the candidate can be placed. If the number of disks is odd, the closest tower is (from+1)%3. Or (from+2)%3 (left) if the number of disks is even

Rinse and Repeat until the second tower or t[1] contains all of the disks

Resource

Template
Output

This is to be submitted on blackboard as a .cpp file. File name follows the following format: lastName_firstName.cpp