Question

I am creating a towers of hanoi game board using c#, and am having trouble understanding...

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:

  • A Class Library project for the Towers class.
  • A UnitTest Project for the required unit tests.

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:

  • Either from or to contain a value other than 1, 2 or 3: “Invalid tower number.”
  • from and to are equal: “Move cancelled.”
  • from pole has no discs: “Tower {from} is empty.”
  • The disc on top of the from pole is larger than the disc on top of the to pole: “Top disc of tower {from} is larger than top disc on tower {to}.

ToArray()

int[][]

Returns a jagged array representation of all three poles as follows:

  • In dimension 1, each element corresponds to a pole.
  • In dimension 2, each element corresponds to a disc on a pole. For each pole, dimension 2 is an integer array of each disc stack, where the first element of the stack (if there is one) is the smallest disc in the stack.

Homework Answers

Answer #1

[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): ");
}
}
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
I have a recursive Tower of Hanoi program but don't know how to include the count...
I have a recursive Tower of Hanoi program but don't know how to include the count variable. Could you tell me how? Main.java import java.io.*; import java.util.List; public class Main { //int count = 0; /**There is a stack of N disks on the first of three poles (call them A, B and C) and your job is to move the disks from pole A to pole B without ever putting a larger disk on top of a smaller disk.*/...
Homework Draw class diagrams for your HW4 - the Tetris Game shown below: Part 1: UML...
Homework Draw class diagrams for your HW4 - the Tetris Game shown below: Part 1: UML As a review, Here are some links to some explanations of UML diagrams if you need them. • https://courses.cs.washington.edu/courses/cse403/11sp/lectures/lecture08-uml1.pdf (Links to an external site.) • http://creately.com/blog/diagrams/class-diagram-relationships/ (Links to an external site.) • http://www.cs.bsu.edu/homepages/pvg/misc/uml/ (Links to an external site.) However you ended up creating the UML from HW4, your class diagram probably had some or all of these features: • Class variables: names, types, and...
I've posted this question like 3 times now and I can't seem to find someone that...
I've posted this question like 3 times now and I can't seem to find someone that is able to answer it. Please can someone help me code this? Thank you!! Programming Project #4 – Programmer Jones and the Temple of Gloom Part 1 The stack data structure plays a pivotal role in the design of computer games. Any algorithm that requires the user to retrace their steps is a perfect candidate for using a stack. In this simple game you...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT