Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cctype>
- #include <iomanip>
- #include <ctime>
- // constants
- const int MAXLEN = 128;
- const int MINLEN = 127;
- const int MAX = 12;
- const int MIN = 1;
- const int RAND = 4;
- // Enumerators
- enum Commands {PLAY = 'P', QUIT = 'Q'};
- // Typedef
- typedef char BUFFER[MAXLEN];
- // structs
- struct Player {
- int win;
- int loss;
- int total;
- };
- // Subroutine prototypes
- // get integer in range 1- 12
- int getIntInRange(int min, int max);
- // Dynamically allocate a 2D array to fill with randoms
- int** allocateIntArray(int &size);
- // Dynamically allocate an array for user to enter guess's
- int* allocUserIntArray(int &size);
- // fill player array
- void fillUserArray(int* arr, int size);
- // output user array
- void outputUserArray(int* arr, int size);
- // fill array with random values between 1 and 4
- void fillIntArray(int** arr, int size);
- // Output the array
- void outputIntArray(int** arr, int size);
- // The keep playing loop
- void keepPlaying();
- // Handles the bulk of the decisions,
- // created this function so keepPlaying didn't get too big
- void arrayHandler();
- // check to see if user guessed correctly
- void checkForMatch(int** arr1, int* arr2, int size);
- // keep track of player score
- void updateScore(int &counter, int &win, int &loss);
- // Next level ASCII art introduction
- void displayOverview();
- /*
- Init main routine
- */
- int main(){
- std::srand(time(nullptr));
- displayOverview();
- keepPlaying();
- // terminate
- std::cout << "\nterminating gracefully\n" << std::flush;
- return 0;
- }
- /*
- Looping function, will continue until result is equal
- QUIT.
- */
- void keepPlaying(){
- bool loop = false;
- BUFFER input;
- char result;
- do{
- std::cout << "\n\nType (P) to play or (Q) to quit: ";
- std::cin.getline(input, MAXLEN);
- for (int i = 0; i < MAXLEN - MINLEN; i++){
- result = std::toupper(input[i]);
- }
- switch (result){
- case PLAY:
- std::cout << "Let the game begin!!\n";
- arrayHandler();
- break;
- case QUIT:
- loop = true;
- break;
- default:
- std::cout << "\nERROR [ " << result << " ] INVALID";
- }
- } while (!(loop));
- }
- /*
- Made the function so keepPlaying doesnt
- get too convoluted. Handles passing all arrays
- to their corresponding functions
- */
- void arrayHandler(){
- int size;
- int** array;
- int* userArr;
- size = getIntInRange(MIN, MAX);
- array = allocateIntArray(size);
- if (array != nullptr){
- fillIntArray(array, size);
- userArr = allocUserIntArray(size);
- if (userArr != nullptr){
- fillUserArray(userArr, size);
- outputUserArray(userArr, size);
- outputIntArray(array, size);
- checkForMatch(array, userArr, size);
- delete [] array;
- delete [] userArr;
- }
- }
- }
- /*
- Function to get integers for both ranges 1-12
- and 1-4
- */
- int getIntInRange(int min, int max){
- int userInput;
- bool isValid = false;
- BUFFER input;
- while (!(isValid)){
- std::cout << "\nEnter a number between "
- << min << " and " << max << ": ";
- std::cin.getline(input, MAXLEN);
- userInput = atoi(input);
- if (userInput < min || userInput > max){
- std::cout << "\nERROR [ " << userInput << " ] INVALID\n";
- }
- else{
- isValid = true;
- }
- }
- return userInput;
- }
- /*
- Create 2d array dynamically to be filled randomly
- */
- int** allocateIntArray(int &size){
- int** array = nullptr;
- do{
- array = new int* [size];
- for (int i = 0; i < size; i++){
- array[i] = new int[size];
- }
- if (array == nullptr){
- std::cout << "Allocation failed\n";
- }
- } while (array == nullptr);
- return array;
- }
- /*
- Dynamically allocate standard array to hold
- user guess's
- */
- int* allocUserIntArray(int &size){
- int* inputArr = nullptr;
- do {
- inputArr = new int[size];
- if (inputArr == nullptr){
- std::cout << "Allocation failed\n";
- }
- } while (inputArr == nullptr);
- return inputArr;
- }
- /*
- fill player array
- */
- void fillUserArray(int arr[], int size){
- if (arr == nullptr){
- std::cout << "Trying to fill null array\n";
- }
- std::cout << "\nPlease enter " << size << " numbers\n";
- for (int i = 0; i < size; i++){
- arr[i] = getIntInRange(MIN, RAND);
- }
- }
- /*
- output user array
- */
- void outputUserArray(int* arr, int size){
- if (arr == nullptr){
- std::cout << "Trying to output null array\n";
- }
- std::cout << "\n Your selections:\n" << std::endl;
- for (int i = 0; i < size; i++){
- std::cout << std::setw(4) << arr[i];
- }
- std::cout << std::endl;
- }
- /*
- Fill the newly allocated array with randoms
- between 1 and 4
- */
- void fillIntArray(int** arr, int size){
- if (arr == nullptr){
- std::cout << "Trying to fill null array\n";
- }
- for (int i = 0; i < size; i++){
- for (int j = 0; j < size; j++){
- arr[i][j] = (rand() % RAND + 1);
- }
- }
- }
- /*
- Output the newly allocated 2d array in a grid
- */
- void outputIntArray(int** arr, int size){
- if (arr == nullptr){
- std::cout << "Trying to output null array\n";
- }
- for (int i = 0; i < size; i++){
- std::cout << std::endl;
- for (int j = 0; j < size; j++){
- std::cout << std::setw(2) << " | " << arr[i][j];
- }
- std::cout << " |";
- }
- std::cout << std::endl;
- }
- /*
- check to see if user guessed correctly
- */
- void checkForMatch(int** arr1, int* arr2, int size){
- int d1Match = 0;
- int d2Match = 0;
- int rMatch = 0;
- int cMatch = 0;
- // checking for row matches
- for (int row = 0; row < size; row++){
- if (arr2[row] == arr1[0][row]){
- rMatch++;
- }
- }
- // check for column matches
- for(int col = 0; col < size; col++){
- if (arr2[col] == arr1[col][0]){
- cMatch++;
- }
- }
- // check for diagonal top to bottom
- for (int i = 0; i < size; i++){
- if (arr2[i] == arr1[0][0]){
- d1Match++;
- }
- }
- // check for diagonal bottom to top?
- for(int i = 0; i < size; i++){
- if (arr2[size - i - 1] == arr1[size - 1][0]){
- d2Match++;
- }
- }
- if (rMatch == size){
- std::cout << "\n You matched on a row\n";
- }if (cMatch == size){
- std::cout << "\n You matched on a column\n";
- }if (d1Match == size){
- std::cout << "\n You matched top to bottom diagonally\n";
- }if (d2Match == size){
- std::cout << "\n You matched bottom to top diagonally\n";
- }else if (rMatch != size && cMatch != size && d1Match != size && d2Match != size){
- std::cout << "\n No matches detected: You Lose!!\n";
- }
- }
- /*
- Next Level ASCII Art and description of program
- */
- void displayOverview(){
- std::system("clear");
- std::cout << "Welcome to the lotto-bingo game\n"
- << "You are going to guess some numbers\n"
- << "Between 1 and 4, and I'm going to generate\n"
- << "a bingo card for you. You will win if you match\n"
- << "a row or a column!"
- << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment