Monday, September 6, 2010

A Simple Adventure Game (C++)

#include <iostream>
#include <conio.h>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#define MAX 100
using namespace std;

struct GAME
{                            //struct definition
    char menu[15];

};

const int row=9,col=8;
int i,j,current_i,current_j,step;                  //global variable
//char* board[row][col];
GAME advGame[MAX];
int no_menu;
int hp=20, mp=20, ehp=30, move, emove;

struct WORLD                                       //world of the game
{
    char* board[row][col];                         //the board, or the playing field
    int game_element[row][col];                    //1 resting,2 question,3 portal/jump,4 battle
}world;                                            //note: make game_element at [0][0],[9][8]

//function prototypes
int dice(void);
void intBoard(void);
void intgame_element(void);
void displayBoard(void);
void stepsBoard(int steps);
void initialMenu(GAME advGame[MAX]);
void GameStart(void);
void S_player(void);                             
void newGame(void);
void guideline(void);
void credit(void);
void selectMenu(int no_menu);
void displayMenu(GAME advGame[MAX]);
void wait(float seconds);
void rest(void);
void portal(void);
int battle (void);
void win(void);
void lose(void);
//void question();

//======================================================================================================

//PRIMARY MAIN FUNCTION
int main(void)
{
   //WORLD world;
   srand(time(NULL)) ;                           //set rand to use time as seed
   
   initialMenu(advGame);
   displayMenu(advGame);    //display game menu
   cout<<"\n Enter Number: ";             //choose menu
   cin>>no_menu;
   system("cls");
   selectMenu(no_menu);
   getch();
}
//======================================================================================================
//create game menu function (initialize)
void initialMenu(GAME advGame[MAX])
{
   strcpy(advGame[0].menu,". NEW GAME");
   strcpy(advGame[1].menu,". GUIDELINE");
   strcpy(advGame[2].menu,". ABOUT");
   strcpy(advGame[3].menu,". EXIT");
}
//======================================================================================================
//display game menu
void displayMenu(GAME advGame[MAX])
{
    cout<<"\n Game Title    : "<<endl<<" Team        : Mytho (Legend) "    <<endl<<" By        : @ZRI & ZomBiedy "<<endl<<"\n GAME MENU:"<<endl;
   for(int i=0;i<4;i++)
       cout<<"\t"<<(i+1)<<advGame[i].menu<<endl;
}
//======================================================================================================
void selectMenu(int no_menu)
{
     if(no_menu==1)
       newGame();
   else if(no_menu==2)
       guideline();
   else if(no_menu==3)
       credit();
   else if(no_menu==4)
      cout<<" EXIT "<<endl;
   else
      cout<<" Wrong number entered"<<endl;
}
//======================================================================================================
void newGame()
{
    int mode;
   system("cls");
     cout<<"\n SELECT M0DE:"<<endl<<"\t1) Single Player "<<endl<<"\t2) Multi Player "<<endl;
   cout<<" Enter Mode: ";
   cin>>mode;
   if(mode==1)
       S_player();
   else if(mode==2)
       cout<<" Multi Player"<<endl;
       //M_player();
   else
       cout<<" Wrong mode entered"<<endl;
}
//======================================================================================================
void guideline()
{
    cout<<" GAME GUIDELINE:"<<endl;
    system("PAUSE");
    system("cls");
   displayMenu(advGame);
   cout<<"\n Enter Number: ";             //choose menu
   cin>>no_menu;
   system("cls");
   selectMenu(no_menu);

}
//======================================================================================================
void credit()
{
    cout<<" ABOUT GAME:"<<endl;
   cout<<"\n This two (2) dimensional game has been created by 2 UiTM student (Diploma"<<endl<<" in Computer Science) named; Muhammad Azri bin Jasni (@zri) and his friend,"<<endl;
   cout<<" Mohd Zabiedy bin Mohd Sulaiman (ZomBiedy)."<<endl<<" \n\tThe game has been done as an Laboratory Assignment for CSC 138 - "<<endl;
   cout<<" Structured Programming during Semestar July - September 2010. One of the"<<endl<<" objective of this assignment is to determine whether computer science (part 2)"<<endl;
   cout<<" understand the concept of structured programming,its component and also know"<<endl<<" how is applying them."<<endl;
   system("PAUSE");
   system("cls");
   displayMenu(advGame);
   cout<<"\n Enter Number: ";             //choose menu
   cin>>no_menu;
   system("cls");
   selectMenu(no_menu);
}
//======================================================================================================
void S_player()
{
    char start;
   int x=0;
   system("cls");
     cout<<"\n You are playing SINGLE MODE."<<endl<<" Press 'S' button to start game: ";
    cin>>start;
   if(start=='S'||start=='s')
       GameStart();
   else
   {    do
       {    cout<<" Wrong input"<<endl;
             cout<<"  "<<(x+1)<<" Enter 'S' button: ";
           cin>>start;
            if(start=='S'||start=='s')
                GameStart();
           x++;
      }while(x<3&&(start!='S'||start!='s'));
      displayMenu(advGame);
      cout<<"\n Enter Number: ";             //choose menu
      cin>>no_menu;
      system("cls");
      selectMenu(no_menu);
   }
}
//======================================================================================================
//starting of game
void GameStart()
{
    int no_menu;
    intBoard();
    intgame_element();
   system("cls");
   cout<<"\n GAME START !!!...GOOD LUCK"<<endl;                                              //initialize board
   displayBoard();
   while(strcmp(world.board[8][7],"  P 1  ")!=0)
   {    cout<<" Press any key to roll the dice.";
      getch();                                                     //display board
      step=dice();                               //get dice value and save as step
      system("cls");
      cout<<endl<<" Your dice value: "<<step<<endl;
      stepsBoard(step);                          //move P1 on board according to the step
      switch (world.game_element[current_i][current_j])
      {
             case 1: 
                  cout<<endl<<"Resting Place"<<endl; 
                  rest();
                  break;
             case 2:
                  cout<<endl<<"Question"<<endl; 
                  //question();
                  break;
             case 3:
                  cout<<endl<<"Portal"<<endl; 
                  portal();
                  break;
             case 4:
                  cout<<endl<<"Battle"<<endl; 
                  battle();
                  displayBoard();
                  break;
      }
   }//loop, not good
   cout<<" Congratulation !!!..You Finish The Game..."<<endl<<" See You Again..Thank You."<<endl<<" Back to Game Menu "<<endl;
   getch();
   system("cls");
   displayMenu(advGame);
   cout<<"\n Enter Number: ";             //choose menu
   cin>>no_menu;
   system("cls");
   selectMenu(no_menu);
}

