Saturday, October 2, 2010

Multiplayer?

I finally code the slowstepmotion to work correctly. Currently, the game is quite complete, with questions finally added. I used trivia for questions from fields of astronomy, computer and asian geographic for now. The questions give 4 choices of answer, player answer by giving input of A, B, C or D.

The main problem now is Multiplayer mode. We were focusing on single player too much, I have a hard time figuring how to make it work for multiplayer mode. Putting string "   P 1   " is a bad idea as I thought.

Anyway, I will be researching a bit about turn-based game involving two players and a board. Like chess.

/*
**GAME TITLE: GameAdv
**A Game For Windows
**Copyright 2010 [@zri & ZomBiedy]
*/
#include <iostream>
#include <conio.h>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <ctype.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 variables
GAME advGame[MAX];
int no_menu;
int hp=20, mp=20, ehp=30, move, emove;
const int size_question=30;//to make it really random, try to get around 70 questions
int LimitBattle=35; //limiting battle to only 35
int LimitRest = 20; //limiting resting to only 20

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],[row-1][col-1]

struct QUESTIONS
{
string q;
char ans;
int status; //keep status of being answer or not. if already use, value 1
}questions[size_question];

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

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

//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;
cout << "This game best played when the window are maximised." << endl << endl;
cout << "There are two modes available: Single Player and Multiplayer. Multiplayer allows two players to play in the same round of the game, by taking turns." <<endl
         << "This game involves moving around the space grids using a dice. There will be 4 elements: Questions, Portal, Resting Place and Battle."<<endl << endl
         << "Questions:" <<endl
         << "Various questions will be asked and player(s) must answer the questions before continue with the journey.[there will be three chances of answering before resulting in a end game."<<endl
         << endl
         << "Portal:" <<endl
         << "Player(s) may encounter portal that will send player(s) back or forth randomly using dice number."<<endl
         << endl
         << "Resting Place:" <<endl
         << "This is a place where you can rest a bit and heal some hp." <<endl
         << endl
         << "Battle:" << endl
         << "You may encounter enemy that stops you from continuing with your journey. You can attack, use force or heal. Using force and heal require MP, so keep your MP in check before using it. Death from battle will result in game over."<<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 Semester 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()
{
   system("cls");
  cout<<"\n You are playing SINGLE MODE."<<endl;
  system("PAUSE");
   GameStart(1);
   displayMenu(advGame);
   cout<<"\n Enter Number: ";             //choose menu
   cin>>no_menu;
   system("cls");
   selectMenu(no_menu);
}
//======================================================================================================
void M_player()
{
     system("cls");
  cout<<"\n You are playing MULTIPLAYER MODE."<<endl;
  system("PAUSE");
//  GameStart(2);
   system("cls");
   displayMenu(advGame);
   cout<<"\n Enter Number: ";             //choose menu
   cin>>no_menu;
   system("cls");
   selectMenu(no_menu);
}
//======================================================================================================
//starting of game
void GameStart(int player)
{
int no_menu;
intBoard();
intgame_element();
initializeQuestion ();
   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");
      slowstepsBoard(step);                          //move P1 on board according to the step
      //display dice value
     cout<<endl<<" Your dice value: "<<step<<endl;
      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;
   system("PAUSE");
   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[row-1][col-1]="  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++)
       {
             int countLimitBattle=0;
             int countLimitRest=0;
             world.game_element[i][j]=rand() % 4 + 1;
             label4:
             if (world.game_element[i][j]==1)
             {
             if (countLimitRest==LimitRest)
                {world.game_element[i][j]=rand() % 4 + 1;
                goto label4;}
             else
              countLimitRest++;
             }
             label3:
             if (world.game_element[i][j]==4)
             {
             if (countLimitBattle==LimitBattle)
                {world.game_element[i][j]=rand() % 4 + 1;
                goto label3;}
             else
              countLimitBattle++;
             }
         }
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-slow motion
void slowstepsBoard(int step)
{
     //new code
     world.board[current_i][current_j]="       ";
     for (int count=0;count<=step;count++,current_j++)
     {
         system("cls");
         if (current_i==row||(current_i==row&&current_j==col))//when reach destination or over it, assign to
         {
         world.board[current_i][current_j]="       ";
         world.board[8][7]="  P 1  ";   //last destination and break out of loop
         displayBoard();
         break;
         }
         if (current_j>=col)
         {
current_j=current_j-col;
     current_i++;
         }
         world.board[current_i][current_j]="  P 1  ";
         displayBoard();
         world.board[current_i][current_j]="       ";
         wait(0.2);
         //a little problem when dealing with negative value portal
     }//for
     current_j--;
}
//======================================================================================================
//to slow down the display of the board
void wait(float seconds)
{
     //error converting clock_t from float
     //error didn't seems to affect gameplay
  clock_t endwait;
  endwait=clock()+seconds*CLOCKS_PER_SEC;
  while(clock()<endwait){}   // jarak antara nombor yang akan dipaparkan
}
//======================================================================================================
//resting function
void rest(void)
{
     //random comments in resting place element
     int randRest = rand() % 2 + 1;
     switch (randRest)
     {
            case 1:
            cout << "You found a space cafe and decided to have a tea before proceed with your adventure."<<endl;
            break;
            case 2:
            cout << "You gazes at the beautiful starry scene in front of you before continuing your journey." <<endl;
            break;
     }
     hp+=10; //resting increase hp
     if (hp>100)
        hp=100;
     cout << "Increase hp by 10.\nCurrent hp: " << hp << 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=(rand() % 2 + 1);
     if (polarity==2)
          step=dice();
     else
          step=(-1)*dice();
        
     system("PAUSE");
     system("cls");
   world.board[(current_i)][(current_j)]="       ";
   current_j=current_j+step;
   if (current_j>=col)
   {
current_j=current_j-col;
     current_i++;
   }
   world.board[(current_i)][(current_j)]="  P 1  ";
   displayBoard();
     //slowstepsBoard(step);
}
//======================================================================================================
//battle mode -battle(),win(),lose()
int battle(void)
{
    hp+=5; //for first battle, hp will be 20. every next battle, hp increase by 5 simulating hp regeneration
    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[10MP]" << endl;
    cout << "(3) Heal[2MP]"<< endl;
  
    cin >> move;
    system("cls");
    if (move>=1&&move<=3)
    { cout << "Status:" << endl;
        switch (move) //note: when lose, end game [still not code-written]
        {
            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-=(rand() % 5 + 1);
                   mp-=10;
                }
                else
                {
                    cout<<"You don't have enough mp to use your force ability."<<endl;
                }
                break;
            case 3:
                if (mp>=2)
                {
                cout<<"You patched yourself quickly in the midst of battle."<<endl;
                hp+=4;
                mp-=2;
                    if (hp>100)
                       hp=100;
                }
                else
                {
                    cout<<"You don't have enough mp to heal yourself."<<endl;
                }
            }//switch(move)
        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+=2;}
        }
    if (hp<=0)
    {   lose();
        return 0;}
    }//if (1<move<3)
    else
    {
        cerr<<"Wrong input. Enter 1,2 or 3."<<endl;
       system("PAUSE");cout<<endl;
       system("cls");}
    goto label;}
void win(void)
{
     cout<<endl<<"You win!";
     system("PAUSE");
}

void lose(void)
{
     cout<<endl<<"You lost...\nGAME OVER";
          system("PAUSE");
          system("cls");
          main();
}
//======================================================================================================
//question functions-question(),initializeQuestion(),initializeStatus()
int question (void)
{
    while(1){
//label2:
int randomQuestion=( (rand()) % size_question); //randomize the questions
if (questions[randomQuestion].status==1)
  continue;//goto label2;
char answer;
cout<<questions[randomQuestion].q<<endl;
cout<<"Your answer: ";
cin>>answer;
answer=toupper(answer);
if (answer == questions[randomQuestion].ans)//...
{ cout<<endl<<"That's correct!"<<endl; questions[randomQuestion].status=1;
    return 1;
    }
else
cout<<endl<<"That's wrong..."<<endl;
system("Pause");
system("cls");}
//goto label2;
}
//=================
//list of questions
void initializeQuestion (void)
{
questions[0].q="There are many theories about the birth of our solar system. Which theory involves a passing star pulling dust and debris from the forming sun?\n\tA: Nebular Hypothesis\n\tB: Tidal Theory\n\tC: Collision Theory\n\tD: Protoplanet Hypothesis";
questions[0].ans='B';
questions[1].q="The planets make up what percentage of the mass in our solar system?\n\tA: 0.0135 %\n\tB: 0.135 %\n\tC: 1.35 %\n\tD: 13.5 %";
questions[1].ans='B';
questions[2].q="What are the only two planets in our solar system without moons?\n\tA: Mercury & Venus\n\tB: Venus & Mars\n\tC: Uranus & Neptune\n\tD: Neptune & Pluto";
questions[2].ans='A';
questions[3].q="What is the name of Pluto's moon?\n\tA: Charon\n\tB: Phobus\n\tC: Pandora\n\tD: Nereid";
questions[3].ans='A';
questions[4].q="The three main parts of a comet are the nucleus, the tail, and the _____?\n\tA: Zenith\n\tB: Umbra\n\tC: Radiant\n\tD: Coma";
questions[4].ans='D';
questions[5].q="What year boasted the first woman in space?\n\tA: 1963\n\tB: 1968\n\tC: 1973\n\tD: 1983";
questions[5].ans='A';
questions[6].q="What is the term for the condition when three celestial bodies are arranged in a straight line?\n\tA: Occultation\n\tB: Parallax\n\tC: Syzygy\n\tD: Triple Transit";
questions[6].ans='C';
questions[7].q="Which of the following was discovered in 2002?\n\tA: Nereid\n\tB: Varuna\n\tC: Titan\n\tD: Quaoar";
questions[7].ans='D';
questions[8].q="What manned U.S. space program eventually put 12 men on the Moon?\n\tA: Apollo\n\tB: Gemini\n\tC: Mercury\n\tD: Voyager";
questions[8].ans='A';
questions[9].q="From 1978 to 1999, which planet was farthest from the Sun?\n\tA: Pluto\n\tB: Neptune\n\tC: Uranus\n\tD: Saturn";
questions[9].ans='B';
questions[10].q="What was the very first personal computer?\n\tA: TRS-80\n\tB: Commodore PET\n\tC: Kenbak-1\n\tD: Apple I";
questions[10].ans='C';
questions[11].q="What year was the word \"computer\" first used to describe a mechanical calculating device?\n\tA: 1897\n\tB: 1912\n\tC: 1926\n\tD: 1942";
    questions[11].ans='A';
questions[12].q="What was the first portable computer?\n\tA: Epson HX-20\n\tB: Cray I\n\tC: Osborne I\n\tD: IBM 5155";
    questions[12].ans='C';
questions[13].q="During the 1970s computer engineers at various research institutions began to utilize telecommunications technologies to link their computers together. This effort, the forefather of the modern Internet, was known as the ...\n\tA: INSTANET\n\tB: ARPANET\n\tC: ORDONET\n\tD: BAYONET";
    questions[13].ans='B';
questions[14].q="What was the first computer to defeat a world champion chess player?\n\tA: Chinook\n\tB: X3D Fritz\n\tC: Deep Blue\n\tD: A.L.I.C.E.";
    questions[14].ans='C';
questions[15].q="What computer device did Douglas Engelbart invent in 1963?\n\tA: Modem\n\tB: Mouse\n\tC: Floppy disk\n\tD: Microchip";
    questions[15].ans='B';
questions[16].q="What \"law\" describes the fact that, on average, computers have doubled in capacity every 18 to 24 months since 1900?\n\tA: Anderson's Law\n\tB: Moore's Law\n\tC: Jefferson's Law\n\tD: Bohr's Law";
    questions[16].ans='B';
questions[17].q="How many lines of code did the Windows 98 operating system contain?\n\tA: 4 million\n\tB: 9 million\n\tC: 18 million\n\tD: 40 million";
    questions[17].ans='C';
questions[18].q="What was the first commercially successful vector processor?\n\tA: Cray I\n\tB: HP-2115\n\tC: Altair 8800\n\tD: Apple I";
    questions[18].ans='A';
questions[19].q="What new product did Apple Computer launch with a $1.5 million commercial during the 1984 Super Bowl?\n\tA: Apple I\n\tB: Apple IIe\n\tC: Apple IIc\n\tD: Macintosh";
    questions[19].ans='D';
questions[20].q="Mt. Fuji is the highest point in what Asian country?\n\tA: Bangladesh\n\tB: Burma\n\tC: China\n\tD: Japan";
    questions[20].ans='D';
questions[21].q="What is western Asia's longest river?\n\tA: Tigris\n\tB: Euphrates\n\tC: Xi Jiang\n\tD: Kurobe";
    questions[21].ans='B';
questions[22].q="What tiny country, known to its inhabitants as Druk Yul (Land of the Thunder Dragon), is sandwiched between China and India?\n\tA: Bhutan\n\tB: Bangladesh\n\tC: Mongolia\n\tD: Burma";
    questions[22].ans='A';
questions[23].q="What mountain range runs along the northern border of India?\n\tA: Urul Mountains\n\tB: Tatra Mountains\n\tC: Xiao Hinggan Ling Mountains\n\tD: Himalayan Mountains";
    questions[23].ans='D';
questions[24].q="Korea is separated from Japan by what strait?\n\tA: Cheju\n\tB: Tsushima\n\tC: Gibraltar\n\tD: Bungo";
    questions[24].ans='B';
questions[25].q="What Chinese city is situated at the mouth of the Chang Jiang (Yangtze) River?\n\tA: Shanghai\n\tB: Beijing\n\tC: Taipei\n\tD: Changchun";
    questions[25].ans='A';
questions[26].q="What is the tallest mountain in Asia?\n\tA: Qogir\n\tB: Kangchenjunga\n\tC: Cho Oyu\n\tD: Mount Everest";
    questions[26].ans='D';
questions[27].q="Iran is slightly larger than what U.S. state?\n\tA: Rhode Island\n\tB: Montana\n\tC: Texas\n\tD: Alaska";
    questions[27].ans='D';
questions[28].q="What Japanese city is the site of the Peace Memorial Park and the eternal Peace Flame which is never to be extinguished until all nuclear weapons are dismantled?\n\tA: Hiroshima\n\tB: Nagasaki\n\tC: Tokyo\n\tD: Kyoto";
    questions[28].ans='A';
questions[29].q="What is the capital of Afghanistan?\n\tA: Teheran\n\tB: Kandahar\n\tC: Baghdad\n\tD: Kabul";
    questions[29].ans='D';

    initializeStatus ();
}

void initializeStatus (void)
{
     for ( i=0; i<size_question; i++)
   questions[i].status=0;
}
//======================================================================================================

No comments:

Post a Comment