Sign in to follow this  
bam104

Where to go from here?

Recommended Posts

Hey, I have been reading up on C++ for a while, and last night i finally got off my ass and programmed my first non-tutorial program. Here it is:
#include <iostream>
#include <string>
using namespace std;

int main ()
{
    bool key = 0;
    int gold = 0;
    int xp = 0;
    int lvl = 0;
    string exit;
    string move;
    string name;
    
    cout << "--------------------------- " << endl;
    cout << "Welcome to the Redmage Rpg! " << endl;
    cout << "--------------------------- " << endl;
    cout << "A Terror Visual C++ Game" << endl;
    cout << "By Dan Lodge " << endl;
    cout << "--------------------------- " << endl;
    cout << "" << endl;
    cout << "Where would you like to go?" << endl;
    cout << " t = Tavern" << endl;
    cout << " d = Dungeon" <<endl;
    cout << "" << endl;
    cout << "Move: "; 
    cin >> move;
    
    if (move == "t") {
             cout << "" << endl;
             cout << "You moved to the Tavern" <<endl;
             cout << "" <<endl;
               cout << "--------------------------- " << endl;
             cout << "The Blue Mermaid is a rough establishment, its wooden floor" <<endl;
             cout << "creaks as you enter but no one looks up." <<endl;
             cout << "--------------------------- " << endl;
             cout << "" <<endl;
             cout << "What would you like to do?" << endl;
             cout << " s = Speak with the Barkeep" <<endl;
             cout << " t = Go to a table" <<endl;
             cout << "" << endl;
             cout << "Move: "; 
             cin >> move;
             }
             
             if (move == "s") {
                      cout << "" << endl;
                       cout << "--------------------------- " << endl;
                      cout << "The Barkeep is short an stout,he looks you up" <<endl;
                      cout << "and down as you approach then smiles his black" <<endl;
                      cout << "tooth grin." <<endl;
                      cout << "--------------------------- " << endl;
                      cout << "" << endl;
                      cout << "d = Ask about the Dungeon" <<endl;
                      cout << "k = Ask about the Key" <<endl;
                      cout << "" << endl;
                      cout << "Ask: ";
                      cin >> move;
             
             }
             if (move == "k") {
                      cout << "" << endl;
                      cout << "--------------------------- " << endl;
                      cout << "oh the key to the dungeon? its right here." <<endl;
                      cout << "--------------------------- " << endl;
                      cout << "" << endl;
                      cout << "Key Attained!" <<endl;
                      key = 1; 
                      cout << "" << endl;
                      cout << "Press z to go to the Dungeon: " <<endl;
                      cin >> move;
                      }
                      
                      if (move == "z") {
                                cout << "" << endl;
                               cout << "Well done! you have reached the dungeon" <<endl;
                               cout << "and slain the dragon!" <<endl;
                                cout << "" << endl;
                                 cout << "Press X to Exit" <<endl;
                                 cin >> exit;
                 }
                      
                      
               
             
             else
             {
                 cout << "You do not have the key!" <<endl;
                 cout << "Press X to Exit" <<endl;
                 cin >> exit;
                 }
    
    std::cin.get();             
    return 0;
}
Whilst its got the nasty bug that you can type any letter and it do something. I am pretty pleased with it. Actually coding something from scratch is somthing i have been itching to do. But now that i have done it, what do i do next? I used most of what i know in this program (bar Arithmetical operators) and so you can tell what kind of lvl im at. Basically im looking for suggestions on what i should do next. Any ideas?

Share this post


Link to post
Share on other sites
Hi,

So far so good. If you're looking to improve your C++ skills and ability, you should probably look into defining classes, inheritance, polymorphism, and namespaces. The book you're supposedly using looks quite sound. As far as what game to program, I'll leave that to somebody else.

Regards,

GCS584

EDIT: Use 'else if' constructs after the first 'if'; otherwise, the code will check with every 'if' (only if the first 'if' statement is a match) when it's clearly not relevant.

Share this post


Link to post
Share on other sites
Nice job on your first solo program. Congrats! So, did you want to move onto graphics or stay with text-based games? Either way, practicing using the things that gcs584 mentioned (classes, etc.) is a good idea.

Share this post


Link to post
Share on other sites
This inspired me to write a really simple choose your own adventure engine.

It is really primitive. Like really primitive.

The first thing I'd add is "trivial room transfers". When you enter a room, the command "" should be checked, and if there is such a command, executed before querying the user.

This would let you have a "enter the room" description that isn't repeated whenever you do something in the room. =) Either that, or teach the game about the difference between entering the room and not entering the room!

Oh, and it makes a copy of the entire game state every round. I did this so I could avoid using STL iterators, and instead use []. To be efficient, one should not copy the room_map every turn, and use room_map.find() instead of room_map[].

Enjoy:


#include <string>
#include <map>
#include <iostream>

using std::cin;
using std::cout;

typedef std::string command;
typedef std::string room_name;

struct Result {
std::string description;
bool goes_anywhere;
bool is_error;
room_name destination;
Result(): goes_anywhere(false), is_error(true) {}
static Result Make() {
Result retval;
retval.is_error = false;
return retval;
}
};

struct Room {
std::string description;
std::map< command, Result > options;
bool game_over;
bool is_error;
Room(): game_over(false), is_error(true) {}
static Room Make() {
Room retval;
retval.is_error = false;
return retval;
}
};

typedef std::map< room_name, Room > room_map_type;

void AddBurp(Room& room);

