phfilly 26 Junior Poster

well just thought I might just help try doing this:
It gives you a random number each time

#include <iostream>
#include <cstdlib> //for the rand and srand function
#include <ctime> //for the time function

using namespace std;

int main()
{
      unsigned seed = time(0);
      srand(seed);
	
	cout << seed << endl;
	



}

just another way to do it...but It isn't implemented for a predefined range a.t.m

phfilly 26 Junior Poster

Thanks for the reply.

It is so simple to say it, but I struggle with programming. Sorry for the lines and lines of code, but please look through it and tell me if I'm at least in the right direction. And if you may, point out where improvements can be made or code be taken out.

I appreciate any reply.

phfilly 26 Junior Poster

I forgot to mention that the undo choice may only be used once, and the redo as well.
This code might help you to help me... :) I'm not that good in C++(or programming for that matter), so if there are dumb mistakes, please tell me about it.

void LayoutMemento::CurrentLayout(Piece*** currentBoard)
{
	ListNode *newNode;
	ListNode *nodePtr;

	newNode = new ListNode;

	newNode->piece = currentBoard;
	newNode->next = NULL;

	if(!head)
	{
		head = newNode;
	}
	else
	{
		nodePtr = head;

		Piece*** tempCurrent;
		Piece*** currentCopy;

		currentCopy = currentBoard;

		for(int i = 0; i < 9; i++)
		{
			for(int j = 0; j < 9; i++)
			{
				if(currentBoard[i][j] != NULL)
				{
					tempCurrent[i][j] = currentCopy[i][j];
				}
			}
		}

		while(nodePtr->next)
		{
			nodePtr = nodePtr->next;
		}

		nodePtr->next = newNode;
	}
}

void LayoutMemento::display() const
{
	ListNode *nodePtr;

	nodePtr = head;

	while(nodePtr)
	{
		cout << nodePtr->piece << endl;
		nodePtr = nodePtr->next;
	}
}

void LayoutMemento::getNext(Piece*** nextBoard)
{
	Piece*** tempNext;
	Piece*** nextCopy;

	nextCopy = nextBoard;

	for(int i = 0; i < 9; i++)
	{
		for(int j = 0; j < 9; i++)
		{
			tempNext[i][j] = nextCopy[i][j];
		}
	}
}

void LayoutMemento::getPrevious(Piece*** previousPiece)
{
	Piece*** tempPrevious;
	Piece*** previousCopy;

	previousCopy = previousPiece;

	for(int i = 0; i < 9; i++)
	{
		for(int j = 0; j < 9; i++)
		{
				tempPrevious[i][j] = previousCopy[i][j];
		}
	}
}
phfilly 26 Junior Poster

Hi. I would like help on undoing and redoing using the memento design pattern. I want to save a state that a board is in. Like a chessboard. If the user uses the undo option a previous board should be loaded and all of the states that the pieces was in. And the redo option should redo a previous move. I'm in quite a hurry to finish this project. This is about all I still have to do. Any help or suggestions on how to do it would be appreciated.

phfilly 26 Junior Poster

It seems after thoroughly testing the value of Selected, it wasn't assigned the correct values! The Xin etc. values passed were incorrect. Thanx alot for the help

phfilly 26 Junior Poster

I've had a look at what you meant. To make it more clear :) (sorry, I have a way of stating things unclear sometimes). The point variable "Selected" is only a pointer to an element of the array, thus it need not be initialized, if I recall correctly?
Furthermore, when the program runs through the array, the nature of the program guarantees that at least one of the elements will be equal to the coordinates of the 9x9 matrix, that's why I'm so confused :/

phfilly 26 Junior Poster

Thanks for your input, i'll try that! I have verified that the two arrays have correct values, jap they do!

phfilly 26 Junior Poster

Hey everyone, I'm having abit of trouble with a seg fault. As said in the topic, the piece of code that gives the error isn't really working with an array.
I ran the program through the gdb tool in g++, and it says the the seg fault occurs in the following function:

bool Board::ValidMove(int _y,char _x,Piece &MovingPiece)
{
	if (MovingPiece.validateMove(_y,_x))
	{
		if ((_x >= 'a' && _x <= 'i') && (_y >= 0 && _y <= 8))
		{
			return true;
		}
	}
	
	return false;
}

If it helps, this is the piece of code that calls this function:

void Board::Move(char Xin,char Xto, int Yin,int Yto)
{
	Piece* Selected;
	
	for(int Row = 0;Row < 9;++Row)
	{
		for(int Col = 0;Col < 9;++Col)
		{
			if (Player1Pieces[Col]->Y == Yin && Player1Pieces[Col]->X == Xin)
			{
				Selected = Player1Pieces[Col];
			}
			else if (Player2Pieces[Col]->Y == Yin && Player2Pieces[Col]->X == Xin)
			{
				Selected = Player2Pieces[Col];
			}
		}
	}
	
	if (ValidMove(Yto,Xto,*Selected))
	{
		Selected->X = Xto;
		Selected->Y = Yto;
	}
	else
		cout << "Invalid move, try again." << endl;
}

"Player1Pieces" and "Player2Pieces" are double-pointer(**) arrays.

Would be nice if someone could help me continue with my program :)

phfilly 26 Junior Poster

Hey, our specific error was resolved, the #include "FootSoldier.h" had a spelling error!
Thanks for your trouble! And sorry for the inconvenience! :D

phfilly 26 Junior Poster

Yeah the program is still in it's early stages, so there are a few things that can fall away, like the inheritance Piece has from Spellcaster and functions like getLife().Sorry for the inconvenience on that.

Footsoldier.C

#include "FootSoldier.h"
#include "Piece.h"

FootSoldier::FootSoldier(int _side,char _x,int _y) : Piece(6,_side,'F')
{
	Movements = new Reach(Symbol);
	Current->X = _x;
	Current->Y = _y;
}

Piece.C

Piece::Piece(int _life,int _side,char _symbol)
{
	Side = _side;
	Life = _life;
	Symbol = _symbol;
}
phfilly 26 Junior Poster

Just to have you understand better, the base class is

Piece.h :

#ifndef PIECE_H
#define PIECE_H

#include "Coordinate.h"
#include "Reach.h"
#include "SpellCaster.h"

#include <iostream>

using namespace std;

class Piece : public Spellcaster
{
	protected:
		int Life;
		int Side;
		char Symbol;
		Reach* Movements;
	
	public:
		Piece();
        	Piece(int,int,char);
        	~Piece();
		int getLife();
		int getIntX();
		Coordinate* Current;
		friend ostream &operator << (ostream& , const Piece& );
		virtual void CastSpell();
};

#endif

Footsoldier.h:

#ifndef FOOTSOLDIER_H
#define FOOTSOLDIER_H
#include "Piece.h"

class FootSoldier : public Piece
{
	public:
		FootSoldier(int,char,int);
};

#endif

The 'CastSpell()' function is really the only aspect that changes in the derived classes.Some can cast spells and others can't(like the FootSoldier).

phfilly 26 Junior Poster

board.h

public:
Piece** Player1Pieces;
Piece** Player2Pieces;

board.C

Player1Pieces = new Piece*[18];
Player2Pieces = new Piece*[18];
 
char arrX[9] = {'a','b','c','d','e','f','g','h','i'};
 
for (int FootCount = 0; FootCount < 9; ++FootCount)
{
--->>	Player1Pieces[FootCount] = new FootSoldier(1,arrX[XCount],1); <<---
Player2Pieces[FootCount] = new FootSoldier(2,arrX[XCount],7)

this is how I edited the code, though I still get exactly the same error. Am I missing something?

phfilly 26 Junior Poster

Thanks for the reply. I'll take a look at it now thanks!

phfilly 26 Junior Poster

sorry that smiley is not supposed to be there!

phfilly 26 Junior Poster

Hey just wondering if someone could help me please with the following:

board.h

public:
Piece* Player1Pieces;
Piece* Player2Pieces;

(a section showing the initialization of the arrays)

board.C:

Player1Pieces = new Piece[18];
Player2Pieces = new Piece[18];

char arrX[9] = {'a','b','c','d','e','f','g','h','i'};

for (int FootCount = 0; FootCount < 9; ++FootCount)
{
--->>	Player1Pieces[FootCount] = new FootSoldier(1,arrX[XCount],1); <<---
Player2Pieces[FootCount] = new FootSoldier(2,arrX[XCount],7)

(this is where I initialize the objects to the specific type I want)

The error:
(between the arrows)

Piece.h:13: note: candidates are: Piece& Piece::operator=(const Piece&)
board.C:28: error: expected `;' before ‘FootSoldier’
board.C:29: error: expected type-specifier before ‘FootSoldier’
board.C:29: error: no match for ‘operator=’ in ‘*(((Board*)this)->Board::Player2Pieces + ((unsigned int)(((unsigned int)FootCount) * 24u))) = (int*)operator new(4u)’

The pointer to the array of objects is thus specified in the .h file, and initialized in the .C file, but I get these weird errors?


Any help would be very much appreciated!

phfilly 26 Junior Poster

ah man...I hate such small mistakes

Thanks for the quick help!

phfilly 26 Junior Poster

hi guys

I got this weird error which I cannot debug in my code. The program is to convert Roman numerals to Arabic numerals using the Interpreter design pattern. Here is a part of the program where the errors occurs

RNInterpreter.C:8: error: new types may not be defined in a return type
RNInterpreter.C:8: note: (perhaps a semicolon is missing after the definition of ‘Thousand’)
RNInterpreter.C:8: error: two or more data types in declaration of ‘RNInterpreter’

I know what the error means but I can't seem to find it.The default constructor doesn't have a return type where I made objects of the classes but the error occurs there

any help will be appreciated!Thanks

#include "RNInterpreter.h"
#include "Thousand.h"
#include "One.h"
#include "Hundred.h"
#include "Ten.h"


RNInterpreter::RNInterpreter(){
	thousands = new Thousand(1);
	hundreds = new Hundred(1);
	tens = new Ten(1);
	ones = new One(1);
}

which is the implementation file of this header file(only part of the file is shown above):

#ifndef RNINTERPRETER_H
#define RNINTERPRETER_H

#include <cstring>

class RNInterpreter
{
	public:
		RNInterpreter();
		RNInterpreter(int);
		int interpret(char*);
		virtual void interpret(char* input,int&total);

	private:
		RNInterpreter* thousands;
		RNInterpreter* hundreds;
		RNInterpreter* tens;
		RNInterpreter* ones;

	protected:
		char one();
		char* four();
		char five();
		char* nine();
		int multiplier();

}; 
#endif
phfilly 26 Junior Poster

maybe try http://sourcemaking.com/design_patterns
it helped me alot

phfilly 26 Junior Poster

Well it certainly helped me...7 years later :)