PROGRAM DESCRIPTION: For this C++ program, you will edit your Homework 5 program
ID: 3572391 • Letter: P
Question
PROGRAM DESCRIPTION: For this C++ program, you will edit your Homework 5 programmin assignment to add functionality to play a very simplified version of the Candy Crush game called Crush where the goal is to match an existing combination of three or more characters on the board to gain points in a finite number of moves. Unless so indicated, all of the ts in 6 also hold for Homework 6. Note that there are some requirement changes to the requirements! You will take your solution fom Homework 5, and edit it as follows: You shall organize your program into three files: prgm.h will hold the include directives for the necessary libraries, any permitted global any enumerated data types, any type definitions, any structure defnitions, and the list of function prototypes (i.e., function dedarations main.epp will hold the local include directive for the header file as wel as the mainfunction for the program fune.epp will hold the local include directive for the header fe as wel as all function definitions (not including aain, of course) used in the program. 2. Define a global structure (ie.. a struct) that contains the folowing members. (1) a character member that holds the row position on the board note that this character may be a single character in the range 7A r. or R (2) an integer member that holds the column positon on the board- note that this integer has a range 0-9. (3) a character member that holds the direction ofthe match that this character may be either the single character V. for vertical, or "H for integer member that keeps track of the number of moves used in the game: and (5) an integer member that keeps track of the score for the 3. In addition to the global struct and enumerated data type, you may also use global constants for any type defnitions, the size of the board, the number of enun values (i.e., 60, and the maximum number of moves in a game (ie., 100 4. Instead of a traditional two-dimensional array as was done in 5, you will declare and create a two-dimensional dynamic array in main represent the 9-by-9 board for the enun type you declared to represent the various values that position may assume. Since the board array is not global, you wil pass the board array to the needed functions as a pointer (actually, as a pointer to a pointer). You wil need to make sure to return the memory for the two- mensional array back to the freestore when your program is done using it (at the end) 5. Inside main (1.you wil add a loop to play the game until the user has exhausted the maximum number of moves, in this case, 12.Explanation / Answer
Please find below the game program similar to the candy crush game:
main.cpp:
#include"Game.h"
#include<cstdlib>
#include<fstream>
#include<limits.h>
#include<iostream>
#include<ctime>
bool makeGameFromConfig(Game& game, std::string inputName) { // makeGameFromConfig() function
std::ifstream inFile;
std::vector<std::vector<Cell> > cellsTranspose;
inFile.open(inputName.c_str());
if (!inFile.is_open())
return false;
srand(time(NULL));
inFile >> game.level >> game.mode;
game.currecntTime = 0;
game.numOfLocks = 0;
game.numOfCoins = 0;
if (game.mode == TimeMode) //time mode on
inFile >> game.endTime;
else
game.endTime = INT_MAX;
if (game.mode == MoveLimit)
inFile >> game.remainingMoves;
else
game.remainingMoves = INT_MAX;
game.board.score = 0;
inFile >> game.numOfBombs >> game.numOfColorChanger >> game.numOfLightnings; //Input's
inFile >> game.board.sizeX >> game.board.sizeY;
readBoard(game , inFile);
inFile.close();
return true;
}
bool makeGameFromCIN(Game& game) //makeGameFromCIN() function
{
game.level = 0;
game.mode = 0;
game.numOfBombs = 0;
game.numOfColorChanger = 0;
game.numOfLightnings = 0;
std::vector<std::vector<Cell> > cellsTranspose;
srand(time(NULL));
game.board.score = 0;
game.endTime = 60;
std::cin >> game.board.sizeY >> game.board.sizeX; //game board size
readBoard(game , std::cin);
return true;
}
void readBoard(Game& game , std::istream& inFile){ //readBoard declaration
for (int i = 0; i < 5; i++) {
double tmpProb;
inFile >> tmpProb;
game.board.probs.push_back(tmpProb);
}
std::vector<std::vector<Cell> >cellsTranspose;
std::vector<Cell> tmpCells;
for (unsigned int i = 0; i <= game.board.sizeX+1; i++) {
Cell tmpCell; //temporary cell
tmpCell.locked = false;
tmpCell.swapAbleContent = false;
tmpCell.moveAbleContent = false;
tmpCell.piece.type = OutPiece;
tmpCell.piece.color = NoneColor;
tmpCell.pos.x = 0;
tmpCell.pos.y = 0;
tmpCells.push_back(tmpCell);
}
cellsTranspose.push_back(tmpCells);
for (unsigned int j = 1; j <= game.board.sizeY; j++) { //condition
tmpCells.clear();
Cell tmpCell;
tmpCell.locked = false;
tmpCell.swapAbleContent = false;
tmpCell.moveAbleContent = false;
tmpCell.piece.type = OutPiece;
tmpCell.piece.color = NoneColor;
tmpCell.pos.x = 0;
tmpCell.pos.y = 0;
tmpCells.push_back(tmpCell);
for (unsigned int i = 1; i <= game.board.sizeX; i++) {
tmpCell.locked = false;
tmpCell.swapAbleContent = true;
tmpCell.moveAbleContent = true;
tmpCell.pos.x = i;
tmpCell.pos.y = j;
tmpCell.piece.type = NormalPiece;
char tmpChar;
inFile >> tmpChar;
switch (tmpChar) { //switch case starts from here
case 'r':
case 'R':
tmpCell.piece.color = RedPiece;
break;
case 'b':
case 'B':
tmpCell.piece.color = BluePiece;
break;
case 'g':
case 'G':
tmpCell.piece.color = GreenPiece;
break;
case 'o':
case 'O':
tmpCell.piece.color = OrangePiece;
break;
case 'y':
case 'Y':
tmpCell.piece.color = YellowPiece;
break;
case 'l':
case 'L':
tmpCell.locked = true;
game.numOfLocks++;
case 'u':
case 'U':
tmpCell.piece.color = giveRandomColor(game.board.probs);
break;
case 'c':
case 'C':
tmpCell.swapAbleContent = false;
tmpCell.piece.type = CoinPiece;
tmpCell.piece.color = NoneColor;
game.numOfCoins++;
break;
case 's': //Block
case 'S':
tmpCell.swapAbleContent = false;
tmpCell.moveAbleContent = false;
tmpCell.piece.type = BlockPiece;
tmpCell.piece.color = NoneColor;
break;
case 'n':
case 'N':
tmpCell.swapAbleContent = false;
tmpCell.moveAbleContent = false;
tmpCell.piece.type = OutPiece;
tmpCell.piece.color = NoneColor;
break;
default:
break;
}
tmpCells.push_back(tmpCell);
}
tmpCell.locked = false;
tmpCell.swapAbleContent = false;
tmpCell.moveAbleContent = false;
tmpCell.piece.type = OutPiece;
tmpCell.piece.color = NoneColor;
tmpCell.pos.x = 0;
tmpCell.pos.y = 0;
tmpCells.push_back(tmpCell);
cellsTranspose.push_back(tmpCells);
}
tmpCells.clear(); //tempcells are cleared here
for (unsigned int i = 0; i <= game.board.sizeX+1; i++) {
Cell tmpCell;
tmpCell.locked = false;
tmpCell.swapAbleContent = false;
tmpCell.moveAbleContent = false;
tmpCell.piece.type = OutPiece;
tmpCell.piece.color = NoneColor;
tmpCell.pos.x = 0;
tmpCell.pos.y = 0;
tmpCells.push_back(tmpCell);
}
cellsTranspose.push_back(tmpCells);
game.board.cells.clear();
for (unsigned int i = 0; i <= game.board.sizeX+1; i++) {
tmpCells.clear();
for (unsigned int j = 0; j <= game.board.sizeY+1; j++)
tmpCells.push_back(cellsTranspose[j][i]);
game.board.cells.push_back(tmpCells);
}
}
void updateNumsOfGame(Game& game) { //updateNumsOfGame declaration
if ((game.mode != MakeItPass) && (game.mode != DestructionMode))
return;
game.numOfCoins = 0;
game.numOfLocks = 0;
for (unsigned int i = 1; i <= game.board.sizeX; i++)
for (unsigned int j = 1; j <= game.board.sizeY; j++) {
if (game.board.cells[i][j].locked)
game.numOfLocks++;
if (game.board.cells[i][j].piece.type == CoinPiece)
game.numOfCoins++;
}
}
bool saveGame(Game& game, std::string saveLocation) { //saveGame declaration
std::ofstream outFile;
outFile.open(saveLocation.c_str());
if (!outFile.is_open())
return false;
outFile << game.level << ' ' << game.mode << ' ' << game.currecntTime << ' ' << game.endTime << ' ' << game.board.score << ' ' << game.numOfBombs
<< ' ' << game.numOfCoins << ' ' << game.numOfColorChanger << ' ' << game.numOfLightnings << ' ' << game.numOfLocks << ' ' << game.remainingMoves << ' ';
outFile << game.board.sizeX << ' ' << game.board.sizeY << ' ' << game.board.probs[0] << ' ' << game.board.probs[1] << ' ' << game.board.probs[2]
<< ' ' << game.board.probs[3] << ' ' << game.board.probs[4] << ' ';
for (unsigned int j = 0; j <= game.board.sizeY + 1; j++) {
for (unsigned int i = 0; i <= game.board.sizeX + 1; i++) {
outFile << game.board.cells[i][j].locked << ' ' << game.board.cells[i][j].moveAbleContent << ' ' << game.board.cells[i][j].swapAbleContent << ' ' <<
game.board.cells[i][j].piece.color << ' ' << game.board.cells[i][j].piece.type << ' ' << game.board.cells[i][j].pos.x << ' ' << game.board.cells[i][j].pos.y << " ";
}
outFile << ' ';
}
outFile.close();
return true;
}
bool loadGame(Game& game, std::string loadLocation) {
std::ifstream inFile;
inFile.open(loadLocation.c_str());
if (!inFile.is_open())
return false;
inFile >> game.level >> game.mode >> game.currecntTime >> game.endTime >> game.board.score >> game.numOfBombs
>> game.numOfCoins >> game.numOfColorChanger >> game.numOfLightnings >> game.numOfLocks >> game.remainingMoves;
inFile >> game.board.sizeX >> game.board.sizeY >> game.board.probs[0] >> game.board.probs[1] >> game.board.probs[2]
>> game.board.probs[3] >> game.board.probs[4];
game.board.cells.clear();
for (unsigned int j = 0; j <= game.board.sizeY + 1; j++) {
std::vector<Cell> tmpCells;
for (unsigned int i = 0; i <= game.board.sizeX + 1; i++) {
Cell tmpCell;
inFile >> tmpCell.locked >> tmpCell.moveAbleContent >> tmpCell.swapAbleContent >>
tmpCell.piece.color >> tmpCell.piece.type >> tmpCell.pos.x >> tmpCell.pos.y;
tmpCells.push_back(tmpCell);
}
game.board.cells.push_back(tmpCells);
}
inFile.close(); //input file will be closed here
return true;
}
function.cpp:
#include<string>
#include<vector>
//Below are the Game Modes
#define TimeMode 0
#define MoveLimit 1
#define DestructionMode 2
#define MakeItPass 3
struct Game { // Game
int endTime;
int currecntTime;
unsigned int mode;
unsigned int numOfLocks; //This will be used in destruction mode
unsigned int numOfLightnings; //This is special item #1
unsigned int numOfColorChanger; //This is special item #2
unsigned int numOfBombs; //This is special item #3
unsigned int numOfCoins; //This will be used in make it pass mode
unsigned int level;
unsigned int remainingMoves;
Board board;
std::vector<std::vector<SDL_Surface*> > pieceImages;
SDL_Surface* gameScreen;
TTF_Font *font;
std::vector<Mix_Music*> musics;
std::vector<Mix_Chunk*> chunks;
};
void updateNumsOfGame(Game& game); //This will not set gameScreen and pieceImages
bool makeGameFromConfig(Game& game, std::string inputName);
bool saveGame(Game& game, std::string saveLocation);
bool loadGame(Game& game, std::string loadLocation);
bool makeGameFromCIN(Game& game);
void readBoard(Game& game , std::istream& inputStream);
#endif
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.