#include <iostream> #include <stdio.h> #include <cstdlib> using namespace std; v
ID: 3698253 • Letter: #
Question
#include <iostream>
#include <stdio.h>
#include <cstdlib>
using namespace std;
void gameheader();
const int SIZE = 9;
void createBoard(int board[][SIZE]);
void displayBoard(int board[][SIZE]);
enum CHARACTERS{exclamation = '!', hashtag = '#', percentage = '%', doubleQuote = '"', dolarSign = '$', ampersand = '&'};
int main()
{
int board[SIZE][SIZE];
gameheader();
createBoard(board);
displayBoard(board);
return 0;
}
void gameheader()
{
cout << " ";
cout << "----------------------------------------------------------- ";
cout << " W e l c o m e t o 1 0 3 0 C r u s h ";
cout << endl;
cout << "This program will randomly initialize your game board using ";
cout << "a set of 6 characters (!, " << static_cast<char>(doubleQuote) << ", #, $, %, &) that a player will ";
cout << "then attempt to match a combination of 3 or more characters ";
cout << "to gain points in a finite set of moves or time duration. ";
cout << "----------------------------------------------------------- ";
cout << endl;
return;
}
void createBoard(int board[][SIZE])
{
int i, j, symbol;
for(i = 0; i < SIZE; i++)
{
for(j = 0; j < SIZE; j++)
{
symbol = rand() % 6;
switch(symbol)
{
case 0:
board[i][j] = exclamation;
break;
case 1:
board[i][j] = hashtag;
break;
case 2:
board[i][j] = percentage;
break;
case 3:
board[i][j] = doubleQuote;
break;
case 4:
board[i][j] = dolarSign;
break;
case 5:
board[i][j] = ampersand;
break;
default:
cout << "ERROR!" << endl;
}
}
}
return;
}
void displayBoard(int board[][SIZE])
{
int i, j;
int letter = 'A';
cout << "Initializing board..." << endl;
cout << " ";
cout << " 1 2 3 4 5 6 7 8 9 " << endl;
cout << " +-------------------+" << endl;
for(i = 0; i < SIZE; i++)
{
cout << static_cast<char>(letter) << " | ";
for(j = 0; j < SIZE; j++)
{
cout << static_cast<char>(board[i][j]) << " ";
}
cout << "| ";
letter++;
}
cout << " +-------------------+" << endl;
cout << " Moves: 0 Score: 0" << endl;
cout << " ";
return;
}
You shall organize your program into three files:
prgm.h will hold the include directives for the necessary libraries, any permitted global constants, any enumerated data types, any type definitions, any structure definitions, and the list of function prototypes (i.e., function declarations).
main.cpp will hold the local include directive for the header file as well as the main function for the program
func.cpp will hold the local include directive for the header file as well as all function definitions (not including main, of course) used in the program.
Define a global structure (i.e., a struct) that contains the following 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 ‘A’ – ‘I’, or ‘R’; (2) an integer member that holds the column position on the board – note that this integer has a range 0 – 9; (3) a character member that holds the direction of the match – note that this character may be either the single character ‘V’, for vertical, or ‘H’, for horizontal; (4) an 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 game.
In addition to the global struct and enumerated data type, you may also use global constants for any type definitions, the size of the board, the number of enum values (i.e., 6), and the maximum number of moves in a game (i.e., 10).
Instead of a traditional two-dimensional array as was done in Homework 5, you will declare and create a two-dimensional dynamic array in main to represent the 9-by-9 board for the enum type you declared to represent the various values that a position may assume. Since the board array is not global, you will pass the board array to the needed functions as a pointer (actually, as a pointer to a pointer). You will need to make sure to return the memory for the two- dimensional array back to the freestore when your program is done using it (at the end).
Inside main(), you will add a loop to play the game until the user has exhausted the maximum number of moves, in this case, 10.
For each move, the user will enter a string (e.g., B7) corresponding to either a coordinate on the board where a match is to be evaluated or to a directive to reshuffle the board if there are no combinations of three or more characters (vertically or horizontally) on the board. Each valid coordinate or directive will increase the counter that keeps track of the number of moves taken.
If the user enters a valid coordinate, you will then prompt the user to enter a direction in the form of a character (either ‘V’ for vertical or ‘H’ for horizontal).
Instead of a coordinate, the user may also select the directive “R0” if there are no character combinations visible to request that the entire board be reshuffled. In this case, a direction is not required as the entire board will be reshuffled.
If the user enters either an invalid coordinate or directive, you will display a meaningful error message and re-prompt the user to enter the coordinate or directive again without incrementing the number of moves taken.
You will parse this input and store it in the structure variable that should be passed to the function to evaluate your move and update the game (i.e., moves, score, and board as applicable).
Following each turn, you will display an updated version of the game.
Add a void function called by main() to check the coordinate entered by the user and update the game (i.e., moves, score, and board as applicable). At a minimum, you will pass the board as well as the structure variable, both of which can be updated as a result of the move.
If the user selects a coordinate and direction that contains a combination of three or more identical enum values in a row, either vertically or horizontally, you will count the number of matched enum values, add this number to the score (stored in the structure variable), and update the board at the coordinates of all the matched enum values by replacing each of those values with a seeded, randomly generated new value. Note that the user may select any coordinate in the matched values (e.g., the left or right coordinate for a horizontal direction).
If the user selects a coordinate and direction that is a valid coordinate, but does not contain a combination of three or more identical enum values in a row, either vertically or horizontally, you will still increment the move counter, but not award any points or update the board (though you should still print out the existing board for the next move).
Once the user has used all of his/her moves, you will display a meaningful message with the final score to coincide the updated board.
You may assume that all input by the user is of the correct data type, though perhaps out-of-range.
Extra Credit Opportunity: Up To 15 Points
For students who have completed all requirements for this program, you may add the
following option to gain additional bonus points to your program score:
You may add a “timer” option where the game will end when either (1) the user exhausts of all his/her moves, or (2) a timer (initially set for 60 seconds?) runs out, whichever comes first. Look at the time() function for more details.
Instead of simply putting a new randomly-generated enum value at the coordinates of the matched values, you may shift the values down the applicable rows or columns similar to what a “real” game might do – see your instructor if you have questions on what this entails.
Instead of simply “reshuffling” your board, you may allow users to “flip” a set of two values (e.g., B7 and B8) to allow “new” matches to become available.
Explanation / Answer
prgm.h
#include <iostream>
#include <stdio.h>
#include <cstdlib>
using namespace std;
void gameheader();
const int SIZE = 9;
void createBoard(int board[][SIZE]);
void displayBoard(int board[][SIZE]);
enum CHARACTERS{exclamation = '!', hashtag = '#', percentage = '%', doubleQuote = '"', dolarSign = '$', ampersand = '&'};
struct st{
char row;
int col;
char direction;
int noOfMoves;
int score;
};
main.cpp
#include "prgm.h"
int main()
{
int board[SIZE][SIZE];
struct st s;
s.noOfMoves=1;
do{
gameheader();
createBoard(board);
displayBoard(board);
s.noOfMoves+=1;
}while(s.noOfMoves<=10);
return 0;
}
func.cpp
void gameheader()
{
cout << " ";
cout << "----------------------------------------------------------- ";
cout << " W e l c o m e t o 1 0 3 0 C r u s h ";
cout << endl;
cout << "This program will randomly initialize your game board using ";
cout << "a set of 6 characters (!, " << static_cast<char>(doubleQuote) << ", #, $, %, &) that a player will ";
cout << "then attempt to match a combination of 3 or more characters ";
cout << "to gain points in a finite set of moves or time duration. ";
cout << "----------------------------------------------------------- ";
cout << endl;
return;
}
void createBoard(int board[][SIZE])
{
int i, j, symbol;
for(i = 0; i < SIZE; i++)
{
for(j = 0; j < SIZE; j++)
{
symbol = rand() % 6;
switch(symbol)
{
case 0:
board[i][j] = exclamation;
break;
case 1:
board[i][j] = hashtag;
break;
case 2:
board[i][j] = percentage;
break;
case 3:
board[i][j] = doubleQuote;
break;
case 4:
board[i][j] = dolarSign;
break;
case 5:
board[i][j] = ampersand;
break;
default:
cout << "ERROR!" << endl;
}
}
}
return;
}
void displayBoard(int board[][SIZE])
{
int i, j;
int letter = 'A';
cout << "Initializing board..." << endl;
cout << " ";
cout << " 1 2 3 4 5 6 7 8 9 " << endl;
cout << " +-------------------+" << endl;
for(i = 0; i < SIZE; i++)
{
cout << static_cast<char>(letter) << " | ";
for(j = 0; j < SIZE; j++)
{
cout << static_cast<char>(board[i][j]) << " ";
}
cout << "| ";
letter++;
}
cout << " +-------------------+" << endl;
cout << " Moves: 0 Score: 0" << endl;
cout << " ";
return;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.