Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create the following game, using the code template given. This is a variation on

ID: 3678905 • Letter: C

Question

Create the following game, using the code template given. This is a variation on "Snake" See: https://en.wikipedia.org/wiki/Snake_(video_game) "U" is you, the player; you always start in the upper left corner. "X" is the opponent, which always starts in the lower right corner. NUMROWS and NUMCOLS delimits the size and shape of the game board. TAILLENGTH gives the length of the "tail" -- if one player hits the other's tail (or the head), the player loses. DO NOT Try to make a version which interacts with the arrow keys; see class discussion regarding why. DO NOT USE GLOBAL VARIABLES! You HAVE TO use the template given below; your job is to write the missing functions. SAMPLE RUN (NUMROWS==6, NUMCOLS==6, TAILLENGTH=5):

U _ _ _ _ _

_ _ _ _ _ _

_ _ _ _ _ _

_ _ _ _ _ _

_ _ _ _ _ _

_ _ _ _ _ X

Enter direction (N/S/E/W): E

u U _ _ _ _

_ _ _ _ _ _

_ _ _ _ _ _

_ _ _ _ _ _

_ _ _ _ _ X

_ _ _ _ _ x

Enter direction (N/S/E/W): E

u u U _ _ _

_ _ _ _ _ _

_ _ _ _ _ _

_ _ _ _ _ X

_ _ _ _ _ x

_ _ _ _ _ x

Enter direction (N/S/E/W): E

u u u U _ _

_ _ _ _ _ _

_ _ _ _ _ X

_ _ _ _ _ x

_ _ _ _ _ x

_ _ _ _ _ x

Enter direction (N/S/E/W): E

u u u u U _

_ _ _ _ _ X

_ _ _ _ _ x

_ _ _ _ _ x

_ _ _ _ _ x

_ _ _ _ _ x

Enter direction (N/S/E/W): S

u u u u u X

_ _ _ _ U x

_ _ _ _ _ x

_ _ _ _ _ x

_ _ _ _ _ x

_ _ _ _ _ x

Enter direction (N/S/E/W): S

_ u u u u x
_ _ _ _ u x
_ _ _ _ U x
_ _ _ _ _ x
_ _ _ _ _ x
_ _ _ _ _ _
*** YOU WIN! ***

CODE TEMPLATE:
const int NUMROWS = 7;// CAN BE CHANGED FOR DIFFERENT GAME PLAY
const int NUMCOLS = 10;// CAN BE CHANGED FOR DIFFERENT GAME PLAY
const int TAILLENGTH = 15;// CAN BE CHANGED FOR DIFFERENT GAME PLAY
struct _cell {
char marker;
int moveNumber;
};
*** OTHER FUNCTIONS THAT YOU WRITE ***
int main() {
int uRow = 0, uCol = 0;
int oRow = NUMROWS - 1, oCol = NUMCOLS - 1;
bool win = true;
int move = 0;
_cell board[NUMROWS][NUMCOLS];
initBoard(board);
placePiece(board, uRow, uCol, 'U', move);
placePiece(board, oRow, oCol, 'X', move);
showBoard(board, move);
while (true) {
move++;
if (!movePlayer(board, uRow, uCol, move)) {
win = false;
break;
}
if (!moveOpponent(board, oRow, oCol, move))
break; // player wins
showBoard(board, move);
}
showBoard(board, move);
if (win)
cout << "*** YOU WIN! ***" << endl;
else
cout << "*** YOU LOSE! ***" << endl;
string tmp; getline(cin, tmp);
}

Explanation / Answer

#include <iostream>
#include <conio.h>

void run();
void printMap();
void initMap();
void move(int dx, int dy);
void update();
void Direction(char key);
void clearScreen();
void Food();

char getValue(int value);
const int mapwidth = 20;
const int mapheight = 20;

const int size = mapwidth * mapheight;
int map[size];

int headxpos;
int headypos;
int direction;

int food = 3;
bool running;

int main()
{
    run();
    return 0;
}

void run()
{
    initMap();
    running = true;
    while (running)
    {
        if (kbhit())
        {
            Direction(getch());
        }
        update();

        clearScreen();

        printMap();

        _sleep(500);
    }

    std::cout << " !!!Game over!" << std::endl << " Your score is: " << food;

    std::cin.ignore();
}

void Direction(char key)
{

    switch (key)
     {
    case 'w':
        if (direction != 2) direction = 0;
        break;
    case 'd':
        if (direction != 3) direction = 1;
        break;
    case 's':
        if (direction != 4) direction = 2;
        break;
    case 'a':
        if (direction != 5) direction = 3;
        break;
    }
}

void move(int dx, int dy) {
    int newx = headxpos + dx;
    int newy = headypos + dy;

    if (map[newx + newy * mapwidth] == -2)
    {
        food++;

        Food();
    }

    else if (map[newx + newy * mapwidth] != 0)
    {
        running = false;
    }

    headxpos = newx;
    headypos = newy;
    map[headxpos + headypos * mapwidth] = food + 1;

}

void clearScreen()
{
    system("cls");
}

void Food()
{
    int x = 0;
    int y = 0;
    do {
        x = rand() % (mapwidth - 2) + 1;
        y = rand() % (mapheight - 2) + 1;

    }
    while (map[x + y * mapwidth] != 0);

    map[x + y * mapwidth] = -2;
}

void update() {
    switch (direction) {
    case 0: move(-1, 0);
        break;
    case 1: move(0, 1);
        break;
    case 2: move(1, 0);
        break;
    case 3: move(0, -1);
        break;
    }

    for (int i = 0; i < size; i++) {
        if (map[i] > 0) map[i]--;
    }
}

void initMap()
{
    headxpos = mapwidth / 2;
    headypos = mapheight / 2;
    map[headxpos + headypos * mapwidth] = 1;

    for (int x = 0; x < mapwidth; ++x) {
        map[x] = -1;
        map[x + (mapheight - 1) * mapwidth] = -1;
    }

    for (int y = 0; y < mapheight; y++) {
        map[0 + y * mapwidth] = -1;
        map[(mapwidth - 1) + y * mapwidth] = -1;
    }

    Food();
}

void printMap()
{
    for (int x = 0; x < mapwidth; ++x)
    {
        for (int y = 0; y < mapheight; ++y)
        {
            std::cout << getValue(map[x + y * mapwidth]);
        }
        std::cout << std::endl;
    }
}

char getValue(int value)
{
    if (value > 0) return 'o';

    switch (value)
    {
    case -1: return 'X';
    case -2: return 'O';
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote