online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code   
Language
#include<iostream> #include<cstdlib> //used for random number generation #include<ctime> //used to seed random numbers #include<string> #include<ncurses.h> #include<fstream> #include"functions.h" #include"PLAYER.h" #include"MONSTER.h" //make health/stats constantly displayed //review erase(); statements to fix crashes. //make health/stats redisplay after all erase(); functions. //add colours, like cyan when dealing damage, red when taking it, green when getting health, etc. //Final score = (experience gained - damage taken) / daysTime. int main() { initscr(); scrollok(stdscr,TRUE); noecho(); srand (time(NULL)); std::string areaDesc[25]; std::fstream areaStream; areaStream.open("areas.txt"); for(int i = 0; i < 25; i++) { getline(areaStream,areaDesc[i]); } bool playing = TRUE; int area[5][5]; int current = 23; for(int j = 0; j < 5; j++) { for(int k = 0; k < 5; k++) { area[j][k] = (k+1)+(j*5); } } //initializers. setting(); PLAYER player1; while(playing) { cbreak(); erase(); std::string str = areaDesc[current - 1]; for(int i = 0; i < str.length(); i++) { addch(str[i]); } player1.outPutStats(); switch(showOptions()) { //Move, show stats/hp, show location/description/id, rest/heal, search area for supplies. case 1: { int temp1 = current; current += movementReturn(current); int temp2 = current; if(check4Monster(current) and temp1 != temp2) //change != to == to test movement/etc. { MONSTER monster1(player1.level); while(monster1.health > 0 and player1.health > 0) { switch(player1.classNum) { case 1: combat1(&monster1,&player1); break; case 2: combat2(&monster1,&player1); break; case 3: combat3(&monster1,&player1); break; } if(player1.health < 1) { playing = false; } else if(monster1.health < 1) { player1.ex += monster1.exReward; if(player1.ex >= player1.nEx) { player1.levelUp(); } } } } } case 2: player1.heal(); break; //no time passes case 3: { //search(); break; } default: printw("\nInvalid input, line 42. Press any key to try again. \n"); break; } //searching has a chance of spawning a monster. //if they succeed, they find items like potions and 1 of the four special drops that are needed to fight the final boss. //make each location have story bits and information about the location in Warsaw citadel. //based off Rayet-96A story. Story is revealed through each of these locations. } getch(); erase(); endwin(); }
#ifndef MONSTER_H #define MONSTER_H class PLAYER; class MONSTER { private: //monsters should have health, and the ability to attack, for now. //we should work on tiny goals before any complex stuff. //this means exclusively HP and attacking right now, then we can implement //levels, dodging, etc. int level; //determined by player's level, will be used to scale health and damage. int monsterType; //determined by constructor, will be used to change certain values like health or damage. float damage; //damage is redefined whenever an attack is done, whether or not this is more memory efficient, I have no clue. //references the player object, so that health is modifiable. //this also means that health must be public on player.h public: void attack(PLAYER *playerRef); int exReward = 10; float health; //levels will inevtiably turn integers into floating points. float maxHealth; MONSTER(int playerLevel); ~MONSTER(); }; #endif
#include<iostream> #include<cstdlib> #include<ctime> #include<ncurses.h> #include"MONSTER.h" #include"PLAYER.h" MONSTER::MONSTER(int playerLevel) { srand (time(NULL)); this->level = playerLevel; this->monsterType = rand() % 3 + 1; //stays 1 for now. if(monsterType == 1) { this->maxHealth = 12*this->level; this->health = this->maxHealth; this->damage = 3 * this->level; this->exReward = (.33 * ( ( exReward + 6 ) * ( exReward + 6) ) ) + 10 ; erase(); printw("\nA monsterType1 appears!\n"); } else if(monsterType == 2) { this->maxHealth = 10 * this->level; this->health = this->maxHealth; this->damage = 4 * this->level; this->exReward = (.50 * ( ( exReward + 6 ) * ( exReward + 6) ) ) + 10 ; erase(); printw("\nA monsterType2 appears!\n"); } else if(monsterType == 3) { this->maxHealth = 8 * this->level; this->health = this->maxHealth; this->damage = 5 * this->level; this->exReward = (.44 * ( ( exReward + 6 ) * ( exReward + 6) ) ) + 10 ; erase(); printw("\nA monsterType3 appears!\n"); } } void MONSTER::attack(PLAYER *playerRef) { //reference is always the player, so function call should be: monsterName.attack(&player1); playerRef->health -= this->damage; } MONSTER::~MONSTER() { printw("You slayed the monster!\n"); }
#ifndef PLAYER_H #define PLAYER_H #include "MONSTER.h" //include one of the classes, //when you include this class, //the other class should be included on that .h file as well. class MONSTER; class PLAYER { private: void getClass(); //gets the class number, used to determine getMult operations. void getMults(); //gets multipliers listed on line 19 and 18. void setStats(); public: void attack(MONSTER *monsterRef); void outPutStats(); void heal(); float damage = 10; //damage dealt. int stims = 3; float health = 10; float maxHealth = 10; //maximum health, different than health. void levelUp(); int level = 1; float healthMult = 1; //used when levelling up to determine max health. float damageMult = 1; //used when levelling up to change damage. int ex = 0; //current experience int nEx = 100; //experience needed before level up. float exMult = 1.2; int classNum = 1; //determines multipliers and base stats. PLAYER(); ~PLAYER(); }; #endif
#include<ncurses.h> #include<string> #include"PLAYER.h" #include"MONSTER.h" void PLAYER::getClass() { char input; printw("Type A, B, or C to see more information about the classes.\n"); bool picking = true; while(picking) { printw("1. Heavy\n"); printw("2. Ranger\n"); printw("3. Assault\n"); input = getch(); scroll(stdscr); if(input == 'a' or input == 'A') { printw("The heavy class has high health but low damage output\nThe minigames you have to complete in order to land an attack are easiest though.\n"); } else if(input == 'b' or input == 'B') { printw("The ranger class is a jack of all trades\nMedium damage, medium health, and medium difficulty minigames.\n"); } else if(input == 'c' or input == 'C') { printw("The assault class has medium health and high damage output\nThe minigames you have to complete in order to land an attack are the hardest though.\n"); } else { input -= '0'; if(input == 1) { this->classNum = 1; picking = false; this->maxHealth = 20; this->health = 20; this->damage = 6; } else if(input == 2) { this->classNum = 2; picking = false; this->maxHealth = 15; this->health = 15; this->damage = 9; } else if(input == 3) { this->classNum = 3; picking = false; this->maxHealth = 15; this->health = 15; this->damage = 12; } } } for(int i = 0; i < 7; i++) { scroll(stdscr); } } void PLAYER::getMults() { //multipliers need to be balanced to reward higher difficulty characters. //they should also probably not be so extreme, and closer together, but the multiplier should //increase every single level. if(this->classNum == 1) { this->damageMult = 1.2; this->healthMult = 2; } else if(this->classNum == 2) { this->damageMult = 1.5; this->healthMult = 1.5; } else if(this->classNum == 3) { this->damageMult = 2; this->healthMult = 1.4; } } void PLAYER::outPutStats() { printw("\nLevel: %d",this->level); printw("\nClass Num: %d",this->classNum); printw("\nHealth: %f",this->health); printw(" / %f",this->maxHealth); printw("\nDamage: %f",this->damage); } void PLAYER::attack(MONSTER *monsterRef) { monsterRef->health -= this->damage; //parameter is a pointer, so passing requires an address for the pointer to equal. //this reference is always a monster, so it should look like this: //player1.attack(&monsterName); //for testing purposes, right now the monster will just take 5 damage. } void PLAYER::levelUp()//after every combat encounter, check to see if they meet the experience threshhold. { //if they do, and are less than level 10(max level), they level up. //if they are level ten they can no longer level up. if(this->level < 10) { printw("You've levelled up! \n"); this->maxHealth *= this->healthMult; this->health = this->maxHealth; this->damage *= this->damageMult; this->nEx *= (this->nEx * 1.2); this->level++; printw("Your new stats: \n"); outPutStats(); } } PLAYER::PLAYER() { //constructor is used to get classNum, and consequentially, base stats. //this classNum can be used with PLAYER::levelUp(); to determine multipliers. getClass(); getMults(); outPutStats(); } void PLAYER::heal() { if(this->health < this->maxHealth) { this->health += (this->maxHealth * 0.5); if(this->health > this->maxHealth) { this->health = this->maxHealth; } this->stims--; } else { printw("\nYou're already at max health.\n"); } } PLAYER::~PLAYER() { erase(); printw("Your adventure ends here.\n"); printw(" _,.-------.,_\n"); printw(" ,;~' '~;,\n"); printw(" ,; ;,\n"); printw(" ; ;\n"); printw(" ,' ',\n"); printw(",; ;,\n"); printw("; ; . . ; ;\n"); printw("| ; ______ ______ ; |\n"); printw("| `/~\" ~\" . \"~ \"~\\' |\n"); printw("| ~ ,-~~~^~, | ,~^~~~-, ~ |\n"); printw(" | | }:{ | |\n"); printw(" | l / | \\ ! |\n"); printw(" .~ (__,.--\" .^. \"--.,__) ~.\n"); printw(" | ---;' / | \\ `;--- |\n"); printw(" \\__. \\/^\\/ .__/\n"); printw(" V| \\ / |V\n"); printw(" | |T~\\___!___!___/~T| |\n"); printw(" | |`IIII_I_I_I_IIII'| |\n"); printw(" | \\,III I I I III,/ |\n"); printw(" \\ `~~~~~~~~~~' /\n"); printw(" \\ . . /\n"); //cool art. printw(" \\. ^ ./\n"); printw(" ^~~~^~~~^\n"); cbreak(); getch(); }
#include"MONSTER.h" #include"PLAYER.h" bool check4Monster(int cArea); void outString(); int showOptions(); int movementReturn(int currentPos); void setting(); //function used to tell user the game's setting and general information. //functions that use a while loop and some evaluative statements //in order to check whether or not the player has finished or failed combat. //these combat functions use ncurses initialization to show and clear the screen. //The player will have a certain amount of time to input one character, //and a larger total amount of time to input all the characters. //these characters are WASD or arrow keys, allow the user to choose which they use. //Afterwards, the player may level up, but will receive experience. void combat1(MONSTER* monster, PLAYER* player); //combat encounter for heavy class void combat2(MONSTER* monster, PLAYER* player); //combat encounter for ranger class void combat3(MONSTER* monster, PLAYER* player); //combat encounter for assault class
#include<ncurses.h> #include<cstdlib> #include<time.h> #include"functions.h" /* *if(check4Monster(current)) {MONSTER monster1;} *put * * */ bool check4Monster(int cArea) { //run this code alongside an if-else statement to determine if a monster spawns respectively srand (time(NULL)); int x = rand() % 100 + 1; switch(cArea) { //value decreases for x, representing higher chance of monster spawn. case 1: if(x > 80) { return true; } case 6: if(x > 77) { return true; } case 2: if(x > 74) { return true; } case 7: if(x > 71) { return true; } case 11: if(x > 68) { return true; } case 3: if(x > 66) { return true; } case 12: if(x > 64) { return true; } case 8: if(x > 60) { return true; } case 13: return false; case 16: if(x > 59) { return true; } case 4: if(x > 58) { return true; } case 17: if(x > 57) { return true; } case 9: if(x > 56) { return true; } case 18: if(x > 56) { return true; } case 14: if(x > 55) { return true; } case 19: if(x > 55) { return true; } case 21: if(x > 54) { return true; } case 5: if(x > 54) { return true; } case 22: if(x > 53) { return true; } case 10: if(x > 53) { return true; } case 23: if(x > 52) { return true; } case 15: if(x > 51) { return true; } case 24: if(x > 49) { return true; } case 20: if(x > 47) { return true; } case 25: if(x > 45) { return true; } default: return false; } } int showOptions() { printw("\n1. Move to an area.\n2. Rest/heal.\n"); printw("3. Search area for supplies/etc.\n"); refresh(); return (getch()-'0'); //make this an integer return, use switch statement for input and 5 different functions that do what //the above captions say the numbers do, respectively. } void setting() { printw("You awake in a bunker, a prisoner in Sector 6, a base hidden away from the GRB.\n"); printw("The door is unlocked to leave, and nobody seems to be present.\n"); printw("It seems that the place has been desolated, and you find it hard to breathe.\n"); printw("Before you go out into the world, pick your class: "); } int movementReturn(int currentPos) { if((currentPos + 5) < 26) { printw("Type 1 to move downwards.\n"); } if((currentPos - 5) > 0) { printw("Type 2 to move upwards.\n"); } //these work this way because I was a moron and programmed one of the variables wrong. if(currentPos % 5 != 0 and (currentPos + 1) < 27) { printw("Type 3 to move rightwards.\n"); } //basically up is down and down is up which is like if((currentPos - 1) % 5 != 0 and (currentPos - 1) > 0) { printw("Type 4 to move leftwards.\n"); } //unfortunate, but nonetheless it works and runs, and doesn't really need changing. int moveInput = (getch() - '0'); if(moveInput == 1 and ( (currentPos + 5) < 26) ) { return 5; } //we need to make the if statements stop the increase to stop segfaulting in case the user types something they shouldn't. else if(moveInput == 2 and ( (currentPos - 5) > 0) ) { return -5; } else if(currentPos % 5 != 0 and moveInput == 3 and ( (currentPos + 1) < 26) ){ return 1; } else if((currentPos - 1) % 5 != 0 and moveInput == 4 and ( (currentPos - 1) > 0) ) { return -1; } //now the player can't move back once they reach an area divisible by 8. else { printw("You can't go that way\n"); return 0; } } void combat1(MONSTER* monster, PLAYER* player) { srand (time(NULL)); halfdelay(15); //player gets 1.5 seconds to input the proper character. int x = rand() % 4 + 1; char ch0; char chInp; if(x == 1) { ch0 = 'w'; } else if(x == 2) { ch0 = 'a'; } else if(x == 3) { ch0 = 's'; } else if(x == 4) { ch0 = 'd'; } addch(ch0); chInp = (getch()); if(chInp == ch0) { printw("\nYou hit the monster!\n"); player->attack(monster); printw("The monster has %f",monster->health); printw("/%f",monster->maxHealth); printw(" health!\n"); } else { printw("\nYou were attacked!\n"); monster->attack(player); printw("You have %f",player->health); printw(" health left, of a maximum %f",player->maxHealth); printw("\n"); } } void combat2(MONSTER* monster, PLAYER* player) { srand (time(NULL)); halfdelay(15); //player gets 1.5 seconds to input the proper character. int x = rand() % 6 + 1; char ch0; char chInp; if(x == 1) { ch0 = 'q'; } else if(x == 2) { ch0 = 'w'; } else if(x == 3) { ch0 = 'e'; } else if(x == 4) { ch0 = 'r'; } else if(x == 5) { ch0 = 't'; } else if(x == 6) { ch0 = 'y'; } addch(ch0); chInp = getch(); if(chInp == ch0) { printw("\nYou hit the monster!\n"); player->attack(monster); printw("The monster has %f",monster->health); printw("/%f",monster->maxHealth); printw(" health!\n"); } else { printw("\nYou were attacked!\n"); monster->attack(player); printw("You have %f",player->health); printw(" health left, of a maximum %f",player->maxHealth); printw("\n"); } } void combat3(MONSTER* monster, PLAYER* player) { srand (time(NULL)); halfdelay(15); //player gets 1.5 seconds to input the proper character. char chs[4]; char chInp[4]; int numRight = 0; for(int i = 0, x = 0; i < 4; i++) { x = rand() % 4 + 1; switch(x) { case 1: chs[i] = 'q'; break; case 2: chs[i] = 'z'; break; case 3: chs[i] = 'p'; break; case 4: chs[i] = 'm'; break; } addch(chs[i]); chInp[i] = getch(); if(chInp[i] == chs[i]) { numRight++; } } if(numRight > 3) { printw("\nYou hit the monster!\n"); player->attack(monster); printw("The monster has %f",monster->health); printw("/%f",monster->maxHealth); printw(" health!\n"); } else { printw("\nYou were attacked!\n"); monster->attack(player); printw("You have %f",player->health); printw(" health left, of a maximum %f",player->maxHealth); printw("\n"); } }
Position 1 Position 2 Position 3 Position 4 Position 5 Position 6 Position 7 Position 8 Position 9 Position 10 Position 11 Position 12 Position 13 Position 14 Position 15 Position 16 Position 17 Position 18 Position 19 Position 20 Position 21 Position 22 Position 23 Position 24 Position 25

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue