Starting from:

$25

ISCG6421 - GUI Programming - Assignment 2 - Towers of Hanoi - Solved

The assignment is intended to help you gain experience with designing and building C# Windows applications and testing them with requirements-based testing.

 

Problem statement 

 

The Towers of Hanoi game is a very old game where you have a board with three pegs on it. A set of differently sized disks are placed on the first peg with the disks being in order with the smallest on the top. The game is to transfer the disks from the first peg to the third by moving only one disk at a time, only taking disks from the top of any pile and never putting a larger disk on top of a smaller disk. The player tries to do this in as few moves as possible.

 

Requirements

 

You are provided with a C# starter project that you must use for this assignment. You are to complete the game so that it:

·         Uses four disks and three pegs and lets the user drag and drop disks from one peg to another.

·         Informs the user that the game is over when all of the disks have been transferred to the third peg.

Informs the user whether or not the game has been completed in the minimum number of moves.
·         Enforces the rules stated above.

·         Keeps count of the number of moves.

·         Allows the user to begin a new game.

·         Stores valid moves and moves target.

·         Allows games (complete or incomplete) to be stored.

·         Allows the running of stored (incomplete) games.

·         Allows a completed game to be played from a list of stored moves using animation controlled by a timer.

·         Uses the following classes:

A Disk class to store data about the disks
A Board class to control what happens to the disks
A DiskMove class for saving moves
A MainForm class to control the game
 

Disk Class

 

The disk class should store the disk’s diameter, colour, current level and current peg number. The disk class should have methods that allow access to the data items.

 

Board Class

 

The board class should have the following data storage and be responsible for the following operations:

 

Data

 

It stores the constants needed to handle the display of disks

It has a two dimensional array of Disk references that represents the board

It has a List for storing the moves made

It has a one dimensional array of 4 Disk references for the Disk objects used in the game.

 

The constructor takes 4 disk references as parameters

     public Board(Disk d1, Disk d2, Disk d3, Disk d4, int movesTarget)

The 4 Disk references in the constructor are to be used to fill the array of Disk objects belonging to the Board object. The integer parameter is set by the user to determine the number of moves they wish to complete the game in. The program should provide a default value.

 

Operations

 

The Board class can: 

Reset the game to the beginning.
Allow the user to select a number of targeted moves at the beginning of a new game.
Check if it is valid to begin a move with a particular Disk (only the top Disk on a peg can move). public bool CanStartMove(Disk aDisk)
Check if it is valid to drop a particular disk on a Peg (drops are only allowed for a Disk that is smaller than the top Disk on a peg or for an empty peg).
 public bool CanDrop(Disk aDisk, int aPeg)

Move a disk to a new Peg.
public void Move(Disk aDisk, int newLevel)   

Save a DiskMove object representing the latest move to the ArrayList of moves. This would be part of the Move() method.
Return a string giving the moves so far, one move per line.
public string AllMovesAsString()

Display the current position of the disks. This is done by changing the Top and Left properties of the disks which in turn changes where the labels show on the screen.
public void Display()   

Get a reference to the disk that matches a label. This is to be used to find which disk object is being dragged on the form.
public Disk FindDisk(Label aLabel)  

Check if the number of moves a user has made exceeds the set target. The program should change the colour scheme of the User Interface and show a (friendly) message.
public void SetGameTheme(bool hasExceededTarget)

 

 


MainForm Class

 

The MainForm class can:

Have private global references for a Board object, the Disk being dragged and the Peg that is the target of the drop.
Have a [Reset] button that creates 4 Disk objects matching the 4 labels and a Board object, and then position the Disks in the starting position.
Check (by asking the Board object) if a move can start when the user does a MouseDown on a disk label and give an error message if it cannot.
Check (by asking the Board object) if a drop can happen on a Peg and only show the AllowDrop cursor when the mouse is over a Peg if a drop is valid.
Display a count of the moves made and show the moves made in a textbox, one line per move.
 

DiskMove Class

 

The DiskMove class can:

·         Store the index of the disk making the move and the new Peg it is dropped on.

·         Have an AsText () method that gives this information as a string, e.g. “1,2” means that Disk 1 moved to peg 2.

·         Have two constructors, one that takes two integers (for disk index and peg) as parameters, the other takes a string in the form “2,1” as its parameter.

 

Required Tests

 

This is the minimum number of tests you need in your testing documentation:

 

The program displays the message “You have successfully completed the game with the minimum number of moves” when all of the disks have been transferred to the third peg with the minimum number of moves.
Interface is displayed correctly when the program runs
Valid moves are stored
Uses four disks and three pegs and lets the user drag and drop disks from one peg to another.
Keeps count of the number of moves.
The program displays the message “You have successfully completed the game but not with the minimum number of moves” when all of the disks have been transferred to the third peg with more than the minimum number of moves.
Enforces the rules
Try moving more than one disk at a time
Try moving a disk at the bottom of a pile
Try moving a disk second from top of a pile
Try putting a larger disk on top of a smaller disk.
Begin a new game after a game has been started
Number of moves set to zero
Disks moved back to starting positions
Load an incomplete stored game and finish it.
Play a completed game from a list of stored moves using animation controlled by a timer.
 

Please note that your tests can involve more than one step and must be reproducible (i.e. explicit test data and user actions) and independent of each other (i.e. please do not use the output of one test as the input to another test.)

 

 


 

 

Test Cases

 

Please using the following format for your test cases

 

Requirement to test
Test Data Input
Expected Outcomes
Actual Outcomes
 
 
 
 
 

 

Save Points
 
 




 

Part
Marks
Actual
Interface behaviour, and data processing
 
 
·         Interface set up correctly (matches screenshot – see page 8)
10
 
·         Uses four disks and three pegs and lets the user drag and drop disks from one peg to another.
 

8
 
·         Enforces the rules of the game
15
 
·         Keeps count of the number of moves.
5
 
·         Allows the user to begin a new game.
5
 
·         Allows the running of stored moves.
5
 
·         Allows the game to be played from a list of stored moves using animation controlled by a timer.
10
 
·         Uses the following classes:

A Disk class to store data about the disks.
A Board class to control what happens to the disks.
A DiskMove class for saving moves.
A MainForm class to control the game.


 

 

Programming Standards for C Sharp Courses



Internal Documentation

 

Your code is such that other programmers can read it without struggling and your users are not left guessing as to what to do.
·         Each class file (including form classes) will begin with comments explaining the purpose of the class, the author and the date written.

Each method will start with comments that explain what the method does.
Any code which does not have an obvious meaning or which uses a specialized technique is to be commented. Use blank lines and further comments to identify where parts of a task begin within a method.
Code will use meaningful variable, class and method names. Components which have event handler code for any of their events must have meaningful names, Components which have properties assigned to in code must also have meaningful names. A naming convention that identifies the type of component involved is recommended. E.g. btnExit, txtStartDate, lblTotal
Layout

 

·         Code will be laid out in the style of the example below, using indentation steps of 4 spaces. Blocks using { and } will use the layout shown here:

 

      ///<Summary> method : btnLeapYear_Click

      ///Check if a date falls in a leap year

      ///</Summary>

private void btnLeapYear_Click(object sender, System.EventArgs e)

      {

          DateTime aDate = getDate();

          if ( DateTime.IsLeapYear(aDate.Year) )

          {

              label6.Text = aDate.Year.ToString() + " IS a leap year";

          }

          else

          {

              label6.Text = aDate.Year.ToString()  

+ " is NOT a leap year";

          }

      }

 

·         Parentheses and spaces will be used to make the meaning clear in arithmetic expressions and conditions.

    sum = (n1 / n2) + n3; 

not 

    sum = n1 / n2 + n3; 

nor

    sum=n1/n2+n3;

 

In general, each method will perform a single simple task.  
In the final version of your project please delete all sections of code that has been ‘commented out’
 

User Interface

 

Always provide the user with clear instructions explaining what they should do. Areas used for input must be labelled to explain what input is required. Use hints or tool tips to explain interface features.
 

The user must be prevented from entering values or taking actions that the program is unable to deal with. All input should be validated; any errors found should be reported back to the user with an error message which clearly and politely explains how to correct the error. The user should be unable to proceed without correcting invalid input.

 

Interface Design Basic Mock-up: Screenshot

More products