// 1) print the board // 2) iteratively prompt for input and plot it // 3) implement the game with a fixed target // 4) implement a randomly assigned target #include #include #include #include using std::cout; using std::cin; using std::string; using std::setw; using std::stringstream; #if 0 // 1a) print the column labels int main() { cout << ' '; for (int i=0; i < 10; i = i + 1) cout << " " << i; } // 1b) print the column and row labels int main() { cout << ' '; for (int i=0; i < 10; i = i + 1) cout << " " << i; cout << '\n'; for (int i=0; i < 10; i = i + 1) cout << i << '\n'; } // 1c) print the board int main() { // print the column labels cout << ' '; for (int i=0; i < 10; i = i + 1) cout << " " << i; cout << '\n'; // print the rows for (int i=0; i < 10; i = i + 1) { cout << i; for (int j=0; j < 10; j = j + 1) cout << " -"; cout << '\n'; } } 0 1 2 3 4 5 6 7 8 9 0 - - - - - - - - - - 1 - - - - - - - - - - 2 - - - - - - - - - - 3 - - - - - - - - - - 4 - - - - - - - - - - 5 - - - - - - - - - - 6 - - - - - - - - - - 7 - - - - - - - - - - 8 - - - - - - - - - - 9 - - - - - - - - - - // 2) iteratively prompt for input and plot it // 2) add array, string, init, setw, cin, update int main() { string grid[10][10]; for (int i=0; i < 10; i = i + 1) for (int j=0; j < 10; j = j + 1) grid[i][j] = "-"; int row, col; string val; while (1) { cout << "\n "; for (int i=0; i < 10; i = i + 1) cout << setw(3) << i; cout << '\n'; for (int i=0; i < 10; i = i + 1) { cout << i; for (int j=0; j < 10; j = j + 1) cout << setw(3) << grid[i][j]; cout << '\n'; } cout << "row col val ... "; cin >> row >> col >> val; grid[row][col] = val; } } 0 1 2 3 4 5 6 7 8 9 0 - - - - - - - - - - 1 - - - - - - - - - - 2 - - - - - - - - - - 3 - - - - - - - - - - 4 - - - - - - - - - - 5 - - - - - - - - - - 6 - - - - - - - - - - 7 - - - - - - - - - - 8 - - - - - - - - - - 9 - - - - - - - - - - row col val ... 2 1 21 0 1 2 3 4 5 6 7 8 9 0 - - - - - - - - - - 1 - - - - - - - - - - 2 - 21 - - - - - - - - 3 - - - - - - - - - - 4 - - - - - - - - - - 5 - - - - - - - - - - 6 - - - - - - - - - - 7 - - - - - - - - - - 8 - - - - - - - - - - 9 - - - - - - - - - - row col val ... 7 8 78 0 1 2 3 4 5 6 7 8 9 0 - - - - - - - - - - 1 - - - - - - - - - - 2 - 21 - - - - - - - - 3 - - - - - - - - - - 4 - - - - - - - - - - 5 - - - - - - - - - - 6 - - - - - - - - - - 7 - - - - - - - - 78 - 8 - - - - - - - - - - 9 - - - - - - - - - - row col val ... // 3) implement the game with a fixed target // 3) add target, stringstream (to convert int to string) int main() { string grid[10][10]; for (int i=0; i < 10; i = i + 1) for (int j=0; j < 10; j = j + 1) grid[i][j] = "-"; int row, col, target_row = 4, target_col = 7, delta_row, delta_col; while (1) { cout << "\n "; for (int i=0; i < 10; i = i + 1) cout << setw(3) << i; cout << '\n'; for (int i=0; i < 10; i = i + 1) { cout << i; for (int j=0; j < 10; j = j + 1) cout << setw(3) << grid[i][j]; cout << '\n'; } cout << "row col ... "; cin >> row >> col; delta_row = target_row - row; if (delta_row < 0) delta_row = delta_row * -1; delta_col = target_col - col; if (delta_col < 0) delta_col = delta_col * -1; stringstream ss; ss << (delta_row + delta_col); ss >> grid[row][col]; } } 0 1 2 3 4 5 6 7 8 9 0 - - - - - - - - - - 1 - - - - - - - - - - 2 - - - - - - - - - - 3 - - - - - - - - - - 4 - - - - - - - - - - 5 - - - - - - - - - - 6 - - - - - - - - - - 7 - - - - - - - - - - 8 - - - - - - - - - - 9 - - - - - - - - - - row col ... 3 5 0 1 2 3 4 5 6 7 8 9 0 - - - - - - - - - - 1 - - - - - - - - - - 2 - - - - - - - - - - 3 - - - - - 3 - - - - 4 - - - - - - - - - - 5 - - - - - - - - - - 6 - - - - - - - - - - 7 - - - - - - - - - - 8 - - - - - - - - - - 9 - - - - - - - - - - row col ... 5 4 0 1 2 3 4 5 6 7 8 9 0 - - - - - - - - - - 1 - - - - - - - - - - 2 - - - - - - - - - - 3 - - - - - 3 - - - - 4 - - - - - - - - - - 5 - - - - 4 - - - - - 6 - - - - - - - - - - 7 - - - - - - - - - - 8 - - - - - - - - - - 9 - - - - - - - - - - row col ... 4 7 0 1 2 3 4 5 6 7 8 9 0 - - - - - - - - - - 1 - - - - - - - - - - 2 - - - - - - - - - - 3 - - - - - 3 - - - - 4 - - - - - - - 0 - - 5 - - - - 4 - - - - - 6 - - - - - - - - - - 7 - - - - - - - - - - 8 - - - - - - - - - - 9 - - - - - - - - - - // 4) implement a randomly assigned target // 4) add time(), srand(), rand() int main() { string grid[10][10]; for (int i=0; i < 10; i = i + 1) for (int j=0; j < 10; j = j + 1) grid[i][j] = "-"; srand( time( 0 ) ); int target_row = rand() % 10; int target_col = rand() % 10; int row, col; while (1) { cout << "\n "; for (int i=0; i < 10; i = i + 1) cout << setw(3) << i; cout << '\n'; for (int i=0; i < 10; i = i + 1) { cout << i; for (int j=0; j < 10; j = j + 1) cout << setw(3) << grid[i][j]; cout << '\n'; } cout << "row col ... "; cin >> row >> col; stringstream ss; ss << abs(target_row - row) + abs(target_col - col); grid[row][col] = ss.str(); } } 0 1 2 3 4 5 6 7 8 9 0 - - - - - - - - - - 1 - - - - - - - - - - 2 - - - - - - - - - - 3 - - - - - - - - - - 4 - - - - - - - - - - 5 - - - - - - - - - - 6 - - - - - - - - - - 7 - - - - - - - - - - 8 - - - - - - - - - - 9 - - - - - - - - - - row col ... 4 8 0 1 2 3 4 5 6 7 8 9 0 - - - - - - - - - - 1 - - - - - - - - - - 2 - - - - - - - - - - 3 - - - - - - - - - - 4 - - - - - - - - 7 - 5 - - - - - - - - - - 6 - - - - - - - - - - 7 - - - - - - - - - - 8 - - - - - - - - - - 9 - - - - - - - - - - row col ... 3 2 0 1 2 3 4 5 6 7 8 9 0 - - - - - - - - - - 1 - - - - - - - - - - 2 - - - - - - - - - - 3 - - 4 - - - - - - - 4 - - - - - - - - 7 - 5 - - - - - - - - - - 6 - - - - - - - - - - 7 - - - - - - - - - - 8 - - - - - - - - - - 9 - - - - - - - - - - row col ... 6 3 0 1 2 3 4 5 6 7 8 9 0 - - - - - - - - - - 1 - - - - - - - - - - 2 - - - - - - - - - - 3 - - 4 - - - - - - - 4 - - - - - - - - 7 - 5 - - - - - - - - - - 6 - - - 0 - - - - - - 7 - - - - - - - - - - 8 - - - - - - - - - - 9 - - - - - - - - - - #endif