Objective
The purpose of this assignment is to make sure:
• You are familiar with dynamic memory allocation and two dimensional arrays.
• You are familiar with the concepts of bounds checking and maintaining state.
• You can work with pointers, structures, and files in C++.
• You can approach a complex problem, break it down into various parts, and put together a solution.
Program 1 - Snake Game
You’re at your Grandmother’s over Thanksgiving, and your aunt is paying you quite handsomely to watch your 6 year old cousin. However, you have homework to do and video games to play, and like any selfrespecting college student, you do not want to hand over your Switch to your cousin. Instead you decide to use one of the old brick cell phones (a family heirloom), as a distraction. Your cousin likes to keep playing a game until the game is well and truly beaten. In order to keep your cousin occupied and still make some money, you rig the game to be unbeatable.
Write a C++ program to simulate a game similar to the snake game. You have a rectangular “field", with a wall around it and obstacles at certain spots. There is one opening in the wall through which the snake enters the field. The snake is infinitely long. The snake makes turns according to input and the game ends when the snake runs into itself. Print an orthographic map (viewed from above) of the field when the game ends.
Specifications
• Write a function called createField.
1.This function takes 2 parameters - the length and the width of the field, and returns a 2 dimensional array of chars.
2.Create a dynamic 2 dimensional array of characters, with “width" number of rows, and “length" columns per row.
3.Mark the boundaries - the wall around the field with a ‘W’. Mark all the area in the field with a ‘.’
4.Read a number ‘N’ from the user. Then, read in ‘N’ points marking the location of obstacles. Obstacles will only occupy one “spot" in the field, specified by a row and column number (0 indexed. Mark the obstacles with an ‘O’.
5.Return this array.
• Write a function called playGame.
6.This function takes the 2-dimensional array of characters, and the number of rows and columns as parameters, and returns nothing. (5 points)
7.Read in the location of the opening in the wall surrounding the field, specified as a 0-indexed row and column number. Mark the spot with a ‘G’, for Gate.
8.Read in the number of turns ‘T’ from the user.
9.Each turn will consist of a direction and a number of spots. The snake will turn in the given direction and travel the given number of spots
10.The snake will begin from the location of the gate.
11.Directions are as follows: U - Up, D - Down, L - Left, R
12.Right. You may assume that the snake will never do a 180 degree turn in one go, that is, if the snake were travelling down, the next turn will only ever be left or right, never up. The directions are also according to your perspective on the map, not the snake’s.
13.You may assume that the first turn will work with the location of the gate. That is, if the gate is on the Eastern wall, the first turn’s direction will be ‘L’. You may also assume the gate will never be at the corner, and the obstacles will not block entry into the field.
14.For every turn, read in the direction and number of spots, and move the snake in the direction, for the given number of spots, and mark the passage of the snake with ‘S’.
15.If the snake ever runs into itself, stop the game.
16.You may assume that the snake will be able to move the given number of spots in the given direction without running into a wall. However, if it runs into an obstacle, move on to the next turn.
17.If we finish out all the turns, and the snake is yet to run into itself, stop the game