Showing posts with label games. Show all posts
Showing posts with label games. Show all posts

Tick Tac Toe Game Soruce Code

By // No comments:
Tic Tac is a paper-and-pencil game for two players with X and 0 the player who  succeed in placing  3 marks in horizontally ,vertically or diagonally win the game . We convert this game In to C++.
In this  game there are two players .Code is in very simple way read the full code below.

Game is develop by students of  Comsats University





#include <iostream>

using namespace std;



char square[10] = {'o','1','2','3','4','5','6','7','8','9'};



int checkwin();

void board();



int main()

{

 int player = 1,i,choice;



 char mark;

 do

 {

  board();

  player=(player%2)?1:2;



  cout << "Player " << player << ", enter a number:  ";

  cin >> choice;



  mark=(player == 1) ? 'X' : 'O';



  if (choice == 1 && square[1] == '1')



   square[1] = mark;

  else if (choice == 2 && square[2] == '2')



   square[2] = mark;

  else if (choice == 3 && square[3] == '3')



   square[3] = mark;

  else if (choice == 4 && square[4] == '4')



   square[4] = mark;

  else if (choice == 5 && square[5] == '5')



   square[5] = mark;

  else if (choice == 6 && square[6] == '6')



   square[6] = mark;

  else if (choice == 7 && square[7] == '7')



   square[7] = mark;

  else if (choice == 8 && square[8] == '8')



   square[8] = mark;

  else if (choice == 9 && square[9] == '9')



   square[9] = mark;

  else

  {

   cout<<"Invalid move ";



   player--;

   cin.ignore();

   cin.get();

  }

  i=checkwin();



  player++;

 }while(i==-1);

 board();

 if(i==1)



  cout<<"==>\aPlayer "<<--player<<" win ";

 else

  cout<<"==>\aGame draw";



 cin.ignore();

 cin.get();

 return 0;

}



/*********************************************



 FUNCTION TO RETURN GAME STATUS

 1 FOR GAME IS OVER WITH RESULT

 -1 FOR GAME IS IN PROGRESS

 O GAME IS OVER AND NO RESULT

**********************************************/



int checkwin()

{

 if (square[1] == square[2] && square[2] == square[3])



  return 1;

 else if (square[4] == square[5] && square[5] == square[6])



  return 1;

 else if (square[7] == square[8] && square[8] == square[9])



  return 1;

 else if (square[1] == square[4] && square[4] == square[7])



  return 1;

 else if (square[2] == square[5] && square[5] == square[8])



  return 1;

 else if (square[3] == square[6] && square[6] == square[9])



  return 1;

 else if (square[1] == square[5] && square[5] == square[9])



  return 1;

 else if (square[3] == square[5] && square[5] == square[7])



  return 1;

 else if (square[1] != '1' && square[2] != '2' && square[3] != '3'

                    && square[4] != '4' && square[5] != '5' && square[6] != '6'

                  && square[7] != '7' && square[8] != '8' && square[9] != '9')



  return 0;

 else

  return -1;

}





/*******************************************************************

     FUNCTION TO DRAW BOARD OF TIC TAC TOE WITH PLAYERS MARK

********************************************************************/





void board()

{

 system("cls");

 cout << "\n\n\tTic Tac Toe\n\n";



 cout << "Player 1 (X)  -  Player 2 (O)" << endl << endl;

 cout << endl;



 cout << "     |     |     " << endl;

 cout << "  " << square[1] << "  |  " << square[2] << "  |  " << square[3] << endl;



 cout << "_____|_____|_____" << endl;

 cout << "     |     |     " << endl;



 cout << "  " << square[4] << "  |  " << square[5] << "  |  " << square[6] << endl;



 cout << "_____|_____|_____" << endl;

 cout << "     |     |     " << endl;



 cout << "  " << square[7] << "  |  " << square[8] << "  |  " << square[9] << endl;



 cout << "     |     |     " << endl << endl;

}



/*******************************************************************

    END OF PROJECT

********************************************************************/
Tic Tac Game In C++

Digital Stop-Watch Program

By // No comments:
Digital Stop watch in C++ is not very difficult using libraries and build in functions which make this program so easy to create and understand even new beginner programmers can also understand it .We create this program as easy as possible if you have any query comment below . you can also convert it to simple c .


stop watch in C++




#include<conio.h>
#include<process.h>
#include<iostream>
#include<dos.h>
using namespace std;

int h=0,m=0,s=0,ms=0; // Hours mins sec and microseconds
char ch='p'; //For Menu to start and pause stop watch

