Jump to content
  • Advertisement
Sign in to follow this  
NPHshreddar

Text Based RPG help

This topic is 2462 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ive been working through the Game Institute's C++ Module I and have completed almost all of the exercises except for adding multiple enemies to combat. Most of the features I did get operational I implemented in unorthodox ways(i.e. Store, Save Game, Load Game, and Random Encounter during rest)

What I'm looking for is some constructive criticism on how to improve the code and any tips on how to implement multiple enemies into the combat scenario. It advises to return a pointer to an array of enemies from Map::checkRandomCounter() then to update the player's attack function to accept an array of monsters. I can't seem to figure it out.

Also, attached is the code for the project.

EDIT: Went through the code again and fixed some bugs. I updated the .zip archive.

Share this post


Link to post
Share on other sites
Advertisement
Can someone please give me some tips on how to clean this up and make it more object-oriented?

Initially I tried to create an enter function within the store(i.e. store.enter(mainPlayer)) and pass a pointer to player into the function but that didn't work out as I am still struggling to grasp the concept of pointers.

Also random encounter during rest is pretty messy. I would have liked to create the the random encounter within the rest function but failed as I'm sure using pointers is the solution.

I'm not asking for someone to just hand me over a solution I would just like some tips on improvement and hopefully help grasping pointers and their mechanics. Thank you in advance.

Share this post


Link to post
Share on other sites
I'm a newbie or I would try to help you.

However, I've found using pastebin.com will make it easier for others to answer your question. Also, paste code here using the code tag. That might get you some answers! :)


EDIT: I'm playing your game now, by the way! :) Edited by Sharpe

Share this post


Link to post
Share on other sites
Sorry it took me so long to respond. I don't mean to bring up an old thread. I would just like to say thank you for your very educational post. It took me at least 5 reads to grasp it but I finally understand pointers and implemented them in my game loop, the result is a much clearer and more optimized one that I'm very grateful for. Thanks again.

Share this post


Link to post
Share on other sites
Went through the code today and cleaned it up a lot and commented it as much as I could to explain how everything works. Got the main source file game.cpp down to 96 lines.


// game.cpp
#include "Map.h"
#include "Player.h"
#include "Store.h"
#include "Display.h"
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
////////////INITIALIZE GLOBAL VARIABLES////////////////////
//Initialize the game map, the player, and the display which contains functions used to display the various menus throughout the game..
//Also initialize an input variable to navigate the different menus of the game.
Map gameMap;
Player mainPlayer;
Display display;
int selection = 0;
////////////////////BEGIN START MENU/////////////////////
//Display the Start menu and prompt the user to load a previous game or start a new game.
display.Splash();
display.StartMenu();
cin >> selection;
cin.ignore();
if(selection == 2) {
//Load Game.
mainPlayer.loadGame();
gameMap.loadPlayerPos();
}
else {
//Create New Player
mainPlayer.createClass();
}
/////////////////////END START MENU /////////////////////////
/////////////////////////////////////////////////////////////
////////////////////BEGIN GAME LOOP//////////////////////////
/*Initialize control variable for main game loop.
If this variable is set true the game loop will exit.*/
bool done = false;
while( !done ) {
//Clear the screen.
system("CLS");
selection = 0;
// Each loop cycle we output the player position and a selection menu.
display.MainMenu(gameMap);
cin >> selection;
// Test the selection. If Selection == valid. Game Loop keeps going.
// If Selection != valid. Game loop restarts and prompts user for another selection.
if(!display.testSelection(selection, 1, 6))
continue;
//Initialize monster and set it to zero, in case the combat simulation is ran.
Monster* monster = 0;
switch( selection ) {
case 1://Player Moves
//Update players position on the game map.
gameMap.movePlayer();
/*If the Player has moved to the stores location on the game map they enter it.
Here the player can purchase weapons, items, and spells.
When the player is finished at the store he exits and returns to the game loop.*/
if( gameMap.getPlayerXPos() == 2 && gameMap.getPlayerYPos() == 3)
{
//Initialize store and enter it.
Store store;
store.enter(mainPlayer);
}
//If the player isnt at the stores location then test for a combat encounter.
else {
if(mainPlayer.combatSimulation( monster, mainPlayer, gameMap) >= 1)
done = true;
}
break;
case 2://Player Rests
if(mainPlayer.rest(monster, mainPlayer, gameMap) >= 1)
done = true;
break;
case 3://View Stats
mainPlayer.viewStats();
break;
case 4://View Map Landmarks.
mainPlayer.viewLandmarks();
break;
case 5://Save Game
mainPlayer.saveGame(gameMap.getPlayerXPos(), gameMap.getPlayerYPos());
break;
case 6://Quit
done = true;
break;
}
}
}
/////////////////END GAME LOOP///////////////////////////


Anymore comments or suggestions would be great. I'm still having some trouble with implementing the multiple enemies.
I'm not sure how I should go about making it balanced. If the player faces 3 enemies, should he have 3 attack turns? I'm just not sure.

An updated version of the game is attached to this post.
NOTE: The dungeon is not operational. It is part of a quest line I'm going to be implementing.

Share this post


Link to post
Share on other sites
NPHshreddar, is it possible for you to put your project somewhere else? Because I can't download it for some reason (the first 5 downloads were my unsuccessful attempts).
With regards to the game.cpp you put on the forum, you probably shouldn't put srand in the game loop.

Share this post


Link to post
Share on other sites

NPHshreddar, is it possible for you to put your project somewhere else? Because I can't download it for some reason (the first 5 downloads were my unsuccessful attempts).
With regards to the game.cpp you put on the forum, you probably shouldn't put srand in the game loop.


External Download Link

There ya go. Its hosted on fileswap.

Also, I removed the srand() function from the game loop and put it into the random function so it is called every time random is. Next feature is a quest line with some side quests. I'll be adding more map landmarks as well. I think I'm gonna be doing a redesign of the view map landmarks function to make more accurate and dynamic map. Ive also been considering consolidating all of the display functions for the store into a single function withtin the display class. We'll see.

Share this post


Link to post
Share on other sites
You should not call srand inside the while loop. That will mess up the logic behind the random functions. Just call it once at the beginning of main.

If the player should get three turns against three enemies doesn't really make sense to me. If he got himself into a gang fight, he must deal with it biggrin.gif

You could give the player a bonus turn if he does something smart or something, but these things is entirely up to you, the game creator

Share this post


Link to post
Share on other sites

Also, I removed the srand() function from the game loop and put it into the random function so it is called every time random is.


Don't do this.
It should be called only once before you start pulling values from the rand function

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!