Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Details Write all the code necessary to implement the Snake class as shown in the Snake UML Class Diagram to the right. Do this in

image text in transcribedimage text in transcribedimage text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

Details Write all the code necessary to implement the Snake class as shown in the Snake UML Class Diagram to the right. Do this in your project named snake (we'll continue adding files to this project as the term progresses). length : int the Snake : Location* Your class definition and implementation should be in separate files. getSnakeLength(): int When complete, you should have the following files in your project: getSnakeHead(): Location location.h getSnakeArray(Location() location.cpp setSnake Array(Location() apple.h moveForward moveUp() apple.cpp moveDown() snake.h snake.cpp moveRight() moveLeft() You will also need a main function to test your class. This can either be placed in a separate cpp file or at the end of your apple.cpp file. You will need to comment out your main function from last week's assignment since each project can only have one main function. Data details are as follows: const int BOARD_SIZE * BOARD_SIZE MAX_SNAKE SIZE length An int representing the actual length of the snake at the moment. theSnake A pointer to an array of MAX_SNAKE_SIZE Locations representing the location of each pixel of the snake's body on the board. The first element in the array holds the Location of the snake's head. Snake (constructor) Use the Snake pointer to dynamically allocate an array of MAX_SNAKE_SIZE locations. The snake will start at a length of 2. Thus, only the first two elements of the Snake array will be initialized to actual board Locations in the constructor. You can choose the Location where you would like to start your snake. I chose a Location near the middle of the board. All other elements of the Snake will automatically be initialized to (-1,-1) by the Location constructor. If an element is set to (-1,-1), that just means the snake isn't long enough to need to use that element yet. -Snake) (destructor) Delete theSnake array getSnakeLength() Accessor function that returns the length of the snake getSnakeHead() Accessor function that returns the Location of the Snake's head getSnakeArray Accessor function that returns the Snake array by reference setSnakeArray Mutator function that accepts one input parameter (an array of Locations) and sets the Snake array to the values in this input array. Also sets the length to match. moveUpO A mutator function that modifies the Snake array to move the head of the snake "up on the board (the rest of the body should follow the head). Since the snake can't move backwards onto itself, this should only work if the snake is not facing down on the board. moveDown A mutator function that modifies the Snake array to move the head of the snake "down on the board the rest of the body should follow the head). Since the snake can't move backwards onto itself, this should only work if the snake is not facing up on the board. moveRight() A mutator function that modifies the Snake array to move the head of the snake "right" on the board (the rest of the body should follow the head). Since the snake can't move backwards onto itself, this should only work if the snake is not facing left on the board. moveLeft() A mutator function that modifies the Snake array to move the head of the snake "left" on the board the rest of the body should follow the head). Since the snake can't move backwards onto itself, this should only work if the snake is not facing right on the board. moveForward Recommend doing this one last since it leverages the other movement functions! A mutator function that modifies the Snake array to move the head of the snake "forward" on the board in whatever direction the snake is already facing (the rest of the body should follow the head). Thorough testing now will save you time later!!!! Write code that tests every method. Here is a peek at a portion of my test code to give you some guidance: std::cout row = row; this->col = col; Qint Location::getRow() return row; Qint Location::getCol() return col; bool Location::isEmpty() return col == -1 && row == -1; #pragma once class Location public: Location(); void setLocation(int row, int col); int getRow(); int getCol(); bool isEmpty(); private: int row; int col; Apple: : Apple(int row, int col) static bool firstTime = true; if (row != -1 && col != -1) { appleLocation.setLocation(row, col); else if (firstTime) { srand (static_cast(time(NULL)); firstTime = false; int random = rand() % BOARD_SIZE; if (row == -1 && col == -1) int random2 = rand() % BOARD_SIZE; appleLocation.setLocation(random, random2); else ; if (row == -1) appleLocation.setLocation(random, col); else if (col == -1) appleLocation.setLocation(row, random); Location Apple::getAppleLocation() { return appleLocation; #pragma once #include "location.h" const int BOARD_SIZE = 17; class Apple public: Location getAppleLocation(); Apple(int row = -1, int col = -1); private: Location appleLocation

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Students also viewed these Databases questions