All pastes #270056 Raw Edit

Unnamed

public text v1 · immutable
·published 2006-12-07 01:10 UTC
rendered paste body
#include <stdio.h>
#include <stdlib.h>
#include "randint.h"
#include <string.h>
#include <time.h>
#define HIGH_SCORES 10
#define HIGH_SCORE_FILE "highscores.txt"
#define HIGH_SCORE_LEN 64
#define NAMES_FILE "monsternames.txt"
#define MAX_NAMES 30
#define MAX_NAME_LEN 12
#define MAX_WEAPON_LEN 16
#define MAX_WEAPONS 17
#define WEAPONS_FILE "weapons.txt"
#define MAX_ROOMS 10
#define ROOMS_FILE "rooms.txt"
#define MAX_ROOM_LEN 768
#define DELIMETER '%'
#define DEFAULT_BUFFER_SIZE 16
#define ROOM_ROWS 5

// prototype section
int clearstdin(void);
void oorzaquit(char*, int);
int battle(int, int, int, int, int, int, int*s);
int generateMonsterLvl(int);
int generateMonsterHP(int);
int generateMonsterStr(int);
void logo(void);
int ismonster(void);
char *nameMonster(void);
char *chooseWeapon(void);
int run(int, int);
int sleep(int);
char *nameRoom(int);
int roomNew(int, int);
int level(int*, int*, int*, int*, int*);
void boss(void);
void highscore(char*, int);
//end prototype section

// my function oorza!
int clearstdin(void)
{
        char buff;
        do {
                buff = getchar();
        } while ((buff != 0x0a) && (buff != EOF));

        clearerr(stdin);
}

//my function - for quitting the program
void oorzaquit(char *name, int level)
{
	printf("Thank you for playing.");
	highscore(name,level);
	exit(1);

}



// This function makes a battle happend
int battle(int playerLevel, int playerHp, int playerStr, int baddielevel, int baddiehp, int baddiestr, int *ran) {
    
int a, b, playerRoll, baddieRoll,i,n;
char choice;

     while (baddiehp >0 && playerHp >0 ) {
           //system("cls");
      	   system("clear");
           do {
              srand ( time(NULL) );
              playerRoll = randint(playerLevel, playerStr) + 3;
              baddieRoll = randint(baddielevel, baddiestr);
              if (playerRoll > baddieRoll) {
                 srand ( time(NULL) );
                 a = (abs(playerStr - baddiestr));
                 b = (2 * abs(playerStr - baddiestr));
                 if (a < 0) 
                    a = 0;
                 if (b < 0) 
                    b = 0;
            printf("You lose %i life and take %i life from the monster!\n",a,b);
            playerHp=playerHp-a; baddiehp=baddiehp-b;
            printf("You: %i\t\t\t\t\tMonster: %i\n\n",playerHp,baddiehp);
            } // end of if
         else {
              srand ( time(NULL) );
              a= ((2 * abs(playerStr - baddiestr)));
              b= ((abs(playerStr - baddiestr)));
              if (a < 0) {a = 0;}
              if (b < 0) {b = 0;}
              printf("You lose %i life and take %i life from the monster!\n",a,b);
              playerHp=playerHp-a; baddiehp=baddiehp-b;
              printf("You: %i\t\t\t\t\tMonster: %i\n\n",playerHp,baddiehp);
              } // end of else
        
         if (playerHp > 0 && baddiehp > 0) {
            do {
                printf("Continue fighting? (Y or N)  ");
                //fflush(stdin); --- hmmm is it needed?
                clearstdin();
                scanf("%c",&choice);
                switch ( choice) {
                   case 'Y' : case 'y' :
                            n = 1;
                            break;
                   case 'N' : case 'n' : 
                            i = randint(0,3);
                            if (i == 0) {
                               printf("As you turn and run, the monster chases you down and stabs you in the back.  \nThanks for playing!\n");
                               playerHp=0;
                               } //end of if
                               else {
                                    *ran = 1;
                                    return playerHp;
                                    } //end of else
                                    n = 1;
                                    break;
                                    default  : printf("What are you?  A coward?  A hero doesn't dilly-dally because he can't make \nhis mind up!\n"); break;
                   } //end of switch
               } while (n != 1); //end of little do-while
            } // end of if
         else 
              return playerHp;
      } while(playerHp > 0); //end of big do-while
      } //end of while

return playerHp;

} //end of battle()

int generateMonsterLvl(int playerLevel) {
    int i = randint(playerLevel-3,playerLevel+4);
    if (i < 1) 
          i = 1;
    return i;
}

int generateMonsterHP(int baddielevel) {
    int i = randint(baddielevel*2, baddielevel*3);
    return i;
}

