Question

Page 1 of 3 (PRO) Project Assignment Instructions Last Charged: 6/1/2020 Read and follow the directions below carefully and p
ceBOOK . . . You can choose the game of your choice. Below is a list of possible games. CHOOSE ONE from the list provided. Do
number to pick the story or have user pick the story. Make it as simple or complex as you like! . (Easy) Magic 8-ball Prompt
. (Easy to Moderate) Palindrome: o Prompt the user for a word or phrase. Read in the word or phrase (its up to you if you wa
num For example, to generate a random number between 0 and 9 (aka. 10 possible numbers), you will need the following code: #i
5. You must include comments! 6. It is imperative that your output looks professionall That is, no typos! Use capital letters

in c++ please
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Screenshot

#include <iostream> #include<string> using namespace std; Microsoft Visual Studio Debug Console 1436 Gamee Menu #define ROWS

Program

#include <iostream>
#include<string>
using namespace std;

#define ROWS 3
#define COLS 3

//Function prototypes
int getMenu();
void playGuessGame();
void playTicTacToe();
void getChoice(char[ROWS][COLS], bool);
void fillBoard(char[ROWS][COLS]);
void showBoard(char[ROWS][COLS]);
bool gameOver(char[ROWS][COLS]);
void checkPalindrome();
bool checkPalindrome(string);
string reverse(string);
string removeOther(string);

int main()
{

    //Get option
   int opt = getMenu();
   //Loop until user prefer
   while (opt != 0) {
       if (opt == 1) {
           playGuessGame();
       }
       else if (opt == 2) {
           playTicTacToe();
       }
       else if (opt == 3) {
           checkPalindrome();
       }
       cout << endl;
       opt = getMenu();
   }
}

/*
   Function: getMenu
   Param In: None
   Param Out: opt, user choice;
   Description: Display 3 different games and exit option
                Read user choice , check correctness and return option
*/
int getMenu() {
   int opt;
   cout << "1436 Gamee Menu\n";
   cout << "----------------\n";
   cout << "1. HI/LO Guessing \n2. Tic Tac Toe\n3. Palindrome Checker\n0. Quit\n";
   cout << "Enter your choice: ";
   cin >> opt;
   while (opt < 0 || opt>3) {
       cout << "Error!!!Option should be 0-3!!\n";
       cout << "Enter your choice: ";
       cin >> opt;
   }
   return opt;
}

/*
Function: playGuessGame
ParamIn: none
Param Out: none
Description: Prompt for user a number 1-100
               If it correct congradulate
               Otherwise mension low or high
*/
void playGuessGame() {
   int num, secretNum = rand() % 100 + 1;
   cout << "\nGuess the secret number between 1-100: ";
   cin >> num;
   if (num == secretNum) {
       cout << "CONGRADULATIONS!!!YOUR GUESS IS CORRECT!!!\n";
   }
   else if (num <secretNum) {
       cout << "OOPS!!!YOUR GUESS IS LOW!!!\n";
   }
   else {
       cout << "OOPS!!!YOUR GUESS IS HIGH!!!\n";
   }
}

/*
Function: playTicTacToe
Param In: none
ParamOut: none
Description: Create a 3*3 charcter board
               Loop until a winner or tie happen
*/
void playTicTacToe() {
   char board[3][3];
   bool playerToggle = false;
   fillBoard(board);
   showBoard(board);

   while (!gameOver(board))
   {
       getChoice(board, playerToggle);
       showBoard(board);
       playerToggle = !playerToggle;
   }
}

/*
Function: fillBoard
ParamIn: board
ParamOut: none
Description: Fill the array with stars
*/
void fillBoard(char gameboard[ROWS][COLS]) {
   for (int i = 0; i < ROWS; i++) {
       for (int j = 0; j < COLS; j++) {
           gameboard[i][j] = '*';
       }
   }
}
/*
Function: showBoard
ParamIn: board
ParamOut: None
Description: Display board details
*/
void showBoard(char board[ROWS][COLS]) {
   cout << " ";
   for (int i = 0; i < COLS; i++) {
       cout << i + 1 << " ";
   }
   cout << endl;

   for (int i = 0; i < ROWS; i++) {
       cout << i + 1 << " ";
       for (int j = 0; j < COLS; j++) {
           cout << board[i][j] << " ";
       }
       cout << endl;
   }
};
/*
Function: getChoice
Paramin: board and bool value
ParamOut: None
Desription: Get player choice row and col
Check availability and add into board
*/
void getChoice(char gameBoard[ROWS][COLS], bool playerToggle)
{
   string player;
   char c;
   int row, col;

   if (playerToggle == false) {
       player = "player 1";
       c = 'X';
   }
   else {
       player = "player 2";
       c = 'O';
   }

   do
   {
       do
       {
           cout << player << ", " << "Row: ";
           cin >> row;

       } while (!(row >= 1 && row <= 3));

       do
       {
           cout << player << ", " << "Column: ";
           cin >> col;
       } while (!(col >= 1 && col <= 3));

   } while ((gameBoard[row - 1][col - 1] == 'X' || gameBoard[row - 1][col - 1] == 'O'));
   gameBoard[row - 1][col - 1] = c;
}
/*
Function: gameOver
Paramin: board
Param out: winner
Description: check the rowwise or column sie or diagonal match of player character
              If match foundreturn true otherwise false
*/
bool gameOver(char gameBoard[ROWS][COLS])
{
   bool gameOver = false;
   bool asterisk = false;
   bool xWin = false;
   bool oWin = false;
   bool Tie = false;
   //xwin check
   if (gameBoard[0][0] == 'X' && gameBoard[0][1] == 'X' && gameBoard[0][2] == 'X')
   {
       xWin = true;
   }
   else if (gameBoard[1][0] == 'X' && gameBoard[1][1] == 'X' && gameBoard[1][2] == 'X') {
       xWin = true;
   }
   else if (gameBoard[2][0] == 'X' && gameBoard[2][1] == 'X' && gameBoard[2][2] == 'X') {
       xWin = true;
   }
   else if (gameBoard[0][0] == 'X' && gameBoard[1][0] == 'X' && gameBoard[2][0] == 'X') {
       xWin = true;
   }
   else if (gameBoard[0][1] == 'X' && gameBoard[1][1] == 'X' && gameBoard[2][1] == 'X') {
       xWin = true;
   }
   else if (gameBoard[0][2] == 'X' && gameBoard[1][2] == 'X' && gameBoard[2][2] == 'X') {
       xWin = true;
   }
   else if (gameBoard[0][0] == 'X' && gameBoard[1][1] == 'X' && gameBoard[2][2] == 'X') {
       xWin = true;
   }
   else if (gameBoard[0][2] == 'X' && gameBoard[1][1] == 'X' && gameBoard[2][0] == 'X') {
       xWin = true;
   }
   //owin check
   if (gameBoard[0][0] == 'O' && gameBoard[0][1] == 'O' && gameBoard[0][2] == 'O')
   {
       oWin = true;
   }
   else if (gameBoard[1][0] == 'O' && gameBoard[1][1] == 'O' && gameBoard[1][2] == 'O') {
       oWin = true;
   }
   else if (gameBoard[2][0] == 'O' && gameBoard[2][1] == 'O' && gameBoard[2][2] == 'O') {
       oWin = true;
       cout << "Player 2 wins!";
   }
   else if (gameBoard[0][0] == 'O' && gameBoard[1][0] == 'O' && gameBoard[2][0] == 'O') {
       oWin = true;
   }
   else if (gameBoard[0][1] == 'O' && gameBoard[1][1] == 'O' && gameBoard[2][1] == 'O') {
       oWin = true;
   }
   else if (gameBoard[0][2] == 'O' && gameBoard[1][2] == 'O' && gameBoard[2][2] == 'O') {
       oWin = true;
   }
   else if (gameBoard[0][0] == 'O' && gameBoard[1][1] == 'O' && gameBoard[2][2] == 'O') {
       oWin = true;
   }
   else if (gameBoard[0][2] == 'O' && gameBoard[1][1] == 'O' && gameBoard[2][0] == 'O') {
       oWin = true;
   }
   if (xWin == true) {
       cout << "Player 1 wins!";
   }
   if (oWin == true) {
       cout << "Player 2 wins!";
   }

   else // Otherwise both are tie!!!
   {

       for (int i = 0; i < ROWS; i++) {
           for (int j = 0; j < COLS; j++) {
               if (gameBoard[i][j] == '*') {
                   asterisk = true;
               }
           }
       }
       Tie = !asterisk;
       if (Tie) {
           cout << "Tie!";
       }
   }
   gameOver = Tie || xWin || oWin;
   return gameOver;
}

/*
Function: checkPalindrome
ParamIn: none
ParamOut: None
Description: Prompt for a word or phrase
               Display palindrome or not
*/
void checkPalindrome() {
   string word;
   cin.ignore();
   cout << "\n a word or phrase to check palindrome(or q to quit): ";
   getline(cin, word);
   //If palindrome
   if (checkPalindrome(word)) {
       cout << word << " is palindrome!!" << endl;
   }
   //Otherwise
   else {
       cout << word << " is not palindrome!!" << endl;
   }
}
/*
   Function to check palindrome
   Param In: word, string to check
   Param Out: True/False
*/
bool checkPalindrome(string word) {
   if (removeOther(word) == removeOther(reverse(word))) {
       return true;
   }
   return false;
}
/*
   Function to reverse a string
   Param In: word, String to reverse
   Param Out: reversed string
*/
string reverse(string word) {
   string reverseString = "";
   for (int i = word.length() - 1; i >= 0; i--) {
       reverseString += word[i];
   }
   return reverseString;
}
/*
Function: removeOther
Param In: Stringto rempve
ParamOut: removed string
Description: Function to remove all characters other than letters and numbers
and change all alphabets into lowercase
*/
string removeOther(string word) {
   string temp = "";
   for (int i = 0; i < word.length(); i++) {
       if (isalpha(word[i])) {
           temp += tolower(word[i]);
       }
       else if (isdigit(word[i])) {
           temp += tolower(word[i]);
       }
   }
   return temp;
}

Output

1436 Gamee Menu
----------------
1. HI/LO Guessing
2. Tic Tac Toe
3. Palindrome Checker
0. Quit
Enter your choice: 1

Guess the secret number between 1-100: 24
OOPS!!!YOUR GUESS IS LOW!!!

1436 Gamee Menu
----------------
1. HI/LO Guessing
2. Tic Tac Toe
3. Palindrome Checker
0. Quit
Enter your choice: 2
1 2 3
1 * * *
2 * * *
3 * * *
player 1, Row: 1
player 1, Column: 1
1 2 3
1 X * *
2 * * *
3 * * *
player 2, Row: 2
player 2, Column: 1
1 2 3
1 X * *
2 O * *
3 * * *
player 1, Row: 1
player 1, Column: 2
1 2 3
1 X X *
2 O * *
3 * * *
player 2, Row: 2
player 2, Column: 2
1 2 3
1 X X *
2 O O *
3 * * *
player 1, Row: 1
player 1, Column: 3
1 2 3
1 X X X
2 O O *
3 * * *
Player 1 wins!
1436 Gamee Menu
----------------
1. HI/LO Guessing
2. Tic Tac Toe
3. Palindrome Checker
0. Quit
Enter your choice: 3

a word or phrase to check palindrome(or q to quit): Malayalam
Malayalam is palindrome!!

1436 Gamee Menu
----------------
1. HI/LO Guessing
2. Tic Tac Toe
3. Palindrome Checker
0. Quit
Enter your choice: 4
Error!!!Option should be 0-3!!
Enter your choice: 0

Add a comment
Know the answer?
Add Answer to:
in c++ please Page 1 of 3 (PRO) Project Assignment Instructions Last Charged: 6/1/2020 Read and...
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
  • CPT 180 Chapter 2 Assignment 3 Directions Using the following guidelines, create a python program. 1....

    CPT 180 Chapter 2 Assignment 3 Directions Using the following guidelines, create a python program. 1. Create a program named printRandomNumbers that gets input from the user and then produces a list of random numbers. 2. Import the random module 3. Add three comment lines at the top of the program that contain: a. Program Name b. Program Description c. Programmer's Name (You) 4. Prompt the user for the following: a. Number of random numbers required b. Lower limit of...

  • Help please, write code in c++. The assignment is about making a hangman game. Instructions: This...

    Help please, write code in c++. The assignment is about making a hangman game. Instructions: This program is part 1 of a larger program. Eventually, it will be a complete Hangman game. For this part, the program will Prompt the user for a game number, Read a specific word from a file, Loop through and display each stage of the hangman character I recommend using a counter while loop and letting the counter be the number of wrong guesses. This...

  • The purpose of this assignment is to get experience with an array, do while loop and...

    The purpose of this assignment is to get experience with an array, do while loop and read and write file operations. Your goal is to create a program that reads the exam.txt file with 10 scores. After that, the user can select from a 4 choice menu that handles the user’s choices as described in the details below. The program should display the menu until the user selects the menu option quit. The project requirements: It is an important part...

  • FUNCTIONS In this assignment, you will revisit reading data from a file, and use that data...

    FUNCTIONS In this assignment, you will revisit reading data from a file, and use that data as arguments (parameters) for a number of functions you will write. You will need to: Write and test a function square_each(nums) Where nums is a (Python) list of numbers. It modifies the list nums by squaring each entry and replacing its original value. You must modify the parameter, a return will not be allowed! Write and test a function sum_list(nums) Where nums is a...

  • COP2221 - Intermediate C++ Programming Module #6 Assignment One 20 points This assignment refers to Learning...

    COP2221 - Intermediate C++ Programming Module #6 Assignment One 20 points This assignment refers to Learning Outcome #2: Create and utilize arrays to store lists of related data Programming Problem You have been hired to create a C++ program to simulate the lottery. Your program allows users to see how often their lottery number choices match a randomly generated set of numbers used to pick lottery winners. The company, "How Lucky Are You?, Inc." wants a modular, professional and user-friendly...

  • Please help with this exercises in c++ language Thanks Lab Activity #5-More Functions/FILES Exercise# Write a...

    Please help with this exercises in c++ language Thanks Lab Activity #5-More Functions/FILES Exercise# Write a program that will generate 1000 random numbers (all of which are between 10 and 20) and then write all of the even numbers (from those 1000 numbers) to a new file called "myEvenRandoms.txt Exercise #2 (DO ONLY IF WE COVERED ARRAYS OF CHARACT Write a program that will ask the user to enter a sentence. Then create a separate function that will count the...

  • Write a program in c++ that generates a 100 random numbers between 1 and 1000 and...

    Write a program in c++ that generates a 100 random numbers between 1 and 1000 and writes them to a data file called "randNum.dat". Then re-open the file, read the 100 numbers, print them to the screen, and find and print the minimum and maximum values. Specifications: If your output or input file fails to open correctly, print an error message that either states: Output file failed to open Input file failed to open depending on whether the output or...

  • For this assignment you are to create two programming projects. The first should have a project...

    For this assignment you are to create two programming projects. The first should have a project directory with Lab02-2WriteNumbers and the second should have a project directory with Lab02-2ReadNumbers. Lab02-03WriteNumbers – Write a program that asks the user to enter five numbers. Use a double data type to hold the numbers. The program should create a file and save all five numbers to the file. Enter a number (1 of 5): 45 Enter a number (2 0f 5): 23 Enter...

  • Please post screenshots only File Tools View CMSC 140 Common Projects Spring 2019(1) E - 3...

    Please post screenshots only File Tools View CMSC 140 Common Projects Spring 2019(1) E - 3 x Project Description The Powerball game consists of 5 white balls and a red Powerball. The white balls represent a number in the range of 1 to 69. The red Powerball represents a number in the range of 1 to 26. To play the game, you need to pick a number for each ball in the range mentioned earlier. The prize of winning the...

  • COSC 1437 C++ Project Assignment 2 Poll Summary Utilize a dynamic array of structure to summarize...

    COSC 1437 C++ Project Assignment 2 Poll Summary Utilize a dynamic array of structure to summarize votes for a local election Specification: Write a program that keeps track the votes that each candidate received in a local election. The program should output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. To solve this problem, you will use one dynamic...

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
ADVERTISEMENT