Thread: [Dev-C++] game logic problem
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Julio L. <ju2...@ya...> - 2001-10-21 00:58:14
|
I cant figure out why the last if statment in the
changeCells function causes a crash....need help
//towersofhanoi3.hpp
class Ring
{
public:
Ring()
{
thisRing= new char[26];
}
virtual ~Ring()
{
delete [] thisRing;
}
virtual void setRing(char
ring[26]){strcpy(thisRing, ring);}
virtual char* getRing() const{ return thisRing;}
private:
char *thisRing;
};
class groupRings: public Ring
{
public:
groupRings()
{
for(int i=0; i<11; i++)
pRings[i]= new Ring;
pRings[0]->setRing(" *3*
");
pRingsL[0]= 3;
pRings[1]->setRing(" **5**
");
pRingsL[1]= 5;
pRings[2]->setRing(" ***7***
");
pRingsL[2]= 7;
pRings[3]->setRing(" ****9****
");
pRingsL[3]= 9;
pRings[4]->setRing(" ****11*****
");
pRingsL[4]= 11;
pRings[5]->setRing(" *****13******
");
pRingsL[5]= 13;
pRings[6]->setRing(" ******15*******
");
pRingsL[6]= 15;
pRings[7]->setRing(" *******17********
");
pRingsL[7]= 17;
pRings[8]->setRing(" ********19*********
");
pRingsL[8]= 19;
pRings[9]->setRing(" *********21**********
");
pRingsL[9]= 21;
pRings[10]->setRing("
");
pRingsL[10]= 26;
}
~groupRings(){ delete [] pRings;}
char* getRings(unsigned short int i) const{ return
pRings[i-1]->getRing();}
int getRingL(unsigned short int i) const{return
pRingsL[i-1];}
private:
Ring *pRings[11];
int pRingsL[11];
};
class Board
{
public:
Board()
{
for(int column=0; column<3; column++)
for(int row=0; row<26; row++)
pbCell[column][row]= new char [26];
}
~Board()
{
delete [] pbCell;
}
void setCell(int column, int row, char ring[26]){
strcpy(pbCell[column-1][row-1], ring);}
char* getCell(int column, int row) const{return
pbCell[column-1][row-1];}
private:
char *pbCell[3][26];
};
//end towersofhanoi3.hpp
//towersofhanoi.cpp
#include <iostream.h>
#include <stdlib.h>
#include "towersofhanoi3.hpp"
void menu();
void infoTOH();
void aboutTOH();
void objectTOH();
void rulesTOH();
void demoGm();
void changeCells(unsigned short int, unsigned short
int, groupRings *, Board *);
void board4(Board *);
int main()
{
menu();
return 0;
}// end main()
void menu()
{
unsigned short int menuChoice;
bool exit= true;
Ring *pRings= new Ring;
Board *pBoard= new Board;
while(exit)
{
system("cls");
cout << "\n\n ***THE
TOWERS OF HANOI***\n\n\n";
cout << " (1)Info on THE
TOWERS OF HANOI\n";
cout << " (2)Demo\n";
cout << " (3)Play\n";
cout << " (4)Exit\n\n";
cout << " Enter Choice
#: ";
cin >> menuChoice;
switch(menuChoice)
{
case 1: infoTOH();
break;
case 2: demoGm();
break;
//case 3: play();
//break;
case 4: exit = false;
break;
default: exit = false;
break;
}//end switch()
}//end while()
delete pRings;
delete pBoard;
}//end menu()
void infoTOH()
{
unsigned short int infoChoice;
bool exit= true;
void (*infoFuncPntr)();
while(exit)
{
system("cls");
cout << "\n\n ***THE
TOWERS OF HANOI Info***\n\n\n";
cout << " (1) About
THE TOWERS OF HANOI\n";
cout << " (2) Object
of THE TOWERS OF HANOI\n";
cout << " (3) Rules of
THE TOWERS OF HANOI\n";
cout << " (4) Return
to Main Menu\n\n";
cout << " Enter
Choice #: ";
cin >> infoChoice;
switch(infoChoice)
{
case 1: infoFuncPntr= aboutTOH;
break;
case 2: infoFuncPntr= objectTOH;
break;
case 3: infoFuncPntr= rulesTOH;
break;
case 4: exit= false;
break;
default: exit= false;
break;
}//end switch()
if(exit != false)
infoFuncPntr();
//end if()
}//end while()
}//end infoTOH()
void aboutTOH()
{
system("cls");
cout << "\n\n ***About THE
TOWERS OF HANOI***\n\n\n";
cout << "The ancient puzzle of THE TOWERS OF HANOI
involves three pegs with numerous\n";
cout << "rings on the first peg. The rings are
arranged in increasing order of size,\n";
cout << "from top to bottm.\n\n";
system("PAUSE");
}
void objectTOH()
{
system("cls");
cout << "\n\n ***Object of
THE TOWERS OF HANOI***\n\n\n";
cout << "The objective of this puzzle is to move
all the rings from the third peg to\n";
cout << "the first peg in the original order that
they began in.\n\n";
system("PAUSE");
}
void rulesTOH()
{
system("cls");
cout << "\n\n ***Rules of
THE TOWERS OF HANOI***\n\n\n";
cout << "The rules are that only one ring may be
moved at a time and that a larger ring\n";
cout << "may never be placed on a smaller
ring.\n\n";
system("PAUSE");
}
void demoGm() //Shows how to win a four ring game
{
groupRings *pRings= new groupRings;
Board *pBoard= new Board;
for(int column= 1; column<3; column++)
for(int row= 1; row<11; row++)
pBoard->setCell(column, row,
pRings->getRings(11));
for(int column= 3; column<4; column++)
for(int row= 1, ring= 9; row<5; row++, ring--)
pBoard->setCell(column, row,
pRings->getRings(ring));
for(int column= 3; column<4; column++)
for(int row= 5; row<11; row++)
pBoard->setCell(column, row,
pRings->getRings(11));
system("cls");
cout << "\n\n
***Demo Game***\n\n\n";
cout << " *4 Ring
Game*\n";
cout << " *No Moves
Made*\n\n";
board4(pBoard);
system("PAUSE");
system("cls");
changeCells(3, 2, pRings, pBoard);
cout << "\n\n
***Demo Game***\n\n\n";
cout << " *4 Ring
Game*\n";
cout << " *One Move
Made*\n\n";
board4(pBoard);
system("PAUSE");
system("cls");
delete pRings;
delete pBoard;
}
void changeCells(unsigned short int fromColumn,
unsigned short int toColumn, groupRings * rRings,
Board *rBoard)
{
unsigned short int fromRow, toRow, fromRingNum,
toRingNum;
bool compareFrom, compareTo, findFromRing,
findToRing;
if(fromColumn != toColumn)
{
//Find first ring that is not empty in from
column
for(int i= 11; i>0; i--)
{
compareFrom=
strcmp(rBoard->getCell(fromColumn, i),
rRings->getRings(11));
if(compareFrom == 1)
{
fromRow= i;
break;
}//end if(compareFrom)
if(compareFrom == 0 && i == 1)
{
fromRow= 1;
}//end if
}//end for i
//Find first ring that is not empty in to
column
for(int i=11; i>0; i--)
{
compareTo=
strcmp(rBoard->getCell(toColumn, i),
rRings->getRings(11));
if(compareTo == 1)
{
toRow= i;
break;
}//end if(compareTo)
if(compareTo == 0 && i == 1)
{
toRow= 1;
}//end if
}//end for i
//Find the ring length of first non empty ring
in from column
for(int i=10; i>0; i--)
{
findFromRing=
strcmp(rBoard->getCell(fromColumn, fromRow),
rRings->getRings(i));
if(findFromRing == 0)
{
fromRingNum = i;
break;
}//end if(findFromRing)
}//end for i
//Find the ring length of first non empty ring
in to column
for(int i=10; i>0; i--)
{
findToRing=
strcmp(rBoard->getCell(toColumn, toRow),
rRings->getRings(i));
if(findToRing == 0)
{
toRingNum = i;
break;
}//end if(findToRing)
}//end for i
//If to column ring length is greater than the
from column ring length
//then move the ring from the from column to
the to column above
if(rRings->getRingL(toRingNum) >
rRings->getRingL(fromRingNum))
{
rBoard->setCell(toColumn, toRow+1,
rRings->getRings(fromRingNum));
rBoard->setCell(fromColumn, fromRow,
rRings->getRings(11));
}//end if(to ring > from ring)
}//end if(from != to)
}//end changeCells
void board4(Board *rBoard)
{
cout << "
-----------------------------------------------------------------------------\n";
cout << "| |
| |\n";
cout << "| | | |
| | |\n";
cout << "|" << rBoard->getCell(1,4) << "|" <<
rBoard->getCell(2,4) << "|" << rBoard->getCell(3,4) <<
"|\n";
cout << "| | | |
| | |\n";
cout << "|" << rBoard->getCell(1,3) << "|" <<
rBoard->getCell(2,3) << "|" << rBoard->getCell(3,3) <<
"|\n";
cout << "| | | |
| | |\n";
cout << "|" << rBoard->getCell(1,2) << "|" <<
rBoard->getCell(2,2) << "|" << rBoard->getCell(3,2) <<
"|\n";
cout << "| | | |
| | |\n";
cout << "|" << rBoard->getCell(1,1) << "|" <<
rBoard->getCell(2,1) << "|" << rBoard->getCell(3,1) <<
"|\n";
cout << "| | | |
| | |\n";
cout << "
-----------------------------------------------------------------------------\n\n";
}
=====
Julio
__________________________________________________
Do You Yahoo!?
Make a great connection at Yahoo! Personals.
http://personals.yahoo.com
|