int generateMonsterStr(int baddielevel) {
    int i = randint(baddielevel*2, baddielevel*3);
    return i;
}

void logo(void) {
     //system("color 4c"); // gives us some nice and scary color
     FILE *logo;
     char *log;
     log = malloc (75 * sizeof(char));
     logo = fopen("logo.txt", "r");
     while ( fgets(log, 75, logo)!= NULL) {
           printf("%s",log);
           } //end of while
}

int ismonster(void) {
    int i = randint(0,1);
    switch( i ) {
              case 0: return 0; break;
              case 1: return 1; break;
              }
}


char *nameMonster(void) {
     int b=0, c, x;
     char buf[MAX_NAME_LEN];
     char *monstername[MAX_NAMES];
     FILE *monsters;
     if((monsters = fopen(NAMES_FILE, "r")) == NULL) {
                  printf("Couldn't open file %s\n", NAMES_FILE);
                  exit(1);
     } // end of if
     while(b < MAX_NAMES && fgets(buf, MAX_NAME_LEN, monsters) != NULL) {
             monstername[b] = malloc(MAX_NAME_LEN);
             strncpy(monstername[b], strtok(buf, "\n"), MAX_NAME_LEN);
             b++;
             } // end of while
     c = randint(0,MAX_NAMES);
     //printf("Read %d names from file %s\n", b, NAMES_FILE);
     fclose(monsters);
     return monstername[c];
     for(x=0; x<MAX_NAMES; x++) {
              free(monstername[x]);
              } // end of for */hi
     //maybe clearstdin?
     clearstdin();
     getchar();
     //system("pause");
}

char *chooseWeapon(void) {
     int b=0, c, d;
     char buf[MAX_WEAPON_LEN];
     char *weaponName[MAX_WEAPONS];
     FILE *weapons;
     if((weapons = fopen(WEAPONS_FILE, "r")) == NULL) {
                  printf("Couldn't open file %s\n", WEAPONS_FILE);
                  exit(1);
     } // end of if
     while(b < MAX_WEAPONS && fgets(buf, MAX_WEAPON_LEN, weapons) != NULL) {
             weaponName[b] = malloc(MAX_WEAPON_LEN);
             strncpy(weaponName[b], strtok(buf, "\n"), MAX_WEAPON_LEN);
             b++;
             } // end of while
     c = randint(0,MAX_WEAPONS);
     //printf("Read %d names from file %s\n", b, NAMES_FILE);
     fclose(weapons);
     return weaponName[c];
     for(d=0; d<MAX_NAMES; d++) {
              free(weaponName[d]);
              } // end of for 
     //clearstdin here?
     clearstdin();
     //system("pause");
     getchar();
}

int run(int playerlvl, int baddielvl) {
    int b =(baddielvl - playerlvl);
    int i = randint(0, (baddielvl - playerlvl)*3);
    if (i < 0) 
          i = randint(0,1);
    return i;
}

int sleep(int hp) {
    int i = randint(0,10), Return;
    if (i == 5) {
          printf("As you lie down to sleep, a monster comes up and kills you in your sleep\n");
          Return = 0;
          }
    else {
         printf("You sleep silently, dreaming of home ...\n");
         Return = hp;
         }
    //clearstdin here?
    //clearstdin();
    //system("pause");
    getchar();
    return Return;
}
    

char *nameRoom(int i) {
	char *bufferA, *bufferB, character, *roomDescription[9];
	int bufferSize = DEFAULT_BUFFER_SIZE, roomNumber = 0, bufferStrlen;
	FILE *rooms;
	rooms = fopen(ROOMS_FILE, "r");

	bufferA = memset(malloc(bufferSize * sizeof(char)),' ',1);
	do {
		if (strlen(bufferA) >= (bufferSize - 1)) {
			bufferB = bufferA;
			bufferSize *= 2;
			bufferA = memset(malloc(bufferSize * sizeof(char)),' ',1);
			strcpy(bufferA,bufferB);
			free(bufferB);
		}
		if (((character = fgetc(rooms)) != EOF) && (character != DELIMETER)) {
			bufferStrlen = strlen(bufferA);
			bufferA[bufferStrlen] = character;
			bufferA[bufferStrlen + 1]='\0';
		} 
        else {
             roomDescription[roomNumber] = malloc(sizeof(bufferA + 1) * sizeof(char));
			 roomDescription[roomNumber] = bufferA;
			 bufferSize = DEFAULT_BUFFER_SIZE;
			 bufferA = memset(malloc(bufferSize * sizeof(char)),' ',1);
			 roomNumber++;
			 if (roomNumber >= MAX_ROOMS)
				return roomDescription[i];
		}			
		
	} while (!ferror(rooms));
	return roomDescription[i];
}

