Objectives Assignment #8 Introduction to C Programming – COP 3223 MUST USE FUNCT
ID: 3822679 • Letter: O
Question
Objectives
Assignment #8 Introduction to C Programming – COP 3223
MUST USE FUNCTIONS
1. To learn how to design and implement functions for program development 2. To reinforce how to use pass by value and pass by reference variables
3. To learn how to use structures to store information and solve problems
Introduction: Ninja Academy
Ninjas are awesome! Your friend has not stopped talking about how cool ninjas and how they would like to become a ninja. To amuse your friend, you have decided to create a series of programs about ninjas.
Problem: Ninja Quest (ninjaquest.c)
Congratulations! You have graduated from the Ninja Academy with the highest honors! You have been placed in charge of a team of six ninjas. Your task is to help your school expand into a new area. This area is contested by other ninjas from rival schools. You will need to defeat them in competitions and shows of ability to claim the area.
Program Details
You will have a team of six ninjas: each ninja has a performance score and an ability score. When their performance score drops to zero, they will no longer be able to compete and show off their ninja skills. Their ability score indicates how impressive their skills are to intimidate and outshine the competition.
Your program will simulate several days of competition for an area. An area is split into a 3x3 grid of competition spaces. Throughout the day, you will need to choose which section of the area to send each of your ninja subordinates. You can only send out one at a time, but you can send each ninja to any square.
As each ninja is deployed, resolve any competitions that occur. If your ninja is the only one in the section, you can claim the section! If there are other ninjas, you must compete for that section. Competitions are determine by the performance and ability scores of the ninjas. These scores are randomly generated values from 1 to 30, inclusive.
Consider the following example:
There are 3 opponent ninjas in the upper right corner. Suppose you send one of your ninjas to this section.
1 opponent ninja Not claimed
2 opponent ninja Not claimed
3 opponent ninja Not claimed
0 opponent ninja Not claimed
4 opponent ninja Not claimed
1 opponent ninja Not claimed
1 opponent ninja Not claimed
1 opponent ninja Not claimed
1 opponent ninja Not claimed
Your ninja: Performance: 13 Ability: 25
Opponent ninjas:
Your ninja competes with the first opponent. Your ninja goes first and his performance impacts the opponent’s ability to respond:
Round 1:
Your ninja: Performance: 13 Ability: 25 Opponent: Performance: 7 Ability: 2
Note how the opponent’s ability score was reduced from 15 to 2 by your ninja’s performance score of 13. Correspondingly, the opponent will show off his ninja skills and impact your ability to follow up:
Round 2:
Your ninja: Performance: 13 Ability: 18 Opponent: Performance: 7 Ability: 2
On your opponent’s turn, your ninja’s ability score is reduced from 25 to 18 by your opponent’s performance score of 7. Next, it’s your turn again!
In the third round your ninja reduces the opponent’s ability below zero. A ninja with an ability score of less than or equal to zero can no longer compete. Only ninja’s with positive ability scores can compete. Then, your ninja will go up against the second defender in the section:
Round 1:
Your ninja: Performance: 13 Ability: 18 Opponent: Performance: 17 Ability: 7
You ninja goes first! The second opponent starts with an ability of 20, but since you go first, you reduce this to 7 with your performance score.
Round 2:
Your ninja: Performance: 13 Ability: 1 Opponent: Performance: 17 Ability: 7
This opponent has a high performance score and reduces your ninja’s ability to 1. But your ninja beats them in round three by reducing their ability from 7 to -6. There is one more ninja to defeat:
Round 1:
Your ninja: Performance: 13 Ability: 1 Opponent: Performance: 12 Ability: 23
Performance: 7 Ability: 15
Performance: 17 Ability: 20
Performance: 12 Ability: 30
Your ninja is defeated in round 2!
Round 2:
Your ninja: Performance: 13 Ability: -11 Opponent: Performance: 12 Ability: 23
You will have to retreat for now, but since you have defeated two ninjas the number of opponents will go down:
1 opponent ninja Not claimed
2 opponent ninja Not claimed
1 opponent ninja Not claimed
0 opponent ninja Not claimed
4 opponent ninja Not claimed
1 opponent ninja Not claimed
1 opponent ninja Not claimed
1 opponent ninja Not claimed
1 opponent ninja Not claimed
You can then send a second ninja later in the day:
Your ninja: Performance: 27 Ability: 20
There is now only one opponent to defeat:
Opponent: Performance: 15 Ability: 25
Note that these numbers may be different later in the day, because they are randomly generated when you enter the section.
Round 1:
Your ninja: Performance: 27 Ability: 20 Opponent: Performance: 15 Ability: -2
Since all of the opponent ninjas have been defeated you can claim this space:
1 opponent ninja Not claimed
2 opponent ninja Not claimed
0 opponent ninja CLAIMED
0 opponent ninja Not claimed
4 opponent ninja Not claimed
1 opponent ninja Not claimed
1 opponent ninja Not claimed
1 opponent ninja Not claimed
1 opponent ninja Not claimed
Because you are a superior commander, when you claim a section it stays yours. If you send a ninja to a location with zero opponents, you can claim it automatically. Your program should run until either all the sections are yours or until all of your ninjas have run out of performance points.
At the end of each day, your ninjas with a positive ability score can restore their ability score to its original value.
Implementation Restrictions
You must use the following structures to store information:
It is not sufficient to simply have the structures in your program, you must use them store information and process operations for the alliance. You may use type definition if you prefer.
Though function prototypes will not be provided, it is expected that you follow good programming design and create several functions with well-specified tasks related to the solution of this problem. Make sure to pay very careful attention to parameter passing.
Hints
Create a 3x3 array for the map. The value playerOwns represents a flag to indicate whether or not that section has been claimed for the player. These should be initialized to zero or false. The number of opponents for the map will come from an input file. See below for specifications.
Create a 1x6 array for your ninjas. Remember to randomly generate the values for each ninja as random integers between 1 and 30. Keep an original copy and a separate copy that can be updated throughout the day. Give the ninjas identification numbers like 1-6 to track their scores.
Input Specification
The input file will contain 9 integers representing the number of opponent ninjas in each square. Remember to prompt the user for the name of the input file and open the file that they specify.
The user will input directions in a 1-based, left to right fashion:
111213 212223 313233
Output Specification
Start each day with “Good Morning!”
Then, for each ninja with a positive ability score, show the user the currently claimed sections of the map.
---
---
--- (example initial map)
-CC
--C
---
(player has claimed sections 1 2, 1 3, and 2 3)
Then prompt the user with their ID number for where to send them: Where would you like to send ninja <ID>?
Immediately resolve this by sending the ninja to the corresponding section on the map. Tell the user how many ninjas their ninja is facing:
There are X opponent ninjas in this section!
Print out the values for both the player and the opponent so it can be tracked:
Your ninja: Performance: A Ability: C
Opponent: Performance: B Ability: D
And update these values as the two ninjas compete, until one ability is reduced to zero or below. When the player’s ninja defeats an opponent, print:
Your ninja defeated an opponent! When the player’s ninja is defeated, print:
Ninja <ID> was defeated!
If the user defeats all opposing ninjas or if the user sends a ninja to an unoccupied space that has not been claimed, print:
You claim this section!
If the user sends a ninja to an unoccupied space that has already been claimed, print:
You already own this section!
If the player claims all of the sections, print:
You have claimed all of the sections. You Win!
If all of the players ninjas are defeated, print:
Your ninjas are all exhausted. Try again next time.
1 opponent ninja Not claimed
2 opponent ninja Not claimed
3 opponent ninja Not claimed
0 opponent ninja Not claimed
4 opponent ninja Not claimed
1 opponent ninja Not claimed
1 opponent ninja Not claimed
1 opponent ninja Not claimed
1 opponent ninja Not claimed
Explanation / Answer
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
typedef struct {
int ID;
int performance;
int ability;
}ninja;
typedef struct {
int playerOwns;
int numOpponents;
}map;
/* function to load a map from file. Return 1 if successfully loaded , 0 otherwise.*/
int load_map(char *filename, map sections[3][3])
{
FILE *fp=fopen(filename,"r");
int i,j;
if(fp==NULL)
{
printf(" Could not load from file %s",filename);
return 0; //failed
}
for(i=0; i<3; i++)
{
for(j=0;j<3;j++)
{
fscanf(fp,"%d",§ions[i][j].numOpponents);
sections[i][j].playerOwns=0; //initialally not owned by player
}
}
fclose(fp);
return 1;//success
}
/*function to display the sections in map*/
void display(map sections[3][3])
{
int i, j;
char ch;
for(i=0; i<3; i++)
{
printf(" ");
for(j=0; j<3; j++)
{
ch = sections[i][j].playerOwns == 0? '-':'C';
printf(" %c",ch);
}
}
printf(" ");
}
//arrays are always passed by reference. changes made to array are seen in calling function
void initialize_ninjas(ninja list[], int count)
{
int i;
for(i=0;i<count; i++)
{
list[i].ID = i+1;
list[i].performance = rand() % 30 + 1; //assign a random number between 1 and 30 inclusive
list[i].ability = rand() % 30 + 1; //assign a random number between 1 and 30 inclusive
}
}
//function to start the fight between player and opponent ninjas. The values are passed by reference
//So any changes made in this function are visible in calling function. The function returns 1 if the
//player ninja defeated opponent and 0 if playr is defeated by opponent.
int fight(ninja *player, ninja *opponent)
{
int turn=1;
/*play as long are both's ability is +ve value. Stop when one of their ability reaches value <=0 */
while(player->ability> 0 && opponent->ability > 0)
{
/*display there performance and abilities*/
printf("Your Ninja Opponent: ");
printf("Performance: %2d Performance: %2d ",player->performance,opponent->performance);
printf("Ability: %2d Ability: %2d ",player->ability, opponent->ability);
if(turn == 1) //player is 1st
{
opponent->ability -= player->performance;
}
else
{
player->ability -= opponent->performance;
}
turn = 1 - turn; //if turn is 1 then it changes to 0 and vice versa
}
if(player->ability > 0) //player still has +ve ability
{
printf("Your ninja defeated an opponent! ");
return 1; //player wins
}
else
{
printf("Ninja %d was defeated! ",player->ID);
return 0;//player ninja lost
}
}
/* function to make a player ninja with opponent ninja. Both ninjas are passed by reference. Changes made in this function
are available in calling function. Updates abililties of ninjas. Also if player wins , section.playerowns is set to 1.
Returns 1 if player defeated all opponents in section otherwise returns 0 */
int send(ninja *player, map *section)
{
ninja opponents[10]; /*max 10 opponents allowed*/
int count=section->numOpponents, defeated=0;
initialize_ninjas(opponents, count);
printf("There are %d opponent ninjas in this section! ",count);
/* as long as player has +ve ability and there are more opponetns to be defeated */
while(player->ability > 0 && defeated < count)
{
fight(player, &opponents[defeated]);
defeated++;
}
if(player->ability > 0 && defeated == count)
{
section->playerOwns = 1;
printf("You claim this section! ");
return 1;
}
else
{
return 0;
}
}
int main()
{
ninja ninjas_list_original[6], ninjas[6];
map sections[3][3];
/*original list is not touched, ninjas is the list is used to fight and update. When the list needs to be revived next day,
values are copied from original list*/
char filename[50];
int active=6, claimed=0, played=0, next, row, col, count;
printf(" What is the name of the file? ");
scanf("%s",filename);
load_map(filename,sections);
srand(time(NULL));
initialize_ninjas(ninjas_list_original, 6);
for(int i=0;i<6; i++)
ninjas[i]=ninjas_list_original[i];
while(claimed != 9 && active > 0)
{
printf("Good Morning! ");
/*Revive each of the player ninjas who have +ve ability to their original values of performance and ability*/
for(int i=0;i<6; i++)
{
if(ninjas[i].ability > 0)
ninjas[i]=ninjas_list_original[i];
}
count = active;
next =0; //index of nex ninja to go for fight
while(count > 0 && claimed!=9) /* allow each of active ninjas to battle once if there are still sections to claim. */
{
display(sections);
while(ninjas[next].ability <=0 ) /*search for ninja with +ve ability*/
next++;
while(1)
{
printf("Where would you like to send ninja %d ? ",ninjas[next].ID);
scanf("%d %d",&row,&col);
if(row<1 || row>3 || col<1 || col>3)
{
printf("row and col should be in the range 1-3 ");
continue;
}
else
break;
}
if(sections[row-1][col-1].playerOwns == 1)
{
printf("You already own this section! ");
}
else
{
if(send( &ninjas[next], §ions[row-1][col-1]) == 0)
active--;
else
claimed++;
count--;
next++;
}
}
}
if(claimed==9)
printf("You have claimed all of the sections. You Win! ");
else
printf("Your ninjas are all exhausted. Try again next time. ");
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.