RPG Game

CMPC :: Computing :: Programming :: C

Go down

RPG Game

Post by Admin on Wed Dec 02, 2009 11:48 pm

Code:
#include <stdio.h>
#include <time.h>
char line[20];
char pass[20]; // yes value of pass causes the program to pass into the next subfunction. Else it ends the program

char typechoice[20]; //human player's choice of fire, water or grass
int typenum; // type becomes a number in order to compare to comptypenum, round-robin style; will institute once I've read a Pokemon rulebook.
int typemod; //depending on which is dominant, either the player or the computer gets the mod score

char class[20]; //choose a class
int classdexbonus = 0;
int classstrbonus = 0;
int classsplbonus = 0;

int str; // based on the 3d6 D&D value assignment
int dex;
int spl;
int strmod; // based on D&D modifier tables
int dexmod;
int splmod;

int playerhp = 20;

struct monster
{
   struct monster *next; //chainable
   char name [100]; // unable to overflow...
   int comptypenum;
   char comptype [100];
   int hp;
   
} monsters[3];

struct monster *monster;

int monster_initnum = 0;

main()
{
   int playerwins;
   
   startgame:
   playerhp = 20;
   
   srand (time(NULL));
   
   monster_init();
   
   monster = &monsters[0];
   
   printprompt ();
   
   if ((strcmp (pass, "yes")) == 0)
         ;
      else if ((strcmp (pass, "no")) == 0)
         goto endgame;
      else
      {
         printf ("invalid answer '%s' to restart prompt. Ending program", pass);
         goto endgame;
      }
   
   typefunc();
   
   classprompt();
   
   attributes();
   
   
   continuegame: // after first battle, gameplay starts from here.
   
   comptypefunc();
   
   typecompare();
   
   
   playerwins = gameplay();
      
      if ((playerwins == 1) && (monster->next != NULL))
      {
         monster = monster->next;
         goto continuegame;
      }
      
      else
      {
         printf ("Would you like to play again?");
         fgets (line, sizeof(line), stdin);
         sscanf (line, "%[a-z]", pass);
   
         if ((strcmp (pass, "yes")) == 0)
         {
            printf ("Restarting:\n");
            goto startgame;
         }   
         else if ((strcmp (pass, "no")) == 0)
            printf ("Thanks for playing!");
         else
            printf ("invalid answer '%s'. Ending program", pass);
      }
   
   endgame:
   printf ("\n\nGAME OVER \n\n");

}

monster_init()
{
   strcpy (monsters[0].name, "Galrog");
   monsters[0].hp = 20;
   monsters[0].next = &monsters[1];
   
   strcpy (monsters[1].name, "Torben");
   monsters[1].hp = 25;
   monsters[1].next = &monsters[2];
   
   strcpy (monsters[2].name, "Bardum");
   monsters[2].hp = 30;
   monsters[2].next = NULL;   
}

printprompt ()
{

   
   printf ("Welcome to World of ASCIIcraft, proto 13. Are you ready to play? yes/no :");
   fgets (line, sizeof(line), stdin);
   sscanf (line, "%[a-z]", pass);
   
      if ((strcmp (pass, "yes")) == 0)
         printf ("Wonderful! Prepare to die!\n");
      else if ((strcmp (pass, "no")) == 0)
         return 0;
      else
      {
         printf ("invalid answer '%s'. Ending program", pass);
         return 0;
      }

   printf ("You have three attacks\n");
   printf ("'sword' is a close combat attack\n");
   printf ("'bow' is a ranged attack\n");
   printf ("'fireball' is a spell attack\n");
}

typefunc ()
{
   typefuncstart:
   
   printf ("Choose your type.\n");
   printf ("You may be fire, water, or grass. Type the full name :");
   fgets (line, sizeof(line), stdin);
   sscanf (line, "%[a-z]", typechoice);

   if (strcmp (typechoice, "fire") == 0)
      typenum = 0;
   else if (strcmp (typechoice, "water") == 0)
      typenum = 1;
   else if (strcmp (typechoice, "grass") == 0)
      typenum = 2;
   else
   {
      printf ("We don't know why you entered '%s', its not a valid choice\n", typechoice);
      printf ("Pick a valid type");
      goto typefuncstart; //arbitrary number that will not compare in typecompare
   }
}

comptypefunc()
{
   monster->comptypenum = (rand() %3); //comptypenum can be 0, 1, or 2 ; 0 if fire, 1 is water, 2 is grass
   
   if (monster->comptypenum == 0)
      strcpy (monster->comptype, "fire"); //new function of the day!
   else if (monster->comptypenum == 1)
      strcpy (monster->comptype, "water");
   else if (monster->comptypenum == 2)
      strcpy (monster->comptype, "grass");
   else
      printf ("not sure what happened, %s has no type. See comptypefunc routine\n", monster->name);
         
   printf ("%s is a %s-type monster\n", monster->name, monster->comptype);
}

typecompare()
{
   typemod = 0;
   
   if (typenum == monster->comptypenum) //same type
      typemod = 0;
   else if (typenum == ((monster->comptypenum + 1) %3)) // player type is the higher (winning) type
      typemod = 1;
   else if (monster->comptypenum == ((typenum + 1) %3)) // player type is the lower (losing) type
      typemod = -1;
   else
   {
      printf ("type compare error - see typecompare subfunction\n");
      printf ("player type value '%d', computer type value '%d'\n", typenum, monster->comptypenum);
      typemod = -2;
   }   
   printf ("Your type advantage is %d\n", typemod);

}

classprompt()
{
   classpromptstart:
   
   classdexbonus = 0;
   classstrbonus = 0;
   classsplbonus = 0;
   
   printf ("choose your class.\n you may be a rogue, fighter, or cleric: ");
   fgets (line, sizeof(line), stdin);
   sscanf (line, "%[a-z]", class);

   if (strcmp (class, "rogue") == 0)
      classdexbonus = 2;
   else if (strcmp (class, "fighter") == 0)
      classstrbonus = 2;
   else if (strcmp (class, "cleric") == 0)
      classsplbonus = 2;
   else if (strcmp (class, "deathknight") == 0)
   {
      classdexbonus = 10;
      classstrbonus = 10;
      classsplbonus = 10;
   }
   else
   {
      printf ("pick a valid class\n");
      goto classpromptstart;
   }

}
attributes()
{

   str = (rand() %6) + (rand() %6) + (rand() %6) + classstrbonus;
   dex = (rand() %6) + (rand() %6) + (rand() %6) + classdexbonus;
   spl = (rand() %6) + (rand() %6) + (rand() %6) + classsplbonus;
   
   if (str < 10)
      strmod = -1;
   else if (str == 10)
      strmod = 0;
   else if (str > 10)
      strmod = 1;
   else
      printf ("error in attributes subfunc: str");
      
   if (dex < 10)
      dexmod = -1;
   else if (dex == 10)
      dexmod = 0;
   else if (dex > 10)
      dexmod = 1;
   else
      printf ("error in attributes subfunc: dex");

   if (spl < 10)
      splmod = -1;
   else if (spl == 10)
      splmod = 0;
   else if (spl > 10)
      splmod = 1;
   else
      printf ("error in attributes subfunc: spl");
      
   printf ("Strength = %d, strength modifier = %d\n", str, strmod);
   printf ("Dexterity = %d, dexterity modifier = %d\n", dex, dexmod);
   printf ("Spellpower = %d, spellpower modifier = %d\n", spl, splmod);
}

int gameplay()
{

   int hitattempt, dmg; // chances to hit based on a d20 system.
   int roundnum = 0;
   int base = 2; // base attack, so that every successful attack does at least 1 dmg
   char line[20];
   char attack [20]; // is a word derived from the string, either 'tackle' or 'punch'
   
   
   printf ("\n\n%s IS ATTACKING! \n\n", monster->name);
   
   while (playerhp > 0 && monster->hp > 0 )
   {
      dmg = 0;
      roundnum++;
      
      printf ("\nRound number %d. What's your attack?  ", roundnum);
      fgets(line, sizeof(line), stdin);
      sscanf (line, "%[a-z]", attack);
      
      if (strcmp (attack, "sword") == 0)
      {
         hitattempt = (rand() %20) + dexmod; // all chances to hit are based on a d20 roll
         
         if (hitattempt > 2) // very high chance to hit
            dmg = (rand() %3) + base + typemod + strmod;
         else
            printf ("your sword misses\n");
      }
      else if (strcmp (attack, "bow") == 0)
      {
         hitattempt = (rand() %20) + dexmod;
         
         if (hitattempt >10) // 50% chance to hit
            dmg = (rand() %5) + base + typemod + dexmod; // damage based on a 2d3 roll
         else
            printf ("your arrow misses\n");
      }
      else if (strcmp (attack, "fireball") == 0)
      {
         hitattempt = (rand() %20) + dexmod;
         
         if (hitattempt >10)
            dmg = (rand() %4) + (rand() %4) + base + typemod + splmod;
         else
            printf ("your fireball misses\n");
      }
      else if (strcmp (attack, "nuke") == 0)
      {
         dmg = monster->hp; //automatic kill
         printf ("KABOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOM!!!\n");
      }
      else if (strcmp (attack, "heal") == 0)
         playerhp = 20;   
      else
         printf ("invalid entry '%s'. you forfeit your turn.\n", attack);
         
      monster->hp = monster->hp - dmg;
      
      printf ("this round you dealt %d damage. ", dmg);
      
      if (monster->hp <=0)
         printf ("%s is dead!", monster->name);
      
      else
      {
         printf ("%s has %d health.\n", monster->name, monster->hp);
      
         dmg = 0;
      
         hitattempt = (rand() %20); // for this version the computer will only make one kind of attack
      
         if (hitattempt > 5)
            dmg = (rand() %3) + base;
         else
            printf ("%s's attack misses you!\n", monster->name);
      
         playerhp = playerhp - dmg;
         
         if (playerhp > 0)
         printf ("this round %s dealt %d damage. You have %d health.\n", monster->name, dmg, playerhp);
         else
         printf ("this round %s dealt %d damage. You are dead.\n", monster->name, dmg);
      }
      
   }
   
   if (monster->hp <=0)
   {
      printf ("\nyou've killed %s! Good job!\n\n", monster->name);
      return 1;
   }
   else if (playerhp <= 0)
   {
      printf ("\nyou've been killed by %s! sux 2bu!\n", monster->name);
      return 0;
   }
   else
   {
      printf ("\nthe game has ended for no particular reason. You have %d health and %s has %d health. Go figure.", playerhp, monster->name, monster->hp);
      return 0;
   }
}
avatar
Admin
Pirate King