//======================================================================================================
//dice function
int dice(void)
{
    return rand() % 6 + 1;
}
//======================================================================================================
//initialize board
void intBoard(void)
{
    for(i=0;i<row;i++)
        for(j=0;j<col;j++)
          world.board[i][j]="       ";
    world.board[0][0]=" START ";
    world.board[8][7]="  END  ";
    current_i=0; current_j=0;
}
//======================================================================================================
//initialize game_element
void intgame_element(void)
{
     for(i=0;i<row;i++)
        for(j=0;j<col;j++)
          world.game_element[i][j]=rand() % 4 + 1;
    world.game_element[row][col]=0; world.game_element[0][0]=0;
}
//======================================================================================================
//show game board
void displayBoard(void)
{
   world.board[0][0]=" START ";
   wait(0.2);
    for(i=0;i<row;i++)
   {     cout<<endl;
        for(j=0;j<(col);j++)
            cout<<"========";
      cout<<endl<<'|';
      if (i%2==1)
          for(j=(col-1);j>=0;j--)
             cout<<world.board[i][j]<<'|';
      else
          for(j=0;j<col;j++)
              cout<<world.board[i][j]<<'|';
   }
   cout<<endl;
       for(j=0;j<(col);j++)
            cout<<"========";
         cout<<endl;
}
//======================================================================================================
//stepping around board
void stepsBoard(int steps)
{
    world.board[current_i][current_j]="       ";
   current_j=current_j+steps;
   if (current_j>=col)
   {
        current_j=current_j-col;
         current_i++;
   }
   world.board[(current_i)][(current_j)]="  P 1  ";
   if (current_i==row||(current_i==8&&current_j==(6)))//when reach destination or over it, assign to
   {
      intBoard();
      world.board[0][0]=" START ";
      world.board[8][7]="  P 1  ";   //last destination and break out of loop
      displayBoard();
   }
   if (current_i<0||current_j<0) //when going back to start
   {
      intBoard();
      world.board[0][0]=" START ";
      current_i=0; current_j=0;
      displayBoard();
   }
   else
      displayBoard();
}
//======================================================================================================
//to slow down the display of the board
void wait(float seconds)
{
  clock_t endwait;
  endwait=clock()+seconds*CLOCKS_PER_SEC;
  while(clock()<endwait){}   // jarak antara nombor yang akan dipaparkan
}
//======================================================================================================
//resting function
void rest(void)
{
     cout<<"You found a space cafe and decided to have a tea before proceed with your adventure."<<endl;
}
//======================================================================================================
//portal function
void portal(void)
{
     cout << "You are unlucky enough to get sucked in into a wormhole." << endl
          << "It sends you somewhere around the universe." << endl;
          
     int polarity;
     if ((rand() % 2 + 1)==2)
          step=dice();
     else 
          step=(-1)*dice();
          
     stepsBoard(step);
}
//======================================================================================================
//battle mode -battle(),win(),lose()
int battle(void)
{
    ehp=30;
     label:
     mp++; //mp regeneration
    cout << "Enemy HP: " << ehp << endl;
    cout << "Your  HP: " <<  hp << endl;
    cout << "Your  MP: " <<  mp << endl;
    cout << "(1) Attack" << endl;
    cout << "(2) Use Force" << endl;
    cout << "(3) Heal "  << endl;
    
    cin >> move;
    system("cls");
    if (move>=1&move<=3)
{    cout << "Status:" << endl;
    
    switch (move) //note: when lose, end game
    {
           case 1: 
                if ((rand() % 3 + 1)==1)
                   {cout<<"You managed to come close enough to punched him in the face."<<endl; ehp-=1;}
                else if ((rand() % 3 + 1)==2)          
                   {cout<<"You shot him from distance using your rifle."<<endl; ehp-=2;}
                else
                   {cout<<"You sliced him skillfully with your sword."<<endl; ehp-=3;}
                break;
           case 2:
                if (mp>=10)
                {
                   cout<<"You used your force and pushed him back, hurting him in the process."<<endl;
                   ehp-=5;
                   mp-=10;
                }
                else
                {
                    cout<<"You don't have enough mp to use your force ability."<<endl;
                }
                break;
           case 3:
                cout<<"You patched yourself quickly in the midst of battle."<<endl;
                hp+=4;
    }
    if (ehp<=0)
    {   win(); return 1;}
    else
        {
             emove=(rand() % 3 + 1);
             
             if (emove==1)
             {   cout << "The enemy shot you using his laser rifle." << endl; hp-=1;}
             else if (emove==2)          
             {   cout << "The enemy charged at you and pushed you onto the walls." <<endl; hp-=2;}
             else if (emove==3)
             {   cout << "The enemy healed himself."<<endl; ehp+=4;}
        }
    if (hp<=0)
    {   lose(); return 0;}
}
    else
    {cout<<"Wrong input. Enter 1,2 or 3. You didn't read the guideline, do you?"<<endl;
       system("PAUSE");cout<<endl;
       system("cls");}    //error input have problem when have char input
    goto label;
}
void win(void)
{
     cout<<endl<<"You win!";
     system("PAUSE");
}

void lose(void)
{
     cout<<endl<<"You lost...";
          system("PAUSE");
}
//======================================================================================================

No comments:

Post a Comment