Objectives 1. To learn how to use 2 dimensional arrays to store and retrieve dat
ID: 3679366 • Letter: O
Question
Objectives
1. To learn how to use 2 dimensional arrays to store and retrieve data to help solving 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: Stolen! (ninjamission.c)
The Ninja Academy has been raided by a rival ninja school! Some of the Academy’s most valuable relics have been stolen:
The founder’s KATANA
A leather scroll CASE
An ornate SCROLL
A velvet POUCH
A JADE statue
An ancient MASK
The Ninja Academy is nominating its best students to go on a mission to retrieve these items. Each you will take turns trying to steal back a relic, simulated by a randomly generated number from 0 to 6. Six of those numbers (0-5) correspond to the numbers above, while 6 corresponds to getting caught. Getting caught by the rival school’s guards will force you to give back a relic you’ve already retrieved.
If you get a number that corresponds to an item you already have, nothing happens. If you get a SCROLL (item 2), you may only pick up the item if you already have a CASE (item 1). If you get a JADE (item 4) or a MASK (item 5), you may only pick up the item if you already have a POUCH (item 3) to put them in. This is because you cannot carry the scroll with a scroll case and you cannot carry the jade stature or the mask with a velvet pouch to keep them from getting damaged.
If you land on “You’ve been caught!”, then you must choose which relic to discard. If you have no items to begin with, then you don’t lose anything. If you have a SCROLL (2), then you cannot discard the CASE (1) and if you have a JADE (4) or a MASK (5), then you cannot discard the POUCH (3).
The first person to retrieve all six items will be a hero of the Academy!
Program Specification
You must use a 2D array to solve the problem.
There will be no more than 6 ninja trying to complete the mission.
Sample Data Items
You may wish to use these data structures to help you solve the problem. Type them into Code::Blocks (do not copy/paste).
int cont = 1;
int ninjas[6][6];
char ITEMS[6][20] = {"KATANA", "CASE", "SCROLL", "POUCH", "JADE", "MASK"};
The values in “ITEMS” are strings and may be printed with %s. For example, to print “CASE” we could use printf(“%s”, ITEMS[1]);
Program Format
Beginning the program
Start by asking how many ninjas will be sent on the mission.
Beginning of Each Turn
Each turn should begin with a prompt with the following format:
Ninja X, it is your turn. Enter 0 to look for an item.
where X is the number given to the current ninja, starting at 1. (Note: Internally, the player numbers will start at 0, so when you print this out, just add 1 to the variable storing the player number.)
Finding an Item
After the random number is generated, check what it corresponds to. If a value other than 6 (“You’ve been caught!”) is generated, a statement of the following form should be printed:
Ninja X, you found a S.
where S is the name of the item the number matches.
If the player does not yet have the item and is allowed to obtain it, follow this with a statement of the form:
You now have a S!
If the player is not allowed to obtain the item because they don’t yet have another necessary item, print out a statement of the form:
Sorry, you can’t obtain a S.
If the player lands on a square for a piece they already have, then print the following:
You already have that item.
Landing on “You’ve been caught!”
If the player gets this number they must choose a piece to lose. First print the warning:
You’ve been caught!
If the player has no pieces to lose, then print the following:
There is no equipment for you to lose. Lucky you, sort of.
If they have any items, prompt them with the question:
Which piece would you like to lose?
Follow this with a numbered list of items that are valid for the player to discard. (Namely, this list will NOT contain the case if the rider has a scroll and it will not contain the pouch if the rider has a mask or a jade) The numbering of the list should correspond to the numbering of each item listed on page 1. Thus, the numbering won’t necessarily be sequential. For example, if the possible pieces a player can lose are the katana, case, scroll, and pouch, the print out would appear as follows:
0. KATANA
2. SCROLL
3. POUCH
You may assume the user will select a valid item. Then, print out a message of the following format:
You have selected the S to discard.
where S is the item to discard.
Final Printout for Each Turn
At the end of each person’s turn, print out a complete list of their items. The first line will be:
Ninja X, here is what you currently have:
Follow this with a blank line and then a numbered list (as previously described), with each item the player currently has.
End of the Game
The game ends when one person has all the items. If you are using the sample data structure cont, then you can use this as a flag for continuing the game or not. The default is 1 for continuing the game. When a person has all the items, change the flag to 0.
At the end of the game, simply print a message with the following format:
Congratulations Ninja X, you are the first to return with all of the stolen items!
Output Sample
Explanation / Answer
#include <stdio.h>
int main(){
int n,i=0;
printf("Enter the number of ninjas that will be sent on mission? ");
scanf("%d",&n); //n is number of ninjas given as input
int ninjas[6][6]={0}; //2-D array to store if ith ninja have jth item. Initialized to zero means initially it contains no items
char ITEMS[6][20] = {"KATANA", "CASE", "SCROLL", "POUCH", "JADE", "MASK"};
int game=1; //To end the game, value of game will be changed to zero
while(game){
for(i=0;i<n;++i){ //iterate through all the n ninjas
printf("Ninja %d, it is your turn. Enter 0 to look for an item. ",i+1);
int temp;
scanf("%d",&temp); //Input zero for each turn
printf(" ");
if(temp==0){ //if input given is zero
srand(time(NULL));
int random = ( rand() % 7 ); //generates random number between 0 and 6
if(random==6){ //If you are cought
printf("You’ve been caught! ");
int j,flag=0;
for(j=0;j<6;++j){ //to check how many items are assigned
if(ninjas[i][j]==1){
flag=1;
break;
}
}
if(flag==0){ //if no item is assighned to ith ninja
printf("There is no equipment for you to lose. Lucky you, sort of. ");
}
else{
printf("Which piece would you like to lose? ");
int pick,j;
for(j=0;j<6;++j){ //To check how many items are available for discarding
if(ninjas[i][j]==1){
if(j==1){
if(ninjas[i][2]==0)
printf("%d. %s ",j,ITEMS[j]);
}
else if(j==3){
if(ninjas[i][4]==0 && ninjas[i][5]==0)
printf("%d. %s ",j,ITEMS[j]);
}
else{
printf("%d. %s ",j,ITEMS[j]);
}
}
}
scanf("%d",&pick); //enter the number to want to discard the item
printf("You have selected the %s to discard. ",ITEMS[pick]);
ninjas[i][pick]=0;
}
}
else{
printf("Ninja %d, you found a %s ",i+1,ITEMS[random]);
if(ninjas[i][random]==0){ //If this item is not picked earlier by ith ninja
if(random==0 || random==1 || random==3){ //These items are directly allowed to pick
printf("You now have a %s! ",ITEMS[random]);
ninjas[i][random]=1;
}
else{ //Checking if conditions are met to pick the items
if( (random==2 && ninjas[i][1]==1) || ((random==4 || random==5) && ninjas[i][3]==1) ){
printf("You now have a %s! ",ITEMS[random]);
ninjas[i][random]=1;
}
else{
printf("Sorry, you can’t obtain a %s ",ITEMS[random]);
}
}
}
else{
printf("You already have that item ");
}
}
printf(" Ninja %d, here is what you currently have: ",i+1);
int j,cnt=0;
for(j=0;j<6;++j){ //Printng all the currently available items for ith ninja
if(ninjas[i][j]==1){
cnt++;
printf("%s ",ITEMS[j]);
}
}
if(cnt==6){ //If all the 6 items are picked by ith ninja, end the game
game=0;
break;
}
}
else{
printf("Please enter zero for next turn ");
}
}
}
printf("Congratulations Ninja %d, you are the first to return with all of the stolen items! ",i+1);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.