Welcome to this tutorial where we will be introducing two exciting game projects, Guess the Number and Snake Water and Gun, both developed in C programming language.
Our aim is to provide you with a fun and educational experience that is easy to follow. These games are both engaging and enjoyable, and we are confident that you will have a great time playing them!
Snake Water and Gun is a two-player game, where the first player is you, and the second player is the computer. The game involves choosing between three options: snake, water, or gun.
For instance, if you select snake and the computer also picks snake, then the game ends in a draw. Alternatively, if you choose snake and the computer opts for water, you win! However, if you select snake and the computer chooses gun, you lose the game.
Code For Guess the Number:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main() {
int number, guess, nguesses=1;
srand(time(0));
number = rand()%100 + 1; // Generates a random number between 1 and 100
// printf("The number is %d\n", number); // this is your guess number
// Keep running the loop until the number is guessed
printf("Welcome to my game??\n\n");
printf("By Ashish_Kumar\n\n");
do {
printf("Guess the number between 1 to 100\n");
scanf("%d", &guess);
if(guess>number) {
printf("Lower number please!\n");
}
else if(guess<number) {
printf("Higher number please!\n");
}
else {
printf("You guessed it in %d attempts\n", nguesses);
}
nguesses++;
} while(guess!=number);
return 0;
}
After Execution:
Code For Snake Water and Gun:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int snakeWaterGun(char you, char comp){
// returns 1 if you win, -1 if you lose and 0 if draw
// Condition for draw
// Cases covered:
// ss
// gg
// ww
if(you == comp){
return 0;
}
// Non-draw conditions
// Cases covered:
// sg
// gs
// sw
// ws
// gw
// wg
if(you=='s' && comp=='g'){
return -1;
}
else if(you=='g' && comp=='s'){
return 1;
}
if(you=='s' && comp=='w'){
return 1;
}
else if(you=='w' && comp=='s'){
return -1;
}
if(you=='g' && comp=='w'){
return -1;
}
else if(you=='w' && comp=='g'){
return 1;
}
}
int main(){
char you, comp;
srand(time(0));
int number = rand()%100 + 1;
if(number<33){
comp = 's';
}
else if(number>33 && number<66){
comp='w';
}
else{
comp='g';
}
printf("\nEnter 's' for snake, 'w' for water and 'g' for gun\n\n");
scanf("%c", &you);
int result = snakeWaterGun(you, comp);
if(result ==0){
printf("\n....Game draw!....\n");
}
else if(result==1){
printf("\n....You win!....\n");
}
else{
printf("\n....You Lose!....\n");
}
printf("You chose %c and computer chose %c.\n\n ", you, comp);
return 0;
}
After Execution:
Enjoy this code (game).
1 Comments
nice
ReplyDelete