/*
 *** TEXTGAME1.CPP ***
 VERSION 1.0 OF TEXTGAME
 Written by lightatdawn
 This is the basic framework for starting a simple text based game.
 There are many different methods I can think of to do this, but
 this is the method I picked. I will work from this stage to a much
 more advanced stage in later versions.
 You cant win or do anything except walk around in this version. Get
 familiar with the framework cuz its gonna get loaded in version 2.0.
 *** GENERAL NOTES ***
 I am going to comment EVERYTHING to make it easy to understand.
 I'm making this program in a Win32 Console platform. If it doesnt function
 right on your compiler... Ugg... (It should though.)
 I'm also not going to use cout or cin. It will be replaced by printf and gets.
 Hope this doesnt pose a problem, its just that i like printf...
 Our WORLD is set up in a grid. The grid is 3 squares by 3 squares:
 -------------
 |0x0|0x1|0x2|
 -------------
 |1x0|1x1|1x2|
 -------------
 |2x0|2x1|2x2|
 -------------
 So we map our players location with 2 variables, x and y. x is the horizontal
 positions and y is the vertical ones.
*/
//******************************************************************************
//* INCLUDE FILES                                                              *
//******************************************************************************
#include <windows.h> //we need this for use of the BOOL type
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
//******************************************************************************
//* MAIN PROCEDURE                                                             *
//******************************************************************************
int main(void)
{
	//***************************************************************************
	//* VARIABLE DECLARATION                                                    *
	//***************************************************************************
	//Dont forget that arrays start at 0 and go to x-1. Meaning that an_array[3]
	//would have 3 elements at 0, 1, 2 respectivly. NOT 1,2,3!!
	//This array will hold the text for each room with up to 5 lines of
	//text in each... Each line may be up to 100 characters long.
	//Hence, [3][3][5][100]. 3x3 World. 5 lines. 100 characters long.
	//I dont feel like explaining multi-dimensional arrays in great detail.
	char text[3][3][5][100];
	//We will set this up to tell us what squares you are allowed
	//to walk to from each room. It would help if you drew your
	//world out on a sheet of graph paper or something...
	//We initialize the exit_ arrays to FALSE before we start. Now they
	//are all zero and we will declare the exit_ to equal TRUE later
	//but ONLY if there IS an exit. :)
	BOOL exit_n[3][3] = {FALSE,FALSE,FALSE,
						 FALSE,FALSE,FALSE,
						 FALSE,FALSE,FALSE,},
		 exit_s[3][3] = {FALSE,FALSE,FALSE,
						 FALSE,FALSE,FALSE,
						 FALSE,FALSE,FALSE},
		 exit_e[3][3] = {FALSE,FALSE,FALSE,
						 FALSE,FALSE,FALSE,
						 FALSE,FALSE,FALSE},
		 exit_w[3][3] = {FALSE,FALSE,FALSE,
						 FALSE,FALSE,FALSE,
						 FALSE,FALSE,FALSE};
	int location_x = 1,		//The X location on our 3x3 World grid of the player
		location_y = 2,		//The Y location
		command_accepted,	// --- Explained later ---
		loop;				//Used in our for loops throughout the function
	char user_input[30];	//Variable that we store the user input in for examination
	//MAP OF THE WORLD
	//Im going to declare the rooms starting with the top left and working right then down...
	//The ---- and the | are the squares. The "#" are the walls (duh!)
    /*
    |------------------|-------------------|-------------------|
    |##################|###################|###################|
    |#                #|#                  |                  #|
    |#                #|#                  |    Another       #|
    |#  You cant ever #|#   Really Dark    |   Really Dark    #|
    |#   get to this  #|#       Room       |      Room        #|
    |#     square     #|#                  |                  #|
    |#                #|#                  |                  #|
    |##################|########   ########|###################|
    |------------------|-------------------|-------------------|
    |##################|########   ########|###################|
    |#                #|#                 #|#                 #|
    |#                 |                  #|#                 #|
    |#                 |     Open Room     |      Moldy       #|
    |#     Empty Room #|#                  |        Room      #|
    |#                #|#                 #|#                 #|
    |#                #|#                 #|#                 #|
    |#                #|#######     #######|#                 #|
    |------------------|-------------------|-------------------|
    |#                #|#######     #######|#                 #|
    |#                #|#                 #|#                 #|
    |#                #|#                 #|#                 #|
    |#  Empty Room    #|#                 #|#                 #|
    |#                #|#                 #|#       Oaken     #|
    |#                #|#    You are      #|#         Throne  #|
    |#                #|#      Here       #|#      [****]     #|
    |##################|###################|###################|
    |------------------|-------------------|-------------------|
    */
	//***************************************************************************
	//* SET UP THE GAME VARIABLES                                               *
	//***************************************************************************
	//COLUMN 0, ROW 0
	//There is no way into this room so everything is blank here...
	strcpy(text[0][0][0], "");
	strcpy(text[0][0][1], "");
	strcpy(text[0][0][2], "");
	strcpy(text[0][0][3], "");
	strcpy(text[0][0][4], "");
	//COLUMN 1, ROW 0
	strcpy(text[1][0][0], "This room is very dark. The floor here is wet and the air is cold.");
	strcpy(text[1][0][1], "You can not even see your fingers in front of your face.");
	strcpy(text[1][0][2], "");
	strcpy(text[1][0][3], "");
	strcpy(text[1][0][4], "");
	//there is an exit here but since its too dark to see, we didnt tell the
	//player about it. :)
	exit_e[1][0] = TRUE;
	exit_s[1][0] = TRUE;
	//COLOUM 2, ROW 0
	strcpy(text[2][0][0], "This room is just as dark as the last.");
	strcpy(text[2][0][1], "");
	strcpy(text[2][0][2], "");
	strcpy(text[2][0][3], "");
	strcpy(text[2][0][4], "");
	//I put some gold in this room. If the player searches, he will find it...
	exit_w[2][0] = TRUE;
	//COLOUM 0, ROW 1
	strcpy(text[0][1][0], "There is a large jagged hole in the ceiling here. You can see blue sky");
	strcpy(text[0][1][1], "above you. This room continues farther to the south. A stone archway");
	strcpy(text[0][1][2], "lies to the east.");
	strcpy(text[0][1][3], "");
	strcpy(text[0][1][4], "");
	exit_e[0][1] = TRUE;
	exit_s[0][1] = TRUE;
	//COLOUM 1, ROW 1 (MIDDLE OF OUR WORLD)
	strcpy(text[1][1][0], "You are in a large room. Tall grass grows through the cracks in the");
	strcpy(text[1][1][1], "flagstones. Sunlight shines through from a large section of the south");
	strcpy(text[1][1][2], "ceiling that has caved in. Three archways lead into darker sections of");
	strcpy(text[1][1][3], "the castle. One north, one west, and one east. An iron door stands");
	strcpy(text[1][1][4], "open to the south.");
	exit_e[1][1] = TRUE;
	exit_s[1][1] = TRUE;
	exit_n[1][1] = TRUE;
	exit_w[1][1] = TRUE;
	//COLOUM 2, ROW 1
	strcpy(text[2][1][0], "This room extends farther to the south. The smell of must and mildew");
	strcpy(text[2][1][1], "wafts from the floor as you disturb the air. Bones of small animals");
	strcpy(text[2][1][2], "crunch under your feet.");
	strcpy(text[2][1][3], "");
	strcpy(text[2][1][4], "");
	exit_w[2][1] = TRUE;
	exit_s[2][1] = TRUE;
	//COLOUM 0, ROW 2
	strcpy(text[0][2][0], "The only light here comes from farther north in the room.");
	strcpy(text[0][2][1], "");
	strcpy(text[0][2][2], "");
	strcpy(text[0][2][3], "");
	strcpy(text[0][2][4], "");
	exit_n[0][2] = TRUE;
	//COLUMN 1, ROW 2 (STARTING POINT)
	strcpy(text[1][2][0], "You are in a dimly lit stone walled room. Rocks and other rubble litters");
	strcpy(text[1][2][1], "the floor. The only light comes from a small crack in the ceiling. There");
	strcpy(text[1][2][2], "is a stone arch to the north.");
	strcpy(text[1][2][3], "");
	strcpy(text[1][2][4], "");
	exit_n[1][2] = TRUE;
	//COLOUM 2, ROW 1
	strcpy(text[2][2][0], "The air here is unnaturally cold. An eerie blue light shines from the");
	strcpy(text[2][2][1], "southern wall. As you get closer you see the light comes from the skull");
	strcpy(text[2][2][2], "of a human skeleton seated upon a great oaken throne. He holds a razor");
	strcpy(text[2][2][3], "edged sword that glints in the light from his eye sockets.");
	strcpy(text[2][2][4], "");
	exit_n[2][2] = TRUE;
	//Display some basic information about the game commands
	printf("Type 'quit' to exit the game.\n");
	//***************************************************************************
	//* MAIN LOOP                                                               *
	//***************************************************************************
	command_accepted = 1; //So that the text is printed the first time around...
	//The program will now loop, catching input, printing text, and generally
	//doing everything that makes a game a game, until the user quits
	do
	{
		//Explaining printf: (Dont know if you know printf. If you do, sorry, ignore this.)
		//  -The %s tells printf that the variable (text[][][]) is to be displayed
		//   at this point. You use different "conversion-type" characters for different
		//   types of variables. %s is for a string, %d is for integers, etc...
		//  -The \n is like pressing ENTER. Without this the next printf would continue
		//   on the same line. Try removing it and watch what happens. \n\n would leave
		//   a blank line between each of your text lines. You get the picture...
		printf("\n\n");
		//If you try to walk in a direction you cannot: command_accepted is 2.
		//And if you typed in nonsense then it equals 0. Now the rooms text wont
		//re-print unless you actually move!
		//The results of the command_accepted variable are calculated at the
		//bottom of the do...loop.
		if (command_accepted == 1)
		{
			//print all the text for this room
			for(loop = 0; loop < 5; loop ++)
			{
				printf("%s\n", text[location_x][location_y][loop]);
			}
			//Explaining what we just did:
			//  -We just printed out the text for the section of world that our character
			//   is standing in. This prints the info we set up in setup_game_variables()
			//   depending on where we are. If we move, we change our location and the loop
			//   will printf the text from there. This is why arrays are so important. You
			//   dont want to have to write out a whole set of printing commands for EVERY
			//   room in your world.
		}//end if
		//************************************************************************
		//* GET INPUT FROM USER                                                  *
		//************************************************************************
		gets(user_input);
		printf("\n\n");
		//************************************************************************
		//* EXECUTE THE INPUT (DO STUFF)                                         *
		//************************************************************************
		//We declare command_accepted = 0 now, and then under each command that is allowed
		//we declare command_accepted = 1. Then, at the end, we check it. If its equal to
		//0 then that means whatever the user typed, we didnt understand. So if this happens
		//we'll print something like: "I didnt understand that!"
		command_accepted = 0;
		//This checks to see if the player wishes to go NORTH
		//I used a few different things i thought a person might say. You may want to change
		//or add more. ... stricmp(char*,char*); is used to compare two strings. If the strings
		//are the same, it returns 0. So thats how it works. if (user_input == "north") ... basically
		if (stricmp(user_input, "north") == 0 ||
			stricmp(user_input, "n") == 0 ||
			stricmp(user_input, "go north") == 0 ||
			stricmp(user_input, "walk north") == 0)
		{
			//One of those commands did something so we remember this. Setting
			//this to 1 will tell us to print the room text again next loop.
			command_accepted = 1;
			if (exit_n[location_x][location_y])
				location_y --;
			else
				command_accepted = 2; //remember that we cant go that way
		}//END OF "GO NORTH"
		//Same deal but for south
		if (stricmp(user_input, "south") == 0 ||
			stricmp(user_input, "s") == 0 ||
			stricmp(user_input, "go south") == 0 ||
			stricmp(user_input, "walk south") == 0)
		{
			command_accepted = 1;
			if (exit_s[location_x][location_y])
				location_y ++;
			else
				command_accepted = 2;
		}//END OF "GO SOUTH"
		//Same deal but for east
		if (stricmp(user_input, "east") == 0 ||
			stricmp(user_input, "e") == 0 ||
			stricmp(user_input, "go east") == 0 ||
			stricmp(user_input, "walk east") == 0)
		{
			command_accepted = 1;
			//east and west change the x coord not the y, of course...
			if (exit_e[location_x][location_y])
				location_x ++;
			else
				command_accepted = 2;
		}//END OF "GO EAST"
		//Yup, you guessed it: west!
		if (stricmp(user_input, "west") == 0 ||
			stricmp(user_input, "w") == 0 ||
			stricmp(user_input, "go west") == 0 ||
			stricmp(user_input, "walk west") == 0)
		{
			command_accepted = 1;
			if (exit_w[location_x][location_y])
				location_x --;
			else
				command_accepted = 2;
		}//END OF "GO WEST"
		//************************************************************************
		//* A FEW VARIOUS COMMANDS                                               *
		//************************************************************************
		if (strnicmp(user_input, "yell", 4) == 0 ||
			strnicmp(user_input, "scream", 6) == 0)
		{
			printf("\nThrowing a tantrum isn't going to get you anywhere.\n");
			command_accepted = 3;
		}
		if (strnicmp(user_input, "jump", 4) == 0 ||
			strnicmp(user_input, "leap", 4) == 0 ||
			strnicmp(user_input, "hop", 3) == 0)
		{
			printf("\nYou're not a frog, you're a human. Do what humans do... Walk.\n");
			command_accepted = 3;
		}
		if (strnicmp(user_input, "smash", 5) == 0 ||
			strnicmp(user_input, "break", 5) == 0 ||
			strnicmp(user_input, "wreck", 5) == 0 ||
			strnicmp(user_input, "destroy", 7) == 0)
		{
			printf("\nWell aren't we feeling violent today! Your not going to do anything but hurt\n");
			printf("yourself if you keep that up.\n");
			command_accepted = 3;
		}
		if (strnicmp(user_input, "climb", 5) == 0)
		{
			printf("\nThere is nothing to climb here.\n");
			command_accepted = 3;
		}
		if (strnicmp(user_input, "eat", 3) == 0)
		{
			printf("\nYou are a little hungry but you'll have to wait until you get back to your\n");
			printf("village before you can do that. Theres nothing edible around here.\n");
			command_accepted = 3;
		}
		//if the user entered a command that is not a valid command, then command_accepted
		//will still be zero (as it was declared above). So do this:
		if (command_accepted == 0)	printf("\nPlease re-phrase your statement...\n");
		if (command_accepted == 2)	printf("\nYou cant go that way!\n");
	//END OF LOOP! IF THE INPUT ISNT "QUIT" THEN GO BACK UP TO THE TOP
	}while(stricmp(user_input, "quit") != 0);
	//Hello! The input must've equalled "QUIT" cus we're out here now... the end
	return 0;
}
