Rate my first C++ program!

Find below an uncompleted version of my text adventure game, the code below is just the basic engine. It is my first real C++ program, so i like to find out the following:

It's overall rating, good and bad practice within the world of Object programming and any pointers, hints, tips, etc...

#include
#include
#include
#include
using namespace std;

int x = 4, y = 1, loop = 1, r = 0;
int n = 0, e = 0, w = 0, s = 0;
int i;

void clrscr(void);

class decision
{
public:
decision();
~decision();
protected:
int i;
};
decision::decision()
{
//Constructors can accept arguments.
i = 1;

while (i == 1) {
if (( y == 1 ) && ( x == 4 )) {
cout << "room one" << endl;
r = 1, n = 1, e = 0, s = 0, w = 0;
break;
}

if (( y == 2 ) && ( x == 3 )) {
cout << "room two" << endl;
r = 2, n = 1, e = 1, s = 0, w = 0;
break;
}

if (( y == 2 ) && ( x == 4 )) {
cout << "room three" << endl;
r = 3, n = 1, e = 1, s = 1, w = 1;
break;
}

if (( y == 2 ) && ( x == 5 )) {
cout << "room four" << endl;
r = 4, n = 1, e = 0, s = 0, w = 1;
break;
}

if (( y == 3 ) && ( x == 2 )) {
cout << "room five" << endl;
r = 5, n = 1, e = 1, s = 0, w = 0;
break;
}

if (( y == 3 ) && ( x == 3 )) {
cout << "room six" << endl;
r = 6, n = 1, e = 1, s = 1, w = 1;
break;
}

if (( y == 3 ) && ( x == 4 )) {
cout << "room seven" << endl;
r = 7, n = 1, e = 1, s = 1, w = 1;
break;
}

if (( y == 3 ) && ( x == 5 )) {
cout << "room eight" << endl;
r = 8, n = 1, e = 1, s = 1, w = 1;
break;
}

if (( y == 3 ) && ( x == 6 )) {
cout << "room nine" << endl;
r = 9, n = 1, e = 0, s = 0, w = 1;
break;
}

if (( y == 4 ) && ( x == 1 )) {
cout << "room ten" << endl;
r = 10, n = 1, e = 1, s = 0, w = 0;
break;
}

if (( y == 4 ) && ( x == 2 )) {
cout << "room eleven" << endl;
r = 11, n = 1, e = 1, s = 1, w = 0;
break;
}

if (( y == 4 ) && ( x == 3 )) {
cout << "room twelve" << endl;
r = 12, n = 0, e = 1, s = 1, w = 0;
break;
}

if (( y == 4 ) && ( x == 4 )) {
cout << "room thirteen" << endl;
r = 13, n = 1, e = 1, s = 1, w = 1;
break;
}

if (( y == 4 ) && ( x == 5 )) {
cout << "room fourteen" << endl;
r = 14, n = 0, e = 0, s = 1, w = 1;
break;
}

if (( y == 4 ) && ( x == 6 )) {
cout << "room fifteen" << endl;
r = 15, n = 1, e = 1, s = 1, w = 0;
break;
}

if (( y == 4 ) && ( x == 7 )) {
cout << "room sixteen" << endl;
r = 16, n = 1, e = 0, s = 0, w = 1;
break;
}

if (( y == 5 ) && ( x == 1 )) {
cout << "room seventeen" << endl;
r = 17, n = 0, e = 1, s = 1, w = 0;
break;
}

if (( y == 5 ) && ( x == 2 )) {
cout << "room eighteen" << endl;
r = 18, n = 0, e = 0, s = 1, w = 1;
break;
}

if (( y == 5 ) && ( x == 4 )) {
cout << "room nineteen" << endl;
r = 19, n = 1, e = 0, s = 1, w = 0;
break;
}

if (( y == 5 ) && ( x == 6 )) {
cout << "room twenty" << endl;
r = 20, n = 0, e = 1, s = 1, w = 0;
break;
}

if (( y == 5 ) && ( x == 7 )) {
cout << "room twenty one" << endl;
r = 21, n = 0, e = 0, s = 1, w = 1;
break;
}

if (( y == 6 ) && ( x == 4 )) {
cout << "room twenty two" << endl;
r = 22, n = 1, e = 0, s = 1, w = 0;
break;
}

if (( y == 7 ) && ( x == 4 )) {
cout << "room twenty three" << endl;
r = 23, n = 1, e = 0, s = 1, w = 0;
break;
}

if (( y == 7 ) && ( x == 2 )) {
cout << "room twenty five" << endl;
r = 25, n = 1, e = 0, s = 0, w = 0;
break;
}

if (( y == 7 ) && ( x == 6 )) {
cout << "room twenty six" << endl;
r = 26, n = 1, e = 0, s = 0, w =0;
break;
}

if (( y == 8 ) && ( x == 4 )) {
cout << "room twenty four" << endl;
r = 24, n = 1, e = 0, s = 1, w = 0;
break;
}

if (( y == 8 ) && ( x == 2 )) {
cout << "room twenty seven" << endl;
r = 27, n = 0, e = 1, s = 1, w = 0;
break;
}

if (( y == 8 ) && ( x == 3 )) {
cout << "room twenty eight" << endl;
r = 28, n = 1, e = 0, s = 0, w = 1;
break;
}

if (( y == 8 ) && ( x == 5 )) {
cout << "room twenty nine" << endl;
r = 29, n = 1, e = 1, s = 0, w = 0;
break;
}

if (( y == 8 ) && ( x == 6 )) {
cout << "room thirty" << endl;
r = 30, n = 0, e = 0, s = 1, w = 1;
break;
}

if (( y == 9 ) && ( x == 2 )) {
cout << "room thirty one" << endl;
r = 31, n = 1, e = 1, s = 0, w = 0;
break;
}

if (( y == 9 ) && ( x == 3 )) {
cout << "room thirty two" << endl;
r = 32, n = 0, e = 1, s = 1, w = 1;
break;
}

if (( y == 9 ) && ( x == 4 )) {
cout << "room thirty three" << endl;
r = 33, n = 1, e = 1, s = 0, w = 1;
break;
}

if (( y == 9 ) && ( x == 5 )) {
cout << "room thirty four" << endl;
r = 34, n = 0, e = 1, s = 1, w = 1;
break;
}

if (( y == 9 ) && ( x == 6 )) {
cout << "room thirty five" << endl;
r = 35, n = 1, e = 0, s = 0, w = 1;
break;
}

if (( y == 10 ) && ( x == 2 )) {
cout << "room thirty six" << endl;
r = 36, n = 0, e = 0, s = 1, w = 0;
break;
}

if (( y == 10 ) && ( x == 4 )) {
cout << "room thirty seven" << endl;
r = 37, n = 1, e = 0, s = 1, w = 0;
break;
}

if (( y == 10 ) && ( x == 6 )) {
cout << "room thirty eight" << endl;
r = 38, n = 0, e = 0, s = 1, w = 0;
break;
}

if (( y == 11 ) && ( x == 4 )) {
cout << "room thirty nine" << endl;
r = 39, n = 0, e = 0, s = 1, w = 0;
break;
}
}
}
decision::~decision()
{
}
class keycheck
{
public:
keycheck();
~keycheck();
};
keycheck::keycheck()
{
int tr = 1;
int ch1, ch2;

while (tr == 1) {
cout << endl << "Enter a command: ";

ch1 = getch();
ch2 = 0;

if (ch1 == 0)
ch2 = getch();

if ((ch1 == 110) && (n == 1 )) {
cout << endl << "north" << endl;
y++, n--;
decision decision;
tr--;
}

if ((ch1 == 101) && (e == 1)) {
cout << endl << "east" << endl;
x++, e--;
decision decision;
tr--;
}

if ((ch1 == 115) && (s == 1)) {
cout << endl << "south" << endl;
y--, s--;
decision decision;
tr--;
}

if ((ch1 == 119) && (w == 1)) {
cout << endl << "west" << endl;
x--, w--;
decision decision;
tr--;
}

if (ch1 == 105)
cout << endl << "inventory";

if (ch1 == 102)
cout << endl << "fight";

if ((ch1 == 98) && (r == 0))
r++, tr--;

if (ch1 == 113) {
loop--;
break;
}
}
}
keycheck::~keycheck()
{
}
class description
{
public:
description();
~description();
};
description::description()
{
//Constructors can accept arguments.

clrscr();

if (r == 1) {
cout << "room one description.";
n = 1;
keycheck keycheck;
}

if (r == 2) {
cout << "room two description.";
keycheck keycheck;
}

if (r == 3) {
cout << "room three description.";
keycheck keycheck;
}
if (r == 4) {
cout << "room four description.";
keycheck keycheck;
}
if (r == 5) {
cout << "room five description.";
keycheck keycheck;
}
if (r == 6) {
cout << "room six description.";
keycheck keycheck;
}
if (r == 7) {
cout << "room seven description.";
keycheck keycheck;
}
if (r == 8) {
cout << "room eight description.";
keycheck keycheck;
}
if (r == 9) {
cout << "room nine description.";
keycheck keycheck;
}
if (r == 10) {
cout << "room ten description.";
keycheck keycheck;
}
if (r == 11) {
cout << "room eleven description.";
keycheck keycheck;
}
if (r == 12) {
cout << "room twelve description.";
keycheck keycheck;
}
if (r == 13) {
cout << "room thirteen description.";
keycheck keycheck;
}
if (r == 14) {
cout << "room fourteen description.";
keycheck keycheck;
}
if (r == 15) {
cout << "room fifteen description.";
keycheck keycheck;
}
if (r == 16) {
cout << "room sixteen description.";
keycheck keycheck;
}
if (r == 17) {
cout << "room seventeen description.";
keycheck keycheck;
}
if (r == 18) {
cout << "room eighteen description.";
keycheck keycheckk;
}
if (r == 19) {
cout << "room nineteen description.";
keycheck keycheck;
}
if (r == 20) {
cout << "room twenty description.";
keycheck keycheck;
}
if (r == 21) {
cout << "room twenty one description.";
keycheck keycheck;
}
if (r == 22) {
cout << "room twenty two description.";
keycheck keycheck;
}
if (r == 23) {
cout << "room twenty three description.";
keycheck keycheck;
}
if (r == 24) {
cout << "room twenty four description.";
keycheck keycheck;
}
if (r == 25) {
cout << "room twenty five description.";
keycheck keycheck;
}
if (r == 26) {
cout << "room twenty six description.";
keycheck keycheck;
}
if (r == 27) {
cout << "room twenty seven description.";
keycheck keycheck;
}
if (r == 28) {
cout << "room twenty eight description.";
keycheck keycheck;
}
if (r == 29) {
cout << "room twenty nine desciption.";
keycheck keycheck;
}
if (r == 30) {
cout << "room thirty description.";
keycheck keycheck;
}
if (r == 31) {
cout << "room thirty one description.";
keycheck keycheck;
}
if (r == 32) {
cout << "room thirty two description.";
keycheck keycheck;
}
if (r == 33) {
cout << "room thirty three description.";
keycheck keycheck;
}
if (r == 34) {
cout << "room thirty four description.";
keycheck keycheck;
}
if (r == 35) {
cout << "room thirty five description.";
keycheck keycheck;
}
if (r == 36) {
cout << "room thirty six description.";
keycheck keycheck;
}
if (r == 37) {
cout << "room thirty seven description.";
keycheck keycheck;
}
if (r == 38) {
cout << "room thirty eight description.";
keycheck keycheck;
}
if (r == 39) {
cout << "room thirty nine description.";
keycheck keycheck;
}

}
description::~description()
{
//Destructors do not accept arguments.
}

