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
  • c++ only Program 2: Linked List Class For this problem, let us take the linked list...

    c++ only 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 Node { int number; Node * nextNode;...

  • 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...

  • C++ Linked List Implementation Motivation As we discussed in class, the data structures that you use...

    C++ Linked List Implementation Motivation As we discussed in class, the data structures that you use to implement your program can have a profound impact on it's overall performance. A poorly written program will often need much more RAM and CPU time then a well-written implementation. One of the most basic data structure questions revolves around the difference between an array and a linked list. After you finish this assignment you should have a firm understanding of their operation. Problem...

  • 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...

  • Homework #5 - Elevator Simulation - Doubly Linked List The program needs to use a doubly...

    Homework #5 - Elevator Simulation - Doubly Linked List The program needs to use a doubly linked list. Homework #5 - Extra Credit Elevator Simulation Algorithm We are going to develop an algorithm and implement in C++ to simulate a single elevator in a 10-story building. There will be some simplifying assumptions that will make this a bit less complex than a real-world implementation. The number of floors is not important, nor is the number of people getting on or...

  • 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...

  • Creating linked list data structure Overview The purpose of this assignment is for you to write...

    Creating linked list data structure Overview The purpose of this assignment is for you to write a data structure called a Linked List, which utilizes templates (similar to Java’s generics), in order to store any type of data. In addition, the nature of a Linked List will give you some experience dealing with non-contiguous memory organization. This will also give you more experience using pointers and memory management. Pointers, memory allocation, and understand how data is stored in memory will...

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