d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > How To Code Ascii In C Programming.
Add Reply New Topic New Poll
Member
Posts: 3
Joined: Aug 21 2015
Gold: 0.00
Aug 21 2015 04:41pm
Hi, I need to create a map that has 30 room that contain treasures and troll. If I define :
0 gold_bar
1 silver_bar
2 diamond
3 copper_ring
4 jumpy_troll
5 air
6 angry_troll
7 plutonium_troll

And the line is : 9 23 @Z then room at 9,23 (character Z with binary: 01011010) has items 1, 3, 4, 6. silver_bar, copper_ring, jumpy_troll, angry_troll.
Can someone clarify why Z has item 1,3,4,6. Thank you. I think it may have to do with mask, false and true. I have no clue for this problem.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Aug 21 2015 05:44pm
what does this have to do with ascii?

01011010 refer to yes/no values, in order:
gold_bar , silver_bar, diamond, copper_ring, jumpy_troll, air, angry_troll, plutonium_troll

so:
gold_bar = no, silver_bar = yes, diamond = no, copper_ring = yes, jumpy_troll = yes, air = no, angry_troll = yes, plutonium_troll = no
Member
Posts: 3
Joined: Aug 21 2015
Gold: 0.00
Aug 21 2015 10:52pm
I had figured out that
set gold_bar as mask0 = 0x01
silver_bar as mask1 =0x02
diamond as mask2 =0x04
.......
Z= 01011010

then use if statement, so
if (mask0 & Z == 0) exit (1);
else (printf "gold_bar");
but after I compile it and run; it doesnt work.
Member
Posts: 71,481
Joined: Jun 15 2007
Gold: 11.11
Aug 22 2015 04:40pm
From your friendly neighborhood duck.

I would create a struct and then iterate the binary value bit by bit and use the comparison mask 0b00000001 to check if the value is enabled.

Code

typedef struct {
int goldBar;
int silverBar;
int diamond;
int copperRing;
int jumpyTroll;
int air;
int angryTroll;
int plutoniumTroll;
} Map;


This struct will hold your map details. Next may be a bit over your head but I would create an array of ints and then iterate your binary with the mask and then assign the pointer to the struct. This will overlay the values of the of the structure on top of the array so that you may access them by name.

Code

int levelArray[8] = {0, 0, 0, 0, 0, 0, 0, 0};
int mapValues = 'Z';
int i = 0;

for(i = 0; i < 8; i++) {
if(((mapValues >> i) & 0x01) == 1) {
levelArray[i] = 1;
}
}


Now that levelArray holds our binary digits we may overlay the address of the array onto the struct so we may access it

Code

Map *levelMap = (Map*)&levelArray;


Now you may access your data via a tidy structure. Just beware that if levelArray gets modified your structure will also be modified. If this is not desired you must make a copy of levelArray in another area of memory and assign that pointer to the structure.

Here is the full code

Code

#include <stdio.h>
#include <stdlib.h>

typedef struct {
int goldBar;
int silverBar;
int diamond;
int copperRing;
int jumpyTroll;
int air;
int angryTroll;
int plutoniumTroll;
} Map;

int main()
{
int levelArray[8] = {0, 0, 0, 0, 0, 0, 0, 0};
int mapValues = 'Z';
int i = 0;

for(i = 0; i < 8; i++) {
if(((mapValues >> i) & 0x01) == 1) {
levelArray[i] = 1;
}
}

Map *levelMap = (Map*)&levelArray;

if(levelMap->silverBar == 1) {
printf("Silver bars are in this map\n");
}

if(levelMap->goldBar == 1) {
} else {
printf("There are no gold bars in this map\n");
}

return 0;
}


Alternatively you can create a mask for each one and use a string of if statements to check, but that's lame.
Member
Posts: 3
Joined: Aug 21 2015
Gold: 0.00
Aug 22 2015 08:26pm
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.
Member
Posts: 71,481
Joined: Jun 15 2007
Gold: 11.11
Aug 22 2015 10:51pm
A stupid duck with abs helped me with this

Code
#include <stdio.h>
#include <stdlib.h>

#define HEIGHT 3
#define WIDTH 3

typedef struct {
int goldBar;
int silverBar;
int diamond;
int copperRing;
int jumpyTroll;
int air;
int angryTroll;
int plutoniumTroll;
} Map;

int main() {
Map map[WIDTH*HEIGHT];
int roomArray[8] = {0, 0, 0, 0, 0, 0, 0, 0};
int roomValue = '@';
int character = 0;
int index = 0;
int i = 0;
int ii = 0;
int iii = 0;

//Fill the map
for(i = 0; i < WIDTH*HEIGHT; i++) {
roomValue = fgetc(stdin);
fflush(stdin);

//Convert the character into a binary array
for(ii = 0; ii < 8; ii++) {
if(((roomValue >> ii) & 0x01) == 1) {
roomArray[ii] = 1;
}
}

//Create the room from the bit array
Map *room = (Map*)&roomArray;

//Assign the room to the current map possition
map[i] = *room;

//Clear the room array, memset can also be used here
for(iii = 0; iii < 8; iii++) {
roomArray[iii] = 0;
}
}

//Display a specific rooms attributes
//Index is calculated as yPos * WIDTH + (xPos
index = 1 * WIDTH + 1;

//Print the data of the specific room
printf("Debugging contents of room 2, 2\n");
printf("goldBar = %d\n", map[index].goldBar);
printf("silverBar = %d\n", map[index].silverBar);
printf("diamond = %d\n", map[index].diamond);
printf("copperRing = %d\n", map[index].copperRing);
printf("jumpyTroll = %d\n", map[index].jumpyTroll);
printf("air = %d\n", map[index].air);
printf("angryTroll = %d\n", map[index].angryTroll);
printf("plutoniumTroll = %d\n\n", map[index].plutoniumTroll);

//Convert a specific rooms attributes into a printable character
//If any of the attributes are true shift 0b00000001 into the
//proper item position
if(map[index].goldBar == 1) { character += (0x01 << 0); }
if(map[index].silverBar == 1) { character += (0x01 << 1); }
if(map[index].diamond == 1) { character += (0x01 << 2); }
if(map[index].copperRing == 1) { character += (0x01 << 3); }
if(map[index].jumpyTroll == 1) { character += (0x01 << 4); }
if(map[index].air == 1) { character += (0x01 << 5); }
if(map[index].angryTroll == 1) { character += (0x01 << 6); }
if(map[index].plutoniumTroll == 1) { character += (0x01 << 7); }
printf("Map 2,2 symbol: %c\n\n", character);


//Printing the entire map
//Iterate the height of the map
for(i = 0; i < HEIGHT; i++) {
//For each height we have to iterate the entire width
for(ii = 0; ii < WIDTH; ii++) {
//Combine previous methods above
character = 0;
index = i * WIDTH + ii;

if(map[index].goldBar == 1) { character += (0x01 << 0); }
if(map[index].silverBar == 1) { character += (0x01 << 1); }
if(map[index].diamond == 1) { character += (0x01 << 2); }
if(map[index].copperRing == 1) { character += (0x01 << 3); }
if(map[index].jumpyTroll == 1) { character += (0x01 << 4); }
if(map[index].air == 1) { character += (0x01 << 5); }
if(map[index].angryTroll == 1) { character += (0x01 << 6); }
if(map[index].plutoniumTroll == 1) { character += (0x01 << 7); }

//Simple character replacement so that we can see the map
if(character == ' ') {
printf(".");
} else {
printf("%c", character);
}
}
//After we print an entire row move down a line to start the next height level
printf("\n");
}

return 0;
}
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll