Saturday, September 11, 2010

slowmotionstep: current code

I tried correcting this coding, but it still not finished. I didn't receive a reply from my partner when I message him at facebook. Well, I guess I should use SMS instead.

/*
Problems:
1.current_j and current_j values is wrong
2.
*/
#include <iostream>
#include <conio.h>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
using namespace std;

const int row=9,col=8;
int i,j,current_i,current_j,step;                  //global variables

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]

int dice(void);
void intBoard(void);
void intgame_element(void);
void displayBoard(void);
void stepsBoard(int steps);
void slowstepsBoard(int steps);
void wait(float seconds);


/*void displayelement (void)
{
     for (i=0;i<row;i++)
     {
         for (j=0;j<col;j++)
             cout<<world.game_element[i][j]<<' ';
      
         cout<<endl;
     }      
}*/

int main()
{
    srand(time(NULL)) ;                           //set rand to use time as seed
    intBoard();
intgame_element();
// displayelement();
    displayBoard();
    label:
    system("PAUSE");
    system("cls");
    int steps=dice();
    //stepsBoard(steps);
    slowstepsBoard(steps);
    cout<<steps<<endl; //dice value
    cout<<"Coordinate:("<<(current_i+1)<<','<<(current_j+1)<<')'<<endl;
    if ( ( strcmp(world.board[8][7],"  P 1  ") != 0))
       goto label;
    else
    {
       cout<<endl<< "You won!"<<endl;
       system("PAUSE");                          
       return 0;
    }
  
}


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-1][col-1]=0; world.game_element[0][0]=0;
}
//======================================================================================================
//show game board
void displayBoard(void)
{
   world.board[0][0]=" START ";

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
}
//======================================================================================================
//stepping around board-slow motion
void slowstepsBoard(int steps)
{
    
     world.board[current_i][current_j]="       ";
     for (int count=0;count<(steps)&&current_i<(row-1);count++)
     {
         if (current_j>=col)
         {
    current_j=current_j-col;
             current_i++;
         }
         system("cls");
         if (current_i==8&&current_j==6)
         {
            world.board[0][0]=" START ";
            world.board[8][7]="  P 1  ";  
            displayBoard();
            current_i=(8); current_j=(7);
         }
         world.board[current_i][current_j]="  P 1  ";
         displayBoard();
         wait(0.2);
         world.board[current_i][current_j]="       ";
         current_j++;      
     }
}
//======================================================================================================

Friday, September 10, 2010

Slow motion movement on the grid

We had an idea of making the movement on the board, or I intend to call it grid, animated one. Instead of just simply displaying the last position, we display the player(s) position one step at a time. There are some issues in implementing the design, but this is the early design that works quite well.
Well, good enough for designing it in 5 hours, while watching Monster vs Alien...

/*
Problems: 
1.current_j and current_j values is wrong
2.
*/
#include <iostream>
#include <conio.h>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
using namespace std;

const int row=9,col=8;
int i,j,current_i,current_j,step;                  //global variables

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]

int dice(void);
void intBoard(void);
void intgame_element(void);
void displayBoard(void);
void stepsBoard(int steps);
void slowstepsBoard(int steps);
void wait(float seconds);


/*void displayelement (void)
{
     for (i=0;i<row;i++)
     {
         for (j=0;j<col;j++)
             cout<<world.game_element[i][j]<<' ';
        
         cout<<endl;
     }        
}*/

