TryingMyBest
TryingMyBest

Reputation: 63

ASCII characters in visual studio look weird

I was coding c in code blocks and wanted to move over to visual studio since it has a tendency to compile programs better, yet for some reason the ascii values that were displaying numbers fine in code blocks eem to be giving me weird results in vs. here's a snippet of what they look like:

(left) bad output, and (right) good output

Since the code is exactly the same on each platform, this leads me to believe there is some setting I overlooked int vs pertaining to ascii which is already set in code blocks. For review, here is my code:

#include <windows.h>

#define WIDTH 18
#define HEIGHT 18
#define BOMBS 50

struct xorshift_state {
    int a;
};

int xorshift(struct xorshift_state *state)
{
    int x = state->a;
    x ^= x << 13;
    x ^= x >> 17;
    x ^= x << 5;
    return state->a = x;
}

void ExpandGrid(int fullGrid[WIDTH][HEIGHT], int knownGrid[WIDTH][HEIGHT], int blankPos[2])
{
    int neighbors[8][2] = { { 0,1 },{ 1,0 },{ 1,1 },
    { 0,-1 },{ -1,0 },
    { -1,-1 },{ -1,1 },{ 1,-1 } };
    int curTile[2];

    knownGrid[blankPos[0]][blankPos[1]] = 1;
    if (fullGrid[blankPos[0]][blankPos[1]] != 0) return;

    for (int blck = 0; blck < 8; ++blck)
    {
        curTile[0] = blankPos[0] + neighbors[blck][0];
        curTile[1] = blankPos[1] + neighbors[blck][1];
        if (curTile[0] > WIDTH - 1 || curTile[1] > HEIGHT - 1 || curTile[0] < 0 || curTile[1] < 0) continue;

        if (fullGrid[curTile[0]][curTile[1]] == 0 && knownGrid[curTile[0]][curTile[1]] == 0)
        {
            knownGrid[curTile[0]][curTile[1]] = 1;
            ExpandGrid(fullGrid, knownGrid, curTile);
        }
        else if (fullGrid[curTile[0]][curTile[1]] > 0) knownGrid[curTile[0]][curTile[1]] = 1;
    }
}

int main(int argc, char *argv[])
{

    COORD characterBufferSize = { WIDTH, HEIGHT };
    COORD characterPosition = { 0, 0 };
    SMALL_RECT consoleWriteArea = { 0, 0, WIDTH - 1, HEIGHT - 1 };
    CHAR_INFO consoleBuffer[WIDTH][HEIGHT];

    HANDLE wHnd = GetStdHandle(-11);
    HANDLE rHnd = GetStdHandle(-10);

    DWORD numEventsRead = 0;
    DWORD numEvents = 0;
    INPUT_RECORD *eventBuffer = { 0 };
    int wait = 1000;

    int startGrid[WIDTH][HEIGHT] = { 0 };
    int knownGrid[WIDTH][HEIGHT] = { 0 };
    int arrowPos[2] = { 0, 0 };
    int bomb[2] = { 0 };
    struct xorshift_state seed = { argc == 2 ? (int)argv[1] : 1 };

    for (int i = 0; i < BOMBS; i++)
    {
        while (startGrid[bomb[0]][bomb[1]] < -1 || bomb[0] <= 0 || bomb[1] <= 0 || bomb[0] >= WIDTH - 1 || bomb[1] >= HEIGHT - 1)
        {
            bomb[0] = (xorshift(&seed) % WIDTH - 1) + 1;
            bomb[1] = (xorshift(&seed) % HEIGHT - 1) + 1;
        }

        startGrid[bomb[0]][bomb[1]] = -9;

        startGrid[bomb[0] + 1][bomb[1] + 1]++;
        startGrid[bomb[0] + 1][bomb[1]]++;
        startGrid[bomb[0]][bomb[1] + 1]++;
        startGrid[bomb[0] - 1][bomb[1] + 1]++;
        startGrid[bomb[0]][bomb[1] - 1]++;
        startGrid[bomb[0] + 1][bomb[1] - 1]++;
        startGrid[bomb[0] - 1][bomb[1] - 1]++;
        startGrid[bomb[0] - 1][bomb[1]]++;
    }


    while (1)
    {
        if (arrowPos[0] > WIDTH - 1) arrowPos[0] = WIDTH - 1;
        if (arrowPos[0] < 0) arrowPos[0] = 0;
        if (arrowPos[1] > HEIGHT - 1) arrowPos[1] = HEIGHT - 1;
        if (arrowPos[1] < 0) arrowPos[1] = 0;

        for (int x = 0; x < WIDTH; ++x)
        {
            for (int y = 0; y < HEIGHT; ++y)
            {

                if (knownGrid[x][y] == 1)
                {
                    if (startGrid[x][y] > 0)
                    {
                        consoleBuffer[x][y].Char.AsciiChar = '0' + startGrid[x][y];
                        consoleBuffer[x][y].Attributes = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
                    }
                    else
                    {
                        consoleBuffer[x][y].Char.AsciiChar = 'o';
                        consoleBuffer[x][y].Attributes = (startGrid[x][y] < 0 ? FOREGROUND_RED : FOREGROUND_BLUE) | FOREGROUND_INTENSITY;
                    }
                }
                else
                {
                    consoleBuffer[x][y].Char.AsciiChar = '00';
                    consoleBuffer[x][y].Attributes = 0;
                }

                if (arrowPos[0] == x && arrowPos[1] == y)
                {
                    consoleBuffer[x][y].Attributes = BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN;
                }
            }
        }

        WriteConsoleOutput(wHnd, *consoleBuffer, characterBufferSize, characterPosition, &consoleWriteArea);

        numEvents = 0;
        numEventsRead = 0;
        GetNumberOfConsoleInputEvents(rHnd, &numEvents);

        if (numEvents)
        {
            eventBuffer = malloc(sizeof(INPUT_RECORD) * numEvents);
            ReadConsoleInput(rHnd, eventBuffer, numEvents, &numEventsRead);
        }

        if (numEventsRead && wait <= 0)
        {
            wait = 1000;
            switch (eventBuffer[0].Event.KeyEvent.wVirtualKeyCode)
            {
            case 38:
                arrowPos[0]--;
                break;
            case 40:
                arrowPos[0]++;
                break;
            case 37:
                arrowPos[1]--;
                break;
            case 39:
                arrowPos[1]++;
                break;
            case 13:
                ExpandGrid(startGrid, knownGrid, arrowPos);
                break;
            }
        }

        wait--;
    }
}

Upvotes: 0

Views: 134

Answers (1)

Sander De Dycker
Sander De Dycker

Reputation: 16243

This does not do what you think it does :

bomb[0] = (xorshift(&seed) % WIDTH - 1) + 1;

From the use of this calculated value right after, you appear to expect this to return a value in the range [1,WIDTH-2].

But :

Given all those issues on that one line, I'm not surprised you see strange things happen. Not only have you not properly limited the range of the calculated value, you're also a victim of implementation defined and undefined behavior.

There might well be other issues in the code, but I'd start with these.

Upvotes: 1

Related Questions