0

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

I_Keep_Trying
  • 395
  • 3
  • 17
  • 1
    Define "better"? Faster? Easier to debug? Less code? – AndyG Aug 20 '21 at 15:15
  • What's the point of `multiplier += 10`? You never use the actual value of `multiplier`, you just check that it is not zero – Pranav Hosangadi Aug 20 '21 at 15:15
  • 2
    C++ have long had the [`std::strtol`](https://en.cppreference.com/w/cpp/string/byte/strtol) function, and since the C++11 standard [`std::stoi`](https://en.cppreference.com/w/cpp/string/basic_string/stol). – Some programmer dude Aug 20 '21 at 15:15
  • sorry that was a mistake – I_Keep_Trying Aug 20 '21 at 15:15
  • why not just pass `roomDatas[4]` (which is spelled wrong btw) to `strtol` ? – Ben Voigt Aug 20 '21 at 15:15
  • By better I think i mean less code/more readable. Or am I doing it close to the correct way already? – I_Keep_Trying Aug 20 '21 at 15:16
  • Ah Pranav , this looks like it might be the answer yes – I_Keep_Trying Aug 20 '21 at 15:17
  • Thanks all. I will read up about it – I_Keep_Trying Aug 20 '21 at 15:18
  • @PranavHosangadi The value is used, as it's "returned" from the "else" branch of the ternary expression. However, `multiplier += 10` is wrong. – Some programmer dude Aug 20 '21 at 15:18
  • @DaveGold: there are all kinds of ways to result in less code that do not necessarily result in more readability. For example, you could serialize the integer directly to a binary file, and then loading it would be as simple as parsing the int back from a binary stream. – AndyG Aug 20 '21 at 15:19
  • 3
    If you don't want to use a library function, the most readable would be: `unsigned exits = 0u; for( char digit : roomDatas[4] ) { exits *= 10; exits += (digit - '0'); }` – Ben Voigt Aug 20 '21 at 15:21
  • thanks Ben, I'll give that a try. Also going to try strtol – I_Keep_Trying Aug 20 '21 at 15:22
  • @Someprogrammerdude When I added the comment, the code was `((multiplier == 0) ? 1 : 10)` – Pranav Hosangadi Aug 20 '21 at 15:47
  • @DaveGold Please don't "fix" the code in the question. That makes it no longer have the problem that you ask about, making the question moot and pretty much useless. If comments contains the solution, then please ask the commenter to post an answer instead. Or post an answer yourself. – Some programmer dude Aug 20 '21 at 16:35
  • I fixed it at the same time you wrote the comment – I_Keep_Trying Aug 20 '21 at 16:38
  • Thought I would give a final update to complete this. My code (above) did not work fully, I am not sure exactly where I went wrong but moving around the game map I got into problems with incorrect exits. Anyway, thanks to your advice I simplified the whole function down to one line `unsigned int exits = std::stoi(roomDatas[4]);` and it works perfectly. Thanks – I_Keep_Trying Aug 21 '21 at 02:09

0 Answers0