Roguelike AoC – part 8

An eight lesson of roguelike game tutorial also builds up on the previous one. Our door-connecting algorithm does not work as expected.

So what can I actually do here?

To answer the above question – nothing. Solution presented in this lesson I’ve just copied 1:1 to my code and that’s all. I saw no additional value added in trying to make it better in any way, as I’ve known, that lesson 11 will deal with remaining problems (at least that’s what its description says). So here is the code of fixed connectLocations method and that’s all folks.

Code

int connectLocations(const Location loc1, const Location loc2)
{
    Position doorTwo = loc2.position;
    Position temp;
    Position previous;

    int count = 0;

    temp.x = loc1.position.x;
    temp.y = loc1.position.y;

    previous = temp;

    while (1)
    {
       // step left
       if ((abs((temp.x - 1) - doorTwo.x) < abs(temp.x - doorTwo.x)) && (mvinch(temp.y, temp.x - 1) == *EMPTY_SIGN)) {
           previous.x = temp.x;
           temp.x = temp.x - 1;
       // step right
       } else if ((abs((temp.x + 1) - doorTwo.x) < abs(temp.x - doorTwo.x)) && (mvinch(temp.y, temp.x + 1) == *EMPTY_SIGN)) {
           previous.x = temp.x;
           temp.x = temp.x + 1;
       // step down
       } else if ((abs((temp.y + 1) - doorTwo.y) < abs(temp.y - doorTwo.y)) && (mvinch(temp.y + 1, temp.x) == *EMPTY_SIGN)) {
           previous.y = temp.y;
           temp.y = temp.y + 1;
       // step up
       } else if ((abs((temp.y - 1) - doorTwo.y) < abs(temp.y - doorTwo.y)) && (mvinch(temp.y - 1, temp.x) == *EMPTY_SIGN)) {
           previous.y = temp.y;
           temp.y = temp.y - 1;
       } else {
           if (count == 0) {
               temp = previous;
               count++;
               continue;
           } else {
               return 0;
           }
       }

       mvprintw(temp.y, temp.x, FLOOR_SIGN);
       getch();
    }

    return 1;
}

You Might Also Like

Leave a Reply

Back to top