Jump to content


Help Support Me

This is the cost to maintain my resources for Crazywink Studios a month.
Amount received so far:
$0.00 / $10.00
Consider donating here: Donate
$0 out of $10

Top Posters

  • Toby12600Photo
    1. Toby12600

    111 posts

  • CrazywinkPhoto
    2. Crazywink

    79 posts

  • TrutatjPhoto
    3. Trutatj

    29 posts

  • ClaPPyyPhoto
    4. ClaPPyy

    14 posts

  • bhupeshPhoto
    5. bhupesh

    8 posts

  3 Views · 0 Replies

 Photo

Learning C++ For Game Hacking Part 22 - Town Game

13 Apr 2014

Posted by Crazywink in C++ For Game Hacking

source code

 

menu.h

//menu.h
#include <iostream>
#include <string>
using namespace std;

namespace menuNamespace
{
	int menu(string* strArray, int size);
}
//menu.cpp
#include <iostream>
#include <string>
using namespace std;

namespace menuNamespace
{
	int menu(string* strArray, int size)
	{
		int userInput = 0;

		cout << "Menu: \n";
		while (userInput < 1 || userInput > size)
		{
			for (int i = 0; i < size; i++)
			{
				cout << i + 1 << "] " << strArray[i] << "\n";
			}
			cin >> userInput;
		}
		return userInput;
	}
}
//town.h
#include <iostream>
#include <string>
#include "menu.h"
using namespace std;


//Function Declarations
namespace street
{
	void menu(void);
}
namespace weaponShop
{
	void menu(void);
}
namespace docks
{
	void menu(void);
}
namespace tavern
{
	void menu(void);
}
//town.cpp
#include <iostream>
#include <string>
#include "town.h"
#include "menu.h"
using namespace std;

//include our menu into the global namespace
using menuNamespace::menu;

//the code - handles everything at the docks
namespace docks
{
	void menu()
	{
		string options[] = {"Jump in the water","Grab a rowboat","Board the boat","Go to town"};
		int userInput = ::menu(options, (sizeof(options) / sizeof(options[0]))); //using global menu, which we used the using statement for

		switch (userInput)
		{
		case 1:
			cout << "You jump into the water!\n";
			menu();
			break;
		case 2:
			cout << "You grab a rowboat and start rowing\n";
			menu();
			break;
		case 3:
			cout << "You board the boat and wait for it to sail.\n";
			menu();
			break;
		case 4:
			street::menu(); //street namespace in our town header
		}
	}
}

//Handles tavern scene
namespace tavern
{
	void menu(void)
	{
		string options[] = {"Buy a drink","Start a fight!","Leave to the streets"};
		int userInput = ::menu(options, (sizeof(options) / sizeof(options[0])));

		switch (userInput)
		{
		case 1:
			cout << "You order a drink.\n";
			menu();
			break;
		case 2:
			cout << "You start a fight in the tavern!\n";
			menu();
			break;
		case 3:
			street::menu();
			break;
		}
	}
}

//Handles our street scene
namespace street
{
	void menu(void)
	{
		string options[] = {"Go to the docks","Go to the tavern","Go to weapon shop","Start a fight!"};
		int userResponse = ::menu(options, (sizeof(options) / sizeof(options[0])));

		switch (userResponse)
		{
		case 1:
			docks::menu();
			break;
		case 2:
			tavern::menu();
			break;
		case 3:
			weaponShop::menu();
			break;
		case 4:
			cout << "You start a fight in the streets!\n";
			street::menu();
			break;
		}
	}
}

//Weapon shop!
namespace weaponShop
{
	void menu(void)
	{
		string options[] = { "Buy a dagger for 300 gold", "Buy a sword for 500 gold", "Buy an axe for 600 gold", "Leave the shop" };
		int userInput = ::menu(options, (sizeof(options) / sizeof(options[0])));

		switch (userInput)
		{
		case 1:
			cout << "You purchase the dagger for 300 gold!";
			menu();
			break;
		case 2:
			cout << "You purchase the sword for 500 gold!";
			menu();
			break;
		case 3:
			cout << "You purchase the axe for 600 gold!";
			menu();
			break;
		case 4:
			street::menu();
			break;
		}
	}
}


//Actually start teh game here
int main(void)
{
	cout << "Welcome to town!\n";
	cout << "You are currently at the docks!\n";
	docks::menu();

	return 0;
}

  13 Views · 0 Replies


Latest Discussions

Online Users

0 members, 1 visitors and 0 anonymous users


  • 262 Total Posts
  • 80 Total Members
  • cumcon1992 Newest Member
  • 137 Most Online

1 users are online (in the past 15 minutes)

0 members, 1 guests, 0 anonymous users   (See full list)


Portal v1.3.0 by DevFuse | Based on IP.Board Portal by IPS