Question

R-S-P Requirement: - write one C++ program that mimics the Rock-Scissor-Paper game. This program will ask...

R-S-P Requirement:

- write one C++ program that mimics the Rock-Scissor-Paper game. This program will ask user to enter an input (out of R-S-P), and the computer will randomly pick one and print out the result (user wins / computer wins).

- User's input along with computer's random pick will be encoded in the following format:

-- user's rock: 10

-- user's scissor:          20

-- user's paper:            30

-- comp's rock:            1

-- comp's scissor:        2

-- comp's paper:          3

Then add user's input and computer's pick toghter to for a summation, and use switch statement over the summation to tell the result. For example, 12 (10 + 2: u's rock and c's scissor, user wins), 33 (30 + 3: u's paper and c's paper, draw).

T-T-T Requirement:

- write one C++ program that uses two dimensional array to allow two players to play tic-tac-toe. The program asks for moves alternately from player X and player O to enter a number (1 - 9) they wish to mark to play the game.The program displays the game positions as follows:

1 2 3

4 5 6

7 8 9

After each move, the program displays the changed board, for example:

X X O

4 5 6

O 8 9

- the program should have following functions:

-- a function to take in user's input.

-- a function to print out the board and move.

-- a function to tell if game is over.

-- a function to tell who wins (or draw.)

Note: These have to be combined in a single program.

Homework Answers

Answer #1

RPS Program:

/* C++ program that simulates Rock, Paper & Scissors Game */

#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

//Getting numeric value from char input
int getNumericVal(char ch)
{
int ip;

//Assigning numeric input
switch(ch)
{
case 'R':
case 'r': ip = 10; break;

case 'P':
case 'p': ip = 30; break;

case 's':
case 'S': ip = 20; break;

default:
ip = -1; cout << "\n Invalid input \n"; break;
}

return ip;
}

//User selection
char Selection()
{
char userIP;
int ip;

//Loop till user enters valid input
do
{
//Reading input from user
cout << "\n\n Please enter your move \n ( 'R' - Rock \t 'P' - Paper \t 'S' - Scissors) : ";
cin >> userIP;

ip = getNumericVal(userIP);

}while(ip < 10 || ip > 30);

//Returning input
return userIP;
}

//Function that plays a random move
int Computer()
{
//For handling computer move
int computerMove;

//Generating a random move
computerMove = rand() % 3 + 1;

//Assigning numeric input
switch(computerMove)
{
case 1: cout << "\n Computer's move is Rock \n"; break;

case 2: cout << "\n Computer's move is Scissors \n"; break;

case 3: cout << "\n Computer's move is Paper \n"; break;
}

return computerMove;
}

//Function that finds the winner of round
void Winner(char playSel, int compSel)
{
int player1, total;

//Getting numeric values
player1 = getNumericVal(playSel);

//Calculating total
total = player1 + compSel;

//Checking total
switch(total)
{
case 11: cout << "\n Draw \n"; break;
case 12: cout << "\n User Wins \n"; break;
case 13: cout << "\n Computer Wins \n"; break;
case 21: cout << "\n Computer Wins \n"; break;
case 22: cout << "\n Draw \n"; break;
case 23: cout << "\n User Wins \n"; break;
case 31: cout << "\n User \n"; break;
case 32: cout << "\n Computer Wins \n"; break;
case 33: cout << "\n Draw \n"; break;
}
}

//Main program
int main()
{
char playerSelection;
int computerSelection;

srand (time(NULL));

//Reading user selection
playerSelection = Selection();

//Reading computer selection
computerSelection = Computer();

//Playing a round
Winner(playerSelection, computerSelection);

return 0;
}

Sample Run:

_______________________________________________________________________________________________

TTT Game:

#include <iostream>
#include <time.h>
#include <random>

using namespace std;

// Initializing board
void initialize(char board [3][3])
{
int i, j, k=1;

//Outer loop for rows
for(i=0; i<3; i++)
{
//Inner loop for columns
for(j=0; j<3; j++)
{
           board[i][j] = char(k + 48);
           k++;
}
}
}

// print prints the board in nice format
void print_board (char board [3][3])
{
int i, j;

cout << "\n\n The board is: \n";

cout << "\n |---|---|---| \n";

//Outer loop for rows
for(i=0; i<3; i++)
{
//Inner loop for columns
for(j=0; j<3; j++)
{
//Printing character
cout << " | " << board[i][j];
}

cout << " |";

//Printing footer
cout << "\n |---|---|---| \n";
}
}

//Function that checks whether board is full or not
int isBoardFull(char board[3][3])
{
int i, j;

//Outer loop for rows
for(i=0; i<3; i++)
{
//Inner loop for columns
for(j=0; j<3; j++)
{
//Board is not Full
           if(board[i][j] != 'X' || board[i][j] != 'O')
return 0;
}
}

//Board Full
return 1;
}

// win returns true if the given player has won on the
// given board, else it returns false
int win (char board [3][3], char player)
{
return (board[0][0] == player && board[0][1] == player && board[0][2] == player) ||
       (board[1][0] == player && board[1][1] == player && board[1][2] == player) ||
       (board[2][0] == player && board[2][1] == player && board[2][2] == player) ||
       (board[0][0] == player && board[1][0] == player && board[2][0] == player) ||
       (board[0][1] == player && board[1][1] == player && board[2][1] == player) ||
       (board[0][2] == player && board[1][2] == player && board[2][2] == player) ||
       (board[0][0] == player && board[1][1] == player && board[2][2] == player) ||
       (board[0][2] == player && board[1][1] == player && board[2][0] == player);

}

//Function that checks for winner
int check_board(char board[3][3], char player1, char player2)
{
//Checking for winning of player
if(win(board, 'X') == 1 && win(board, 'O') == 1)
{
return 2;
}
//Checking for winning of player
else if(win(board, 'X') == 1)
{
return 0;
}
//Checking for winning of computer
else if(win(board, 'O') == 1)
{
return 1;
}
//Tie
else
{
return -1;
}
}

//Function that plays the game
void playGame(char board[3][3], char player1, char player2)
{
int row, col;
int winner;

//Loop till board is full
while(isBoardFull(board) != 1)
{
//Player turn
while(1)
{
//Reading positions from user
cout << "\n Enter the row, column pair for inserting " << player1 << " : ";
cin >> row >> col;

//Making suitable for array indexing
row = row - 1;
col = col - 1;

//Checking for empty position
if(board[row][col] != 'X' || board[row][col] != 'O')
               break;
           else
               cout << "\n Invalid choice... Try again!! \n ";
}

//Storing in array
board[row][col] = player1;

//Printing board
print_board(board);

//Finding winner
winner = check_board(board, player1, player2);

//If either of winner is found
if(winner >= 0 && winner <= 2)
{
//Printing winner
switch(winner)
{
//Displaying winner
case 0: cout << "\n Player 1 won the game... \n"; break;
case 1: cout << "\n Player 2 won the game... \n"; break;
case 2: cout << "\n Game Tie ... \n"; break;
}

return;
}

//Player turn
while(1)
{
//Reading positions from user
cout << "\n Enter the row, column pair for inserting " << player2 << " : ";
cin >> row >> col;

//Making suitable for array indexing
row = row - 1;
col = col - 1;

//Checking for empty position
if(board[row][col] != 'X' || board[row][col] != 'O')
               break;
           else
               cout << "\n Invalid choice... Try again!! \n ";
}

//Storing in array
board[row][col] = player2;

//Printing board
print_board(board);

//Finding winner
winner = check_board(board, player1, player2);

//If either of winner is found
if(winner >= 0 && winner <= 2)
{
//Printing winner
switch(winner)
{
//Displaying winner
case 0: cout << "\n Player 1 won the game... \n"; break;
case 1: cout << "\n Player 2 won the game... \n"; break;
case 2: cout << "\n Game Tie ... \n"; break;
}

return;
}
}
}

//Main function
int main()
{
//Board
char board[3][3];

//Player characters
char player1Character='X', player2Character='O';

//Initializing random function
srand(time(NULL));

cout << "\n\n\t\t TIC TAC TOE \n\n";
cout << "\n\t\t WELCOME \n\n";
cout << "\n Instructions: The object of Tic Tac Toe is to get three in a row. \n\n";

//Initializing board
initialize(board);

//Printing board
print_board(board);

//Playing game
playGame(board, player1Character, player2Character);

return 0;
}

Sample Run:

Know the answer?
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
Similar Questions
(Game: scissor, rock, paper) Write a program that plays the popular scissor-rockpaper game. (A scissor can...
(Game: scissor, rock, paper) Write a program that plays the popular scissor-rockpaper game. (A scissor can cut a paper, a rock can knock a scissor, and a paper can wrap a rock.) The program randomly generates a number 0, 1, or 2 representing scissor, rock, and paper. The program prompts the user to enter a number 0, 1, or 2 and displays a message indicating whether the user or the computer wins, loses, or draws.
Using Java, write a program that allows the user to play the Rock-Paper-Scissors game against the...
Using Java, write a program that allows the user to play the Rock-Paper-Scissors game against the computer through a user interface. The user will choose to throw Rock, Paper or Scissors and the computer will randomly select between the two. In the game, Rock beats Scissors, Scissors beats Paper, and Paper beats Rock. The program should then reveal the computer's choice and print a statement indicating if the user won, the computer won, or if it was a tie. Allow...
Write a program that allows two players to play a game of tic-tac-toe. Use a twodimensional...
Write a program that allows two players to play a game of tic-tac-toe. Use a twodimensional char array with three rows and three columns as the game board. Each element in the array should be initialized with an asterisk (*). The program should run a loop that: • Displays the contents of the board array. • Allows player 1 to select a location on the board for an X. The program should ask the user to enter the row and...
in c++ : Problem Domain: The classic TicTacToe game alternatively place Xs and Os on a...
in c++ : Problem Domain: The classic TicTacToe game alternatively place Xs and Os on a 3x3 grid. The winner is the first player to place 3 consecutive marks in a horizontal, vertical or diagonal row. Understanding the Problem: In this assignment, you will: 1) Create a tic tac toe board data structure. 2) Check for game over. 3) Clear the tic tac toe game for each game. 4) Display the tic tac toe board to screen. 5) Simulate playing...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
   Dice Game – Accumulator Pattern and Conditionals (40 pts) IN PYTHON Write a program dicegame.py...
   Dice Game – Accumulator Pattern and Conditionals (40 pts) IN PYTHON Write a program dicegame.py that has the following functions in the following order: in python Write a function roll that takes an int n and returns a random number between 1 and n inclusive. Note: This function represents a n sided die so it should produce pseudo random numbers. You can accomplish this using the random module built into python.         (3 pts) Write a function scoreRound that...
I am making a game like Rock Paper Scissors called fire water stick where the rules...
I am making a game like Rock Paper Scissors called fire water stick where the rules are Stick beats Water by floating on top of it Water beats Fire by putting it out Fire beats Stick by burning it The TODOS are as follows: TODO 1: Declare the instance variables of the class. Instance variables are private variables that keep the state of the game. The recommended instance variables are: 1. 2. 3. 4. 5. 6. A variable, “rand” that...