Page 1 of 1

C++ Text Based Adventure Engine

Posted: 07 Apr 2015, 22:48
by Lawnboy
So I've been working on some stuff for fun because of extra time I've had in a C++ class, and I've been having trouble with one function (because of poor programming ethics), but I feel like posting it anyway and maybe one of you could help me with it. It's basically a console-text based adventure game with a system straight ripped from D&D. I haven't done much beyond basic fight/skill check/choice scenarios, and it's a huge WIP, but you can check the source code if you want.

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;
}

Re: C++ Text Based Adventure Engine

Posted: 07 Apr 2015, 22:54
by Lawnboy
also this was a main.cpp that i whipped up as a test

Code: Select all

#include "Player.h"
#include <iostream>
using namespace std;
bool check_map(int x){
    bool check = false;
    if (x != 5){
        check = true;
    }
    return check;
}
int main(){
    srand(time(NULL));
    int active_map;
    Player user;
    user.set_stats(16, 14, 13, 12, 11, 10);
    cout << "Welcome to this game!\nYou are trapped within a dangerous castle. Your goal is to find the key. \nGood luck! \n";
    Room map[10];
    int avail_x[] = { 0, 1, 2, 0, 1, 2, 0, 1, 2, 2 };
    int avail_y[] = { 1, 1, 1, 2, 2, 2, 3, 3, 3, 4 };
    int rand_num;
    for (int x = 0; x < 10; x++){
        bool quit = false;
        while(!quit){
            rand_num = rand() % 10;
            if(check_map(avail_x[rand_num])){
                map[x].coordinate[0] = avail_x[rand_num];
                avail_x[rand_num] = 5;
            }
        }
    }
    for (int x = 0; x < 10; x++){
        bool quit = false;
        while(!quit){
            rand_num = rand() % 10;
            if(check_map(avail_y[rand_num])){
                map[x].coordinate[1] = avail_y[rand_num];
                avail_y[rand_num] = 5;
            }
        }
    }
    map[0].set_stats("You are in an empty room.", "null");
    map[1].set_stats("A rat comes from the darkness!", "apple");
    map[2].set_stats("Oh no, an ogre!.", "spear");
    map[3].set_stats("A dragon approaches!", "sword");
    map[4].set_stats("The room is littered with skeletons, but at least you're safe.", "null");
    map[5].set_stats("You seem to have triggered a trap...", "sword");
    map[6].set_stats("You hear the echo of your footsteps as you walk through this empty room.", "null");
    map[7].set_stats("The room is empty, but at least you're not being attacked.", "null");
    map[8].set_stats("A zombie knight comes forth!", "armor");
    map[9].set_stats("You found the key!.", "key");
    bool quit = false;
    for (int x = 0; x < 10; x++){
        cout << map[x].coordinate[0] << "," << map[x].coordinate[1] << "\n";
    }
    do{
        for (int x = 0; x < 10; x++){
            if (user.x == map[x].coordinate[0] && user.y == map[x].coordinate[1]){
                active_map = x;
            }
            else {
                active_map = 10;
            }
        }
        cout << map[active_map].description << "\n";
        switch (active_map){
            case 0:
                break;
            case 1:
                user.inventory[4] = user.scenario("The rat attempts to hit you!", 5, 5, 5, 5, map[active_map].content);
                break;
            case 2:
                user.inventory[5] = user.scenario("The ogre approaches you!", 15, 15, 15, 15, map[active_map].content);
                break;
            case 3:
                user.inventory[6] = user.scenario("The dragon attacks!", 20, 20, 20, 20, map[active_map].content);
                break;
            case 4:
                break;
            case 5:
                user.inventory[7] = user.scenario("A sword flies at you!", "You dodged the sword!", "The sword pierces your leg!", user.dex_mod, 16, 5,map[active_map].content);
                break;
            case 6:
                break;
            case 7:
                break;
            case 8:
                user.inventory[8] = user.scenario("He goes to attack you!", 16, 16, 16, 16, map[active_map].content);
                break;
            case 9:
                user.inventory[9] = map[active_map].content;
                quit = true;
                goto end;
                break;
            default:
                cout << "This room is empty. \n";
        }
        quit = user.checkdeath();
        if (quit){
            break;
        }
        cout << "Coordinates: " << user.x << "," << user.y << "\n";
        cout << "Press i to check inventory, or any other key to continue. \n";
        bool inv_quit = false;
        while (!inv_quit){
            if (getchar() == 'i'){
                user.check_inventory();
            }
            else if (getchar()){
                inv_quit = true;
            }
        }
        cout << "Which way will you go, (n)orth, (s)outh, (e)ast, or (w)est?";
        char dir;
        cin >> dir;
        switch (dir){
            case 'n':
                if (user.y + 1 != 4){
                    user.y = user.y + 1;
                }
                else if (user.x == 2){
                    user.y += 1;
                }
                else {
                    cout << "You cannot go this way. \n";
                }
                break;
            case 's':
                if (user.y - 1 != 0){
                    user.y -= 1;
                }
                else {
                    cout << "You cannot go this way. \n";
                }
                break;
            case 'e':
                if (user.x + 1 != 3){
                    user.x += 1;
                }
                else {
                    cout << "You cannot go this way. \n";
                }
                break;
            case 'w':
                if (user.x - 1 != -1){
                    user.x -= 1;
                }
                else {
                    cout << "You cannot go this way. \n";
                }
                break;
        }
        system("PAUSE");
    } while (!quit);
end:
    if (user.inventory[9] == "key"){
        printf("Congratulations, you won!");
    }
    system("PAUSE");
    return 0;