int main()
{
clrscr();

cout << "Welcome to the text adventure game!" << endl;
cout << endl << endl;
cout << "Command keys are:" << endl;
cout << "n = north" << endl;
cout << "e = east" << endl;
cout << "s = south" << endl;
cout << "w = west" << endl;
cout << endl << "i = inventory" << endl;
cout << "f = fight" << endl;
cout << "q = quit" << endl;
cout << endl << endl;
cout << "Press b to start";

keycheck keycheck;

while (loop == 1) {
description description;
}

}

Comments

  • [blue]
    1. Use code tags, so we can read the code.

    2. Too bulky - a lot of useless code - use maps instead of pile of IF statements. Notice patterns in code and try to minimize the code itself.

    3. Name your variables better than r,w,s...

    [/blue]
  • Two things,

    1) better named variables would help.

    2)Also I would suggest that you comment the code, especially the "hard" parts. Do things like explain why you are doing X to Y? what will the newly created variable do? other than that, better than my first C++ program was!!!:-) Try to comment in a way that a complete stranger to the code can understand it without much effort.
  • : [blue]
    : 1. Use code tags, so we can read the code.
    :
    : 2. Too bulky - a lot of useless code - use maps instead of pile of IF statements. Notice patterns in code and try to minimize the code itself.
    :
    : 3. Name your variables better than r,w,s...
    :
    : [/blue]
    :
    Thanks for you input. I also read the website cprogramming.com for help and quizies. But i can not find any reference to maps. What do you mean by this? Can you give me an example using my code?

    Thanks!
  • : : [blue]
    : : 1. Use code tags, so we can read the code.
    : :
    : : 2. Too bulky - a lot of useless code - use maps instead of pile of IF statements. Notice patterns in code and try to minimize the code itself.
    : :
    : : 3. Name your variables better than r,w,s...
    : :
    : : [/blue]
    : :
    : Thanks for you input. I also read the website cprogramming.com for help and quizies. But i can not find any reference to maps. What do you mean by this? Can you give me an example using my code?
    :
    : Thanks!
    :

    I can see now that i don't need a double up information for the rooms. Also i just found out about the switch command, which has help reduce the code even further.

    But if there is an easier way to do this, please let me know.

    Thanks!


  • [b][red]This message was edited by AsmGuru62 at 2006-5-6 5:44:29[/red][/b][hr]
    [blue]This code, for example:[/blue]
    [code]
    : if (( y == 1 ) && ( x == 4 )) {
    : cout << "room one" << endl;
    : r = 1, n = 1, e = 0, s = 0, w = 0;
    : break;
    : }
    [/code]
    [blue]Lets see what we have here. 'r' is a room number, if I am correct, so "room seven" will have (r==7).

    Also, to match to that number you use pair of coordinates: x,y. What if you can use a structure, like this one:
    [code]
    typedef struct {
    int x;
    int y;
    int n;
    int e;
    int s;
    int w;
    } ROOM_DEF;
    [/code]
    Then you make an array (a map) of these structures and lay them out in the order of rooms (and initialize them with the values from these monstrous IF statements), like so:
    [code]
    ROOM_DEF AllRooms [] = {
    // x y n e s w
    { 1, 4, 1, 0, 0, 0 }[red][b],[/b][/red]
    ... describe all rooms in that fashion.
    Make sure the last entry does [b]not[/b] have comma (RED) after it.
    Not all compilers can swallow that.
    };
    [/code]
    After this write a function to search for the room in that array using x,y coordinates. Once coordinates found in one of the structures - the rest of the structure members will automatically point out n,s,w,e variables:
    [code]
    void FindRoomFromXY (int x, int y)
    {
    ROOM_DEF* pRoom = AllRooms;
    int nRoomsInMap = sizeof (AllRooms) / sizeof (ROOM_DEF);

    for (int i=0; i < nRoomsInMap; i++, pRoom++)
    {
    if ((pRoom->x == x) && (pRoom->y == y))
    {
    // Room is found. Set the global variables:
    n = pRoom->n;
    s = pRoom->s;
    w = pRoom->w;
    e = pRoom->e;

    // Set the room number. Index starts with 0, and
    // room number starts with 1, so:
    r = i-1;
    return;
    }
    }
    // if this point reached - the x,y parameters are NOT in the map!
    }
    [/code]
    The size of the code is dramatically less than before and it is easier to read. AND to add a room is simply to add one structure in the map - just one line with values and you are done![/blue]


  • : [b][red]This message was edited by AsmGuru62 at 2006-5-6 5:44:29[/red][/b][hr]
    : [blue]This code, for example:[/blue]
    : [code]
    : : if (( y == 1 ) && ( x == 4 )) {
    : : cout << "room one" << endl;
    : : r = 1, n = 1, e = 0, s = 0, w = 0;
    : : break;
    : : }
    : [/code]
    : [blue]Lets see what we have here. 'r' is a room number, if I am correct, so "room seven" will have (r==7).
    :
    : Also, to match to that number you use pair of coordinates: x,y. What if you can use a structure, like this one:
    : [code]
    : typedef struct {
    : int x;
    : int y;
    : int n;
    : int e;
    : int s;
    : int w;
    : } ROOM_DEF;
    : [/code]
    : Then you make an array (a map) of these structures and lay them out in the order of rooms (and initialize them with the values from these monstrous IF statements), like so:
    : [code]
    : ROOM_DEF AllRooms [] = {
    : // x y n e s w
    : { 1, 4, 1, 0, 0, 0 }[red][b],[/b][/red]
    : ... describe all rooms in that fashion.
    : Make sure the last entry does [b]not[/b] have comma (RED) after it.
    : Not all compilers can swallow that.
    : };
    : [/code]
    : After this write a function to search for the room in that array using x,y coordinates. Once coordinates found in one of the structures - the rest of the structure members will automatically point out n,s,w,e variables:
    : [code]
    : void FindRoomFromXY (int x, int y)
    : {
    : ROOM_DEF* pRoom = AllRooms;
    : int nRoomsInMap = sizeof (AllRooms) / sizeof (ROOM_DEF);
    :
    : for (int i=0; i < nRoomsInMap; i++, pRoom++)
    : {
    : if ((pRoom->x == x) && (pRoom->y == y))
    : {
    : // Room is found. Set the global variables:
    : n = pRoom->n;
    : s = pRoom->s;
    : w = pRoom->w;
    : e = pRoom->e;
    :
    : // Set the room number. Index starts with 0, and
    : // room number starts with 1, so:
    : r = i-1;
    : return;
    : }
    : }
    : // if this point reached - the x,y parameters are NOT in the map!
    : }
    : [/code]
    : The size of the code is dramatically less than before and it is easier to read. AND to add a room is simply to add one structure in the map - just one line with values and you are done![/blue]
    :
    :
    :
    I did try something like what you have done here, but i couldn't get it to work. I will give this a try and let you know.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories