g++ main.cpp chmod +x a.out ./a.out
Created
November 23, 2017 02:25
-
-
Save kennycason/303fc008465f3725eb5104109820d117 to your computer and use it in GitHub Desktop.
Simple C++ Mudd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <cstdlib> | |
using namespace std; | |
// function declarations | |
void newGame(); | |
void initPlayer(); | |
void printPlayer(); | |
void initArea(); | |
void generateRandomEnemies(); | |
void printArea(); | |
void printMenu(); | |
void getPlayerInput(); | |
bool isAlive(); | |
int isEnemy(); | |
void fight(struct Enemy& enemy); | |
void gameOver(); | |
int dice(int sides); | |
// common structures | |
struct Location { | |
int x; // the x position in the area | |
int y; // the y position in the area | |
}; | |
struct Weapon { | |
string name; // name of weapon | |
int damage; // max damage | |
int crit; // critical hit range (1 to 20) | |
}; | |
struct Armor { | |
string name; // name of armor | |
int defence; // defense modifier | |
}; | |
struct Shield { | |
string name; // name of armor | |
int defence; // defense modifier | |
}; | |
struct Player { | |
string name;// name of player | |
int job; // job (class) of the player | |
int hp; // current hp | |
int hpm; // hp max | |
int str; // strength | |
int dex; // dexterity | |
int con; // constitution | |
int iq; // intelligence | |
int wis; // widsom | |
int cha; // charisma | |
int bab; // base attack bonus | |
int fort; // fortitude | |
int reflex; // reflex | |
int will; // will | |
int ac; // armor class | |
int level; // level | |
int xp; // current xp | |
int gold; // amount of gold | |
struct Weapon weapon; | |
struct Armor armor; | |
struct Shield shield; | |
struct Location location; | |
}; | |
struct Enemy { | |
string name;// enemy name | |
int hp; // current hp | |
int str; // strength | |
int ac; // armor class | |
int gold; // amount of gold enemy is carrying | |
struct Location location; | |
}; | |
// create a simple area default 30x30 | |
int AREA_WIDTH = 20; | |
int AREA_HEIGHT = 12; | |
int** area; // 2 dimensional array for the area | |
const int AREA_WALKABLE = 0; | |
const int AREA_WALL = 1; | |
// default player structure | |
Player player; | |
// default array of enemies (for now lets assume 10 enemies per area) | |
Enemy* enemies; | |
const int ENEMIES_MAX_NUM = 100; | |
// main function | |
int main() { | |
srand(time(0)); // generate new random seed (for use with rand() in the dice() function) | |
newGame(); | |
while(isAlive()) { | |
printArea(); | |
printMenu(); | |
getPlayerInput(); | |
int enemyIndex = isEnemy(); | |
if(enemyIndex> -1) { | |
fight(enemies[enemyIndex]); | |
} | |
} | |
gameOver(); | |
return 0; | |
} | |
// function definitions | |
bool isAlive() { | |
if(player.hp > 0) { | |
return true; | |
} | |
return false; | |
} | |
void gameOver() { | |
cout << "Thou Art Dead" << endl; | |
} | |
void newGame() { | |
initArea(); | |
generateRandomEnemies(); | |
initPlayer(); | |
printPlayer(); | |
} | |
void initArea() { | |
area = new int*[AREA_WIDTH]; | |
for(int x = 0; x < AREA_WIDTH; ++x) { | |
area[x] = new int[AREA_HEIGHT]; | |
for(int y = 0; y < AREA_HEIGHT; ++y) { // initialize the values, this is optional, but recommended | |
if(dice(4) >= 2) { | |
area[x][y] = AREA_WALKABLE; | |
} else { | |
area[x][y] = AREA_WALL; | |
} | |
} | |
} | |
} | |
void generateRandomEnemies() { | |
enemies = new Enemy[ENEMIES_MAX_NUM]; | |
for(int i = 0; i < ENEMIES_MAX_NUM; i++) { | |
enemies[i].name = "slime"; | |
enemies[i].hp = 4; | |
enemies[i].str = 2; | |
enemies[i].ac = 8; | |
enemies[i].gold = dice(5); | |
enemies[i].location.x = dice(AREA_WIDTH) - 1; | |
enemies[i].location.y = dice(AREA_HEIGHT) - 1; | |
} | |
} | |
void initPlayer() { | |
player.name = "Kenny"; | |
player.hp = 20; | |
player.hpm = 20; | |
player.str = 10; | |
player.dex = 10; | |
player.con = 10; | |
player.iq = 10; | |
player.wis = 10; | |
player.cha = 10; | |
player.bab = 0; | |
player.fort = 2; | |
player.reflex = 2; | |
player.will = 2; | |
player.ac = 10; | |
player.level = 1; | |
player.xp = 0; | |
player.gold = 200; | |
player.weapon.name = "Wood Sword"; | |
player.weapon.damage = 4; | |
player.weapon.crit = 19; | |
player.armor.name = "Leather"; | |
player.armor.defence = 1; | |
player.shield.name = "Wood"; | |
player.shield.defence = 1; | |
player.location.x = 5; | |
player.location.y = 5; | |
} | |
void printPlayer() { | |
cout << player.name << endl; | |
cout << "HP " << player.hp << "/" << player.hpm << endl; | |
cout << "XP " << player.xp << "\t Lvl " << player.level << endl; | |
cout << "STR " << player.str << "\t INT " << player.iq << endl; | |
cout << "DEX " << player.dex << "\t WIS " << player.wis << endl; | |
cout << "CON " << player.con << "\t CHA " << player.cha << endl; | |
cout << "BAB +" << player.bab << "\tAC " << player.ac << endl; | |
cout << "FORT +" << player.fort << "\tREFLEX +" << player.reflex << "\tWILL +" << player.will << endl; | |
cout << "GOLD " << player.gold << endl; | |
cout << "WEAPON " << player.weapon.name << ": 1D" << player.weapon.damage << " crit (" << player.weapon.crit << "-20) x2" << endl; | |
cout << "ARMOR " << player.armor.name << ": +" << player.armor.defence << endl; | |
cout << "SHIELD " << player.shield.name << ": +" << player.shield.defence << endl; | |
} | |
void printArea() { | |
for(int y = 0; y < AREA_HEIGHT; ++y) { | |
for(int x = 0; x < AREA_WIDTH; ++x) { | |
if(player.location.x == x && player.location.y == y) { | |
cout << "@"; | |
} else { | |
if(area[x][y] == AREA_WALKABLE) { | |
cout << " "; | |
} else if(area[x][y] == AREA_WALL) { | |
cout << "#"; | |
} | |
} | |
} | |
cout << endl; | |
} | |
} | |
void printMenu() { | |
cout << "Commands" << endl; | |
cout << "L - move left\tR - move right" << endl; | |
cout << "U - move up\tD - move down" << endl; | |
cout << "S - print stats\tX - exit game" << endl; | |
cout << ">"; | |
} | |
void getPlayerInput() { | |
char input; | |
cin >> input; | |
cout << endl; // go to the next line | |
if(input == 'U' || input == 'u') { // move up | |
if(player.location.y - 1 >= 0 | |
&& area[player.location.x][player.location.y - 1] != AREA_WALL) { | |
player.location.y--; | |
} else { | |
cout << "Can not move there!" << endl; | |
} | |
} else if(input == 'D' || input == 'd') { // move down | |
if(player.location.y + 1 < AREA_HEIGHT | |
&& area[player.location.x][player.location.y + 1] != AREA_WALL) { | |
player.location.y++; | |
} else { | |
cout << "Can not move there!" << endl; | |
} | |
} else if(input == 'L' || input == 'l') { // move left | |
if(player.location.x - 1 >= 0 | |
&& area[player.location.x - 1][player.location.y] != AREA_WALL) { | |
player.location.x--; | |
} else { | |
cout << "Can not move there!" << endl; | |
} | |
} else if(input == 'R' || input == 'r') { // move right | |
if(player.location.x + 1 < AREA_WIDTH | |
&& area[player.location.x + 1][player.location.y] != AREA_WALL) { | |
player.location.x++; | |
} else { | |
cout << "Can not move there!" << endl; | |
} | |
} else if(input == 'X' || input == 'x') { | |
gameOver(); | |
} else if(input == 'S' || input == 's') { | |
printPlayer(); | |
} | |
} | |
int isEnemy() { | |
for(int i = 0; i < ENEMIES_MAX_NUM; i++) { | |
if(enemies[i].location.x == player.location.x && | |
enemies[i].location.y == player.location.y) { | |
return i; // the index of the enemy array | |
} | |
} | |
return -1; // no enemy | |
} | |
void fight(struct Enemy& enemy) { | |
cout << "You just got in a fight with " << enemy.name << " and won!" << endl; | |
// write fight algorithm | |
player.xp += dice(4); | |
player.gold += enemy.gold; | |
// delete the dead enemy | |
} | |
int dice(int sides) { | |
int value = rand()%sides+1; | |
return value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment