This is my code for the problem #include <iostream> #include <iomanip> using nam
ID: 3730887 • Letter: T
Question
This is my code for the problem
#include <iostream>
#include <iomanip>
using namespace std;
void singleplayer(int choose);
void multiplayer(int p1, int p2);
int computer, n,player1, player2,game;
char response;
int main()
{
while(response='Y'||response=='y')
{
cout << "Do you want to play single player game or multiplayer game?(Enter s for singleplayer and m for multiplayer) " << endl;
fflush(stdin);
cin >> game;
if (game == 's' || game == 'S')
{
cout << "Please enter 1 for rock, 2 for paper or 3 for scissors " << endl;
cin >> player1;
singleplayer(player1);
cout << "Do you want play again?" << endl;
fflush(stdin);
cin >> response;
}
if (game == 'm' || game == 'M')
{
cout << "Player1 please enter 1 for rock, 2 for paper or 3 for scissors " << endl;
cin >> player1;
cout << "Player2 please enter 1 for rock, 2 for paper or 3 for scissors " << endl;
cin >> player2;
multiplayer(player1, player2);
cout << "Do you want play again?" << endl;
fflush(stdin);
cin >> response;
}
}
return 0;
}
void singleplayer(int choose)
{
while (1){
computer = rand() % 3 + 1;
if (choose = 1 && computer == 3)
{
cout << "You win! The computer chose scissors for this game. " << endl;
break;
}
if (choose = 1 && computer == 2)
{
cout << "You lose, The computer chose the scissors for this game. " << endl;
break;
}
if (choose = 1 && computer == 1)
{
cout << "The computer also chose the rock for this game. " << endl;
}
if (choose = 2 && computer == 1)
{
cout << "You win, The computer chose the paper for this game." << endl;
break;
}
if (choose = 2 && computer == 3)
{
cout << "You lose, The computer chose the scissors for this game." << endl;
break;
}
if (choose = 2 && computer == 2)
{
cout << "The computer also chose the paper for this game. " << endl;
}
if (choose = 3 && computer == 1)
{
cout << "You lose, The computer chose the rock for this game." << endl;
break;
}
if (choose = 3 && computer == 2)
{
cout << "You win, The computer chose the paper for this game." << endl;
break;
}
if (choose = 3 && computer == 3)
{
cout << "The computer also chose the paper for this game. " << endl;
}
while (choose == computer)
{
cout << "Since you had same choice with the computer, please enter the 1 for rock, 2 for paper or 3 for scissors again " << endl;
cin >> choose;
}
}
}
void multiplayer(int p1, int p2)
{
while (1){
if (p1 = 1 && p2 == 3)
{
cout << "The player1 win! The player2 chose the scissors for this game. The player1 chose rock for this game. " << endl;
break;
}
if (p1 = 1 && p2 == 2)
{
cout << "The player2 win, The player2 chose the paper for this game.The player1 chose the rock for this game " << endl;
break;
}
if (p1 = 1 && p2 == 1)
{
cout << "The player1 and player2 both chose the rock for this game. " << endl;
}
if (p1 = 2 && p2 == 1)
{
cout << "The player1 win, The player2 chose the rock for this game.The player1 chose the paper for this game." << endl;
break;
}
if (p1 = 2 && p2 == 3)
{
cout << "The player1 win, The player1 chose the paper for this game.The player2 chose the scissors for this game." << endl;
break;
}
if (p1 = 2 && p2 == 2)
{
cout << "The player1 and player2 both chose the paper for this game. " << endl;
}
if (p1 = 3 && p2 == 1)
{
cout << "The player2 win, The player2 chose the rock for this game.The player1 chose the scissors for this game." << endl;
break;
}
if (p1 = 3 && p2 == 2)
{
cout << "The player1 win, The player1 chose the scissors for this game.The player2 chose the paper for this game." << endl;
break;
}
if (p1 = 3 && p2 == 3)
{
cout << "The player1 and player2 both chose the scissors for this game. " << endl;
}
while (p1 == p2)
{
cout << "Since you had same choice with eath other, please enter the 1 for rock, 2 for paper or 3 for scissors again " << endl;
cout << "Player1 please enter 1 for rock, 2 for paper or 3 for scissors " << endl;
cin >> p1;
cout << "Player2 please enter 1 for rock, 2 for paper or 3 for scissors " << endl;
cin >> p2;
}
}
}
it just never stop when I enter the s for input
What is my problem?Can you fix my code or tell me am I totally wrong and give your code to solve the problem .
Also, please help me with
If you are willing to do so, this is in one problem. thx
24. Rock, Paper, Scissors Game Write a program that lets the user play the game of Rock, Paper, Scissors against the computer The program should work as follows: 1. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. (Don't display the computer's choice yet.) The user enters his or her choice of "rock, "paper", or "scissors" at the keyboard. (You can use a menu if you prefer.) 2. 3. The computer's choice is displayed. 4. A winner is selected according to the following rules If one player chooses rock and the other player chooses scissors, then rock win:s The rock smashes the scissors.) wins. (Scissors cuts paper.) (Paper wraps rock.) If one player chooses scissors and the other player chooses paper, then scissors If one player chooses paper and the other player chooses rock, then paper wins. · If both players make the same choice, the game must be played again to determine the winner Be sure to divide the program into functions that perform each major task.Explanation / Answer
Hi.. I have added some code to your code and now its working fine.
Main.cpp
#include <iostream>
#include <iomanip>
using namespace std;
void singleplayer(int choose);
void multiplayer(int p1, int p2);
int computer, n,player1, player2;
char response,game;
int main()
{
while(response='Y'||response=='y')
{
cout << "Do you want to play single player game or multiplayer game?(Enter s for singleplayer and m for multiplayer) " << endl;
cin >> game;
if (game == 's' || game == 'S')
{
cout << "Please enter 1 for rock, 2 for paper or 3 for scissors " << endl;
cin >> player1;
singleplayer(player1);
cout << "Do you want play again?" << endl;
fflush(stdin);
cin >> response;
}
if (game == 'm' || game == 'M')
{
cout << "Player1 please enter 1 for rock, 2 for paper or 3 for scissors " << endl;
cin >> player1;
cout << "Player2 please enter 1 for rock, 2 for paper or 3 for scissors " << endl;
cin >> player2;
multiplayer(player1, player2);
cout << "Do you want play again?" << endl;
fflush(stdin);
cin >> response;
}
if(response!='y' || response!='Y'){
break;
}
}
return 0;
}
void singleplayer(int choose)
{
while (1){
computer = rand() % 3 + 1;
if (choose == 1 && computer == 3)
{
cout << "You win! The computer chose scissors for this game. " << endl;
break;
}else if (choose == 1 && computer == 2)
{
cout << "You lose, The computer chose the paper for this game. " << endl;
break;
}else if (choose == 1 && computer == 1)
{
cout << "The computer also chose the rock for this game. " << endl;
}
else if (choose == 2 && computer == 1)
{
cout << "You win, The computer chose the rock for this game." << endl;
break;
}else if (choose == 2 && computer == 3)
{
cout << "You lose, The computer chose the scissors for this game." << endl;
break;
}else if (choose == 2 && computer == 2)
{
cout << "The computer also chose the paper for this game. " << endl;
}else if (choose == 3 && computer == 1)
{
cout << "You lose, The computer chose the rock for this game." << endl;
break;
}
else if (choose == 3 && computer == 2)
{
cout << "You win, The computer chose the paper for this game." << endl;
break;
}else if (choose == 3 && computer == 3)
{
cout << "The computer also chose the paper for this game. " << endl;
}
else{
break;
}
while (choose == computer)
{
cout << "Since you had same choice with the computer, please enter the 1 for rock, 2 for paper or 3 for scissors again " << endl;
cin >> choose;
}
}
}
void multiplayer(int p1, int p2)
{
while (1){
if (p1 == 1 && p2 == 3)
{
cout << "The player1 win! The player2 chose the scissors for this game. The player1 chose rock for this game. " << endl;
break;
}
else if (p1 == 1 && p2 == 2)
{
cout << "The player2 win, The player2 chose the paper for this game.The player1 chose the rock for this game " << endl;
break;
}
else if (p1 == 1 && p2 == 1)
{
cout << "The player1 and player2 both chose the rock for this game. " << endl;
}
else if (p1 == 2 && p2 == 1)
{
cout << "The player1 win, The player2 chose the rock for this game.The player1 chose the paper for this game." << endl;
break;
}
else if (p1 == 2 && p2 == 3)
{
cout << "The player1 win, The player1 chose the paper for this game.The player2 chose the scissors for this game." << endl;
break;
}
else if (p1 == 2 && p2 == 2)
{
cout << "The player1 and player2 both chose the paper for this game. " << endl;
}
else if (p1 == 3 && p2 == 1)
{
cout << "The player2 win, The player2 chose the rock for this game.The player1 chose the scissors for this game." << endl;
break;
}
else if (p1 == 3 && p2 == 2)
{
cout << "The player1 win, The player1 chose the scissors for this game.The player2 chose the paper for this game." << endl;
break;
}
else if (p1 == 3 && p2 == 3)
{
cout << "The player1 and player2 both chose the scissors for this game. " << endl;
}else{
break;
}
while (p1 == p2)
{
cout << "Since you had same choice with eath other, please enter the 1 for rock, 2 for paper or 3 for scissors again " << endl;
cout << "Player1 please enter 1 for rock, 2 for paper or 3 for scissors " << endl;
cin >> p1;
cout << "Player2 please enter 1 for rock, 2 for paper or 3 for scissors " << endl;
cin >> p2;
}
}
}
Please test it and let me know any issues. Thank you. All the best.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.