I am making a little text-only game. I am storing the 'exits' and 'items' as bitset
#pragma once
#include <string>
#include <bitset>
class Room
{
public:
Room();
Room(int isRoom, std::string title, std::string description, std::bitset<32> items, std::bitset<4> exits);
Room(const Room& room);
Room& operator= (const Room& room);
bool isRoom;
std::string title;
std::string description;
std::bitset<32> items;
std::bitset<4> exits;
};
I have successfully written this by myself for the exits (so its probably a very weird way of doing things). But I am wondering if this will work ok for larger bitsets. Basically the confusion began because I store my levels as a .txt
file:
1,Main Entrance,Entrance description goes here. Blah Blah Blah,0,8
0, , ,0,0
0, , ,0,0
0, , ,0,0
1,Corridor0-1,A dark corridor,1,12
0, , ,0,0
0, , ,0,0
0, , ,0,0
1,Corridor0-2,A dark corridor,0,12
0, , ,0,0
0, , ,0,0
0, , ,0,0
1,Corridor0-3,A dark corridor,0,5
1,Corridor1-3,A dark corridor,0,3
1,Corridor2-3,A dark corridor,0,3
1,Corridor3-3,A dark corridor,1,2
;
The last two numbers are the items, exits.
I check each bit to see which exits are open etc. When I create a dungeon it looks like this, this is only part of the function, the part which iterates through the string of level data and converts it into an int32 which is then passed as an argument to the Room
when it is instantiated. As you will see I had to iterate backwards and then created this multiplier variable to multiply the second number in the string.
Here is the part I am not sure about...
unsigned int exits = 0;
unsigned int multiplier = 0;
for (int i = roomDatas[4].length()-1; i >= 0; i--)
{
exits += (roomDatas[4][i] - '0') * ((multiplier == 0) ? 1 : multiplier);
multiplier *= 10;
}
As I said earlier, this does work as far as I can tell. But I couldn't help thinking there might be an easier way to do it.
NB. I am aware "data" is already plural, and "datas" is not a word. But I have a weird habit of always putting s at the end of arrays. My bad :D