int main()
{
 void watch(); // Build in Function of watch
 watch();

  while(1)
 {
  if(kbhit())
   ch=getch();
  if(ch=='s'||ch=='S')
   break;
  if(ch=='e'||ch=='E')
   exit(0);
 }

  while(1)
 {
  watch();
  

   if(kbhit())
   ch=getch();

   if(ch=='r'||ch=='R')
  {
   h=m=s=ms=0;
   watch();

    while(1)
   {
    if(kbhit())
     ch=getch();
    if(ch=='s'||ch=='S')
     break;
    if(ch=='e'||ch=='E')
     exit(0);
   }
  }
  else
   if(ch=='p'||ch=='P')
    while(1)
    {
     if(kbhit())
      ch=getch();
     if(ch=='s'||ch=='S')
      break;
     if(ch=='e'||ch=='E')
      exit(0);
     if(ch=='r'||ch=='R')
     {
      ch='c';
      h=m=s=ms=0;
      watch();
     }
    }
   else
    if(ch=='e'||ch=='E')
     exit(0);

   if(ms!=99)
   ms++;
  else
  {
   ms=0;
   if(s!=59)
    s++;
   else
   {
    s=0;
    if(m!=59)
     m++;
    else
    {
     m=0;
     h++;
    }
   }
  }
 }
}


int watch()
{
 system("cls");
 cout<<"\n\n\n\n\n\t\t\t\t-------------";
 cout<<"\n\t\t\t\t| Stopwatch |";
 cout<<"\n\t\t\t\t-------------";
 cout<<"\n\n\t\t\t\t  "<<h<<":"<<m<<":"<<s<<":"<<ms;

    cout<<"\n\t\t\t\t\t\t\t---------";
  cout<<"\n\t\t\t\t\t\t\t Menu";
 cout<<"\n\t\t\t\t\t\t\t---------";
 cout<<"\n\t\t\t\t\t\t\ts -> Start";
 cout<<"\n\t\t\t\t\t\t\tp -> Stop";
 cout<<"\n\t\t\t\t\t\t\tr -> Reset";
 cout<<"\n\t\t\t\t\t\t\te -> Exit";
 
}


This is a requested program you can also request a program 
on our group on facebook join today 

Pacman Game In C++

By // 1 comment:
pacman is one of the most famous game in all over the world . In this game there is a pac-man ( H) and Eater (E) .Eat the dots produced by the Eater to gain points . If your pac-man get collapse with eater game will over . You move your hero with arrow keys and keep running away from eater and eat the dots to increase points . You can select one of three difficulty  modes  to make game more interesting.





This program is in object oriented approach focus on to make it easy to understand if you have any query you can comment below. 



#include <iostream>
#include <stdio.h>
#include <windows.h>
#include <string>
#include <vector>

using namespace std;
char tmp_map[18][32];

char map[18][32] = 
{
 "+#############################+",
 "|                             |",
 "|                             |",
 "|## ########### ##   #########|",
 "|   |                         |",
 "| | |### |  |           |     |",
 "| |      |  | |###  |   |  |  |",
 "| | #####|  | |      ## |     |",
 "| |           |###  |      |  |",
 "| |##### ###         ##       |",
 "|          ######  ####### ###|",
 "|                             |",
 "|# ### ####      ###   #######|",
 "|                             |",
 "|                             |",
 "|                             |",
 "|                             |",
 "+#############################+"
 };

void ShowMap()
{
 for(int i = 0; i < 18; i++) 
 {
 printf("%s\n",map[i]) ;
 }
}

void gotoxy( short x, short y )
{
    HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE) ;
    COORD position = { x, y } ;

    SetConsoleCursorPosition( hStdout, position ) ;
}

class entity 
{
public:
 entity( int x, int y )
 {
  this ->x = x;
  this ->y = y;
 }

 void move_x( int p )

 {
  if( map[y][x+p] == ' ' ) x += p;
 }

 void move_y( int p )
 {
  if( map[y+p][x] == ' ' ) y += p;
 }

 void move( int p, int q ){
  x += p;
  y += q;
 }

 int get_x(){ return x; }
 int get_y(){ return y; }

 void draw( char p ){
  map[x][y] = p;
  gotoxy( x, y ); printf( "%c", p );
 }

private:
 int x;
 int y;
};

struct walk {
 short walk_count;
 short x;
 short y;
 short back;
};

struct target {
 short x;
 short y;
};

vector<target> walk_queue;

vector<walk> BFSArray;

void AddArray( int x, int y, int wc , int back )
{
 if( tmp_map[y][x] == ' ' || tmp_map[y][x] == '.' )
 {
  tmp_map[y][x] = '#';
  walk tmp;
  tmp.x = x;
  tmp.y = y;
  tmp.walk_count = wc;
  tmp.back = back;
  BFSArray.push_back( tmp );
 }
}

void FindPath( int sx, int sy, int x, int y ){
 memcpy( tmp_map, map, sizeof(map) );
 BFSArray.clear();
 walk tmp;
 tmp.x = sx;
 tmp.y = sy;
 tmp.walk_count = 0;
 tmp.back = -1;
 BFSArray.push_back( tmp );

 int i = 0;
 while( i < BFSArray.size() ){
  if( BFSArray[i].x == x && BFSArray[i].y == y ){
   walk_queue.clear();
   target tmp2;
   while( BFSArray[i].walk_count != 0 ){
    tmp2.x = BFSArray[i].x;
    tmp2.y = BFSArray[i].y;
    walk_queue.push_back( tmp2 );

    i = BFSArray[i].back;
   }

   break;
  }

  AddArray( BFSArray[i].x+1, BFSArray[i].y, BFSArray[i].walk_count+1, i );
  AddArray( BFSArray[i].x-1, BFSArray[i].y, BFSArray[i].walk_count+1, i );
  AddArray( BFSArray[i].x, BFSArray[i].y+1, BFSArray[i].walk_count+1, i );
  AddArray( BFSArray[i].x, BFSArray[i].y-1, BFSArray[i].walk_count+1, i );

  /*
   AddArray( BFSArray[i].x+1, BFSArray[i].y+1, BFSArray[i].walk_count+1, i );
   AddArray( BFSArray[i].x-1, BFSArray[i].y+1, BFSArray[i].walk_count+1, i );
   AddArray( BFSArray[i].x+1, BFSArray[i].y+1, BFSArray[i].walk_count+1, i );
   AddArray( BFSArray[i].x+1, BFSArray[i].y-1, BFSArray[i].walk_count+1, i );

   AddArray( BFSArray[i].x+1, BFSArray[i].y-1, BFSArray[i].walk_count+1, i );
   AddArray( BFSArray[i].x-1, BFSArray[i].y-1, BFSArray[i].walk_count+1, i );
   AddArray( BFSArray[i].x-1, BFSArray[i].y+1, BFSArray[i].walk_count+1, i );
   AddArray( BFSArray[i].x-1, BFSArray[i].y-1, BFSArray[i].walk_count+1, i );
  */
  i++;
 }

 BFSArray.clear();
}


int main()
{
    bool running = true;
 int x = 15; // hero x
 int y = 16; // hero y
 int old_x;
 int old_y;

 int ex = 1;
 int ey = 1;


 int pts = 0;

 cout<<"Instruction:\n1. Arrow Keys to move your hero\n2. Eat the dots produced by the Eater to gain poins\n3. Don't get caught by the Eater\n\n";
 cout<<"H -> Hard\nN -> Normal\nE -> Easy\n\nInput : ";

 char diffi;
 int speedmod = 3;

 cin >> diffi;

 if( diffi == 'N' ){
  speedmod = 2;
 }else if( diffi == 'H' ){
  speedmod = 1;
 }

 system("cls");
    ShowMap();

 gotoxy( x, y ); cout << "H";

 int frame = 0;

 FindPath( ex,ey,x,y );

 while( running ){
  gotoxy( x, y ); cout << " ";

  old_x = x;
  old_y = y;

  if ( GetAsyncKeyState( VK_UP ) ){
   if( map[y-1][x] == '.' ){ y--; pts++; } else
   if( map[y-1][x] == ' ' ) y--;
  }
  if ( GetAsyncKeyState( VK_DOWN ) ){
   if( map[y+1][x] == '.' ){ y++; pts++; } else
   if( map[y+1][x] == ' ' ) y++;
  }
  if ( GetAsyncKeyState( VK_LEFT ) ){
   if( map[y][x-1] == '.' ){ x--; pts++; } else
   if( map[y][x-1] == ' ' ) x--;
  }
  if ( GetAsyncKeyState( VK_RIGHT ) ){
   if( map[y][x+1] == '.' ){ x++; pts++; } else
   if( map[y][x+1] == ' ' ) x++;
  }

  if( old_x != x || old_y != y ){
   FindPath( ex,ey,x,y );
  }

  gotoxy( x,y ); cout << "H";

  map[ey][ex] = '.';
  gotoxy( ex, ey ); cout << ".";

  if( frame%speedmod == 0 && walk_queue.size() != 0 ){
   ex = walk_queue.back().x;
   ey = walk_queue.back().y;
   walk_queue.pop_back();
  }

  gotoxy( ex, ey ); cout << "E";

  if( ex == x && ey == y ){
   break;
  }


  gotoxy( 32, 18 );
  gotoxy( 32, 1 ); cout << pts;
  Sleep( 100 );
  frame++;
 }

 system("cls");
 cout<<"You Lose and your score is : "<< pts<<endl ;
 cin.get();
 cin.get();
 cin.get();
 cin.get();
 cin.get();
 cin.get();
 cin.get();
 cin.get();

 return 0;
}