Monday, September 27, 2010

Firing Game


C PROGRAM

#include<stdio.h>
#include<conio.h>
#include<dos.h>
void main()
{
int i, j, r, c, s, main;
char ans, opt;
menu:
textbackground(BLUE);
textcolor(YELLOW + BLINK);
clrscr();
textbackground(BLUE);
textcolor(YELLOW + BLINK);
do
{
gotoxy(24, 6);
printf("%c", 15);
gotoxy(33, 4);
printf("%c", 15);
gotoxy(40, 6);
printf("%c", 15);
gotoxy(50, 5);
printf("%c", 15);
gotoxy(60, 7);
printf("%c", 15);
gotoxy(70, 3);
printf("%c", 15);
gotoxy(18, 2);
printf("%c", 15);
gotoxy(7, 6);
printf("%c", 15);
gotoxy(18, 9);
printf("%c", 15);
gotoxy(63, 11);
printf("%c", 15);
gotoxy(20, 14);
printf("%c", 15);
gotoxy(57, 14);
printf("%c", 15);
//B
for(i = 8; i <= 12; i++)
{
gotoxy(26, i);
printf("%c", 173);
gotoxy(30, i);
printf("%c", 173);
}
for(i = 8; i <= 12; i = i + 2)
{
gotoxy(27, i);
printf("%c%c%c", 173, 173, 173);
}
//O
for(i = 8; i <= 12; i++)
{
gotoxy(33, i);
printf("%c", 173);
gotoxy(37, i);
printf("%c", 173);
}
for(i = 8; i <= 12; i = i + 4)
{
gotoxy(34, i);
printf("%c%c%c", 173, 173, 173);
}
//M
for(i = 8; i <= 12; i++)
{
gotoxy(40, i);
printf("%c", 173);
gotoxy(42, i);
printf("%c", 173);
gotoxy(44, i);
printf("%c", 173);
}
gotoxy(41, 8);
printf("%c", 173);
gotoxy(43, 8);
printf("%c", 173);
//B
for(i = 8; i <= 12; i++)
{
gotoxy(47, i);
printf("%c", 173);
gotoxy(51, i);
printf("%c", 173);
}
for(i = 8; i <= 12; i = i + 2)
{
gotoxy(48, i);
printf("%c%c%c", 173, 173, 173);
}
gotoxy(28, 15);
printf("T H EG A M E");
gotoxy(28, 19);
printf("1. New Game");
gotoxy(28, 20);
printf("2. Instructions.");
gotoxy(28, 21);
printf("3. Exit");
opt = getche();
if(opt == '1')
{
clrscr();
textbackground(BLUE);
textcolor(YELLOW + BLINK);
printf("ENTER THE SPEED WITH WHICH YOU WANT YOUR AEROPLANE TO MOVE ( > than 0 and < than 100): ");
scanf("%d", &s);
for(main = 1; main <= 3; main++)
{
clrscr();
textbackground(BLUE);
textcolor(YELLOW + BLINK);
gotoxy(37, 20);
printf("%c", 219);
gotoxy(1, 2);
printf(">%c--->", 173);
getch();
while(!kbhit())
{
for(i = 1; i < 75; i++)
{
if(kbhit())
{
getch();
nosound();
goto drop;
}
else
{
gotoxy(i, 2);
printf(">%c--->", 173);
r = 2;
c = i;
gotoxy(37, 20);
printf("%c", 219);
sound(100);
delay(100 - s);
if(i != 36 && !kbhit())
{
clrscr();
textbackground(BLUE);
textcolor(YELLOW + BLINK);
}
}
}
}
drop:
for(j = 3; j <= 20; j++)
{
gotoxy(c, r);
printf(">---->");
gotoxy(37, 20);
printf("%c", 219);
gotoxy(i, j);
printf("%c", 173);
delay(50);
sound(3000);
if(j != 15)
{
clrscr();
textbackground(BLUE);
textcolor(YELLOW + BLINK);
}
}
nosound();
if(i == 37)
{
gotoxy(34, 20);
printf("%cBOOM!!!%c", 15, 15);
gotoxy(29, 21);
//delay(100);
printf("%c%c%c", 15, 15, 15);
gotoxy(33, 19);
//delay(100);
printf("%c%c", 15, 15);
for(i = 0; i < 10; i++)
{
delay(30);
sound((i * 100) + 50);
delay(30);
nosound();
}
getch();
clrscr();
textbackground(BLUE);
textcolor(YELLOW + BLINK);
break;
}
else
{
gotoxy(i, 20);
clreol();
gotoxy(30, 15);
printf("SORRY, YOU MISSED THE TARGET");
sound(2000);
delay(100);
nosound();
}
getch();
}
}
}
while(opt == '1');
if(opt == '2')
{
clrscr();
textbackground(BLUE);
textcolor(YELLOW + BLINK);
printf("INSTRUCTIONS :-");
printf("\n\n\n");
printf("1. The objective of this game is to destroy the target by dropping a bomb on it.\n2. The player needs to drop the bomb from an aeroplane.\n\n3. Press any key to start flying the aeroplane.\n\n4. Then again press any key to drop the bomb when you are above the target.\n\n5. The target will look like this %c.", 219);
getch();
goto menu;
}
if(opt == '3')
{
}
else
{
goto menu;
}
}

Match stick game

Matchstick Game using C Programming

Write a program for a matchstick game being played between the computer and a user.
Your program should ensure that the computer always wins. Rules for the game are as follows:

-There are 21 matchsticks.
-The computer asks the player to pick 1, 2, 3 or 4 matchsticks.
-After the person picks, the computer does its picking.
-Whoever is forced to pick up the last matchstick loses the game.


-There are 21 matchsticks.
-Whoever is forced to pick up the last matchstick loses the game.
So the last one is special, so it's all about how to get rid of 20 matches in pairs of turns.

-The computer asks the player to pick 1, 2, 3 or 4 matchsticks.
So if we reduce the total by 5 each round, the sequence will go
21 16 11 6 1
In effect, whatever number the user picks (n), the computer picks 5-n



#include<stdio.h>
main()
{

int matchsticks=21usercomputer;

printf("Do not enter Invalid Numbers.\nNumbers above 4 are invalid.");

printf("\nIf you do so, the computer automatically wins.");

while (matchsticks>=1)
{

printf("\nNumber of matchsticks available right now is %d."matchsticks);

printf("\n\nYour Turn...\n\n\n");

printf("\nPick up the matchstick(s)-- (1-4): ");
scanf ("%d"&user);

if (user>4)

{
printf("Invalid Selection");
break;
}

computer=5-user;

printf("\nComputer's Turn..\n" );
printf("\nComputer chooses:%d"computer);

matchsticks=matchsticks-user-computer;
continue;

if(matchsticks==1)

break;
}

matchsticks--;
printf("\nComputer Wins");

}

Simple snake game


Playing the game
To move the snake, use 'a' for up, 'z' for down, 'o' for left and 'p' for right. Again, there are constants you can change if you want to alter these settings. Press 'x' to exit the game at any time.
The aim of the game is to collect the dots (food) and avoid the obstacles (crosses, borders, and the snake itself). As you collect food, the snake gets longer, so increasing your likelihood of crashing into yourself. When you have collected enough food, you progress onto the next level, where your snake gets longer, and the amount of food to collect to progress through the level gets larger.
You get scored according to the length of the snake and the number of 'x' obstacles on the screen.
The speed increases every 5 levels.
You get a bonus when you complete the level of 1000, increasing by 1000 each level (e.g. complete level 5, you get a 5000 bonus).
There is no concept of lives. Once you hit an obstacle, that's it, game over.
Make sure you do not have the caps lock on, otherwise the keys will fail to respond.
The Listing
This is the C program for the game. it is reasonably well structured, but by no means perfect.  Note that it uses library functions that are not available on all C compilers - it was designed using Borland C++ Builder / Turbo C.  Also it uses a few C++ concepts not available in standard C - e.g. const for a constant (you would use #define in normal C). You may also have to change your compiler settings if you have them set to ANSI C to C++ instead - the for loops define a local instance of i as a loop counter, a concept to available in standard C.
#include <stdlib.h>

#include <stdio.h>

#include <conio.h>



/* prototypes */

void draw_line(int col, int row);

void show_score();

void add_segment();

void setup_level();



/* constants */

const int maxrow=15, maxcol=77;

const int snake_start_col=33,snake_start_row=7;

const char up_key='a', down_key='z', left_key='o', right_key='p';

const int pause_length=500000;



/* global variables */

int score, snake_length, speed, obstacles, level, firstpress, high_score=0;

char screen_grid[maxrow][maxcol];

char direction = right_key;

struct snake_segment {

  int row,col;

} snake[100];

void main()
{



  /* Variable declarations within main() only */

  char keypress;



  do /* restart game loop */
{

    obstacles=4; level=1; score=0; speed=14;



    randomize(); /* Ensure random seed initiated */



    setup_level();



    /* main loop */

    do
{

      for (int i=0;i<(speed*pause_length);i++) int j=1+i; /*pause*/



      /* If key has been hit, then check it is a direction key - if so,

         change direction */

      if (kbhit())
{

        keypress=(char)getch();

        if((keypress==right_key)||(keypress==left_key)||

           (keypress==up_key)||(keypress==down_key))

          direction = keypress;

      }



      /* Add a segment to the end of the snake */

      add_segment();



      /* Blank last segment of snake */

      gotoxy(snake[0].col,snake[0].row);

      cprintf(" ");

      /* ... and remove it from the array */

        for (int i=1;i<=snake_length;i++)

          snake[i-1]=snake[i];

      /* Display snake in yellow */

      textcolor(YELLOW);

      for (int i=0;i<=snake_length;i++)
{

        gotoxy(snake[i].col,snake[i].row);

        cprintf("O");

      }

      /* keeps cursor flashing in one place instead of following snake */

      gotoxy(1,1);



      /* If first press on each level, pause until a key is pressed */

      if (firstpress) { while(!kbhit()); firstpress = 0; }



      /* Collision detection - walls (bad!) */

      if ((snake[snake_length-1].row>maxrow+1)||(snake[snake_length-1].row<=1)||

          (snake[snake_length-1].col>maxcol+1)||(snake[snake_length-1].col<=1)||

      /* Collision detection - obstacles (bad!) */

          (screen_grid[snake[snake_length-1].row-2][snake[snake_length-1].col-2]=='x'))

        keypress='x'; /* i.e. exit loop - game over */

      /* Collision detection - snake (bad!) */

      for (int i=0;i<snake_length-1;i++)

        if ( (snake[snake_length-1].row)==(snake[i].row) &&

             (snake[snake_length-1].col)==(snake[i].col))

        {

          keypress='x'; /* i.e. exit loop - game over */

          break; /* no need to check any more segments */

        }

      /* Collision detection - food (good!) */

      if (screen_grid[snake[snake_length-1].row-2][snake[snake_length-1].col-2]=='.')
{

        /* increase score and length of snake */

        score+=snake_length*obstacles; show_score(); snake_length++; add_segment();

        /* if length of snake reaches certain size, onto next level */

        if (snake_length==(level+3)*2)
{

          score+=level*1000; obstacles+=2; level++;  /* add to obstacles */

          if ((level%5==0)&&(speed>1)) speed--; /* increase speed every 5 levels */

          setup_level(); /* display next level */

        }

      }

    } while (keypress!='x');



    /* game over message */

    if (score > high_score) high_score = score;

    show_score();

    gotoxy(30,6); textcolor(LIGHTRED); cprintf("G A M E   O V E R");

    gotoxy(30,9); textcolor(YELLOW); cprintf("Another Game (y/n)? ");

    do keypress=getch(); while((keypress!='y')&&(keypress!='n'));



  } while (keypress=='y');

}



void setup_level()
{



 /* variables local to setup_level() */

 int row,col;



  /* Set up global variables for new level */

  snake_length=level+4; direction = right_key;

  firstpress = 1;

 /* Fill grid with blanks */

  for(row=0;row<maxrow;row++)

    for(col=0;col<maxcol;col++)

     screen_grid[row][col]= ' ';



  /* Fill grid with Xs and food */

  for(int i=0;i<obstacles*2;i++)
{

   row= rand()%maxrow;

   col= rand()%maxcol;

   if(i<obstacles)

     screen_grid[row][col]='x';

   else

     screen_grid[row][col]='.';

  }



  /* Create snake array of length snake_length */

  for(int i=0;i<snake_length;i++)
{

    snake[i].row=snake_start_row;

    snake[i].col=snake_start_col+i;

  }



  /* Draw playing board */

  draw_line(1,1);

  for(row=0;row<maxrow;row++)
{

    gotoxy(1,row+2);

    textcolor(LIGHTBLUE); cprintf("|");

    textcolor(WHITE);

    for(col=0;col<maxcol;col++)

      cprintf("%c",screen_grid[row][col]);

    textcolor(LIGHTBLUE);

    cprintf("|");

  }

  draw_line(1,maxrow+2);



  show_score();

  gotoxy(2,maxrow+5);

  textcolor(LIGHTRED);

  cprintf("~~ SNAKE GAME~~ Left: %c, Right: %c, Up: %c, Down: %c, Exit: x. Any key to start.",

          left_key,right_key,up_key,down_key);

}



void draw_line(int col, int row)
{

  gotoxy(col,row); textcolor(LIGHTBLUE);

  for (int col=0;col<maxcol+2;col++) cprintf("=");

}



void show_score()
{

  textcolor(LIGHTCYAN);

  gotoxy(2,maxrow+3);

  cprintf("Level: %05d",level);

  gotoxy(40,maxrow+3);

  textcolor(LIGHTGREEN);

  cprintf("Score: %05d",score);

  gotoxy(60,maxrow+3);

  textcolor(LIGHTMAGENTA);

  cprintf("High Score: %05d",high_score);

}
void add_segment()
{

  switch(direction)
{

    case(right_key): snake[snake_length].row=snake[snake_length-1].row;

                     snake[snake_length].col=snake[snake_length-1].col+1;

                     break;

    case(left_key) : snake[snake_length].row=snake[snake_length-1].row;

                     snake[snake_length].col=snake[snake_length-1].col-1;

                     break;

    case(up_key)   : snake[snake_length].row=snake[snake_length-1].row-1;

                     snake[snake_length].col=snake[snake_length-1].col;

                     break;

    case(down_key) : snake[snake_length].row=snake[snake_length-1].row+1;

                     snake[snake_length].col=snake[snake_length-1].col;

  }

}