Thank you carteblanche and Princess for reply. I have to use bitmask to show the content of the room.
This is how my room look like after input. I compare those char ^ " ) @ 9 7 to mask and counting how many item are total.
..............................
.............................^
............................".
...........................)..
..........................(@
.........................-.@
........................9..
.......................7..@
this my code for map:
Code
#include <stdlib.h>
#include <stdio.h>
int main()
{
int row, col;
int x_coord, y_coord; // input value
char simple[100];
char fourth[100] ;
char myMap[30][30];
// print map with dot
for(row=0;row<30;row++){
for( col=0; col<30; col++){
myMap[row][col] = '.';
}
}
while(scanf("%d %d %s", &x_coord, &y_coord, simple ) !=-1){ // repeat the loop untill the end of file
if (x_coord ==0|| y_coord ==0) exit(0); // error and exit if x_coord and y_coord = 0
if (x_coord <0 || x_coord>30 || y_coord <0|| y_coord >30){
printf("Error :input need to be >0 and <30 \n"); exit(1); //Error if x_coord and y_coord need to be >0 and <30
}
myMap [y_coord-1][x_coord-1] =simple[1];
fgets(fourth, 100, stdin);
}
// print map
for(row=0;row<30;row++){
for(col=0;col<30; col++){
printf("%c",myMap[row][col]);
}
printf("\n");
}
return 0;
}
here is another code using bit comparison.
Code
#include <stdlib.h>
#include <stdio.h>
int main ()
{
int mask0 = 0x01; //0000 0001 gold_bar
int mask1 = 0x02; //0000 0010 silver_bar
int mask2 = 0x04; //0000 0100 diamond
int mask3 = 0x08; //0000 1000 copper_ring
int mask4 = 0x10; //0001 0000 jumpy_troll
int mask5 = 0x20; //0010 0000 air
int mask6 = 0x40; //0100 0000 angry_troll
int mask7 = 0x80; //1000 0000 pultonium_troll
char curchar;
int a;
int gold_bar = 0;
int silver_bar = 0;
int diamond = 0;
int copper_ring = 0;
int jumpy_troll = 0;
int air = 0;
int angry_troll =0;
int pultonium_troll =0;
int i;
printf("enter the char:\n",curchar);
while (curchar != '.')
{
scanf("%c",&curchar);
if ((curchar & mask0) != 0 ) {gold_bar++;} // count number of gold_bar
if ((curchar & mask1) != 0 ){ silver_bar++;} // count number of silver_bar
if ((curchar & mask2) != 0 ){ diamond++;} //count number of diamond
if ((curchar & mask3) != 0 ){ copper_ring++;} // count number of copper_ring
if ((curchar & mask4) != 0 ){ jumpy_troll++;} // count number of jumpy_troll
if ((curchar & mask5) != 0 ){ air++;} // count number of air
if ((curchar & mask6) != 0 ){ angry_troll++;} // count numb of angry_troll
if ((curchar & mask7) != 0 ){ pultonium_troll++;} // count number of pultonium_troll
}
printf("%d \t goldbar\n", gold_bar++);
printf("%d \t silverbar\n",silver_bar++);
printf("%d \t diamond\n", diamond++);
printf("%d \t copper_ring\n", copper_ring++);
printf("%d \t jumpy_troll\n", jumpy_troll++);
printf("%d \t air\n", air++);
printf("%d \t angry_troll\n", angry_troll++);
printf("%d \t pultonium_troll\n", pultonium_troll++);
return 0;
}
I dont know how to make the second one as a function like void functionname() and combine them together. Array simple[1] contain the character, but it is a pointer, I dont know how to relate simple[1] to currchar from my second code. Can someone help please.