Question: What would the following C++ code look like in C#? #include #include #include using namespace std; const int rows = 10; const int columns =
What would the following C++ code look like in C#?
#include
const int rows = 10; const int columns = 10;
//Print Maze void printMaze(char maze[][columns]) { for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { cout << maze[i][j] << " "; } cout << endl; } }
//Walls & Paths void generateMaze(char maze[][columns]) { for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { if (i == 0 || i == rows - 1 || j == 0 || j == columns - 1) { maze[i][j] = '*'; } else { maze[i][j] = ' '; } } } //Place hidden treasure in maze randomly srand(time(NULL)); int treasure_row = rand() % (rows - 2) + 1; int treasure_col = rand() % (columns - 2) + 1; maze[treasure_row][treasure_col] = '$'; }
int main() { char maze[rows][columns]; generateMaze(maze); printMaze(maze);
int PlayerRow = 1, PlayerColumn = 1; char move; bool TreasureFound = false; while (!TreasureFound) { cout << "Where do you want to go? (WASD): "; cin >> move; switch (move) { case 'W': if (maze[PlayerRow - 1][PlayerColumn] != '#') { PlayerRow--; } break; case 'A': if (maze[PlayerRow][PlayerColumn - 1] != '#') { PlayerColumn--; } break; case 'S': if (maze[PlayerRow + 1][PlayerColumn] != '#') { PlayerRow++; } break; case 'D': if (maze[PlayerRow][PlayerColumn + 1] != '#') { PlayerColumn++; } break; default: break; } if (maze[PlayerRow][PlayerColumn] == '$') { TreasureFound = true; cout << "You found the treasure!" << endl; } else { maze[PlayerRow][PlayerColumn] = 'X'; printMaze(maze); } } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
