Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Help compacting code for simple text RPG


  • Please log in to reply

#1
Remludar

Remludar

    Member

  • Member
  • PipPip
  • 13 posts
So here it is... I just wrote my first piece of game type code in C++. It's just a 3x3 tiled room (text only) and all you can do is walk around and bump into the walls... but a pretty big accomplishment for me, as this is my 3rd week of learning C++.

Here's my question:

Below, find my code. I'm having trouble finding compact and efficient ways to compare strings stored by the user into an array. You'll see it plainly in the "DetermineVerb" and "DetermineAdj" functions.
Any suggestions will be greatly appreciated.



#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include <iomanip.h>


enum ActionType {WALK};                         //enum type for lable
enum DirectionType {EAST, NORTH, SOUTH, WEST};  //enum type for lable


void PrintHeader();
void GetVerb(char[]);
void DetermineVerb(char[], char[], int&, int&, ActionType&, DirectionType&, int&);
void GetAdj(char[]);
void DetermineAdj(char[], ActionType&, DirectionType&, int&, int&);
void CheckWalk(int&, DirectionType&, int&);

int main()
{
      typedef int Boolean;      ////////////////////////////////////////////////
      Boolean quit = false;     //
      Boolean walkOK = false;   //
      char verb[20];            //            variable
      char adj[20];             //              declarations
      int currPosition = 9;     //
      ActionType action;        //
      DirectionType direction;  ////////////////////////////////////////////////

      PrintHeader();

      while (!quit)
       {
        GetVerb(verb);
        DetermineVerb(verb, adj, quit, currPosition, action, direction, walkOK);
       }


      cout<<endl<<endl;
      system("PAUSE");
      return 0;
}


//******************************************************************************
//This function is self-explanatory
//******************************************************************************
void PrintHeader()
{
  cout<<"        ****************************************************************"<<endl;
  cout<<"                   This is a 3 x 3 tiled room."<<endl<<endl;
  cout<<"                  -------------------------------"<<endl;
  cout<<"                  |         |         |        |"<<endl;
  cout<<"                  |   1     |   2     |   3    |    Commands:"<<endl;
  cout<<"                  |         |         |        |    --------"<<endl;
  cout<<"                  ------------------------------    WALK NORTH"<<endl;
  cout<<"                  |         |         |        |    WALK EAST"<<endl;
  cout<<"                  |   4     |   5     |   6    |    WALK SOUTH"<<endl;
  cout<<"                  |         |         |        |    WALK WEST"<<endl;
  cout<<"                  ------------------------------    QUIT"<<endl;
  cout<<"                  |         |         |        |"<<endl;
  cout<<"                  |   7     |   8     |   9    |"<<endl;
  cout<<"                  |         |         |        |"<<endl;
  cout<<"                  ------------------------------"<<endl<<endl;
  cout<<"        ****************************************************************"<<endl<<endl;
}


//******************************************************************************
// Prompt user for verb string
//******************************************************************************
void GetVerb(char verb[])
{
      cout<<"Enter command: ";
      cin>>verb;
}


//******************************************************************************
// Use "switch" to interpret verb string by first 2 letters (i need help here)
//******************************************************************************
void DetermineVerb(char verb[],
                   char adj[],
                   int& quit,
                   int& currPosition,
                   ActionType& action,
                   DirectionType& direction,
                   int& walkOK)
{
   switch (verb[0])
   {
    case 'W': switch (verb[1])
              {
               case 'A': action = WALK;
                         GetAdj(adj);
                         DetermineAdj(adj, action, direction, currPosition, walkOK);
                         break;
              }
              break;

    case 'Q': quit = true;
              break;

    default : cout<<"not a verb"<<endl;
              break;
   }
}


//******************************************************************************
// Prompt user for adjective string
//******************************************************************************
void GetAdj(char adj[])
{
   cin>>adj;

}


//******************************************************************************
// Use "switch" and "if" to interpret adjective string (need help here too)
//******************************************************************************
void DetermineAdj(char adj[],
                  ActionType& action,
                  DirectionType& direction,
                  int& currPosition,
                  int& walkOK)
{
   switch (adj[0])
   {
    case 'E': switch (adj[1])
              {
               case 'A': if (action == WALK)
                           {
                            direction = EAST;
                            CheckWalk(currPosition, direction, walkOK);
                            if (walkOK)
                              currPosition++;     //adjust position in matrix
                            else
                              {
                               cout<<setw(45)<<"--------------------"<<endl;
                               cout<<setw(45)<<"***You hit a wall***"<<endl;
                               cout<<setw(45)<<"--------------------"<<endl;
                              }
                           }
                         break;
              }
              break;

    case 'N': switch (adj[1])
              {
               case 'O': if (action == WALK)
                           {
                            direction = NORTH;
                            CheckWalk(currPosition, direction, walkOK);
                            if (walkOK)
                              currPosition -= 3;    //adjust position in matrix
                            else
                              {
                               cout<<setw(45)<<"--------------------"<<endl;
                               cout<<setw(45)<<"***You hit a wall***"<<endl;
                               cout<<setw(45)<<"--------------------"<<endl;
                              }
                           }
                         break;
              }
              break;

    case 'S': switch (adj[1])
              {
               case 'O': if (action == WALK)
                           {
                            direction = SOUTH;
                            CheckWalk(currPosition, direction, walkOK);
                            if (walkOK)
                              currPosition += 3;   //adjust position in matrix
                            else
                              {
                               cout<<setw(45)<<"--------------------"<<endl;
                               cout<<setw(45)<<"***You hit a wall***"<<endl;
                               cout<<setw(45)<<"--------------------"<<endl;
                              }
                           }
                         break;
              }
              break;

    case 'W': switch (adj[1])
              {
               case 'E': if (action == WALK)
                           {
                            direction = WEST;
                            CheckWalk(currPosition, direction, walkOK);
                            if (walkOK)
                              currPosition--;     //adjust position in matrix
                            else
                              {
                               cout<<setw(45)<<"--------------------"<<endl;
                               cout<<setw(45)<<"***You hit a wall***"<<endl;
                               cout<<setw(45)<<"--------------------"<<endl;
                              }
                           }
                         break;
              }
              break;
   }
}


//******************************************************************************
// Checks for collision
//******************************************************************************
void CheckWalk(int& currPosition,
               DirectionType& direction,
               int& walkOK)
{
   switch (currPosition)
   {
    case 1: if (direction == EAST || direction == SOUTH)
              walkOK = true;
            else
              walkOK = false;
            break;

    case 2: if (direction == EAST || direction == SOUTH || direction == WEST)
               walkOK = true;
            else
               walkOK = false;
            break;

    case 3: if (direction == SOUTH || direction == WEST)
              walkOK = true;
            else
              walkOK = false;
            break;

    case 4: if (direction == EAST || direction == NORTH || direction == SOUTH)
              walkOK = true;
            else
              walkOK = false;
            break;

    case 5: walkOK = true;
            break;

    case 6: if (direction == NORTH || direction == SOUTH || direction == WEST)
              walkOK = true;
            else
              walkOK = false;
            break;

    case 7: if (direction == EAST || direction == NORTH)
              walkOK = true;
            else
              walkOK = false;
            break;

    case 8: if (direction == EAST || direction == NORTH || direction == WEST)
              walkOK = true;
            else
              walkOK = false;
            break;

    case 9: if (direction == NORTH || direction == WEST)
              walkOK = true;
            else
              walkOK = false;
            break;
   }
}


//******************************************************************************

  • 0

Advertisements


#2
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
here are some URLs for strcmp():

http://www.cprogramm...fod/strcmp.html
http://www.glenmccl.com/perf_001.htm
http://www.cplusplus...ing/strcmp.html
http://msdn.microsof...2c_._mbscmp.asp
  • 0

#3
Remludar

Remludar

    Member

  • Topic Starter
  • Member
  • PipPip
  • 13 posts
Thanks. That helped a bit. I was able to replace this:

void DetermineAdj(char adj[],
                  ActionType& action,
                  DirectionType& direction,
                  int& currPosition,
                  int& walkOK)
{
   switch (adj[0])
   {
    case 'E': switch (adj[1])
              {
               case 'A': if (action == WALK)
                           {
                            direction = EAST;
                            CheckWalk(currPosition, direction, walkOK);
                            if (walkOK)
                              currPosition++;     //adjust position in matrix
                            else
                              {
                               cout<<setw(45)<<"--------------------"<<endl;
                               cout<<setw(45)<<"***You hit a wall***"<<endl;
                               cout<<setw(45)<<"--------------------"<<endl;
                              }
                           }
                         break;
              }
              break;

    case 'N': switch (adj[1])
              {
               case 'O': if (action == WALK)
                           {
                            direction = NORTH;
                            CheckWalk(currPosition, direction, walkOK);
                            if (walkOK)
                              currPosition -= 3;    //adjust position in matrix
                            else
                              {
                               cout<<setw(45)<<"--------------------"<<endl;
                               cout<<setw(45)<<"***You hit a wall***"<<endl;
                               cout<<setw(45)<<"--------------------"<<endl;
                              }
                           }
                         break;
              }
              break;

    case 'S': switch (adj[1])
              {
               case 'O': if (action == WALK)
                           {
                            direction = SOUTH;
                            CheckWalk(currPosition, direction, walkOK);
                            if (walkOK)
                              currPosition += 3;   //adjust position in matrix
                            else
                              {
                               cout<<setw(45)<<"--------------------"<<endl;
                               cout<<setw(45)<<"***You hit a wall***"<<endl;
                               cout<<setw(45)<<"--------------------"<<endl;
                              }
                           }
                         break;
              }
              break;

    case 'W': switch (adj[1])
              {
               case 'E': if (action == WALK)
                           {
                            direction = WEST;
                            CheckWalk(currPosition, direction, walkOK);
                            if (walkOK)
                              currPosition--;     //adjust position in matrix
                            else
                              {
                               cout<<setw(45)<<"--------------------"<<endl;
                               cout<<setw(45)<<"***You hit a wall***"<<endl;
                               cout<<setw(45)<<"--------------------"<<endl;
                              }
                           }
                         break;
              }
              break;
   }
}

with this:

void DetermineAdj(char adj[],
                  ActionType& action,
                  DirectionType& direction,
                  int& currPosition,
                  int& walkOK,
                  int& quit)
{
   switch (action)
   {
     case WALK:   if (strcmp(adj, VOCAB_ADJ_EAST) == 0)
                   {
                    direction = EAST;
                    CheckWalk(currPosition, direction, walkOK);
                    if (walkOK)
                     currPosition++;     //adjust position in matrix
                    else
                     PrintWallCollide();
                   }

                  else if (strcmp(adj, VOCAB_ADJ_NORTH) == 0)
                   {
                    direction = NORTH;
                    CheckWalk(currPosition, direction, walkOK);
                    if (walkOK)
                     currPosition -= 3;     //adjust position in matrix
                    else
                     PrintWallCollide();
                   }

                  else if (strcmp(adj, VOCAB_ADJ_SOUTH) == 0)
                   {
                    direction = SOUTH;
                    CheckWalk(currPosition, direction, walkOK);
                    if (walkOK)
                     currPosition += 3;     //adjust position in matrix
                    else
                     PrintWallCollide();
                   }

                  else if (strcmp(adj, VOCAB_ADJ_WEST) == 0)
                   {
                    direction = WEST;
                    CheckWalk(currPosition, direction, walkOK);
                    if (walkOK)
                     currPosition--;     //adjust position in matrix
                    else
                     PrintWallCollide();
                   }

                  else
                    cout<<"\""<<adj<<"\" not an adjective"<<endl<<endl;

                   break;

     case QUIT:   if (strcmp(adj, VOCAB_ADJ_GAME) == 0)
                   quit = true;
                  else
                   cout<<"\""<<adj<<"\" not an adjective"<<endl<<endl;
                  break;
   }
}