int roomNew(int roomP, int change){           
               
     if (change == 1) { //NORTH
        if (roomP != (3*ROOM_ROWS-1) && roomP != ((3*ROOM_ROWS)+1)) {
           if (roomP == 1)  //the only room that breaks the formula is room 1.  This fixes that
              roomP = 3;  
           else
               roomP = roomP + 3; 
                  }
        else printf("You can't go North from here!\n");
        }
        
     if (change == 2) { //EAST
        if (roomP % 3 != 1)
           roomP = roomP + 1; 
        else 
             printf("You can't go East from here!\n");
        }     
        
     if (change == 3) { //SOUTH
        if (roomP != 2 && roomP != 4)
           roomP = roomP - 3; 
        else 
             printf("You can't go South from here!\n");
        }
        
     if (change == 4) { //WEST
        if ((roomP % 3) != 2)
           roomP = roomP - 1; 
        else 
             printf("You can't go West from here!\n"); 
        }
     return roomP;
}


int level(int *lvl, int *exp, int *hp, int *maxhp, int *str) { //life would be no fun without levels
    /* The way this function works is rather simple.  You need 1+current level
    total experience to reach the next one. */
    
    if (*exp > *lvl) {  //do they have enough EXP?
       (*lvl)++; (*exp) = 0; // increase their level, then reset their experience
       //system("cls"); // clear the screen for the triumphant levelup screen
       system("clear");
       printf("**************************CONGRATULATIONS!***********************\n");
       printf("You have reached level %i! You need %i experience to level again!\n\n",*lvl,(*lvl + 1));
       *maxhp = *maxhp + randint(5,20); // increase hp
       //next little tidbit makes it so that strength gets boosted every other level
       if (*lvl % 2 == 0) {
          *str = *str + 1;
          }
          
       *hp = *maxhp;// give a hard-earned health boost
       
    }
    else { //too bad they didn't level
        int i = (*lvl + 1) - (*exp);
        printf("You still need %i exp to level\n\n",i);
        }
}

void boss(void) {
     printf("The girl looks at you and starts to speak. You are terrified.\n");
     printf("She says, \"congratulations, you have made it this far, I do not\n");
     printf("Need to kill you.  If you had found me earlier I would have.\"\n");
     printf("The girl vanishes and a door to the North opens up and you walk\n");
     printf("of the towerm smiling! Congratulations, you win!");
     //clearstdin here?
     clearstdin();
     //system("pause");
     getchar();
     exit(0);
     }

void highscore(char *name, int level) { //bubble-sorting high-score list
     FILE *hs;
     int b=0,c,x,y;
     char *highscores[HIGH_SCORES+1];
     char buf[HIGH_SCORE_LEN], buf2[HIGH_SCORE_LEN];
     
     //next part reads the file for writing and fills the array from file
     if((hs = fopen(HIGH_SCORE_FILE, "r+")) == NULL) {
            printf("Couldn't open file %s\n", HIGH_SCORE_FILE);
                  exit(1);
                  } //end of if
     while(b < HIGH_SCORES && fgets(buf, HIGH_SCORE_LEN, hs) != NULL) {
             highscores[b] = malloc(HIGH_SCORE_LEN);
             strncpy(highscores[b], buf, HIGH_SCORE_LEN);
             b++;
             }
     fclose(hs);
     
     //next add the string to the bottom so we can bubble sort it
     if (level < 10) 
               sprintf(buf, "0%d\t\t%s\n", level, name);
     else
         sprintf(buf, "%d\t\t%s\n", level, name);
     //printf("%s", buf); //uncomment to make sure we have buf right
     
     highscores[HIGH_SCORES] = malloc(HIGH_SCORE_LEN);
     strncpy(highscores[HIGH_SCORES], buf, HIGH_SCORE_LEN);
     // printf("%s",highscores[HIGH_SCORES]); //unedit this line for bug-checks
     // makes sure that we've copied into the array correctly
      for(x=0;x< HIGH_SCORES + 1;x++) //bubble sorts everything
           for(y=0;y<HIGH_SCORES;y++)
                  if(strcmp(highscores[y],highscores[y+1])<0) {
                  strcpy(buf, highscores[y+1]);
                  strcpy(highscores[y+1],highscores[y]);
                  strcpy(highscores[y], buf);
           } 
           
     hs = fopen(HIGH_SCORE_FILE, "w+"); // warning! this deletes the highscores file!
     //writes a new high score file!
     for (b=0;b<HIGH_SCORES;b++) { //only writes top 10.
         fprintf(hs, "%s",highscores[b]);
         }
     //system("cls");
     system("clear");
     printf("*****HIGH SCORES*****\n");
     for (b=0;b<HIGH_SCORES;b++) { //only writes top 10.
         printf("%s",highscores[b]);
         }
} // end of highscore()