Search code examples
arrayscsyntaxsymbols

trouble reading output of the board


This is my tic tac toe game.

The idea is simple, we just change it in the main function and it is reflected on the board, but I'm getting an answer that is basically an error even though the code works.

Output:

|1|2|3|4|5|6|7|

1|�|�|�|�|�|�|�|

2|�|�|�|�|�|�|�|

3|�|�|�|�|�|�|�|

4|�|�|�|�|�|�|�|

5|�|�|�|�|�|�|�|

6|�|�|�|�|�|�|�|

7|�|�|�|�|�|�|�|

Code:

#include <stdio.h>
#include <stdlib.h>

#ifndef __TRUE_FALSE__
#define __TRUE_FALSE__
#define True 1
#define False 0
#endif

#define ROWS 7
#define COLS 7
#define MARKONE "X"
#define MARKTWO "O"
#define Blank ".."

void InitializeBoard(char[ROWS][COLS]);
void DisplayBoard(char[ROWS][COLS]);    
int PlayerMove(int,int,char[ROWS][COLS],char);

int main(void) {
  char board[ROWS][COLS];

  InitializeBoard(board);

  PlayerMove(1,1,board, MARKONE);
  PlayerMove(1,2,board, MARKONE);
  PlayerMove(4,3,board, MARKONE);
  PlayerMove(1,1,board, MARKONE);
  PlayerMove(6,2,board, MARKTWO);
  PlayerMove(4,12,board, MARKTWO);
  
  DisplayBoard(board);

  return 0;
}

void InitializeBoard(char board[ROWS][COLS])  {
    for(int i=0; i<ROWS; i++)
      for(int j=0; j<COLS; j++)
        board[j][i] = Blank;
};
  
void DisplayBoard(char board[ROWS][COLS]) {
  printf(" |1|2|3|4|5|6|7|\n");

  for(int i = 0; i < COLS; ++i) {
    printf("%c|", '1' + i);

    for(int j = 0; j < ROWS; ++j) {
      printf("%c|", board[i][j]);
    }

    printf("\n");
  }

}

int PlayerMove(int row,int col,char board[ROWS][COLS], char symbol){
  if(board[row][col]!= Blank) {
    printf("That space is already occupied\n");
  }
  else if(row > ROWS) {
    printf("That move is not on the board\n");

  }
  else if(col > COLS) {
    printf("That move is not on the board\n");
  }
  else board[row][col] = symbol;
}

Solution

  • You should be using single quoted character literals:

    'O', 'X' and '.' instead of the char* constants.

    #define MARKONE 'X'
    #define MARKTWO 'O'
    #define Blank '.'