I am creating a towers of hanoi game board using c#, and am having trouble understanding my instructors instructions. So far I have an outline setup as follows:
using System;
namespace Towers
{
public class Towers
{
int numberOfDiscs; //Number of discs to be place on the leftmost
pole (between 1 and 9)
int numberOfMoves; //Number of moves executed thus far
bool isComplete; //True if all discs have been succesfully moved to
the righmost pole
int minimumPossibleMoves;
public Towers(int numberOfDiscs)
{
this.numberOfDiscs = numberOfDiscs;
}
public void Move(int from, int to)
{
}
public int[][] ToArray()
{
}
}
}
My question is where do I instantiate my three stacks and how do I create an array of said stacks? I feel like I am missing something.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Please ignore the unit tests part as i understand how to do that.
Phase 1 – Build the basic Towers class and associated unit tests
The Towers class is an abstract representation of the Tower of Hanoi game board. Just as the board has three poles on which discs can be stacked, the Towers class includes three stack data structures. Each stack corresponds to a pole. See the detailed class specification for the Towers class below.
Each of the stacks is a .NET generic Stack. You do not have to build your own custom stack ADT for this assignment.
There is no user experience required in this phase, but you must demonstrate that all of this plumbing you’re building to support the game works. To that end, you’ll build unit tests to validate the Towers class.
At the end of this phase you should have a VS solution with 2 projects:
Towers specification
This class is an abstract implementation of the Tower of Hanoi game board. Its main internal element is an array of Stack objects, but those are never seen by callers directly. Here are the elements of its public interface.
Class name: Towers
Constructor
Signature |
Description |
Towers(int numberOfDiscs) |
Initializes a Towers object for a game where numberOfDiscs indicates how many discs will be placed on the leftmost pole at the beginning of the game. numberOfDiscs must be an integer with a value between 1 and 9 inclusive. Otherwise an InvalidHeightException is thrown. Each game disc is itself represented by an integer. 1 represents the smallest disc, 2 the next larger, and so on through numberOfDiscs. |
Properties
Property name |
Data type |
Description |
NumberOfDiscs |
int |
The number of discs in the current Towers object as specified by the constructor. |
NumberOfMoves |
int |
Number of moves executed thus far. |
IsComplete |
bool |
True if all discs have been successfully moved to the rightmost pole. |
MinimumPossibleMoves |
int |
The minimum number of moves required to solve of puzzle with NumberOfDiscs discs. |
Methods
Signature |
Returns |
Description |
Move(int from, int to) |
void |
Moves the top disc from the from pole to the to pole. Poles are numbered from left to right: 1, 2 and 3. Thus, the object of the game is to move all discs from pole 1 to pole 3 while following the game’s rules. if all discs are on pole 3, IsComplete is set to true. Otherwise, it is false. The following conditions result in the Move method throwing an InvalidMoveException with the indicated error string:
|
ToArray() |
int[][] |
Returns a jagged array representation of all three poles as follows:
|
[NOTE: I was not clear with toArray() method. so, didn't included. But the remaining methods are included along with some additional methods that will make your project more effective. I hope you find it helpful.]
SOLUTION :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hanoi
{
class Towers
{
private int moves;
private int nDisc;
private Peg[] pegs;
// Constructor :
// takes the number of discs ( nDisc ) in the game.
// 1. Creates 3 pegs
// 2. Creates n discs with size 1, 2, 3 etc. and pushes them to peg
0
// 3. Sets Console size to Map.MaxLeft and Map.MaxTop
// 4. Push the discs in Pegs[0]
public Towers(int numberOfDiscs)
{
pegs = new Peg[3];
pegs[0] = new Peg(numberOfDiscs, Map.PegLeft[0], Map.PegTop,
Map.PegBot);
pegs[1] = new Peg(numberOfDiscs, Map.PegLeft[1], Map.PegTop,
Map.PegBot);
pegs[2] = new Peg(numberOfDiscs, Map.PegLeft[2], Map.PegTop,
Map.PegBot);
for (int i = numberOfDiscs-1, index = 0; i >= 0; i--,
index++)
{
pegs[0].Push(new Disc(i+1, Map.PegLeft[0], Map.PegBot - index,
Map.DiskColors[i]));
}
Console.SetWindowSize(Map.MaxLeft, Map.MaxTop);
this.nDisc = numberOfDiscs;
moves = 0;
}
// Computer solve the game
public void ComputerPlays(int discs, int src, int aux, int
dst)
{
if(discs > 0)
{
ComputerPlays(discs - 1, src, dst, aux);
Move(src, dst);
ComputerPlays(discs - 1, aux, src, dst);
}
}
// Moves top disk of Pegs[src] to Pegs[dst] if
// IF src and dst are in [0...2] and
// Pegs[src] has a disc and
// (Pegs[dst] is empty or
// top disc in Pegs[dst] > top disc in Pegs[src])
// returns true if the move was done
// Does not redraw the board
public bool Move(int from, int to)
{
try
{
// update movements number
moves++;
// proceed with moves
if (from >= 0 && from <= 2 && to >= 0
&& to <= 2 && pegs[from].DiscCount() >
0)
{
if(pegs[to].Push(pegs[from].Peek()))
{
pegs[to].Peek().Move(Map.PegLeft[to], Map.PegBot -
pegs[to].DiscCount()+1);
pegs[from].Pop();
return true;
}
}
return false;
}
catch
{
return false;
}
}
// Prints msg to Map.MsgLeft, Map.MsgTop location
// in yellow text on dark red background
public void Message(string msg)
{
Console.BackgroundColor = ConsoleColor.DarkRed;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.SetCursorPosition(Map.MsgLeft, Map.MsgTop);
Console.Write(msg);
Console.ResetColor();
}
// Draws the followings:
// 0. Clear the screen first
// 1. Base line from <Map.Baseleft, Map.BaseTop> to
// <Map.BaseRight, Map.BaseTop>
// 2. Three pegs and the discs in them
// 3. Number of moves at <Map.MovesLeft, Map.MovesTop>
public void Draw()
{
Console.Clear();
// create the base
Console.SetCursorPosition(Map.BaseLeft, Map.BaseTop);
for(int i = Map.BaseLeft; i <= Map.BaseRight; i++)
{
Console.Write("=");
}
// create the pegs
for(int i = 0; i < pegs.Length; i++)
{
pegs[i].Draw();
}
// create moves
Console.SetCursorPosition(Map.MovesLeft, Map.MovesTop);
Console.Write("Moves: {0}", moves);
}
// Checks if the game is over,
// i.e. right peg has all the discs
public bool Win()
{
return (pegs[2].DiscCount() == nDisc);
}
// Prints source peg prompt "Src peg (1,2,3,q):"
// at position <Map.SrcLeft, Map.SrcTop>
public void SrcPegPrompt()
{
Console.SetCursorPosition(Map.SrcLeft, Map.SrcTop);
Console.Write("Src peg (1,2,3,q): ");
}
// Prints destination peg prompt "Dst peg (1,2,3,q): "
// at position <Map.DstLeft, Map.DstTop>
public void DstPegPrompt()
{
Console.SetCursorPosition(Map.DstLeft, Map.DstTop);
Console.Write("Dst peg (1,2,3,q): ");
}
}
}
Get Answers For Free
Most questions answered within 1 hours.