int main()
{
    srand(time(NULL)) ;                           //set rand to use time as seed
    intBoard();
    intgame_element();
//    displayelement();
    displayBoard();
    label:
    system("PAUSE");
    system("cls");
    int steps=dice();
    //stepsBoard(steps);
    slowstepsBoard(steps);
    cout<<steps<<endl; //dice value
    cout<<"Coordinate:("<<(current_i+1)<<','<<(current_j+1)<<')'<<endl;
    if ( ( strcmp(world.board[current_i][current_j],"  P 1  ") != 0))
       goto label;
    else
    {
       cout<<endl<< "You won!"<<endl;
       system("PAUSE");                            
       return 0;
    }
    
}
   

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-1][col-1]=0; world.game_element[0][0]=0;
}
//======================================================================================================
//show game board
void displayBoard(void)
{
   world.board[0][0]=" START ";

    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
}
//======================================================================================================
//stepping around board-slow motion
void slowstepsBoard(int steps)
{
     
     world.board[current_i][current_j]="       ";
     for (int count=0;count<(steps)&&current_i<(row-1);count++)
     {
         if (current_j>=col)
         {
             current_j=current_j-col;
             current_i++;
         }
         system("cls");
         if (current_i==8&&current_j==6)
         {
            world.board[0][0]=" START ";
            world.board[8][7]="  P 1  ";   
            displayBoard();
            current_i=(8); current_j=(7);
         }
         world.board[current_i][current_j]="  P 1  ";
         displayBoard();
         wait(0.2);
         world.board[current_i][current_j]="       ";
         current_j++;       
     }
}
//======================================================================================================

Monday, September 6, 2010

Question Function part

#include <iostream>
#include <conio.h>
#include <time.h>
#include <string.h>
using namespace std;
const int size_question=4; //to make it really random, try to get around 70 questions
int i;

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

void initializeQuestion (void);
void initializeStatus (void);

int main(void)
{
    srand(time(NULL));
    initializeQuestion ();
    label2:
    int randomQuestion=(rand()%size_question); //randomize the questions
    if (questions[randomQuestion].status==1)
       goto label2;
    string answer;
    cout<<"Question: "<<questions[randomQuestion].q<<endl;
    cout<<"Your answer: ";
    cin>>answer; //want to lower capitals answer 
    if (questions[randomQuestion].ans==answer)
    {    cout<<endl<<"That's correct!"<<endl; //return 1;
    }
    else
        cout<<endl<<"That's wrong..."<<endl;
    system("Pause");
    system("cls");
    goto label2;
}

void initializeQuestion (void)
{
    questions[0].q="1+2= ";
    questions[0].ans="3";
    questions[1].q="What is the name of the developer team that made this game?";
    questions[1].ans="mytho";
    questions[2].q="Some of memory access technique are random access, sequential access, direct access and _____ access.";
    questions[2].ans="associative";
    questions[3].q="4*120= ";
    questions[3].ans="480";   
       
    initializeStatus ();
}

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

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");
}
//======================================================================================================

Questionaires

Current Project: Mytho: A Simple Adventure Game

Correction...

Current Project: A Simple Adventure Game
Game Developer Team: Mytho.
Status: Game mechanics. This involves following game elements: resting places, portals, questions and battle.
Due date: October [before the beginner of study week]

A little hard to understand when I didn't explain first. Storyline still in progress but this is generally a space adventure. The player will journey through space and encounter 4 things. The playing field is a 2 dimension board, similar to map grid.
  1. Resting Places
  2. Portals
  3. Questions
  4. Battle
1. Resting Places
It is more or less just empty space of the game. Maybe you found a space cafe and stop for a cup of tea. Then you go on with your journey. The coding will be only on displaying that and then ask to continue.

2. Portals
This is the elements to move player back and forth randomly. In the journey, you may encounter a wormhole that suck you in and send you somewhere else. Our current design use our 'stepppingaroundboard' function.

3. Questions
Answer questions to proceed. We are currently collecting the questions. The coding seems simple enough for now. Will be improved.

4. Battle.
Finally I succeeded in designing the battle code part. Still a bit problem about enemy hp, because only 1 enemy variable.
/* I want to emphasis that we still didn't learn class and such. currently reach struct and pointer. so don't ever mention about vector and such.*/
Interesting enough, I manage to randomize the enemy actions, have mp regeneration and such.

Current problem lies most on quiz part. What genre of the questions, the input/output, should I give choices to answer(coz that seems simply input, but at the same time, not challenging enough...) or subjective(using string... hmm...) and how to make it random while making it not repeat the same question...

Next post I will share the current coding. It lack comment though.

Well, if someone ever read this blog anyway. Peace. :)