+1 (315) 557-6473 

Create a Program to Implement Snake Game in C++ Assignment Solution.


Instructions

Objective
Write a c++ assignment program to implement snake game.

Requirements and Specifications

program to implement snake game in c++

Source Code

#include

#include

#include

using namespace std;

// Structure to hold a "move" information

struct Move

{

char name[50];

int damage;

};

// Structure to hold a character information

struct Character

{

char name[50];

char _class[50];

int hitPoints;

int armorClass;

bool isAlive;

char moveName[50];

};

// Load the next string tokenfrom the input file

void readString(ifstream &inFile, char token[])

{

int j = 0;

char value;

// Clear the token

for (int i = 0; i < 50; i++)

token[i] = '\0';

// Clear whitespaces

bool whiteSpaceFound = false;

while (!whiteSpaceFound && inFile >> noskipws >> value)

{

if (value == '\t' || value == '\n')

whiteSpaceFound = true;

else

token[j++] = value;

}

}

// Load the moves from file

Move *loadMoves(ifstream &inFile, int &numMoves)

{

char token[50];

readString(inFile, token);

numMoves = atoi(token);

Move *moves = new Move[numMoves];

for (int i = 0; i < numMoves; i++)

{

// Get name

readString(inFile, token);

strcpy(moves[i].name, token);

// Get damage

readString(inFile, token);

moves[i].damage = atoi(token);

}

return moves;

}

// Load the characters from file

Character *loadCharacters(ifstream &inFile, int &numCharacters)

{

char token[50];

readString(inFile, token);

numCharacters = atoi(token);

Character *characters = new Character[numCharacters];

for (int i = 0; i < numCharacters; i++)

{

// Get name

readString(inFile, token);

strcpy(characters[i].name, token);

//Get class

readString(inFile, token);

strcpy(characters[i]._class, token);

// Get HP

readString(inFile, token);

characters[i].hitPoints = atoi(token);

// Get AC

readString(inFile, token);

characters[i].armorClass = atoi(token);

// Get move name

readString(inFile, token);

strcpy(characters[i].moveName, token);

}

return characters;

}

// Find the character that holds the name

int findCharacter(Character *characters, int numCharacters, char *targetName)

{

for (int i = 0; i < numCharacters; i++)

if (strcmp(characters[i].name, targetName) == 0)

return i;

return -1;

}

// Find the move that holds the name

int findMove(Move *moves, int numMoves, char *targetName)

{

for (int i = 0; i < numMoves; i++)

if (strcmp(moves[i].name, targetName) == 0)

return i;

return -1;

}

// Sort the characters based on a given sort criteria

void sort(Character *characters, int numCharacters, char sortBy)

{

for (int i = 0; i < numCharacters - 1; i++)

{

for (int j = i + 1; j < numCharacters; j++)

{

if (sortBy == 'n')

{

// Sort by name in ascending order

if (strcmp(characters[i].name, characters[j].name) > 0)

{

Character temp = characters[i];

characters[i] = characters[j];

characters[j] = temp;

}

}

else if (sortBy == 'h')

{

// Sort by hit points in descending order

if (characters[i].hitPoints < characters[j].hitPoints)

{

Character temp = characters[i];

characters[i] = characters[j];

characters[j] = temp;

}

}

}

}

}

// Entry point of the program

int main()

{

// Load the characters file

ifstream charactersFile("characters.txt");

// Load the moves

int numMoves;

Move *moves = loadMoves(charactersFile, numMoves);

// Load the characters

int numCharacters;

Character *characters = loadCharacters(charactersFile, numCharacters);

charactersFile.close();

// Load the session file and make the characters fight with each other

ifstream sessionFile("session.txt");

char token[50];

readString(sessionFile, token);

int numAttacks = atoi(token);

for (int i = 0; i < numAttacks; i++)

{

// Get the character

readString(sessionFile, token);

int characterIdx = findCharacter(characters, numCharacters, token);

// Get the move

readString(sessionFile, token);

int moveIdx = findMove(moves, numMoves, token);

// Get the target characters

readString(sessionFile, token);

int targetIdx = findCharacter(characters, numCharacters, token);

// Get the roll

readString(sessionFile, token);

int roll = atoi(token);

// Character can't make a move if it's dead or the

// target is dead

if (characters[characterIdx].isAlive && characters[targetIdx].isAlive)

{

// Check move if it is the characetr's signature move

if (strcmp(characters[characterIdx].moveName, moves[moveIdx].name) == 0)

{

// Check if the the roll for the attack is at least equal to the armore class

// of the character being attacked

if (roll >= characters[targetIdx].armorClass)

{

// Apply damage

characters[targetIdx].hitPoints -= moves[moveIdx].damage;

// Character dies if it goes below 0

if (characters[targetIdx].hitPoints < 0)

characters[targetIdx].isAlive = false;

}

}

}

}

// Do extra credit

sort(characters, numCharacters, 'n');

// Write those who are alive to a file

ofstream outFile("alive.txt");

for (int i = 0; i < numCharacters; i++)

if (characters[i].isAlive)

outFile << characters[i].name << "\t" << characters[i].hitPoints << endl;

outFile.close();

// Free pointers

delete[] moves;

delete[] characters;

return 0;

}