Source Code Simple Adventure Game Skeleton

Hexui Undetected CSGO Cheats Sinkicheat PUBG Cheat

Solaire

Respected Hacker
Dank Tier VIP
Dec 15, 2013
1,051
16,353
62
This is written in C++. It's a simple adventure game skeleton that features enemies, a player, and a map. All of the information on the source is at the top. I did not comment the code so it may be a bit hard to read, though I'll fix that eventually. At some point I may add classes (once I get a full grasp of them) to shorten the code by a LOT.

I put it in a pastebin as to keep the length of this page short (There are over 700 lines of code).

Pastebin

This is mostly for learning, so if anyone wants to expand upon this and add some of the features suggested at the top of the code, please go ahead.

Any tips, or suggestions would be very useful to me.

Have fun guys!
 

Liduen

Hacker
Dank Tier VIP
May 19, 2013
702
8,478
33
Yep could shorten this code a lot by using a class with a member move function which moves the player/enemy.
 

Syntax Error

Coder
Silenced
Meme Tier VIP
Nov 6, 2014
239
2,928
6
Hey bro, theres a great book called Clean Code that I suggest you read. I can post an attachment in the forum or send it to you via skype/email if you'd like. I'm going to critique your code and I hope you dont take offense. There is a lot of repetitive code going on. In your main function you have a bunch of if and else if statements like here

C++:
f (choice == 'u')
                {
                        int storeX = PlayerX;
                        int storeY = PlayerY;
                        char storeSymbol = board[PlayerX][PlayerY];
 
                        int Attack = PlayerY - 1;
 
                        if (PlayerX == EnemyX && Attack == EnemyY)
                        {
                                EnemyDead = true;
                        }
 
                        else if (PlayerX == EnemyX2 && Attack == EnemyY2)
                        {
                                Enemy2Dead = true;
                        }
 
                        else if (PlayerX == EnemyX3 && Attack == EnemyY3)
                        {
                                Enemy3Dead = true;
                        }
 
                        PlayerX = storeX;
                        PlayerY = storeY;
                       
                        EnemyMove();
                        EnemyMove2();
                        EnemyMove3();
                }
 
                if (choice == 'j')
                {
                        int storeX = PlayerX;
                        int storeY = PlayerY;
                        char storeSymbol = board[PlayerX][PlayerY];
 
                        int Attack = PlayerY + 1;
 
                        if (PlayerX == EnemyX && Attack == EnemyY)
                        {
                                EnemyDead = true;
                        }
 
                        else if (PlayerX == EnemyX2 && Attack == EnemyY2)
                        {
                                Enemy2Dead = true;
                        }
 
                        else if (PlayerX == EnemyX3 && Attack == EnemyY3)
                        {
                                Enemy3Dead = true;
                        }
 
                        PlayerX = storeX;
                        PlayerY = storeY;
                       
                        EnemyMove();
                        EnemyMove2();
                        EnemyMove3();
                }
 
                if (choice == 'h')
                {
                        int storeX = PlayerX;
                        int storeY = PlayerY;
                        char storeSymbol = board[PlayerX][PlayerY];
 
                        int Attack = PlayerX + 1;
 
                        if (Attack == EnemyX && PlayerY == EnemyY)
                        {
                                EnemyDead = true;
                        }
 
                        else if (Attack == EnemyX2 && PlayerY == EnemyY2)
                        {
                                Enemy2Dead = true;
                        }
 
                        else if (Attack == EnemyX3 && PlayerY == EnemyY3)
                        {
                                Enemy3Dead = true;
                        }
 
                        PlayerX = storeX;
                        PlayerY = storeY;
                       
                        EnemyMove();
                        EnemyMove2();
                        EnemyMove3();
                }
 
                if (choice == 'k')
                {
                        int storeX = PlayerX;
                        int storeY = PlayerY;
                        char storeSymbol = board[PlayerX][PlayerY];
 
                        int Attack = PlayerX - 1;
 
                        if (Attack == EnemyX && PlayerY == EnemyY)
                        {
                                EnemyDead = true;
                        }
 
                        else if (Attack == EnemyX2 && PlayerY == EnemyY2)
                        {
                                Enemy2Dead = true;
                        }
 
                        else if (Attack == EnemyX3 && PlayerY == EnemyY3)
                        {
                                Enemy3Dead = true;
                        }
 
                        PlayerX = storeX;
                        PlayerY = storeY;
                       
                        EnemyMove();
                        EnemyMove2();
                        EnemyMove3();
                }

The only thing i see changing is the int attack = player. A more efficient way of doing this could be to move it into its own function.
C++:
void movePlayerY(int coordinate, int direction)
 {
    int storeX = PlayerX;
    int storeY = PlayerY;
    char storeSymbol = board[PlayerX][PlayerY];
 
    int Attack = coordinate + direction;
 
    if (PlayerX == EnemyX && Attack == EnemyY){
        EnemyDead = true;
    } else if (PlayerX == EnemyX2 && Attack == EnemyY2) {
        Enemy2Dead = true;
    } else if (PlayerX == EnemyX3 && Attack == EnemyY3) {
        Enemy3Dead = true;
    }
 
    PlayerX = storeX;
    PlayerY = storeY;
                       
    EnemyMove();
    EnemyMove2();
    EnemyMove3();

 }

