I have coded a game program...game deals with 24 sticks...The game is played between user and the computer... Each of them can take any number of sticks between 1 to 4... who ever gets the last turn losses...
I am bit selfish and i want the computer to always win.... A Cheating I mean... So I edited my code to do that.... So the edited code is...
#include <stdio.h>
#include <time.h>
/* Global Declaration */
int array[] = {1,6,11,16,21}; /* These are the numbers which makes computer wins */
/* Description abt the game */
void GameDep()
{
printf("This program is abt a game.... This game deals with 24 sticks... \
The game is played between user and the computer... Each of them \
can take any number of sticks between 1 to 4... who ever gets the\
last turn losses...\n");
}
int UserInput()
{
int stick;
while(1) {
printf("No of sticks U take...(1 to 4) : ");
scanf("%d", &stick);
if(stick > 0 && stick < 5) {
return stick;
} else {
printf("Enter the correct input..\n");
}
}
}
/* The game */
void Play()
{
int sticks = 24;
int UserStick;
int CompStick;
int i;
enum{user,computer}player;
srand(time(0));
printf("The game starts\n");
printf("Total No of sticks = 24 \n" );
printf("User: make ur first move\n");
player = user; /* current player */
while(sticks > 1) {
UserStick = UserInput();
sticks -= UserStick;
printf("No of Sticks Left = %d\n", sticks);
player = computer;
if(sticks == 1)
break;
for(i=4; i>=0; i--) {
if(sticks > array[i]) {
printf("No of sticks I (computer) take.... : %d \n\n", (sticks - array[i]));
sticks = array[i];
break;
} else if(sticks == array[i]) {
CompStick = (rand()%(4-1)) + 1;
printf("No of sticks I (computer) take.... : %d \n\n", CompStick);
sticks -= CompStick;
break;
}
}
printf("No of Sticks Left = %d\n\n", sticks);
player = user;
}
if (player == user) {
printf("U lose.... Try once again... All The best...\n");
} else if (player == computer) {
printf("I lose.... Congrats dude... Good job...\n");
}
}
/* Program starts here */
int main()
{
int choice;
int limitcheck = 0;
char wish[3];
while(limitcheck < 10 ) { /* limitcheck is used to check the limit... I gave it to prevent coding while(1) */
printf(" Welcome to the Game \n");
printf(" Enter 1: to play \n Enter 2: Help \n Enter 3: To exit \n");
printf("Enter ur choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
Play();
break;
case 2:
GameDep();
break;
case 3:
printf("Exiting the game && Thanks for playing \n");
return 0;
default:
printf("Enter the correct choice....\n");
break;
}
limitcheck++;
if(limitcheck == 10) {
printf("U have already tired 10 times... \n Do u wish to continue: y or n\n");
scanf("%s", wish);
if(strcasecmp(wish, "y") == 0) {
limitcheck = 0;
} else if(strcasecmp(wish, "n") == 0) {
printf("Exiting the game && Thanks for playing ");
return 0;
}
}
}
return 0;
}
For all combinations expect one my code win.....
Welcome to the Game Enter 1: to play Enter 2: Help Enter 3: To exit Enter ur choice: 1 The game starts Total No of sticks = 24 User: make ur first move No of sticks U take...(1 to 4) : 3 No of Sticks Left = 21 No of sticks I (computer) take.... : 1 No of Sticks Left = 20 No of sticks U take...(1 to 4) : 4 No of Sticks Left = 16 No of sticks I (computer) take.... : 1 No of Sticks Left = 15 No of sticks U take...(1 to 4) : 4 No of Sticks Left = 11 No of sticks I (computer) take.... : 3 No of Sticks Left = 8 No of sticks U take...(1 to 4) : 2 No of Sticks Left = 6 No of sticks I (computer) take.... : 3 No of Sticks Left = 3 No of sticks U take...(1 to 4) : 2 No of Sticks Left = 1 I lose.... Congrats dude... Good job... Welcome to the Game Enter 1: to play Enter 2: Help Enter 3: To exit Enter ur choice:For this input I loose ( I mean computer).... I now understand that one can't cheat always..:lolflag:
I think my logic is faulty... Can anyone tell me a better one...


Sign In
Create Account


Back to top