Posts : 559

Back to top Go down

Re: RPG Game

Post by Unchained on Thu Dec 03, 2009 12:03 am

I remember this, did it actually get fixed or did she just turn it in as is?
avatar
Unchained
Mod

Posts : 448

Back to top Go down

Re: RPG Game

Post by Admin on Thu Dec 03, 2009 12:07 am

Unchained wrote:I remember this, did it actually get fixed or did she just turn it in as is?
i don't see what needs fixing. lol after 13 rough drafts, this is her final product. works fine but doesn't work in my C compiler. hmm she used Miracle C which i never figured out how to install. I use my old cheap Quincy 2005 compiler for C. It picks up ALOT of errors. Works fine for her Razz
avatar
Admin
Pirate King

Posts : 559

Back to top Go down

Re: RPG Game

Post by Unchained on Thu Dec 03, 2009 12:10 am

The problem she had was that it never changed and always got the same result, it was probably fixed though.
avatar
Unchained
Mod

Posts : 448

Back to top Go down

Re: RPG Game

Post by Admin on Thu Dec 03, 2009 12:18 am

Unchained wrote:The problem she had was that it never changed and always got the same result, it was probably fixed though.
DO YOU REALLY EXPECT ME TO UNDERSTAND WHAT "IT" MEANS when you say "it never changed and always got the same result"?????
avatar
Admin
Pirate King

Posts : 559

Back to top Go down

Re: RPG Game

Post by Paul on Thu Dec 03, 2009 2:49 am

She needed help seeding the random number generator. or rather she wasn't seeding it. The reason behind the "it never changed." I showed her how to seed it based on time.
avatar
Paul
Pickaxe

Posts : 611

Back to top Go down

Re: RPG Game

Post by Unchained on Thu Dec 03, 2009 6:41 am

Allen: You knew exactly what I meant, don't become one of the crazy admins.

another_pd: Good to know someone could help, I knew the base of the problem but I don't know any C so I wasn't able to help much. Who are you?
avatar
Unchained
Mod

Posts : 448

Back to top Go down

Re: RPG Game

Post by Admin on Thu Dec 03, 2009 6:59 am

i'm confused O.o
avatar
Admin
Pirate King

Posts : 559

Back to top Go down

Re: RPG Game

Post by Paul on Fri Dec 04, 2009 1:48 am

My real name would be Paul. I became interested in computers in 4th grade and learned html. In 6th, I learned basic after a failed attempt to pick up perl as my first programming language. During the summer between 7th to 8th grade I learned C and a little C++. I now mostly program in C++ and Java.

Anyways. . .
avatar
Paul
Pickaxe

Posts : 611

Back to top Go down

Re: RPG Game

Post by I_don't_know on Tue Dec 08, 2009 12:27 am

can you try this in a c++ compiler?
avatar
I_don't_know
Skid

Posts : 17

Back to top Go down

Re: RPG Game

Post by Paul on Tue Dec 08, 2009 3:11 am

I_don't_know wrote:can you try this in a c++ compiler?
C++ will run most C code.

You can try it might work.

You'll need to change the includes to
Code:

#include <cstdio>
#include <ctime>
avatar
Paul
Pickaxe

Posts : 611

Back to top Go down

Re: RPG Game

Post by I_don't_know on Tue Dec 08, 2009 6:23 am

thanks, i wanna try it Smile
avatar
I_don't_know
Skid

Posts : 17

Back to top Go down

Re: RPG Game

Post by Paul on Thu Dec 10, 2009 12:30 am

C++ was intended to extend C. The main goal was to add object orientation to C.
avatar
Paul
Pickaxe

Posts : 611

Back to top Go down

Re: RPG Game

Post by Paul on Thu Dec 10, 2009 12:31 am

If you tried it, did it work?
avatar
Paul
Pickaxe

Posts : 611

Back to top Go down

Re: RPG Game

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

CMPC :: Computing :: Programming :: C

 
Permissions in this forum:
You cannot reply to topics in this forum