Text Based Adventure Classes

Text Based Adventure Classes

Oct 30, 2018. | By: Evan Diamond
2 mins

For my class system in my text based adventure I have three main ones. These are Location, Item, and Player which all inherit from my game object class. Location == The class that is a location in the game. The game stores an array of these for the whole map The location class stores:

  1. A pointer to a item class - the item in the room (nullptr if there is none)
  2. The name of the room
  3. An array of 4 directions enum which hold the directions the player can move from that location (N, E, S, W, NONE)
  4. It’s location index (ID)

Item

The class that is an item in the game. The item class stores:

  1. It’s the verb to use the item as an enum
  2. The item’s name
  3. The item’s description of what happens when it is used correctly
  4. The index of the room it is used in
  5. The score it gives the player when they take it
  6. If the location has been discovered or not.

Player

The class that is a player in the game. The player class stores:

  1. An array of item pointers which is the players inventory
  2. The current score of the player
  3. A location pointer which is the current location of the player.
  4. The current index of the locations array where the player is.

Player Movement

When the player whishes to move to a different location they must enter the direction they whish to go. This is inputted as a string and is converted to the directions enum prevoiusly mentioned. This direction is then passed into a movePlayer function in the player and compared to the array of directions within the location stored within the player. If the player can move in that direction the movePlayer function returns the change in index needed to move in that direction. For example, if the player wanted to go east and they could then movePlayer would return 1 which would get added onto the player’s location array index making them go to the next location in the index (or east). If the player cannont go in that direction the game tells the user this and their location is not changed.

Using Items

When the player wants to use an item they input the item’s name and correspoding verb as strings. The verb is then converted to the verb enum and these are passed into the useItem function in the player class. The name of the item is checked with the names of the items the player has in their inventory and the item is returned if found. Next, the item’s use index is checked with the player’s location’s index to see if the player is using the item in the correct room. Thirdly, the verb passed in is checked with the item’s verb to see if the user is giving the right verb. If any of these checks fail the user is notified of where they failed and the function returns false. Otherwise the description of the item is rendered and the function returns true so that the use of the item can be performed. [hampden]: https://github.com/jekyll/jekyll

About

Gamers developing new games.

Social Links

Our Bunker

University of the West of England,
Frenchay Campus, Bristol, BS16 1QY,
United Kingdom.