Hey everyone, Im creating a Tic-Tac-Toe game and i was wondering if i would need to hard code my function in order to get "pretty" output. Like underscores between the rows and a line between the columns.
Pretty much have the game in a table labeled columns/rows with the column number and row number there.

void printGameBoard(const char array[][COLSIZE], const int rowSize) {
    //print the game board
    cout << setw(30) << "Tic-Tac-Toe" << endl << setw(55)<< " " << "Player Scores" << endl << endl
         << setw(65) << "Player 1: " << xCounter << endl << setw(65) << "Player 2: " << oCounter << endl           
       << setw(5) << " " << "* Player 1 is (X) and Player 2 is (O) *" << endl << endl;
    //nested loop to loop the rows/columns
    for (int row = 0; row < rowSize; row++) {
        cout << endl;
        for (int column = 0; column < COLSIZE; column++) {
            cout << setw(8) << " " << setw(4) << array[row][column];
        }
        cout << endl << endl;
    }
}

I'm not sure what you mean, exactly. If you want to vary the types of seperators that you use just allow for arguments to the function. Something like:

void printBoard (board_t *b, char col_sep, char row_sep) {
   for (...) {
      for (...) {
         printf ("%c%d", col_sep, b[x][y]);
      }
      printf ("\n");
      for (...) {
         printf ("%c", col_sep);
      }
      printf ("\n");
   }
}

Or something similar. There is no need to 'hard code' anything specific.

If you want to get really fancy, you can create a function callback type that takes a board and prints it out. In this way, you can simply assign a function to the function pointer and pass the board in when you want to print. Changing the printing mechanism is as simple as assigning a different function to the points.

[edit]
Since this is C++, you would really want to implement all of that function pointer behavior within a class or struct. Conceptually, it is the same but then you use the interface of the class instead of just a raw function pointer.

Edited 2 Years Ago by L7Sqr

This article has been dead for over six months. Start a new discussion instead.