+1 480 409 0818 

Snake Game Simulation 

#include using namespace std; char** createField(int r, int c){ char **arr; // initializing array arr = new char*[r]; for (size_t i = 0; i < r; i++){ arr[i] = new char[c]; } // Marking boundaries for (int i = 0; i < c; i++){ arr[0][i] = 'W'; arr[r - 1][i] = 'W'; } for (int i = 1; i < r - 1; i++){ arr[i][0] = 'W'; arr[i][c - 1] = 'W'; for (int j = 1; j < c - 1; j++){ arr[i][j] = '.'; } } // Marking obstacles int n, x, y; char d; cout << "Enter the number of obstacles: "; cin >> n; cout << "Enter the location of obstacles: " << endl; while (n--){ cin >> x >> d >> y; arr[x][y] = 'O'; } return arr; } void printArr(int r, int c, char**arr){ for (size_t i = 0; i < r; i++) { for (size_t j = 0; j < c; j++) { cout << arr[i][j]; } cout << endl; } } int boardEdit(char ***board, int gx, int gy, int newgx, int newy){ } void playGame(char **board, int r, int c){ int gx, gy; char d; cout << "Enter the location of the gate: "; cin >> gx >> d >> gy; board[gx][gy] = 'G'; int t, num; char *darr; int * numarr; bool end = false; cout << "Enter the number of turns: "; cin >> t; darr = new char[t]; numarr = new int[t]; cout << "Enter the turns:" << endl; for (size_t i = 0; i < t; i++) { cin >> darr[i] >> numarr[i]; } int index = 0; while (t-- && !end){ d = darr[index]; num = numarr[index++]; if (d == 'U'){ // iterating up to the new x and edit the cell value for (size_t i = gx - 1; !end && (i >= gx - num); i--) { if (board[i][gy] == 'S'){ // if going over itself => end game end = true; } else if (board[i][gy] != 'O'){ // if not an obstacle => edit cell safely board[i][gy] = 'S'; } } gx -= num; } else if (d == 'D'){ // iterating down to the new x and edit the cell value for (size_t i = gx + 1; !end && (i <= gx + num); i++) { if (board[i][gy] == 'S'){ end = true; } else if (board[i][gy] != 'O'){ board[i][gy] = 'S'; } } gx += num; } else if (d == 'L'){ // iterating left to the new x and edit the cell value for (size_t i = gy - 1; !end && (i >= gy - num); i--) { if (board[gx][i] == 'S'){ end = true; } else if (board[gx][i] != 'O'){ board[gx][i] = 'S'; } } gy -= num; } else if (d == 'R'){ // iterating right to the new x and edit the cell value for (size_t i = gy + 1; !end && (i <= gx + num); i++) { if (board[gx][i] == 'S'){ end = true; } else if (board[gx][i] != 'O'){ board[gx][i] = 'S'; } } gy += num; } } printArr(r, c, board); } int main(){ char ** board; int rows, cols; cout << "Enter the number of rows: "; cin >> rows; cout << "Enter the number of columns: "; cin >> cols; board = createField(rows, cols); playGame(board, rows, cols); // Clearing the memory for (int i = 0; i < rows; i++){ delete board[i]; } delete[]board; return 0; }