Wednesday, 17 February 2016

tic tac toe game in c

Tic Tac Toe Game

#include<graphics.h>
#include<iostream.h>
#include <conio.h>
void main()
{
start: int a[3][3]={5,6,7,8,9,10,11,12,13},i,j,k,x,y,t,cn=0,co=0;
char c;
static int nod=0,nn=0,sc[2]={0,0};
nod++;
clrscr();
int driver=DETECT,mode;
initgraph(&driver,&mode,"c:\tc\bgi");
setbkcolor(RED);
line(100,200,400,200);
line(100,300,400,300);
line(200,100,200,400);
line(300,100,300,400);
gotoxy(20,2);
cout<<"TIC TAC TOE";
gotoxy(20,3);
cout<<"Unregistered Vesion 1.0";
gotoxy(10,5);cout<<" BOX NO ";
gotoxy(15,7);cout<<"0";
gotoxy(30,7);cout<<"1";
gotoxy(45,7);cout<<"2";
gotoxy(15,14);cout<<"3";
gotoxy(30,14);cout<<"4";
gotoxy(45,14);cout<<"5";
gotoxy(15,21);cout<<"6";
gotoxy(30,21);cout<<"7";
gotoxy(45,21);cout<<"8";
gotoxy(55,22);cout<<"RESULT";
gotoxy(55,23);cout<<"*************************";
gotoxy(55,15);cout<<"P1-"<<sc[0];
gotoxy(55,16);cout<<"P2-"<<sc[1];
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{ re:gotoxy(65,4);
k=((i+j)%2+1);
cout<<"Enter player"<<k;
gotoxy(60,5);
cout<<" Box no from 0 to 8   ";
gotoxy(58,6);cout<<"[   ]";
gotoxy(60,6);
t=getch();
t=t-48;
if(t==0){x=0;y=0;goto play;}
if(t==1){x=1;y=0;goto play;}
if(t==2){x=2;y=0;goto play;}
if(t==3){x=0;y=1;goto play;}
if(t==4){x=1;y=1;goto play;}
if(t==5){x=2;y=1;goto play;}
if(t==6){x=0;y=2;goto play;}
if(t==7){x=1;y=2;goto play;}
if(t==8){x=2;y=2;goto play;}
else
{goto re ;}
play:
if((a[x][y]==1)||(a[x][y]==2))
{goto re;}
if((a[x][y]!=1)&&(a[x][y]!=2))
{ if(k==1)
{circle(100*x+100+50,100*y+100+50,50);
a[x][y]=k;co++;} if(k==2)
{line(x*100+100,y*100+100,x*100+200,y*100+200);
line(x*100+100,y*100+200,x*100+200,y*100+100);
a[x][y]=k;co++;}
}
gotoxy(60,23);
if((a[0][0]==a[1][0])&&(a[1][0]==a[2][0]))
{cout<<"Player "<<k<<" wins";cn=1;sc[k-1]++;}
if((a[0][0]==a[0][1])&&(a[0][1]==a[0][2]))
{cout<<"Player "<<k<<" wins";cn=1;sc[k-1]++;}
if((a[0][0]==a[1][1])&&(a[1][1]==a[2][2]))
{cout<<"Player "<<k<<" wins";cn=1;sc[k-1]++;}
if((a[1][0]==a[1][1])&&(a[1][1]==a[1][2]))
{cout<<"Player "<<k<<" wins";cn=1;sc[k-1]++;}
if((a[2][0]==a[2][1])&&(a[2][1]==a[2][2]))
{cout<<"Player "<<k<<" wins";cn=1;sc[k-1]++;}
if((a[2][0]==a[1][1])&&(a[1][1]==a[0][2]))
{cout<<"Player "<<k<<" wins";cn=1;sc[k-1]++;}
if((a[0][2]==a[1][2])&&(a[1][2]==a[2][2]))
{cout<<"Player "<<k<<" wins";cn=1;sc[k-1]++;}
if((a[0][1]==a[1][1])&&(a[1][1]==a[2][1]))
{cout<<"Player "<<k<<" wins";cn=1;sc[k-1]++;}
if(cn==1)
{goto que;}
if(co==9)
{gotoxy(62,23);
cout<<"DRAW";
nn++;
goto que;}
}
}
que:
gotoxy(60,24);
cout<<"Play again y/n";
c=getch();
if((c=='y')||(c=='Y'))
{ goto start;}
else if((c=='n')||(c=='N'))
{goto end;}
else goto que;

end:
closegraph();
cout<<"Final score is "<<sc[k-1]<<"-"<<nod-nn-sc[k-1];
gotoxy(20,10);
cout<<"Thanks for playing this game";
getch();
}





Tuesday, 16 February 2016

snake game in c++

hello all
this is the new snake game coding in c++
do try once




#include <iostream>
#include <conio.h>

void run();
void printMap();
void initMap();
void move(int dx, int dy);
void update();
void changeDirection(char key);
void clearScreen();
void generateFood();

char getMapValue(int value);

// Map dimensions
const int mapwidth = 20;
const int mapheight = 20;

const int size = mapwidth * mapheight;

// The tile values for the map
int map[size];

// Snake head details
int headxpos;
int headypos;
int direction;

// Amount of food the snake has (How long the body is)
int food = 3;

// Determine if game is running
bool running;

int main()
{
    run();
    return 0;
}

// Main game function
void run()
{
    // Initialize the map
    initMap();
    running = true;
    while (running) {
        // If a key is pressed
        if (kbhit()) {
            // Change to direction determined by key pressed
            changeDirection(getch());
        }
        // Upate the map
        update();

        // Clear the screen
        clearScreen();

        // Print the map
        printMap();

        // wait 0.5 seconds
        _sleep(500);
    }

    // Print out game over text
    std::cout << "\t\t!!!Game over!" << std::endl << "\t\tYour score is: " << food;

    // Stop console from closing instantly
    std::cin.ignore();
}

// Changes snake direction from input
void changeDirection(char key) {
    /*
      W
    A + D
      S

      1
    4 + 2
      3
    */
    switch (key) {
    case 'w':
        if (direction != 2) direction = 0;
        break;
    case 'd':
        if (direction != 3) direction = 1;
        break;
    case 's':
        if (direction != 4) direction = 2;
        break;
    case 'a':
        if (direction != 5) direction = 3;
        break;
    }
}

// Moves snake head to new location
void move(int dx, int dy) {
    // determine new head position
    int newx = headxpos + dx;
    int newy = headypos + dy;

    // Check if there is food at location
    if (map[newx + newy * mapwidth] == -2) {
        // Increase food value (body length)
        food++;

        // Generate new food on map
        generateFood();
    }

    // Check location is free
    else if (map[newx + newy * mapwidth] != 0) {
        running = false;
    }

    // Move head to new location
    headxpos = newx;
    headypos = newy;
    map[headxpos + headypos * mapwidth] = food + 1;

}

// Clears screen
void clearScreen() {
    // Clear the screen
    system("cls");
}

// Generates new food on map
void generateFood() {
    int x = 0;
    int y = 0;
    do {
        // Generate random x and y values within the map
        x = rand() % (mapwidth - 2) + 1;
        y = rand() % (mapheight - 2) + 1;

        // If location is not free try again
    } while (map[x + y * mapwidth] != 0);

    // Place new food
    map[x + y * mapwidth] = -2;
}

// Updates the map
void update() {
    // Move in direction indicated
    switch (direction) {
    case 0: move(-1, 0);
        break;
    case 1: move(0, 1);
        break;
    case 2: move(1, 0);
        break;
    case 3: move(0, -1);
        break;
    }

    // Reduce snake values on map by 1
    for (int i = 0; i < size; i++) {
        if (map[i] > 0) map[i]--;
    }
}

// Initializes map
void initMap()
{
    // Places the initual head location in middle of map
    headxpos = mapwidth / 2;
    headypos = mapheight / 2;
    map[headxpos + headypos * mapwidth] = 1;

    // Places top and bottom walls 
    for (int x = 0; x < mapwidth; ++x) {
        map[x] = -1;
        map[x + (mapheight - 1) * mapwidth] = -1;
    }

    // Places left and right walls
    for (int y = 0; y < mapheight; y++) {
        map[0 + y * mapwidth] = -1;
        map[(mapwidth - 1) + y * mapwidth] = -1;
    }

    // Generates first food
    generateFood();
}

// Prints the map to console
void printMap()
{
    for (int x = 0; x < mapwidth; ++x) {
        for (int y = 0; y < mapheight; ++y) {
            // Prints the value at current x,y location
            std::cout << getMapValue(map[x + y * mapwidth]);
        }
        // Ends the line for next x value
        std::cout << std::endl;
    }
}

// Returns graphical character for display from map value
char getMapValue(int value)
{
    // Returns a part of snake body
    if (value > 0) return 'o';

    switch (value) {
        // Return wall
    case -1: return 'X';
        // Return food
    case -2: return 'O';
    }
}