The project in C is to create a game in which a player goes through a series of rooms in which they can find a prize or a monster to fight. Game Rules:
• A player will enter the number of rooms (greater than 1) they want to endure. The program will dynamically allocate an array of Rooms and populate it with prizes and monsters. Then, the player will enter each room in which an action will occur: o If the player enters a room with a prize, they will get a random value between 5 and 10, inclusive, added to their hitPoints. o If the player enters a room with a monster, the player will lose hitPoints based on the monster’s hitPoints and the player’s experiencePoints. The monster’s hitPoints is a random value between 10 and 30, inclusive. The total number of hitPoints lost is equal to the monster’s hitPoints minus the player’s experiencePoints. Should the player’s experiencePoints be greater than the monster’s hitPoints, the player does not lose any hitPoints. Each time a player battles a monster (win or lose), the player will get an increase to their experiencePoints. This increase is a random value of 0 or 1.
• If a player’s hitPoints goes to zero or below at any point, they automatically lose. • If a player completes all the rooms without their hitPoints going to zero at any time, they win. TASK 1 – create and write monsterbattle.c and monsterbattle.h Write the source (.c) and header (.h) files for the game. The header file will have the definitions for all the struct’s you need, an enum, and the prototypes for the functions below. enum Include this enum in your header file so you can use as values for determining what the room has. typedef enum TYPE { EMPTY, PRIZE, MONSTER } TYPE; struct’s
• Character – this struct has two data members hitPoints and experiencePoints. This struct will represent a monster in the game. In this game, a monster’s hitPoints represents the amount of damage it can inflict on the player and the experiencePoints is the number of experience points the player gains from battling this monster. Note that this struct will also be used to represent the player in which case the hitPoints represents their life points. Should this value go to zero or below, they have died. The experiencePoints for a player are used to deflect a monster’s attack.
• Prize – this struct has one data members points. The points is what is awarded to a player’s hitPoints should they enter a room with a prize.
• Room – this struct represents a room. The game will create an array of these structs. The struct has three data members: type (see the enum above) to determine if this room is empty, has a monster, or a prize. If the room has a prize, then the prize data member will be initialized (see fillRooms()). If the room has a monster, then the monster data member will be initialized (see fillRooms()). If the room is empty, nothing happens with these data members. This struct is provided to you here: typedef struct Room { TYPE type; Character monster; Prize prize; } Room; Prototypes See functions in Task 2 below. Write all the prototypes. TASK 2 – create and write monsterbattle.c (Monster Battle functions) Write the following functions that use all the struct's above.
• int getRandomNumber( int min, int max )– This function computes a random number between min and max, inclusive, and returns it.
• void fillRooms( Rooms *rooms, int length ) – This function fills the array of rooms with a prize, monster, or nothing (empty room). There is a 10% chance that this room is empty, a 40% chance that this room has a prize, and 50% chance that this room has a monster in it. To do this, you can use the function getRandomNumber() to generate a random number between 0 and 9, inclusive, and use those values to determine which type to set. o Set the room’s type data member to EMPTY, PRIZE, or MONSTER o If the type is PRIZE, set the room’s prize data member’s points data member to a random value between 5 and 10, inclusive. o If the type is MONSTER, set the room’s monster data member’s hitPoints data member to a random value between 10 and 30, inclusive.using getRandomNumber().If the room’s monster hitPoints is a multiple of 3, set the room’s monster data member’s experiencePoints data member to 1. Otherwise, set its experiencePoints to 0. o If the type is EMPTY, do not set any other of the room’s data members.
• void enterRoom( Room room, Character player, int *resultHP, int *resultXP ) – This function simulates the character entering a room. o If the room type is EMPTY, print out a message that says the room is empty. Set the dereferenced values of resultHP and resultXP to 0. o If the room type is PRIZE, print out a message that says the room has a prize and how many points it has. Set the dereferenced values of resultHP to the prize’s points and resultXP to 0. o If the room type is MONSTER, print out a message saying that the room has a monster. Do the battle calculations (see the rules above) to figure out how much damage was done (this should be a negative number). Set the dereferenced values of resultHP to the total damage and resultXP to the number of experiencePoints awarded by the monster. This function returns nothing.
• void printCharacter( Character c ) – This function prints out the Character’s data member. It could look like this: Player (HP: 20, XP: 9). Assume it will only be used to print out the player’s information. TASK 3: Complete the project1-main.c
• Download the attached project1-main.c • Follow the instructions written in the comments in the main() function. The main() is the driver of the program. It calls the functions above set up the game and to simulate the battles.
project1 - main.c:
#include <stdio.h> #include <stdlib.h> int main() { /* 1. Declare your variables here */ /* 2. change the seed for random */ /* 3. Initialize the player struct object, to have 50 hitpoints and 0 experience points by default */ /* 4. Dynamically allocate an array of Room structs with the number the user entered. Assume that the user has entered a positive number */ /* 5. Call the fillRooms function() to create the rooms */ /* 6. Print out a message to say how many rooms there are and that the challenge has started. */ /* 7. Call printPlayer() to print out the beginning stats */ /* 8. Taverse the array of rooms, for each room: 8.1 Call enterRoom() 8.2 Print out a message about how many hitpoints were lost or gained while in this room. Print out this value in a positive number (You lost 5 hitpoints while in this room). 8.3 Determine if entering the room resulted in a gain in experience points, and print out an appropriate message stating as such. If not experience points were gained, do not print out a message. 8.4 Update the player's hitpoints with the loss or gain. Never allow for the player's hitpoints to be negative. If it goes below zero, simply set the player's hitpoints to 0. If the player's hitpoints is zero, break the loop as the game is over. 8.5 Update the player's experience points 8.6 Call printPlayer() to print out the updated player's stats. 9. Outside the loop, print out a message of congratulations if the player has any hitpoints left. If a player has zero hitpoints, print out a message of regret. 10. deallocate the array of rooms. */ return 0; }
program code:
// monsterbattle.h:
#ifndef MONSTERBATTLE_H
#define MONSTERBATTLE_H
typedef enum TYPE { EMPTY=1, PRIZE, MONSTER } TYPE;
//Character represents monster
typedef struct {
int hitPoints;
int experiencePoints;
}Character;
typedef struct {
int points;
}Prize;
typedef struct Room {
TYPE type;
Character monster;
Prize prize;
} Room;
#endif
// ###############################
// monsterbattle.c
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
#include "monsterbattle.h"
inline int getRandomNumber(int min,int max) {
time_t t;
int num;
srand((unsigned) time(&t));
num = rand()%(max-min+1) + min;
//printf("\nMin: %d, Max:%d, Rand generated:
%d\n",min,max, num);
return num;
}
void fillRooms(Room* rooms, int length){
for(int i=0;i<length;++i) {
int r = getRandomNumber(0,9);
// r<1 for 10%
if(r<1) { rooms[i].type =
EMPTY;}
// 5 because 10% + 40% = 50%
else if(r<5) {
rooms[i].type = PRIZE;
(rooms[i].prize).points =
getRandomNumber(5,10);
}
else {
rooms[i].type =
MONSTER;
(rooms[i].monster).hitPoints = getRandomNumber(10,30);
(rooms[i].monster).experiencePoints =
(((rooms[i].monster).hitPoints %3 == 0)?1:0 );
}
}
}
void enterRoom (Room room, Character* player, int *resultHP, int
*resultXP ) {
if (room.type == EMPTY) {
printf("\nRoom is empty");
*resultHP = 0;
*resultXP = 0;
}
else if(room.type == PRIZE) {
printf("\nRoom has a prize. It has
%d points", room.prize.points);
*resultHP =
room.prize.points;
*resultXP = 0;
player->hitPoints +=
*resultHP;
}
else {
printf("\nRoom has a
monster.");
*resultHP =
(room.monster).hitPoints;
*resultXP =
room.monster.experiencePoints;
player->hitPoints -=
*resultHP;
}
player->experiencePoints += *resultXP;
}
void printCharacter(Character c) {
printf("\nPlayer(HP:%d,XP:%d)",c.hitPoints,
c.experiencePoints);
}
int main() {
/* 1. Declare your variables here
*/
unsigned int numRooms=0;
Room* roomArr;
int resultHP=0, resultXP=0;
/* 2. change the seed for random */
time_t tm;
srand((unsigned) time(&tm));
/* 3. Initialize the player struct object, to have 50
hitpoints and 0 experience points by default */
Character player = {50,0};
/* 4. Dynamically allocate an array of Room
structs with the number the user
entered. Assume that the user
has entered a positive number */
printf("Enter the number of rooms:");
scanf("%d",&numRooms);
roomArr = (Room*) malloc (numRooms *
sizeof(Room));
/* 5. Call the fillRooms function() to create
the rooms */
fillRooms(roomArr,numRooms);
/* 6. Print out a message to say how many rooms
there are and that the challenge has started. */
printf("\nNumber of rooms: %d",numRooms);
printf("\nChallenge has started");
/* 7. Call printPlayer() to print out the
beginning stats */
printCharacter(player);
// 8. Taverse the array of rooms, for each
room:
for(int i=0;i<numRooms;++i) {
// 8.1
Call enterRoom()
enterRoom(roomArr[i],&player,&
resultHP,&resultXP);
// 8.2
Print out a message about how many hitpoints were lost or gained
while in this room. Print out this value in a positive number (You
lost 5 hitpoints while in this room).
if (resultHP>0) {printf("\nYou
gained %d hit-points in this room.",resultHP);}
else {printf("\nYou lost %d
hit-points in this room:%d",resultHP);}
// 8.3
Determine if entering the room resulted in a gain in experience
points, and print out an appropriate message stating as such. If
not experience points were gained, do not print out a
message.
if (resultXP > 0) {
printf("\nYou gained %d experience-points in
this room.",resultXP); }
// 8.4
Update the player's hitpoints with the loss or gain. Never allow
for the player's hitpoints to be negative. If it goes below zero,
simply set the player's hitpoints to 0. If the player's hitpoints
is zero, break the loop as the game is over.
if (player.hitPoints < 0)
{
player.hitPoints
= 0;
}
else if(player.hitPoints == 0)
{
printf ("\nGAME
OVER !!");
break;
}
//
8.5 Update the player's experience points
// 8.6 Call
printPlayer() to print out the updated player's stats.
printCharacter(player);
player.hitPoints = player.hitPoints
+ player.experiencePoints;
}
// 9. Outside the loop, print out a message of
congratulations if the player has any hitpoints left. If a player
has zero hitpoints, print out a message of regret.
player.hitPoints = player.hitPoints +
player.experiencePoints;
if(player.hitPoints > 0) {
printf("\nCongratulations!! You have won!!");}
else { printf("\nSorry!! You lost!!");}
// 10. deallocate the array of
rooms.
free(roomArr);
return 0;
}
// ##################################
Output snapshots:
Snapshot 2:
The project in C is to create a game in which a player goes through a...
Problem 5: Monster Factory (10 points) (Game Dev) Create a Monster class that maintains a count of all monsters instantiated and includes a static method that generates a new random monster object. In software engineering, a method that generates new instances of classes based on configuration information is called the Factory pattern. UML Class Diagram: Monster - name: String - health: int - strength: int - xp: int + spawn(type:String): Monster + constructor (name: String, health: int, strength: int, xp:...
(C++) Please create a tic tac toe game. Must write a Player class to store each players’ information (name and score) and to update their information (increase their score if appropriate) and to retrieve their information (name and score).The players must be called by name during the game. The turns alternate starting with player 1 in the first move of round 1. Whoever plays last in a round will play second in the next round (assuming there is one).The rows...
You will write a two-class Java program that implements the Game of 21. This is a fairly simple game where a player plays against a “dealer”. The player will receive two and optionally three numbers. Each number is randomly generated in the range 1 to 11 inclusive (in notation: [1,11]). The player’s score is the sum of these numbers. The dealer will receive two random numbers, also in [1,11]. The player wins if its score is greater than the dealer’s...
Two player Dice game In C++ The game of ancient game of horse, not the basketball version, is a two player game in which the first player to reach a score of 100 wins. Players take alternating turns. On each player’s turn he/she rolls a six-sided dice. After each roll: a. If the player rolls a 3-6 then he/she can either Roll again or Hold. If the player holds then the player gets all of the points summed up during...
First, you will need to create the Creature class. Creatures have 2 member variables: a name, and the number of gold coins that they are carrying. Write a constructor, getter functions for each member, and 2 other functions: - void addGoldCoins(int) adds gold coins to the Creature. - void identify() prints the Creature information. The following should run in main: Creature d{"dragon", 50}; d.addGoldCoins(10); d.identify(); // This prints: The dragon is carrying 60 gold coins. Second, you are going to...
JAVA We will write a psychic game program in which a player guesses a number randomly generated by a computer. For this project, we need 3 classes, Player, PsychicGame, and Driver. Description for each class is as following: Project Folder Name: LB05 1. Player class a.Input - nickName : nick name of the player - guessedNumber : a number picked for the player will be assigned randomly by a computer later. b. Process - constructor: asks a user player’s nickName...
C++ Homework: This will be the first in a series of assignments to create a game called Zilch. In this assignment, you will create the basic structure of the program and demonstrate the ability to create programs with multiple files. It should serve as a refresher in the basics of C++ programming, especially in the handling of arrays. The initial assignments only establish a rough outline of the game. We will be adding the rules that make the game more...
In C++. Write a class named FBoard for playing a game, where player x is trying to get her piece to row 7 and player o is trying to make it so player x doesn't have any legal moves. It should have: An 8x8 array of char for tracking the positions of the pieces. A data member called gameState that holds one of the following values: X_WON, O_WON, or UNFINISHED - use an enum type for this, not string (the...
Write a class called Player that has four data members: a string for the player's name, and an int for each of these stats: points, rebounds and assists. The class should have a default constructor that initializes the name to the empty string ("") and initializes each of the stats to -1. It should also have a constructor that takes four parameters and uses them to initialize the data members. It should have get methods for each data member. It...
C++ Please Provide .cpp Overview For this assignment, implement and use the methods for a class called Player that represents a player in the National Hockey League. Player class Use the following class definition: class Player { public: Player(); Player( const char [], int, int, int ); void printPlayer(); void setName( const char [] ); void setNumber( int ); void changeGoals( int ); void changeAssists( int ); int getNumber(); int getGoals(); int getAssists(); private: char name[50]; int number; int goals;...