Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, in my CSCE class we are suppose to make a game that lets the player move

ID: 3547298 • Letter: H

Question

Hello, in my CSCE class we are suppose to make a game that lets the player move around a large, open area, collecting gold coins and avoiding falling into open pits and I need help because I haven't been to class due to my illness and I'm lost.

(Oh and we're not making the full game, we're just building the software infrastructure and then I'm finishing the game in the next assignment and the language is in C) The instructions says:


1.) Define the appropriate data structure (i.e. an array, or a variable, or a struct, or

whatever you will use to store your data - that's called a "data structure") to

represent a 20x20 open area. Each element in this 20x20 data structure can be

visualized as a tile in a room, and it will have on it either a pit or gold (never both),

and it may also be occupied by the player. Something like this (this is a 5x5 area):

(there's a picture of a 5x5 box with gold and pit in random places)


2. Define an appropriate data structure for the player (there will be only one player),

which will store the name, the lives the player has, the gold collected, and the

(x,y) coordinates of where he or she is.


3. You will declare and implement the following:

a. A function that initializes the player information. The name should be read

from the keyboard. The original location is (0,0). The player initially has

no gold. The lives will be set randomly by using the function rand() and

cannot be more than 50.

b. A function that initializes each location in the room. The locations will be

initialized randomly using the function rand(), so that there is a 10%

chance of an area having gold, and 20% chance of an area having a pit.

The player also needs to be put in (0,0). Ensure that (0,0) is always empty

(i.e. no gold, no pit).

c. A function that prints the player information

d. A function that prints the room so that one can see the pits, the gold, and

the player. When printing, the axes for this room should be the "traditional"

ones, with (0,0) being the southwest corner of the room, and (MAX,MAX)

being the northeast corner.


4. In your main you will create the player and the room, and then print the player

and room on the screen. Next you will enter a loop where the user may enter a

single character indicating the move.

a. You will define a function to read the move char from standard input, and

use pass by reference to send the move to main

b. If the character read is q you will exit the loop and the program, by first

printing the player information.

c. Else, you will print the player information and the room and continue



Can anyone help me on this assignment please? Or at least help me get started please? Thank you!

Explanation / Answer

#include<stdio.h>

struct player
{
char name[30];
int lives;
int gold;
int xcord, ycord;
};

init_player(struct player *player1)
{
printf("Enter the name of the player : ");
scanf("%s",player1->name);
player1->gold=0;
player1->lives=rand()%51;
player1->xcord=0;
player1->ycord=0;
}

init_location(char room[20][20])
{
int i,j;

for(i=0;i<20;i++)
{
for(j=0;j<20;j++)
{
if(rand()%100<10)
{
room[i][j]='*';
}
else if(rand()%100<30)
{
room[i][j]='O';
}
else
{
room[i][j]=' ';
}
}
}

room[0][0]='P';
}

void print_player_info(struct player *player1)
{
printf(" Player Name : %s ", player1->name);
printf("Lives left : %d ", player1->lives);
printf("Gold collected : %d ", player1->gold);
}

void print_room(char room[20][20])
{
int i,j;

for(i=19;i>=0;i--)
{
for(j=0;j<20;j++)
{
printf("%c",room[i][j]);
}
printf(" ");
}
}

void read_player_move(char *playermove)
{
printf(" Your move (l left, r right, t top, d down): ");
scanf("%c",playermove);

if(*playermove==' ')
{
scanf("%c",playermove);
}
}

int main()
{
char room[20][20];
struct player player1;
char playermove;

init_player(&player1);
init_location(room);

print_player_info(&player1);
print_room(room);

read_player_move(&playermove);
while(1)
{
if(playermove=='q')
{
print_player_info(&player1);
print_room(room);
break;
}
else if(playermove=='l' || playermove=='r' || playermove=='t' || playermove=='d')
{
room[player1.ycord][player1.xcord]=' ';

if(playermove=='l')
{
player1.xcord -= 1;
if(player1.xcord<0)
{
player1.xcord=0;
}
}
else if(playermove=='r')
{
player1.xcord += 1;
if(player1.xcord>=20)
{
player1.xcord=19;
}
}
else if(playermove=='t')
{
player1.ycord += 1;
if(player1.ycord>=20)
{
player1.ycord=19;
}
}
else if(playermove=='d')
{
player1.ycord -= 1;
if(player1.ycord<0)
{
player1.ycord=0;
}
}

if(room[player1.ycord][player1.xcord]==' ')
{
room[player1.ycord][player1.xcord]='P';
}
else if(room[player1.ycord][player1.xcord]=='*')
{
player1.gold+=1;
room[player1.ycord][player1.xcord]='P';
}
else if(room[player1.ycord][player1.xcord]=='O')
{
player1.lives-=1;
room[0][0]='P';
player1.xcord=0;
player1.ycord=0;
}
}
else
{
printf("Invalid move. Press l,r,t or d ");
}

print_player_info(&player1);
print_room(room);

read_player_move(&playermove);
}

return 0;
}


This is the required code in C for the program.
Comment for any doubts.
Cheers!

Dr Jack
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote