home *** CD-ROM | disk | FTP | other *** search
/ Beginning C++ Through Gam…rogramming (2nd Edition) / BCGP2E.ISO / source / chapter03 / tic-tac-toe_board.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-04-11  |  869 b   |  39 lines

  1. // Tic-Tac-Toe Board
  2. // Demonstrates multidimensional arrays
  3.  
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     const int ROWS = 3;
  11.     const int COLUMNS = 3;
  12.     char board[ROWS][COLUMNS] = { {'O', 'X', 'O'},
  13.                                   {' ', 'X', 'X'},
  14.                                   {'X', 'O', 'O'} };
  15.  
  16.     cout << "Here's the tic-tac-toe board:\n";
  17.     for (int i = 0; i < ROWS; ++i)
  18.     {
  19.         for (int j = 0; j < COLUMNS; ++j)
  20.             cout << board[i][j];
  21.         cout << endl;
  22.     }
  23.  
  24.     cout << "\n'X' moves to the empty location.\n\n";
  25.     board[1][0] = 'X';
  26.  
  27.     cout << "Now the tic-tac-toe board is:\n";
  28.     for (int i = 0; i < ROWS; ++i)
  29.     {
  30.         for (int j = 0; j < COLUMNS; ++j)
  31.             cout << board[i][j];
  32.         cout << endl;
  33.     }
  34.  
  35.     cout << "\n'X' wins!";
  36.  
  37.     return 0;
  38. }
  39.