dimanche 23 février 2020

Random number generator in c program is not generating correct random numbers

I am working on a project about a game that involves creating randomly generated rooms with either monsters, prizes, or empty. However, every time I run the program it sets the room as a prize, and even generates incorrect numbers for the amount of points the prize gives you.

#include <stdio.h>
#include <stdlib.h>
#include "monsterbattle.h"
#include <time.h>

int main(int argc, char *argv[]) {

Character user;
int numRooms;

srand(time(NULL));

user.hitPoints = 50;
user.experiencePoints = 0;

printf("Welcome to Monster-Battle!\n");
printf("Enter the amount of rooms you would like to endure: ");
scanf("%d", &numRooms);

printf("You have decided to endure %d rooms, good luck!\n", numRooms);
printf("----------------\n");
printCharacter(user);

struct Room *roomArray = malloc(sizeof(struct Room) * numRooms);

fillRooms(roomArray, numRooms);

enterRoom(roomArray, user, numRooms);

free(roomArray);

return 0;

}

Here is my main function, where I call all of my functions.

#include <stdio.h>
#include <stdlib.h>
#include "monsterbattle.h"
#include <time.h>

void printCharacter(Character c) {

printf("Player Stats: (HP: %d XP: %d)\n", c.hitPoints, c.experiencePoints);

}

int getRandomNumber(int min, int max) {

return min + rand() % (max+1 - min);

}

void fillRooms(Room *rooms, int length) {

for (int i = 0; i < length; i++) {

int randomNum = getRandomNumber(0, 9);

if (randomNum == 0) {
rooms[i].type = EMPTY;
}

else if ((randomNum >= 1) && (randomNum <= 4)) {
rooms[i].type = PRIZE;
rooms[i].prize.points = getRandomNumber(5, 10);
}

else if ((randomNum >= 5) && (randomNum <= 9)) {
rooms[i].type = MONSTER;
rooms[i].monster.hitPoints = getRandomNumber(10, 30);

if (rooms[i].monster.hitPoints % 3 == 0) {
rooms[i].monster.experiencePoints = 1;
}
else if (rooms[i].monster.hitPoints % 3 != 0) {
rooms[i].monster.experiencePoints = 0;
}

}

}

}

void enterRoom(Room *room, Character player, int length) {

for (int i = 0; i < length; i++) {

printf("----------------\n");
printf("ROOM %d OF %d...\n", i+1, length);

if (room[i].type = EMPTY) {
printf("This room is empty.\n");
printCharacter(player);
}
if (room[i].type = EMPTY) {
printf("This room is empty.\n");
printCharacter(player);
}

else if (room[i].type = PRIZE) {
printf("This room has a prize. (PTS: %d)\n", room[i].prize.points);
player.hitPoints = player.hitPoints + room[i].prize.points;
printf("You gained %d hit points!\n", room[i].prize.points);
printCharacter(player);
}

else if (room[i].type = MONSTER) {
printf("This room has a monster. (HP: %d XP: %d)\n", room[i].monster.hitPoints, room[i].monster.experiencePoints);
player.hitPoints = player.hitPoints - room[i].monster.hitPoints;
player.experiencePoints = player.experiencePoints + room[i].monster.experiencePoints;
printf("You lost %d hit points!\n", room[i].monster.hitPoints);

if(room[i].monster.experiencePoints = 1) {
printf("You gained %d experience point!\n", room[i].monster.experiencePoints);
}

if(player.hitPoints <= 0) {
printf("Sorry, you died! Better luck next time...\n");
}

printCharacter(player);
}

}

if(player.hitPoints > 0) {
printf("----------------\n");
printf("Congratulations! You survived the game!\n");
}

}

and here is where I define all of my functions. Any help would be appreciated!




Aucun commentaire:

Enregistrer un commentaire