0

I'm currently developing a simple naval battle game for my first exam at my college but i'm getting a strange output on my gameboard...

GameBoard

It should be iterating my "j" variable, but instead I get that strange character...

Here's my code:

//CREATES COORDENATES OF THE GAMEBOARD
    //ATTRIBUTE ONE LETTER TO EACH TRAY LINE
        for (i=0;i<11;i++){
            tabuleiro[i][0] = letra[i-1];
        }
    //ATTRIBUTE ONE NUMBER TO EACH TRAY COLUMN
        for (j=1;j<11;j++){
            tabuleiro[0][j] = j;
        }

    //CREATES THE "SEA"
        for (i=1;i<11;i++){
            for (j=1;j<11;j++){
                tabuleiro[i][j] = '~';
            }
        }

I've tryied to change my tabuleiro[0][j] = j; to tabuleiro[0][j] = (j+'0'); but then it only iterates until 9 and give me strange characters again...

GameBoard_2

If I'm not wrong I think this has something to do with the ASCII code (please correct me if I'm wrong) but I've no clue how to fix this.

Could you explain me how can I solve this please.

10
  • 2
    You need to post a minimal reproducible example. Commented Jan 5, 2018 at 16:02
  • 4
    You write: "I've tryied to change my tabuleiro[0][j] = j; to tabuleiro[0][j] = (j+'0'); but then it only iterates until 9 and give me strange characters again..." is : the "strange" character you're mentioning? What do you expect instead of the "strange character" ? Commented Jan 5, 2018 at 16:04
  • 2
    Need more info, but tabuleiro[i][0] = letra[i-1]; when i == 0, you'll get a negative index. Probably not what you want. Commented Jan 5, 2018 at 16:04
  • 2
    Please do not post text as graphics. As it's a text based game you could very well have pasted the stuff as text. Commented Jan 5, 2018 at 16:06
  • 1
    what single character do you want at the top of the 10th column? Commented Jan 5, 2018 at 16:08

2 Answers 2

4

to have precise control of the character i suggest

tabuleiro[0][j] = "123456789T"[j];

This will pick the jth character from that string

BTW the reason you got ':' is becasue ':' is the next ascii character after '9' - see http://www.asciitable.com/

Sign up to request clarification or add additional context in comments.

2 Comments

Excellent suggestion to use a character array (which I presume is what the letra array is, too). Character arithmetic is a sketchy thing. Might want to make it "123456789D", though, for "dez". However, the ":" is probably not because of ASCII. More generally, it is because of the execution character set that the compiler was told to use or defaults to (maybe UTF-8) and the encoding of the terminal (maybe CP860). Technically, they should match but the program user could get by if they match at the characters that the program uses.
no - its because ':' is the next char after 9, nothing mysterious here
3

The issue is the char code for 10 + '0' = 58 which is the char code for ':'. You might consider removing the column and row names out of the game array. They are just labels and not part of the game (I assume).

#define board_size 10

And

// Create game board and initialize grid to '~', in main() possibly
// Game is 10x10 grid
char tabuleiro[board_size][board_size];
for (int row = 0; row < board_size; row++) {
    for (int col = 0; col < board_size; col++) {
        tabuleiro[row][col] = '~';
    }
}

Have a function that draws the game board:

void drawBoard(char tabuleiro[board_size][board_size]) {
    // Print top line
    printf("  ");
    for (int col = 0; col < board_size; col++) {
        printf(" %-2d", col+1);
    }
    printf("\n");

    // Print grid
    for (int row = 0; row < board_size; row++) {
        // Print letter
        printf("%c ", 'A' + row);
        // Print board
        for (int col = 0; col < board_size; col++) {
            printf(" %c ", tabuleiro[row][col]);
        }
        printf("\n");
    }
}

2 Comments

So I can't just say tabuleiro[1][1] = "~" and when I write printf("%c",tabuleiro[1][1]) it prints me the "~" character back? because if I do this... it prints me "5"
chars go in single quotes. Double quotes are for strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.