#include #include #include #include #include #include using std::cout; using std::cin; using std::string; using std::stringstream; using std::vector; using std::setw; // Just draw the board int main( void ) { char grid[10][10]; for (int i=0; i < 10; ++i) for (int j=0; j < 10; ++j) grid[i][j] = '-'; cout << " "; for (int i=0; i < 10; ++i) cout << i << ' '; cout << '\n'; for (int i=0; i < 10; ++i) { cout << i << ' '; for (int j=0; j < 10; ++j) cout << grid[i][j] << ' '; cout << '\n'; } } 0 1 2 3 4 5 6 7 8 9 0 - - - - - - - - - - 1 - - - - - - - - - - 2 - - - - - - - - - - 3 - - - - - - - - - - 4 - - - - - - - - - - 5 - - - - - - - - - - 6 - - - - - - - - - - 7 - - - - - - - - - - 8 - - - - - - - - - - 9 - - - - - - - - - - // Encapsulate the drawing of the board char grid[10][10]; void draw_board() { cout << " "; for (int i=0; i < 10; ++i) cout << i << ' '; cout << '\n'; for (int i=0; i < 10; ++i) { cout << i << ' '; for (int j=0; j < 10; ++j) cout << grid[i][j] << ' '; cout << '\n'; } } int main( void ) { for (int i=0; i < 10; ++i) for (int j=0; j < 10; ++j) grid[i][j] = '-'; draw_board(); } // Randomly compute target X and Y char grid[10][10]; void draw_board() { cout << " "; for (int i=0; i < 10; ++i) cout << i << ' '; cout << '\n'; for (int i=0; i < 10; ++i) { cout << i << ' '; for (int j=0; j < 10; ++j) cout << grid[i][j] << ' '; cout << '\n'; } } int main( void ) { int target_row, target_col; srand( time( 0 ) ); target_row = rand() % 10; target_col = rand() % 10; cout << "target_row " << target_row << ", target_col " << target_col << '\n'; for (int i=0; i < 10; ++i) for (int j=0; j < 10; ++j) grid[i][j] = '-'; draw_board(); } // Prompt for input and compute the delta X plus delta Y string grid[10][10]; void draw_board() { cout << ' '; for (int i=0; i < 10; ++i) cout << " " << i; cout << '\n'; for (int i=0; i < 10; ++i) { cout << i; for (int j=0; j < 10; ++j) cout << " " << grid[i][j]; cout << '\n'; } } int main( void ) { int target_row, target_col, row, col; srand( time( 0 ) ); target_row = rand() % 10; target_col = rand() % 10; cout << "target_row " << target_row << ", target_col " << target_col << '\n'; for (int i=0; i < 10; ++i) for (int j=0; j < 10; ++j) grid[i][j] = '-'; draw_board(); while (1) { cout << "row col ... "; cin >> row >> col; stringstream ss; ss << abs(target_row - row) + abs(target_col - col); grid[row][col] = ss.str(); draw_board(); } } // Use setw() to right-justify string grid[10][10]; void draw_board() { cout << ' '; for (int i=0; i < 10; ++i) cout << setw(3) << i; cout << '\n'; for (int i=0; i < 10; ++i) { cout << i; for (int j=0; j < 10; ++j) cout << setw(3) << grid[i][j]; cout << '\n'; } } int main( void ) { int target_row, target_col, row, col; srand( time( 0 ) ); target_row = rand() % 10; target_col = rand() % 10; for (int i=0; i < 10; ++i) for (int j=0; j < 10; ++j) grid[i][j] = '-'; draw_board(); while (1) { cout << "row col ... "; cin >> row >> col; stringstream ss; ss << abs(target_row - row) + abs(target_col - col); grid[row][col] = ss.str(); draw_board(); } } // Use sprintf() instead string grid[10][10]; void draw_board() { cout << ' '; for (int i=0; i < 10; ++i) cout << setw(3) << i; cout << '\n'; for (int i=0; i < 10; ++i) { cout << i; for (int j=0; j < 10; ++j) cout << setw(3) << grid[i][j]; cout << '\n'; } } int main( void ) { int target_row, target_col, row, col; srand( time( 0 ) ); target_row = rand() % 10; target_col = rand() % 10; for (int i=0; i < 10; ++i) for (int j=0; j < 10; ++j) grid[i][j] = '-'; char delta[6]; draw_board(); while (1) { cout << "row col ... "; cin >> row >> col; sprintf( delta, "%d", abs(target_row - row) + abs(target_col - col) ); grid[row][col] = delta; draw_board(); } } // Replace 2D array with vector of vectors vector > grid(10); void draw_board() { cout << ' '; for (int i=0; i < 10; ++i) cout << setw(3) << i; cout << '\n'; for (int i=0; i < 10; ++i) { cout << i; for (int j=0; j < 10; ++j) cout << setw(3) << grid[i][j]; cout << '\n'; } } int main( void ) { int target_row, target_col, row, col; srand( time( 0 ) ); target_row = rand() % 10; target_col = rand() % 10; vector grid_row( 10, "-" ); for (int i=0; i < 10; ++i) grid[i] = grid_row; char delta[6]; draw_board(); while (1) { cout << "row col ... "; cin >> row >> col; sprintf( delta, "%d", abs(target_row - row) + abs(target_col - col) ); grid[row][col] = delta; draw_board(); } } // Encapsulate the game in a class class Grid_Game { vector > grid; int target_row, target_col; public: Grid_Game() { srand( time( 0 ) ); target_row = rand() % 10; target_col = rand() % 10; vector grid_row( 10, "-" ); for (int i=0; i < 10; ++i) grid.push_back( grid_row ); draw_board(); } void draw_board() { cout << ' '; for (int i=0; i < 10; ++i) cout << setw(3) << i; cout << '\n'; for (int i=0; i < 10; ++i) { cout << i; for (int j=0; j < 10; ++j) cout << setw(3) << grid[i][j]; cout << '\n'; } } void make_guess( int row, int col ) { char delta[6]; sprintf( delta, "%d", abs(target_row - row) + abs(target_col - col) ); grid[row][col] = delta; draw_board(); } }; int main( void ) { Grid_Game game; int row, col; while (1) { cout << "row col ... "; cin >> row >> col; game.make_guess( row, col ); } } row col ... 3 3 0 1 2 3 4 5 6 7 8 9 0 15 - - - - - - - - - 1 - 13 - - - - - - - - 2 - - 11 - - - - - - - 3 - - - 9 - - - - - - 4 - - - - - - - - - - 5 - - - - - - - - - - 6 - - - - - - - - - - 7 - - - - - - - - - - 8 - - - - - - - 2 - - 9 - - - - - - 0 - - 3