it's a bit more compact and clean. I wish there was a way to use a switch statement with a string. Is there any way to do this, or any equivelant technique?
Thanks again for the help.

Edited by Remludar, 21 July 2005 - 02:24 PM.

  • 0

#4
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
the expression for a switch is an integer. a string won't work. see switch info

here's a general design using a for loop
store VOCAB_ADJ_xxx in an array
store EAST, North, SOUTH, WEST in an array
store 1, -3, 3, -1 in an array

for( ... ){
if(strcmp(adj, vocab[i]) == 0){
direction = dir[i];
position += pos[i];
}
}

Edited by bdlt, 22 July 2005 - 09:04 AM.

  • 0

#5
Remludar

Remludar

    Member

  • Topic Starter
  • Member
  • PipPip
  • 13 posts
Awesome. That makes so much more sense. I took your advice & I made everything fit into one command with a struct.

I have a new problem now, though... heh.

I'm reading in a set of commands from a file into a struct array. The commands have a space in them, so I used "cin.get()" to both read them in from file, and to read the user command in.

Here's the problem. It works great for just one iteration, but on the next iteration the cin.get() just uses the same input instead of prompting the user to input a new command.

Here's the question:
How do I empty out the array that the user inputs to if I have used cin.get() to read it in?

#include <iostream.h>
#include <stdlib.h>
#include <fstream.h>

const int NUM_COMMANDS = 5;
const int COMMANDS_LENGTH = 20;


struct CommandStruct
{
 char command[COMMANDS_LENGTH];
};
CommandStruct knownCommands[NUM_COMMANDS];


void InitializeCommands(ifstream&, int, char);
void GetUserCommand(char[]);
void CheckKnownCommands(char[], int, int&);

int main()
{

      typedef int Boolean;
      Boolean known = false;
      Boolean quit = false;
      ifstream CommandsFile;
      CommandsFile.open("files/commands.dat");
      char userCommand[COMMANDS_LENGTH];
      char dummy;
      int counter;


      InitializeCommands(CommandsFile, counter, dummy);
      while (!quit)
       {
        GetUserCommand(userCommand);
        CheckKnownCommands(userCommand, counter, known);
       }

      cout<<endl<<endl;
      system("PAUSE");
      return 0;
}


//******************************************************************************
void InitializeCommands(ifstream& CommandsFile,
                        int       counter,
                        char      dummy)
{
    for (counter = 0; counter < NUM_COMMANDS; counter++)
      {
       CommandsFile.get(knownCommands[counter].command, COMMANDS_LENGTH);
       CommandsFile.get(dummy);
      }
}


//******************************************************************************
void GetUserCommand(char userCommand[])
{
     cout<<"Enter command: ";
     cin.get(userCommand, COMMANDS_LENGTH);
}


//******************************************************************************
void CheckKnownCommands(char userCommand[],
                        int  counter,
                        int&  known)
{
     for (counter = 0; counter < NUM_COMMANDS; counter++)
        if (strcmp(userCommand, knownCommands[counter].command) == 0)
          {
           known = true;
           cout<<"It worked"<<endl<<endl;
          }
     if (known == false)
        cout<<"Unknown Command"<<endl;
}


//******************************************************************************

  • 0

#6
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
useCommand[] below is only a copy of userCommand[]. try passing a pointer to userCommand into GetUserCommand().

void GetUserCommand(char userCommand[])
{
    cout<<"Enter command: ";
    cin.get(userCommand, COMMANDS_LENGTH);
}

  • 0

#7
Remludar

Remludar

    Member

  • Topic Starter
  • Member
  • PipPip
  • 13 posts
Well...after reading a bit on pointers and not understanding how to pass a pointer to an array in a function:

EDIT:

I realized all I had to do was 'eat' the newline character to get it out of the input stream.

I remembered that arrays, when passed to a function, are passed as reference values allready... so I knew that a pointer wasn't my solution.

Thanks for all of the help... learned about pointers anyway.

Edited by Remludar, 22 July 2005 - 06:58 PM.

  • 0

#8
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
you're welcome. nice job on the newline character.
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP