BOT FIGHT
(Album art by Vamsi)
This is a simple text based strategy game... an evolution of Rock, paper, scissors game. In this game, two robots are pitted against each other and your main aim is to make the health of another bot 0. To accomplish this, each player has got three options; Charge, Shoot, Defend.
- Charge: Charges you up and enables you to attack. You cannot shoot with 0 charges.
- Shoot: Deals 5 damage to the opponent. (Requires at least 1 Charge)
- Defend: Defends and protects your health.
This first ever game published by me... hope the Exe file below works on your PC, anyway, I've provided code below!
NOTE: Not compatible with all systems... Hope it works on yours...
.exe Download ( Click Run Anyway when you are prompted )
If it doesn't run on your system, nothing to fret about. I provided the code below, paste it into your C IDE and compile it.
Prerequisites to understand the code:
- C language Basics.
- Looping statements
- Pointers
- Functions
- Structures
- How to use rand() function
- Be Creative :)
For enthusiastic developers, here's my code! TAKE THAT
#include <stdio.h>
#include <stdlib.h>
struct player
{
char name[20];
int health,charge;
};
void clear()
{
system("cls");
}
void game();
void rules();
void about();
void charge(struct player *uf,struct player *cf,int c_selection);
void defend(struct player *uf,struct player *cf,int c_selection);
void attack(struct player *uf,struct player *cf,int c_selection);
int main()
{
int opt;
printf("\n\n\t\t\tBOT-FIGHT VER 2.0");
printf("\n\n\n===============================================================================\n");
printf("\n\n\n\n\n\t\t\t1.Play\n\n\t\t\t2.Rules\n\n\t\t\t3.About\n\n\t\t\t4.Exit");
printf("\n\n\n\t\tenter your selection : ");
scanf("%d",&opt);
switch(opt)
{
case 1:
clear();
game();
break;
case 2:
clear();
rules();
break;
case 3:
clear();
about();
break;
case 4:
exit(0);
default :
clear();
printf("Invalid Entry");
main();
}
return 0;
}
void game()
{
int n,u_selection,c_selection,again;
//creating structures for user and his enemy
struct player u={"Unknown",15,1},c={"Unknown",15,1},*uf,*cf;
uf=&u;
cf=&c;
do
{
printf("\n\n\n================================================================================\n");
//Getting to know the names!
printf("\n\nEnter your Bot's name\n\n");
scanf("%s",u.name);
printf("\n\nEnter your Rival's name\n\n");
scanf("%s",c.name);
//Setting Preferences
printf("\n\nSet Max health of each player to (each successful attack deals -5 health) : ");
scanf("%d",&u.health);
c.health=u.health;
printf("\n\nSet initial number of charges to : ");
scanf("%d",&u.charge);
c.charge=u.charge;
//WARNING! the message
printf("\n\n%s!!! PREPARE FOR BATTLE WITH %s!\n\n\n",u.name,c.name);
for(n=1;u.health>0&&c.health>0;)
{
//status display
printf("\n\n\t\t----------\n\n\n\n\t\t--TURN %d--\n%s! You have %d Health left and %d charge(s)\n\n%s has %d Health left and %d charge(s)...\n",n,u.name,u.health,u.charge,c.name,c.health,c.charge);
//decision menu user
printf("\n\nWhat do you want to do %s?\n press : 0 to charge\n\t 1 to defend\n\t 2 to attack (you must have at least one charge)\n",u.name);
scanf("%d",&u_selection);
//decision menu computer
if(c.charge<1)
c_selection=rand()%2;
else
c_selection=rand()%3;
//The working algorithm
switch (u_selection)
{
case 0:
printf("\n\nYou have chosen to Charge!\n\n");
charge(uf,cf,c_selection);
break;
case 1:
printf("\n\nYou have chosen to Defend yourself!\n\n");
defend(uf,cf,c_selection);
break;
case 2:
if(u.charge<1)
{
printf("\n\nYou must have charges in order to attack\n\n repeat TURN %d\n\n",n);
continue;
}
else
{
printf("You attacked %s!",c.name);
attack(uf,cf,c_selection);
}
break;
default:
printf("Invalid selection... please repeat the turn");
continue;
}
n++;
}
if(u.health<=0)
{
printf("\n\n----YOU LOST :(-----\n\n");
printf("\nFINAL STATS: \n\n\%s! You have %d Health left and %d charge(s)\n\n%s has %d Health left and %d charge(s)...\n\nNo of turns played = %d\n\n",u.name,u.health,u.charge,c.name,c.health,c.charge,n-1);
printf("WANT TO PLAY AGAIN (press 1 to play or any other key to exit) : ");
scanf("%d",&again);
}
else
{
printf("\n\n----YOU WON!----\n\n");
printf("\nFINAL STATS: \n\n%s! You have %d Health left and %d charge(s)\n\n%s has %d Health left and %d charge(s)...\n\nNo of turns played = %d\n\n",u.name,u.health,u.charge,c.name,c.health,c.charge,n-1);
printf("Your Score : %d\n\n",(u.health*10)+(u.charge*5)-(n-1));
printf("WANT TO PLAY AGAIN (press 1 to play or any other key to exit) : ");
scanf("%d",&again);
}
clear();
}while(again==1);
clear();
main();
}
void rules()
{
int ex;
printf("\n\n\n\t\t\tRULES\n\n\n");
printf("*This game is a plain strategy game of fight between two Bots \n\n");
printf("*A Bot is controlled by the user and the other by Computer \n\n");
printf("*Your aim is to make the health of the other Bot zero \n\n");
printf("*For this you need to have luck! and a little planning \n\n");
printf("\n\npress any key and hit enter to exit : ");
scanf("%d",&ex);
clear();
main();
}
void about()
{
int ex;
printf("\n\n\n\t\t\tABOUT\n\n\n\n\n\n");
printf(" \t\tDEVOLOPED BY *******Cherub7*******\n\n\n");
printf(" \t\tPOWERED BY MARS V ");
printf("\n\n\n\npress any key and hit enter to exit : ");
scanf("%d",&ex);
clear();
main();
}
void charge(struct player *uf,struct player *cf,int c_selection)
{
switch (c_selection)
{
case 0:
printf("\n\n%s has chosen to Charge!\n\n",cf->name);
uf->charge++;
cf->charge++;
break;
case 1:
printf("\n\n%s has chosen to Defend itself!\n\n",cf->name);
uf->charge++;
break;
case 2:
printf("\n\n%s has chosen to Attack!\n\n",cf->name);
uf->charge++;
uf->health=uf->health-5;
cf->charge--;
}
}
void defend(struct player *uf,struct player *cf,int c_selection)
{
switch (c_selection)
{
case 0:
printf("\n\n%s has chosen to charge!\n\n",cf->name);
cf->charge++;
break;
case 1:
printf("\n\n%s has chosen to Defend itself!\n\n",cf->name);
break;
case 2:
printf("\n\n%s has chosen to Attack!\n\n",cf->name);
cf->charge--;
}
}
void attack(struct player *uf,struct player *cf,int c_selection)
{
switch (c_selection)
{
case 0:
printf("\n\n%s has chosen to charge!\n\n",cf->name);
cf->charge++;
cf->health=cf->health-5;
uf->charge--;
break;
case 1:
printf("\n\n%s has chosen to Defend itself!\n\n",cf->name);
uf->charge--;
break;
case 2:
printf("\n\n%s has chosen to Attack!\n\n",cf->name);
cf->charge--;
uf->charge--;
}
}
Any doubts regarding the code? I'd be glad to answer you via comment section!
Awesome bro! waiting for some more!!
ReplyDeleteThanks bro! More are on the way!
DeleteThope
ReplyDeleteThanks buddy
DeleteKiller dude
ReplyDeleteThank you dude! make sure to check other posts :D
Delete