And then just create a switch statement.
C++:
switch(choice){
    case 'u':
        movePlayer(PlayerY, -1);
        break;
    case 'j':
        movePlayer(PlayerY, 1);
        break;
    ...
}

I'm not sure if my code works or not but it's just an example. All in all, thank you for sharing! :]
 

Solaire

Respected Hacker
Dank Tier VIP
Dec 15, 2013
1,051
16,353
62
Thanks for the replies! I'll definitely make use of your tips, guys :D! Classes would definitely shorten the code, as I'm now up to 1205 lines. I changed up the map and made it a bit cooler looking (as well as better in terms of playing the game), and added 3 more enemies as well as a win/lose system. I'm working on a function that records your gameplay then displays it at the end (if you select the Yes option). I'm also planning on switching the movement from entering a letter to just pushing the button. I'm learning how to make and use classes right now, so once I fully understand their use and how to apply them, I'll do so. Currently I'm not fully comfortable attempting it.

Just curious, could I do an array that holds the entities, and an int that increases every time an entity is made so that I could loop through them and make sure they don't conflict in movement? Or would there be a better way?

I'll take a look at the book, Syntax Error, thanks!
 
Last edited:

Solaire

Respected Hacker
Dank Tier VIP
Dec 15, 2013
1,051
16,353
62
I've rewritten quite a bit of it and I'm now using classes. It's down to 381 lines of code, and I plan on shortening it further by combining the Enemy and Player class into an Entity class and have the Player object have it's own special move function, or use a bool in the constructor for movement. I'm still figuring out the attack function, but I'll have it working at some point. I also plan on adding random spawning so that you have to fight the enemies differently each time (kinda).

Pastebin
 

Solaire

Respected Hacker
Dank Tier VIP
Dec 15, 2013
1,051
16,353
62
Thanks :)

I've completed quite a bit more, as I've added randomized spawning for the enemies, as well as health for the player (needs to be lowered a bit ;)), and a gold amount for the player. I've gotten rid of system("CLS") and system("PAUSE") and replaced them with more appropriate pieces of code. The classes are a bit longer now, and had to be mixed up a bit due to scope. I plan on fixing this in the future. I used a pointer array for the enemies to add them without having to declare them. If you want them declared differently, just edit the single line of code where they're created in the for loop.

Possible ways to declare enemies:
C++:
Enemy Enemy1('@', '7', '12');//@ is the symbol the enemy will take, 7 is the X coord, and 12 is the Y coord.
Enemy Enemy2('7', '12');//This is the same as the one above, but now it randomly picks a symbol.
Enemy Enemy3('%');//You can pick the sign it goes as, and it will spawn randomly into the map.
Enemy Enemy4;//This spawns an enemy randomly.
I've commented the code so it should be a little bit easier to read. You are scored based on several factors including your health, the amount of gold you have, and the amount of turns that have passed. I'm currently working on adding an item system so that you can restore your health and get new weapons, as well as making enemies a bit harder to kill and a bit more aggressive.

This one has two files due to a function needed for clearing the screen without system("CLS").

Main.cpp

Includes.h

Credits to the wonderful people on stackoverflow for the posts on clearing and pausing consoles information.

PS. The game is now hackable since there are variables to change! Have fun! :D
 

Solaire

Respected Hacker
Dank Tier VIP
Dec 15, 2013
1,051
16,353
62
So, I've added a LOT of things since the last post. You now can go to the shop and buy upgrades for your character! Your character has a range, amount of damage, and health. Enemies also have an amount of health, and trying to fight a lot of enemies in the beginning without any upgrades will get you killed.

Use WASD to move, and UHJK to attack. Press B to go to the store and buy things. If you get to the '?', you win the game with an extra 500 points.

There's still a lot of things I need to do with the code, and it still needs to be fixed in some areas. I plan on changing the map from a char array to a string, because having all of the characters printed one by one slows down showing the map by a lot.

I'm still working on making the player and enemy classes completely independent of each other.

Entities.h
https://pastebin.com/tcmQMQb3

Functions.h
https://pastebin.com/cJMry65u

Main.cpp
https://pastebin.com/HGymzWFm

My challenge for you guys is this: Make an external program the predicts your end-game score based on the equation I use to get it when you win.

Also, try to beat my High Score of 10450! (Without hacking :p)
 
Attention! Before you post:

Read the How to Ask Questions Guide
99% of questions are answered in the Beginner's Guide, do it before asking a question.

No Hack Requests. Post in the correct section.  Search the forum first. Read the rules.

How to make a good post:

  • Fill out the form correctly
  • Tell us the game name & coding language
  • Post everything we need to know to help you
  • Ask specific questions, be descriptive
  • Post errors, line numbers & screenshots
  • Post code snippets using code tags
  • If it's a large project, zip it up and attach it

If you do not comply, your post may be deleted.  We want to help, please make a good post and we will do our best to help you.

Community Mods