Code: Select all
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
class Player{
public:
int str = 1, str_mod, ac, hp, dex = 1, dex_mod, intel = 1, intel_mod, con = 1, con_mod, wis = 1, wis_mod, cha = 1, cha_mod, orig_hp;
int xp = 0;
int x = 0;
int y = 1;
int armor_hp;
int active_weapon = 0;
int level = 1;
int available_stats[6];
string name;
void set_stats();
void set_stats(int, int, int, int, int, int);
void take_damage(int);
bool check_stat(int);
void print_avail_stats();
void set_enemy_stats(int, int, int);
int user_fight(int, int, int,int);
string scenario(string, string, string, int, int, int,string);
string scenario(string, int, int, int,int,string);
int choice(string, int);
string choice(string, int, string);
bool checkdeath();
string inventory[10];
void set_inventory();
void print_stats();
void check_inventory();
};
void Player::print_stats(){
cout << "Strength = " << str;
}
void Player::set_inventory(){
if (inventory[active_weapon] == "sword"){
str = str + 3;
inventory[active_weapon] = "sword (in use)";
}
if (inventory[active_weapon] == "dagger"){
str = str + 3;
inventory[active_weapon] = "dagger (in use)";
}
if (inventory[active_weapon] == "spear"){
str = str + 2;
inventory[active_weapon] = "spear (in use)";
}
for (int x = 0; x < 5; x++){
if (inventory[x] == "sword (in use)" && x != active_weapon){
inventory[x] = "sword";
}
if (inventory[x] == "dagger (in use)" && x != active_weapon){
inventory[x] = "dagger";
}
if (inventory[x] == "spear (in use)" && x != active_weapon){
inventory[x] = "spear";
}
}
str_mod = (str - 9) / 2;
for (int x = 0; x < 10; x++){
if (inventory[x] == "armor"){
hp = hp + 5;
armor_hp += 5;
inventory[x] = "armor (in use)";
break;
}
}
if (hp > 100){
hp = 100;
}
}
void Player::check_inventory(){
for (int x = 0; x < 10; x++){
cout << x + 1 << ". " << inventory[x] << "\n";
}
cout << "Press 1 to set your weapon, 2 to eat something, and 3 to quit.";
int answer;
cin >> answer;
switch (answer){
case 1:
cout << "enter the number of your weapon.";
cin >> answer;
if (inventory[answer-1] == "sword"){
str = str + 3;
}
else if (inventory[answer-1] == "dagger"){
str = str + 3;
}
else if (inventory[answer-1] == "spear"){
str = str + 2;
}
active_weapon = answer - 1;
break;
case 2:
cout << "enter the number of your food.";
cin >> answer;
if (inventory[answer-1] == "apple"){
hp += 5;
inventory[answer - 1] = " ";
}
break;
default:
break;
}
set_inventory();
}
string Player::choice(string a, int y, string b){
bool death = checkdeath();
if (!death){
cout << a;
int answer;
bool quit = false;
for (int x = 0; x <= y; x++){
cout << x + 1 << "\n";
}
while (!quit){
cin >> answer;
if (answer - 1 <= y){
quit = true;
}
}
return b;
}
else {
return "null";
}
}
int Player::choice(string a, int y){
bool death = checkdeath();
if (!death){
cout << a;
int answer = 0;
bool quit = false;
for (int x = 0; x <= y; x++){
cout << x+1 << "\n";
}
while (!quit){
cin >> answer;
if (answer-1 <= y){
quit = true;
}
}
return answer-1;
}
else {
return 0;
}
}
bool Player::checkdeath(){
if (hp <= 0){
return true;
}
else {
return false;
}
}
int roll_check(int x, int y){ //Player's stat mod, stat Player's going up against (used for finding whether something hits/skill checks)
int roll = (rand() % 20) + x;
//cout << "You rolled " << roll << ". \n";
if (roll + x >= y){
printf("Successful! ");
return 1;
}
else {
printf("Unsuccessful. \n");
return 0;
}
}
int find_damage(int t, int x){ //return from roll_check, str_mod;
int damage;
if (t == 1){
damage = (rand() % 8) + x;
cout << " \n A hit of ";
cout << damage;
cout << " damage. \n";
}
else if (t == 0) {
damage = 0;
}
return damage;
}
bool Player::check_stat(int x){ // stat being checked
bool check = false;
for (int i = 0; i<6; i++){
if (x == available_stats[i]){
available_stats[i] = 0;
check = true;
}
}
return check;
};
void Player::print_avail_stats(){
printf("Available stats: ");
for (int i = 0; i<6; i++){
if (available_stats[i] != 0){
cout << available_stats[i] << " ";
}
}
printf(" \n");
}
void Player::set_stats(){
for (int x = 0; x < 6; x++){
switch (x){
case 0:
available_stats[x] = 16;
break;
case 1:
available_stats[x] = 14;
break;
case 2:
available_stats[x] = 13;
break;
case 3:
available_stats[x] = 12;
break;
case 4:
available_stats[x] = 11;
break;
case 5:
available_stats[x] = 10;
break;
}
}
ac = 16;
hp = 20;
orig_hp = hp;
printf("Enter your name: ");
cin >> name;
print_avail_stats();
printf("Enter a number for strength: ");
while (check_stat(str) == false){
cin >> str;
}
print_avail_stats();
printf("Enter a number for dexterity: ");
while (check_stat(dex) == false){
cin >> dex;
}
print_avail_stats();
printf("Enter a number for intelligence: ");
while (check_stat(intel) == false){
cin >> intel;
}
print_avail_stats();
printf("Enter a number for constitution: ");
while (check_stat(con) == false){
cin >> con;
}
print_avail_stats();
printf("Enter a number for wisdom: ");
while (check_stat(wis) == false){
cin >> wis;
}
print_avail_stats();
printf("Enter a number for charisma: ");
while (check_stat(cha) == false){
cin >> cha;
}
str_mod = (str - 9) / 2;
dex_mod = (dex - 9) / 2;
intel_mod = (intel - 9) / 2;
con_mod = (con - 9) / 2;
wis_mod = (wis - 9) / 2;
cha_mod = (cha - 9) / 2;
armor_hp = orig_hp;
inventory[0] = "dagger";
inventory[1] = "apple";
inventory[2] = "apple";
set_inventory();
}
void Player::set_stats(int a, int b, int c, int d, int e, int f){ //str dex intel con wis cha
printf("Enter your name: ");
cin >> name;
ac = 16;
hp = 20;
orig_hp = hp;
str = a;
dex = b;
intel = c;
con = d;
wis = e;
cha = f;
str_mod = (str - 9) / 2;
dex_mod = (dex - 9) / 2;
intel_mod = (intel - 9) / 2;
con_mod = (con - 9) / 2;
wis_mod = (wis - 9) / 2;
cha_mod = (cha - 9) / 2;
inventory[0] = "dagger";
inventory[1] = "apple";
inventory[2] = "apple";
armor_hp = orig_hp;
set_inventory();
}
void Player::set_enemy_stats(int x, int y, int z){ //str, ac, hp
str = x;
ac = y;
hp = z;
str_mod = (str - 9) / 2;
}
void Player::take_damage(int x){ //damage he's taking
hp = hp - x;
}
int Player::user_fight(int x, int y, int z, int b){ //enemy str,ac,hp,dex
bool death = checkdeath();
if (!death){
string choice;
Player enemy;
int percentage;
enemy.set_enemy_stats(x, y, z);
while ((hp >= 0) || (enemy.hp >= 0)){
printf("Enter 1 to fight, 2 to run away, and 3 to check your inventory.");
cin >> choice;
if (choice == "1"){
printf("You attempt to hit the enemy. \n");
enemy.take_damage(find_damage(roll_check(str_mod, enemy.ac), str_mod));
if (enemy.hp <= 0){
printf("Success! The enemy died! \n");
return 1;
break;
}
printf("The enemy attempts to hit you. \n");
take_damage(find_damage(roll_check(enemy.str_mod, ac), enemy.str_mod));
if (hp <= 0){
printf("You died. \n");
return 0;
break;
}
}
else if (choice == "2"){
if (roll_check(dex_mod, b) == 1){
return 0;
break;
}
else{
printf("The enemy attempts to hit you. \n");
take_damage(find_damage(roll_check(enemy.str_mod, ac), enemy.str_mod));
if (hp <= 0){
printf("You died. \n");
return 0;
break;
}
}
}
else if (choice == "3"){
check_inventory();
printf("The enemy attempts to hit you. \n");
take_damage(find_damage(roll_check(enemy.str_mod, ac), enemy.str_mod));
if (hp <= 0){
printf("You died. \n");
return 0;
break;
}
}
else{
printf("Please enter a valid number. \n");
}
if (hp <= 0){
printf("You died. \n");
return 0;
}
percentage = (hp * 100 / armor_hp);
cout << "[ ";
cout << "You have " << percentage << "% health left.";
cout << " ] \n";
}
if (hp <= 0){
printf("You died. \n");
return 0;
}
}
else {
return 0;
}
}
string Player::scenario(string b, int x, int y, int z, int d, string c){ //dexterity
bool death = checkdeath();
if (!death){
cout << b << " \n";
int fight = user_fight(x, y, z, d);
if (fight == 1){
cout << "You fond a(n) " << c << "!";
return c;
}
else
{
return "null";
}
}
else {
return "null";
}
}
string Player::scenario(string b, string c, string d, int x, int y, int z, string a){ //string b = story text,,
bool death = checkdeath();
if (!death){
cout << b << " \n"; // check, c = failure text, d = success text, x = user's stat y = other num, z = health subtraction
//y = (y - 9) / 2;
if (roll_check(x, y) == 1){ //if failed
cout << d;
}
else {
cout << c << " You lost " << z << " health. \n";
hp = hp - z;
}
}
if (!death){
cout << "You fond a(n) " << a << "!";
return a;
}
else
{
return "null";
}
}
class Room{
public:
int width = 0, height = 0;
int coordinate[2];
string description = " ";
string content;
//void WriteLevel(int);
//void set_stats(int, int, string, string, int, int);
void set_stats(string, string);
void set_coord(int,int);
};
//void Room::set_stats(int x, int y, string a, string c, int d, int f){
void Room::set_stats(string a, string c){ //description , content, x, y
description = a, content = c;
}
void Room::set_coord(int x, int y){
coordinate[0] = x;
coordinate[1] = y;
}