void AddFirstRoom(room_map_type& room_map) {
Room first_room = Room::Make();
first_room.description = "You are in a small room\nYou see a door to the south\n";

command cmd1 = "south";
Result rslt1 = Result::Make();
rslt1.description = "You go through the door.\n";
rslt1.goes_anywhere = true;
rslt1.destination = "victory";
first_room.options[cmd1] = rslt1;

AddBurp(first_room);

command cmd3 = "look";
Result rslt3 = Result::Make();
rslt3.description = "You are in a small room.\nYou see a door to the south.\n";
rslt3.goes_anywhere = false;
first_room.options[cmd3] = rslt3;
room_map["first room"] = first_room;
};

void AddBurp(Room& room) {
command cmd = "burp";
Result rslt = Result::Make();
rslt.description = "You burp loudly.\n";
rslt.goes_anywhere = false;
room.options[cmd] = rslt;
}

void AddEndRoom(room_map_type& room_map) {
Room end_room = Room::Make();
end_room.description = "Congrats, the game is over!\nQuit if you like.\n";
AddBurp(end_room);

command cmd1 = "quit";
Result rslt1 = Result::Make();
rslt1.description = "Quitter.\n";
rslt1.goes_anywhere = true;
rslt1.destination = "quit";
end_room.options[cmd1] = rslt1;

// you can't do anything else in the end room
room_map["victory"] = end_room;
}

void AddQuit(room_map_type& room_map) {
Room quit_room = Room::Make();
quit_room.description = "Bye!\n";
quit_room.game_over = true;
room_map["quit"] = quit_room;
}

void BuildUniverse(room_map_type& room_map) {
// ideally you'd read config files and load them into the universe:
AddFirstRoom(room_map);
AddEndRoom(room_map);
AddQuit(room_map);
}

struct GameState {
room_name current_room;
bool finished;
bool error;
GameState():finished(false), error(false) {}
};

GameState PlayRound( GameState state, room_map_type room_map ) {
Room current_room = room_map[state.current_room];
cout << current_room.description;

if (current_room.game_over) {
state.finished = true;
return state;
}

if (current_room.is_error) {
state.error = true;
return state;
}

Result r;

while(r.is_error) {
cout << "What do you want to do?\n";
std::string command;
cin >> command;
r = current_room.options[command];
if (r.is_error) {
cout << "I do not understand\n";
}
}
cout << r.description;
if (r.goes_anywhere) {
state.current_room = r.destination;
}
return state;
}

void PlayGame() {
room_map_type room_map;
BuildUniverse(room_map);
GameState state;
state.current_room = "first room";
while (!state.finished && !state.error) {
state = PlayRound(state, room_map);
}
if (state.finished) {
cout << "game ended\n";
} else if (state.error) {
cout << "game error\n";
} else {
cout << "unexpected exit\n";
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by bam104
Yea i want to get into graphics but i thought it might be a little early?

Thanks gcs584 im looking into those now.


Perhaps so; I would stay off graphics until you learn classes and such. Anyhow: Nice first game! I would suggest you continue with your book's training whilst continueing to implement what it teaches you into your game; thus when you finish with the book this game shall be more advanced and have more rooms and such. Once again, nice job!

In case you care, my first(non-tut)game wasn't as good as yours; here it is:


#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int main()
{
cout << " ___________________________________\n"
<< "| ~ Guess The Box ~ |\n"
<< "| |\n"
<< "| Find the box with the coin |\n"
<< "| under it.Input '1' for the |\n"
<< "| first box,'2' for box two, |\n"
<< "| and '3' for the third box. |\n"
<< "| |\n"
<< "| [] [] [] |\n"
<< "|___________________________________|\n";

int guess;
int answer;
int score;
int total;

score = 0;
total = 0;

srand(time(NULL));

for(;;)
{
answer = (rand() % 3) + 1;

cout << "Which one has the coin?\n"
<< " [ ] [ ] [ ] \n";
cin >> guess;

if(answer == 1)
{
if(guess == 1)
{
cout << " [o] [ ] [ ] \n"
<< "Right!\n";

total = total + 1;
score = score + 1;
}

if(guess == 2)
{
cout << " [ ] [x] [ ] \n"
<< "Try again!\n";

total = total + 1;
score = score + 0;
}

if(guess == 3)
{
cout << " [ ] [ ] [x] \n"
<< "Nothing!\n";

total = total + 1;
score = score + 0;
}
}

if(answer == 2)
{
if(guess == 1)
{
cout << " [x] [ ] [ ] \n"
<< "Nice try!\n";

total = total + 1;
score = score + 0;
}

if(guess == 2)
{
cout << " [ ] [o] [ ] \n"
<< "Correct!\n";

total = total + 1;
score = score + 1;
}

if(guess == 3)
{
cout << " [ ] [ ] [x] \n"
<< "Wrong one!\n";

total = total + 1;
score = score + 0;
}
}

if(answer == 3)
{
if(guess == 1)
{
cout << " [x] [ ] [ ] \n"
<< "Wrong!\n";

total = total + 1;
score = score + 0;
}

if(guess == 2)
{
cout << " [ ] [x] [ ] \n"
<< "Nope!\n";

total = total + 1;
score = score + 0;
}

if(guess == 3)
{
cout << " [ ] [ ] [o] \n"
<< "Bingo!\n";

total = total + 1;
score = score + 1;
}
}

cout << "You won "
<< score
<< " of "
<< total
<< "\n\n\n\n\n\n\n\n\n\n\n\n\n";

}
}




Lol; looking over my old code makes me wince and want to touch it up alot. or completely remake it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this