Code:
//-------------------------------------------------------------------------
// Kirdra 2002 Haunted Forest v0.1
//-------------------------------------------------------------------------
#include <conio.h> //getch()
#include <iostream> //cin/cout etc
#include <stdlib.h> //Clear the screen - system("cls");
//-------------------------------------------------------------------------
// Variable names pretty much decribe what they do
//-------------------------------------------------------------------------
int Win_Condition = 0;
int Treasure = 0;
int Weapon = 0;
int User_Location_X = 0;
int User_Location_Y = 0; //Position on the grid
int User_Input, Q = 0; //I like to use Q as a Quit Game variable
int Game_Over = 100; //Turns before game over
int Display_Room() //Function - Include for example purposes
{
cout << "X position = "<<User_Location_X<<"\n";
cout << "Y position = "<<User_Location_Y<<"\n\n";
}
//-------------------------------------------------------------------------
// The program begins
//-------------------------------------------------------------------------
int main()
{
cout << "This game is designed to be used on the number pad.\n";
cout << "To quit the game press 0 then Enter, to see your\n";
cout << "current co-ordinates press 5 then Enter. To use the\n";
cout << "directions simple press the number besides the\n";
cout << "direction name."; //Information for users
getch(); //Waits for a key press before continuing
//-------------------------------------------------------------------------
// The loop begins
//-------------------------------------------------------------------------
do
{
//-------------------------------------------------------------------------
// Define our barriers/walls
//-------------------------------------------------------------------------
if (User_Location_X == 6)
{
cout << "You can not continue in this direction.\n";
User_Location_X--;
getch();
}
else if(User_Location_X == -6)
{
cout << "You can not continue in this direction.\n";
User_Location_X++;
getch();
}
else if(User_Location_Y == 6)
{
cout << "You can not continue in this direction.\n";
User_Location_Y--;
getch();
}
else if(User_Location_Y == -6)
{
cout << "You can not continue in this direction.\n";
User_Location_Y++;
getch();
}
//-------------------------------------------------------------------------
// Game over if 100 turns expire
//-------------------------------------------------------------------------
Game_Over = Game_Over--; //Game_Over is reduced by 1 each loop when
if(Game_Over == 0) //it reaches 0 the game ends
{
system("cls");
cout << "Night falls, the monsters of the Forest come out to play.\n\n";
cout << "Game Over.";
getch();
return 0; //This teminates the program
}
//-------------------------------------------------------------------------
// This text will display every loop
//-------------------------------------------------------------------------
system("cls");
cout << "Haunted Forest v0.1\n\n";
cout << "8)North\n6)East\n2)South\n4)West\n\n";
cin >> User_Input;
cout << "\n";
//-------------------------------------------------------------------------
// If user wants to quit
//-------------------------------------------------------------------------
if(User_Input == 0)
{
system("cls");
cout << "Night falls, the monsters of the Forest come out to play.\n\n";
cout << "Game Over.";
getch();
return 0; //This teminates the program
}
//-------------------------------------------------------------------------
// Updating Co-ordinates and outputing relavent text to screen
//-------------------------------------------------------------------------
switch(User_Input)
{
case 8: cout << "You venture north.\n\n";
User_Location_Y++; break;
case 6: cout << "You venture east.\n\n";
User_Location_X++; break;
case 2: cout << "You venture south.\n\n";
User_Location_Y--; break;
case 4: cout << "You venture west.\n\n";
User_Location_X--; break;
case 5: Display_Room();
}
//-------------------------------------------------------------------------
// Now to include special areas (rooms that do something)
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
// Treasure room
//-------------------------------------------------------------------------
if(User_Location_X == 0 && User_Location_Y == 5)
{
if(Treasure == 1) //If the user has already been here
{
cout << "You see an empty treasure chest.\n\n";
}
else if(Treasure == 0) //If the user has not been to this area
{
cout << "You have found the treasure of the Haunted Forest.\n\n";
Treasure = 1; //Telling the program the user has been here
} //for the next loop for the relevant message
}
//-------------------------------------------------------------------------
// Weapon room
//-------------------------------------------------------------------------
if(User_Location_X == 5 && User_Location_Y == 0)
{
if(Weapon == 1)
{
cout << "You see marks in a tree made from a knife.\n\n";
}
else if(Weapon == 0)
{
cout << "You have found the Haunted Dagger.\n\n";
Weapon = 1;
}
}
//-------------------------------------------------------------------------
// Output for a winner
//-------------------------------------------------------------------------
if(Win_Condition == 1)
{
system("cls");
cout << "You have completed Haunted Forest v0.1\n\n";
getch();
return 0; //This teminates the program
}
//-------------------------------------------------------------------------
// The message after a turn expires
//-------------------------------------------------------------------------
cout << "The Sun begins to fade it is getting darker.";
getch();
system("cls");
//-------------------------------------------------------------------------
// End the do loop
//-------------------------------------------------------------------------
} //This Bracket ends the "do" that was opened for the loop
while(Q!=1); //Because the loop has ended the program will read to the
//end of the code thus quiting
//-------------------------------------------------------------------------
// End the program
//-------------------------------------------------------------------------
return 0; //This teminates the program
} //int main()
I was so happy when I finished this game. Short but I learned a lot through it, more so through ideas than accual code. It took a bit of thinking to come up with the barrier/wall system. although it is very simple
This source code has been stiriped down from the original btw. Just want to show the basic structure of the program.
*Edit* Quick bug fixed