Towers of Hanoi with dynamic array and 2 classes

I am at my wits end at this point. I cannot get the contents of the dynamic array to stay set once I exit the function that sets all the contents for the peg. I have included the code here:
Header file for the pegs:
javascript:tx('
#ifndef PEG1_H
#define PEG1_H

#include <iostream>
#include <cstdlib>
#include <memory>

using namespace std;

class Peg
{
friend class Towers;
public:
Peg();
/* constructor inits the Peg
* with n rings. The diameter peg's
* rings are from one inch(on top) to
* nth inch(on the bottom). */
Peg(int n);
~Peg();
/* returns the number of rings on the peg. */
size_t many_rings()const;
size_t* initialize_Array(int);
size_t current_diameter(int index);
/* returns the value of the diameter of
* the top most ring. */
size_t top_diameter()const;
/* adds a new ring to the peg. */
void add_ring_to_top(int ring_diameter);
/* remove the topmost ring of the peg */
void remove_top_ring();
/* overload output operator to print the peg object
* along with its rings in a understable format. */
friend ostream& operator<<(ostream& os, const Peg& p);

private:
/* use appropriate data structure for the a pegs */
int noOfRings;
int ring_diameter;
size_t *arrayPointer;
int max;
};

#endif
')

The implementation file for the pegs:
javascript:tx('
#include "peg.h"
#include <iostream>

using namespace std;
Peg::Peg() //this is my default constructor, towers.cpp didn't like not having it even though the constructor I need is the
//second constructor with int n as input.
{
noOfRings = 0;
ring_diameter = 0;
int max = 64;
arrayPointer = new size_t[max];
}
Peg::~Peg() {
if (arrayPointer != NULL)
delete[] arrayPointer;
}

size_t Peg::current_diameter(int index) {
return arrayPointer[index - 1];
}
/* constructor inits the Peg
* with n rings. The diameter peg's
* rings are from one inch(on top) to
* nth inch(on the bottom). */
size_t* Peg::initialize_Array(int n) {
return new size_t[n];
}
Peg::Peg(int n) {
noOfRings = n;
size_t *tempArray = initialize_Array(n);
static_cast<size_t> (n);
int j = n; //this is so that I can set the contents of the array in reverse order for the size of the rings, making
//the array at array[0] equal to n while array[n] would equal 1;
for (int i = 0; i < n; i++) { //this is the first loop, it steps through the dynamic array from point 0 to point n-1
tempArray[i] = j;//this is setting the data contents of the array for the diameter of the rings
cout << tempArray[i] << endl;
j--;//decrementing j so that the contents are in reverse order of the array index
}
arrayPointer = tempArray;
}

/* returns the number of rings on the peg. */
size_t Peg::many_rings()const { //the number of rings should be kept track of by incrementing when rings are added and
//decrementing when rings are removed, therefore the value for many_rings() should just be noOfRings
return noOfRings;
}

/* returns the value of the diameter of
* the top most ring. */
size_t Peg::top_diameter()const { //this can either just return the contents of the array at the topmost index (minus 1)
//or be passed to an int variable to return the int variable
size_t tmp = arrayPointer[noOfRings-1];
return tmp;
}

/* adds a new ring to the peg. */
void Peg::add_ring_to_top(int ring_diameter) {
int tmp = noOfRings;
tmp++;
size_t* temp = new size_t[tmp]; // create new bigger array.
for (int i = 0; i < (tmp-1); i++) {
temp[i] = arrayPointer[i];
cout << temp[i] << endl;
}
temp[tmp - 1] = ring_diameter;
delete[] arrayPointer;
arrayPointer = temp;

// copy values to new array.
//this works by going to the next point in the array and inputting the contents as the ring diameter passed
//to the function
}

/* remove the topmost ring of the peg */
void Peg::remove_top_ring() {
int tmp = noOfRings;
tmp--;
size_t* temp = new size_t[tmp];
for (int i = 0; i < tmp; i++) {
temp[i] = arrayPointer[i];
}
delete[] arrayPointer;
arrayPointer = temp;
}

/* overload output operator to print the peg object
* along with its rings in a understable format. */
ostream& operator<<(ostream& os, const Peg& p) {
size_t *tmp = NULL;
size_t newNum;
size_t i= 0, j = 0;
size_t anotherTemp = 0;
newNum = p.noOfRings;
tmp = new size_t[newNum];
tmp = p.arrayPointer;
for (i = 0; i < newNum; i++) {
anotherTemp = tmp[i];
os << "test" << endl;
for (j = 0; j < anotherTemp; j++) {
if (j < 64) {
os << "*";
}
}
os << endl;
}
return os;
}
')


Last edited on
The header file for the tower game, with three calls to the peg class for the 3 pegs:

javascript:tx('
#ifndef TOWERS1_H
#define TOWERS1_H

#include <iostream>
#include "peg.h"

using namespace std;

class Towers : public Peg
{
public:
/* constructor inits the towers
* with n rings on the first peg and no rings
* on the others. The diameter of the first peg's
* rings are from one inch(on top) to
* nth inch(on the bottom). */
Towers();
Towers(size_t n);
/* returns the number of rings for the peg specified. */
size_t many_rings(int peg_number)const;
size_t currentPosition();
size_t current_diameter(int peg_Num, int index);
/* checks there are any rings on the peg specified,
* if true returns the value of the diameter of the
* top most ring, else returns 0. */
size_t top_diameter(int peg_number)const;
/* top ring is moved from start peg to end peg
* function also checks if move is legal or not
* and returns 1 if it is legal else returns 0. */
int move_ring(int start_peg, int end_peg);
/* checks if the Tower Of Hanoi problem is solved
* and returns 1 if solved else returns 0. */
int check_solved();
/* overload output operator to print the towers object
* in a understable format. */
friend ostream& operator<<(ostream& os, const Towers& t);

private:
/* use appropriate data structure for the storing 3 pegs */
//Hint: use the peg class (for storing the pegs and to implement the member functions of the tower class)
size_t positionOnBoard;
Peg peg1;
Peg peg2;
Peg peg3;
};

#endif
')

The Implementation file for the Towers
javascript:tx('
#include "towers.h"
#include "peg.h"

#include <iostream>

using namespace std;

Towers::Towers() {
positionOnBoard = 0;
peg1 = Peg(0);
peg2 = Peg(0);
peg3 = Peg(0);
}
/* constructor inits the towers
* with n rings on the first peg and no rings
* on the others. The diameter of the first peg's
* rings are from one inch(on top) to
* nth inch(on the bottom). */
Towers::Towers(size_t n) {
positionOnBoard = (n-1);
peg1 = Peg(n);
}

size_t Towers::current_diameter(int peg_Num, int index) {
int ring_size;
if (peg_Num = 1) {
ring_size = peg1.current_diameter(index);
}
else if (peg_Num = 2) {
ring_size = peg2.current_diameter(index);
}
else if (peg_Num = 3) {
ring_size = peg3.current_diameter(index);
}
else {
ring_size = 0;
}
return ring_size;
}

/* returns the number of rings for the peg specified. */
size_t Towers::many_rings(int peg_number) const {
if (peg_number == 1) {
return peg1.many_rings();
}
if (peg_number == 2) {
return peg2.many_rings();
}
if (peg_number == 3) {
return peg3.many_rings();
}
else {
return 0;
}
}

size_t Towers::currentPosition()
{
return positionOnBoard;
}
/* checks there are any rings on the peg specified,
* if true returns the value of the diameter of the
* top most ring, else returns 0. */
size_t Towers::top_diameter(int peg_number) const {
if (peg_number = 1) {
return peg1.top_diameter();
}
if (peg_number = 2) {
return peg2.top_diameter();
}
if (peg_number = 3) {
return peg3.top_diameter();
}
else {
return 0;
}
}

/* top ring is moved from start peg to end peg
* function also checks if move is legal or not
* and returns 1 if it is legal else returns 0. */
int Towers::move_ring(int start_peg, int end_peg) {
int tmp;
int ring_diameter;
if (start_peg = 1) {
ring_diameter = peg1.top_diameter();
if (end_peg != 2 && end_peg != 3) {
return 0;
}
else {
peg1.remove_top_ring();
if (end_peg = 2) {
tmp = peg2.top_diameter();
if (tmp < ring_diameter) {
return 0;
}
else {
peg2.add_ring_to_top(ring_diameter);
return 1;
}
}
else {
if (end_peg = 3) {
tmp = peg3.top_diameter();
if (ring_diameter < tmp) {
peg3.add_ring_to_top(ring_diameter);
return 1;
}
else
return 0;
}
else return 0;
}
}
}
else if (start_peg = 2) {
ring_diameter = peg2.top_diameter();
if (end_peg != 1 && end_peg != 3) {
return 0;
}
else {
peg2.remove_top_ring();
if (end_peg = 1) {
tmp = peg1.top_diameter();
if (tmp < ring_diameter) {
return 0;
}
else {
peg1.add_ring_to_top(ring_diameter);
return 1;
}
}
else {
if (end_peg = 3) {
tmp = peg3.top_diameter();
if (ring_diameter < tmp) {
peg3.add_ring_to_top(ring_diameter);
return 1;
}
else
return 0;
}
}
}
}
else if (start_peg = 3) {
ring_diameter = peg3.top_diameter();
if (end_peg != 1 && end_peg != 2) {
return 0;
}
else {
peg3.remove_top_ring();
if (end_peg = 1) {
tmp = peg1.top_diameter();
if (tmp < ring_diameter) {
return 0;
}
else {
peg1.add_ring_to_top(ring_diameter);
return 1;
}
}
else {
if (end_peg = 2) {
tmp = peg2.top_diameter();
if (ring_diameter < tmp) {
peg2.add_ring_to_top(ring_diameter);
return 1;
}
else return 0;
}
else return 0;
}
}
}
return 0;
}
/* checks if the Tower Of Hanoi problem is solved
* and returns 1 if solved else returns 0. */
int Towers::check_solved() {
int first;
int second;
first = peg1.many_rings();
second = peg2.many_rings();
if (first == 0 && second == 0) {
delete[] arrayPointer;
arrayPointer = NULL;
return 1;
}
else
return 0;
}

/* overload output operator to print the towers object
* in a understable format. */
ostream& operator<<(ostream& os, const Towers& t) {

os << "Peg 1: " << t.peg1 << t.peg1.many_rings() << endl;
os << "Peg 2: " << t.peg2 << t.peg2.many_rings() << endl;
os << "Peg 3: " << t.peg3 << t.peg3.many_rings() << endl;

return os;

}
')

Topic archived. No new replies allowed.