Question

(C++ program) Write a game that uses linked list. There should be 20 nodes. You can...

(C++ program)

Write a game that uses linked list. There should be 20 nodes. You can change the struct to your preferences in order to make the game better. Make sure the user is not able to move to a NULL location. The story game should be rated PG-13. No bad words or gore.You can add special conditions to where the story will not progress unless you clear a certain obstacle in a different room, others did enemy encounters where the enemy will drop a key to open a door. You should make your story / game interesting (it should also be clear to navigate). A sort of map may help the user to navigate.

( Please add comments and show output)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

small C++ millipede and insect program

inputs are W,A,S,D,X

kindly upvote .

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

using std::cout; using std::cin; using std::endl;

const int width = 20;
const int height = 20;
bool gameState = true;
int score;

struct Node {
int x;
int y;
struct Node *next;
};

Node *newNode(int x, int y) {
Node *link = new(Node);
link->x = x;
link->y = y;
return link;
}

struct InsectClass {
int x;
int y;
};


void rotateList(Node *head, int x, int y) {
int i;
Node *current = head;
int tempX[2], tempY[2];
for (i = 0; current != nullptr; i++) {

if (i == 0) {
tempX[0] = current->x;
tempY[0] = current->y;
current->x = x;
current->y = y;
}
else {
tempX[(i % 2 == 0) ? 0 : 1] = current->x;
tempY[(i % 2 == 0) ? 0 : 1] = current->y;
current->x = tempX[(i % 2 == 0) ? 1 : 0];
current->y = tempY[(i % 2 == 0) ? 1 : 0];
}

current = current->next;
}

}


void append(Node *&head, Node *&link) {
Node *ptr;
ptr = head;

head = link;
head->next = ptr;
}

Node *head;
InsectClass insect;

class millipedeClass {
private:
int posX;
int posY;
int tailLen = 0;
enum eDirection{STOP = 0, UP, LEFT, RIGHT, DOWN};
eDirection dir;
public:
void init () {
//Initialise millipede's position
posX = width/2;
posY = height/2;

//Initialise first tail node
head = newNode(posX, posY);

//Get insect
insect.x = rand() % width;
insect.y = rand() % height;

//Initialise score
score = 0;

//Initialise direction
dir = STOP;
}
void draw() {
system("cls");

//Draw top of playing field
for (int i = 0; i < width+2; i++)
cout << '#';
cout << '\n';

//Main draw
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {


bool found = false;

//Draw left of playing field
if (j == 0)
cout << '#';

//Draw millipede's head
if (i == posY && j == posX) {
cout << 'O';
found = true;
}

Node *current = head;

//Draw millipede's body by extracting data from a linked list
while (current != nullptr && found != true) {
if (i == current->y && j == current->x) {
cout << 'o';
found = true;
}
current = current->next;
}

//If millipede's body is not found check to see if there is anything else in the location
if (!found) {
if (i == insect.y && j == insect.x)
cout << 'F';
else
cout << ' ';
}

//Draw right side of playing field
if (j == width-1)
cout << '#';
}
//Add new lines where needed
if (i != height-1)
cout << '\n';
}
cout << '\n';

//Draw bottom of playing field
for (int i = 0; i < width+2; i++)
cout << '#';
cout << endl;

//Score screen
cout << "Score: " << score << " || Tail len: " << tailLen << '\n';

}

void input() {
  
//Controls
switch(getch()) {
case 'W' : case 'w' :
if (dir == STOP)
dir = UP;
break;
case 'A' : case 'a' :
if (dir == STOP)
dir = LEFT;
else if (dir == UP)
dir = LEFT;
else if (dir == DOWN)
dir = RIGHT;
else if (dir == LEFT)
dir = DOWN;
else if (dir == RIGHT)
dir = UP;
break;
case 'S' : case 's' :
if (dir == STOP)
dir = DOWN;
break;
case 'D' : case 'd' :
if (dir == STOP)
dir = RIGHT;
else if (dir == UP)
dir = RIGHT;
else if (dir == DOWN)
dir = LEFT;
else if (dir == LEFT)
dir = UP;
else if (dir == RIGHT)
dir = DOWN;
break;
case 'x' : case 'X' :
gameState = false;
break;
}
//}
}

void logic() {
//Movement and millipede tail logic
switch (dir) {
case UP :
//Rotate list rotates the nodes for the millipede tail
rotateList(head, posX, posY);
posY--;
break;
case DOWN :
//Rotate list rotates the nodes for the millipede tail
rotateList(head, posX, posY);
posY++;
break;
case LEFT :
//Rotate list rotates the nodes for the millipede tail
rotateList(head, posX, posY);
posX--;
break;
case RIGHT :
//Rotate list rotates the nodes for the millipede tail
rotateList(head, posX, posY);
posX++;
break;
}
//if insect is caught up
if (posX == insect.x && posY == insect.y) {
//Create new milliped tail node
Node *link = newNode(posX, posY);
append(head, link);

//Add score
score += 100;

//reget iinsect
insect.x = rand() % width;
insect.y = rand() % height;

//Play noise
cout << '\a' << endl;

//Update tail length score
++tailLen;
}

//Game over logic

//If millipede goes out of bounds end game
if (posX > width-1 || posX < 0 || posY > height-1 || posY < 0)
gameState = false;

//If millipede touches tail end game (Cycles through linked list nodes to check)
Node *current = head->next;
while (current != nullptr) {
if (posX == current->x && posY == current->y)
gameState = false;
current = current->next;
}
}


};
int main () {
//Run all the code
millipedeClass milli;
milli.init();
while (gameState) {
milli.draw();
milli.input();
milli.logic();
}
return 0;
}

main.cpp input sh: 1: cls: not found oo# Score: 0 Tail len: 0 Program finished with exit code 0 Press ENTER to exit console

Add a comment
Know the answer?
Add Answer to:
(C++ program) Write a game that uses linked list. There should be 20 nodes. You can...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need this in C++. This is all one question Program 2: Linked List Class For...

    I need this in C++. This is all one question Program 2: Linked List Class For this problem, let us take the linked list we wrote in a functional manner in a previous assignment and convert it into a Linked List class. For extra practice with pointers we'll expand its functionality and make it a doubly linked list with the ability to traverse in both directions. Since the list is doubly linked, each node will have the following structure: struct...

  • In C++ syntax please Write a program that implements and demonstrates a linked list using functions....

    In C++ syntax please Write a program that implements and demonstrates a linked list using functions. Your program should first dehne a node that stores an integer and then your program will include the following functions appendo- This function accepts the head pointer (by reference) of a linked list and an integer as it's only arguments. It then creates a node, stores the integer argument in the node, and adds it to the end of the list. findo-This function accepts...

  • PROGRAM DESCRIPTION: In this assignment, you will be creating a memory matching game in C++. In t...

    c++ PROGRAM DESCRIPTION: In this assignment, you will be creating a memory matching game in C++. In this game, the user will need to match up the pairs symbols A,B,C,D,E on a 4x4 array. For example, the array could be initialized like the following: In this case, X represents an empty slot. The goal is for the user to match the A to the A, the B to the B, etc, until all pairs are matched up to win the...

  • WRITE IN LANGUAGE C. THANKS Heroic Game Write a program that plays a simple game. Mostly...

    WRITE IN LANGUAGE C. THANKS Heroic Game Write a program that plays a simple game. Mostly the purpose of the program is to practice using structs. Here is a struct that contains information about a character in the game: typedef struct {   int hitPoints;    /* how much life */   int strength;     /* fighting strength */   char name[MAXNAME]; } Hero; The same structure is used for all characters (even non-heroic.) First write the function void printHero( Hero hr )that prints a...

  • This program should be in c++. Rock Paper Scissors: This game is played by children and...

    This program should be in c++. Rock Paper Scissors: This game is played by children and adults and is popular all over the world. Apart from being a game played to pass time, the game is usually played in situations where something has to be chosen. It is similar in that way to other games like flipping the coin, throwing dice or drawing straws. There is no room for cheating or for knowing what the other person is going to...

  • Write a program in the Codio programming environment that allows you to play the game of...

    Write a program in the Codio programming environment that allows you to play the game of Rock / Paper / Scissors against the computer. Within the Codio starting project you will find starter code as well as tests to run at each stage. There are three stages to the program, as illustrated below. You must pass the tests at each stage before continuing in to the next stage.  We may rerun all tests within Codio before grading your program. Please see...

  • Objective: Write a program that implements the Game of Life cellular automata system invented by John...

    Objective: Write a program that implements the Game of Life cellular automata system invented by John Conway. 1. Create two game grids of size at least 50x50. These grid cells can be either Boolean or integer. In the following, I’ll refer to these as gridOne and gridTwo. 2. Set all cells in both grids to false. 3. Start by initializing gridOne. Allow the user to specify two different ways of initializing the grid: 1) by specifying a pattern file to...

  • C programming A linked list is a linear data structure that allows us to add and remove items fro...

    c programming A linked list is a linear data structure that allows us to add and remove items from the list very quickly, by simply changing a few pointers. There are many different variations of linked lists. We have studied the doubly-linked, circular, with a dummy-header-node version of a linked list. In the class notes we studied several functions to manipulate a Linked List. For this assignment you must write the code for the following additional linked list functions: addFirst,...

  • C++ for this exercise, you will make a simple Dungeon Crawl game. For an enhanced version,...

    C++ for this exercise, you will make a simple Dungeon Crawl game. For an enhanced version, you can add monsters that randomly move around. Program Requirements While the program is an introduction to two-dimensional arrays, it is also a review of functions and input validation. check: Does the program compile and properly run? does all functions have prototypes? Are all functions commented? Is the program itself commented? Are constants used where appropriate? Are the required functions implemented? Is the dungeon...

  • Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU...

    Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU START: // This homework is built on homework 06. The given program is an updated version of hw06 solution. It begins by displaying a menu to the user // with the add() function from the last homework, as well as some new options: add an actor/actress to a movie, display a list of movies for // an actor/actress, delete all movies, display all movies,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT