F
felixnielsen
imagine a hollow cube.
every wall can have one of two states: open or closed. This would take
6 bits to represent.
Now, i allso neew to know whitch way i entered the room, this could be
representet with a integer from 0 to 5 = 3 bits.
As 2^6*6-1 = 383 and an 8 bit integer can hold a max value of 255, we
ofcourse have to do some creative thinking, it is however, not as hard
as it sounds, because the wall/door/direction in witch i entered the
room doesnt need a state, so now we are at 2^5*6-1 = 191 and thats a
great deal lower than 255.
However, with this aproach one problem remains.
The walls/doors no longer have a fixes position in the bit field, fx.
if the direction from witch the room was entered is 5 and therefor
wall/door no. 6 will be represented by bit no. 8 (as the 6th wall/door
doesnt need to be represented), if the direction ID is 4, the 5th
wall/door, will not be represented, it doesnt have to be. If the the
direction ID is 3 the 5th wall/door will be representet by the 7th bit,
ect.
This is all a bit confusing and im not quite sure i have explained it
good enough, however i have written a "class" that in my oppinion
should work, but dont(it compiles fine though, but the result is
wrong). maybe that could give some inspiration.
@code start
class Stack {
private:
std::vector<unsigned char> P_Stack;
unsigned char T_Stack;
public:
Stack() {
T_Stack = 0;
}
~Stack() {}
bool Put_Dir(int a) { // should work
T_Stack = (a >= 0 && a < 6) ? a : T_Stack;
}
void Put_Door(int a) { // should work
if (a >= 0 && a < 6) {
if (a >= (T_Stack & 7)) {
a--;
}
T_Stack |= (1 << a);
}
}
unsigned int Get_Dir() { //doesnt work
return (T_Stack & 7);
}
bool Get_Door(int a) { // seems to work, but missing door = 1
implementation is missing.
if (a >= 0 && a < 6) {
if (a >= (T_Stack & 7)) {
a--;
}
return (T_Stack & (1 << a));
}
}
bool Dead_End() { // probably doesnt work
if (T_Stack & (31 << 3)) {
return 1;
}
else {
return 0;
}
}
void Next() { // should work
P_Stack.push_back(T_Stack);
T_Stack = 0;
}
void Pop() { // should work
P_Stack.pop_back();
T_Stack = P_Stack.back();
}
};
class Grid {};
int main() {
Stack stack;
stack.Put_Dir(0);
stack.Put_Door(1);
stack.Put_Door(2);
stack.Put_Door(3);
stack.Put_Door(4);
stack.Put_Door(5);
std::cin.get();
// when i ask for direction the result should be 0.
// when i ask for door 0 the result should be 1, but this information
isnt stored in the bitfield.
// when i ask for door 1-5 the result in thiscase should allso be 1,
but only because i have set them, otherwise the should be 0;
}
@code end
Regards
Felix Zacariaz Bloutarski
every wall can have one of two states: open or closed. This would take
6 bits to represent.
Now, i allso neew to know whitch way i entered the room, this could be
representet with a integer from 0 to 5 = 3 bits.
As 2^6*6-1 = 383 and an 8 bit integer can hold a max value of 255, we
ofcourse have to do some creative thinking, it is however, not as hard
as it sounds, because the wall/door/direction in witch i entered the
room doesnt need a state, so now we are at 2^5*6-1 = 191 and thats a
great deal lower than 255.
However, with this aproach one problem remains.
The walls/doors no longer have a fixes position in the bit field, fx.
if the direction from witch the room was entered is 5 and therefor
wall/door no. 6 will be represented by bit no. 8 (as the 6th wall/door
doesnt need to be represented), if the direction ID is 4, the 5th
wall/door, will not be represented, it doesnt have to be. If the the
direction ID is 3 the 5th wall/door will be representet by the 7th bit,
ect.
This is all a bit confusing and im not quite sure i have explained it
good enough, however i have written a "class" that in my oppinion
should work, but dont(it compiles fine though, but the result is
wrong). maybe that could give some inspiration.
@code start
class Stack {
private:
std::vector<unsigned char> P_Stack;
unsigned char T_Stack;
public:
Stack() {
T_Stack = 0;
}
~Stack() {}
bool Put_Dir(int a) { // should work
T_Stack = (a >= 0 && a < 6) ? a : T_Stack;
}
void Put_Door(int a) { // should work
if (a >= 0 && a < 6) {
if (a >= (T_Stack & 7)) {
a--;
}
T_Stack |= (1 << a);
}
}
unsigned int Get_Dir() { //doesnt work
return (T_Stack & 7);
}
bool Get_Door(int a) { // seems to work, but missing door = 1
implementation is missing.
if (a >= 0 && a < 6) {
if (a >= (T_Stack & 7)) {
a--;
}
return (T_Stack & (1 << a));
}
}
bool Dead_End() { // probably doesnt work
if (T_Stack & (31 << 3)) {
return 1;
}
else {
return 0;
}
}
void Next() { // should work
P_Stack.push_back(T_Stack);
T_Stack = 0;
}
void Pop() { // should work
P_Stack.pop_back();
T_Stack = P_Stack.back();
}
};
class Grid {};
int main() {
Stack stack;
stack.Put_Dir(0);
stack.Put_Door(1);
stack.Put_Door(2);
stack.Put_Door(3);
stack.Put_Door(4);
stack.Put_Door(5);
std::cin.get();
// when i ask for direction the result should be 0.
// when i ask for door 0 the result should be 1, but this information
isnt stored in the bitfield.
// when i ask for door 1-5 the result in thiscase should allso be 1,
but only because i have set them, otherwise the should be 0;
}
@code end
Regards
Felix Zacariaz Bloutarski