Categories > Coding > C++ >
rock paper scissor
Posted
rock paper scissor
rock paper scissor
rock paper scissor
#include <iostream>
#include <ctime>
using namespace std;
char get_user_input();
char get_computer_choice();
void choice(char l);
void winner(char plr, char comp);
int main() {
char plr;
char comp;
bool x = true;
do {
plr = get_user_input();
printf("Your Choice Is: ", '\n');
choice(plr);
comp = get_computer_choice();
printf("Computer Choice Is: ", '\n');
choice(comp);
winner(plr, comp);
} while (x == true);
}
char get_user_input() {
char player;
cout << "Rock - Paper - Scissor\n";
do {
cout << "'r' for rock, 's' for scissor, 'p' for paper: ";
cin >> player;
} while (player != 'r' && player != 's' && player != 'p');
return player;
}
char get_computer_choice() {
int computer = (rand() % 3) + 1;
switch (computer)
{
case 1:
return 'r';
case 2:
return 's';
case 3:
return 'p';
}
}
void choice(char l) {
switch (l)
{
case 'r':
cout << "Rock\n";
break;
case 's':
cout << "Scissor\n";
break;
case 'p':
cout << "Paper\n";
break;
}
}
void winner(char plr, char comp) {
//i know i could have used switch but i used if statments so ok
if (plr == 's' && comp == 'r') {
cout << "Computer Won\n";
}
else if (plr == 'r' && comp == 's')
{
cout << "You Won\n";
}
else if (plr == 'p' && comp == 's')
{
cout << "Computer Won\n";
}
else if (plr == 's' && comp == 'p')
{
cout << "You Won\n";
}
else if (plr == 'p' && comp == 'r')
{
cout << "You Won\n";
}
else if (plr == 'r' && comp == 'p')
{
cout << "You Won\n";
}
else if (plr == 'p' && comp == 'p')
{
cout << "Tie\n";
}
else if (plr == 'r' && comp == 'r')
{
cout << "Tie\n";
}
else if (plr == 's' && comp == 's')
{
cout << "Tie\n";
}
}
want something? add me on disccord !Spl||HasH!#7562
Replied
kind of sloppy code but its ok
Cancel
Post
Exploits i own:
Script-Ware (Down rn) Electron (UWP) Shadow (UWP)
i dont use verm that much so dont contact me there
Replied
You could just do: else if (plr == comp); for tie detection but ok
Cancel
Post
Replied
Jesus what the hell is that code my man?
Cancel
Post
Replied
0/10 because
using namespace std;
Cancel
Post
Random quote here...
Replied
Here you go instead of whatever that is
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{
srand(time(NULL)); // Initialize random seed
// Display game instructions
cout << "Welcome to Rock Paper Scissors!" << endl;
cout << "Choose your move: (r)ock, (p)aper, or (s)cissors." << endl;
// Get player's move
char playerMove;
cout << "Your move: ";
cin >> playerMove;
// Generate computer's move
char computerMove;
int randomNum = rand() % 3;
if (randomNum == 0) {
computerMove = 'r';
} else if (randomNum == 1) {
computerMove = 'p';
} else {
computerMove = 's';
}
// Determine winner
if (playerMove == computerMove) {
cout << "It's a tie!" << endl;
} else if ((playerMove == 'r' && computerMove == 's') ||
(playerMove == 'p' && computerMove == 'r') ||
(playerMove == 's' && computerMove == 'p')) {
cout << "You win!" << endl;
} else {
cout << "Computer wins!" << endl;
}
return 0;
}
Cancel
Post
nltr | Xaml & C# Developer | Former Fluxus Administrator
------------------------------------------------------------------------------------
💜 Developer of Kronos 💜
Senior Dev of Orbit
Replied
@ItsNitro, here I fixed the code for you
#include <iostream>
#include <stdlib.h>
#include <time.h>
int main()
{
srand(time(NULL)); // Initialize random seed
// Display game instructions
std::cout << "Welcome to Rock Paper Scissors!" << std::endl;
std::cout << "Choose your move: (r)ock, (p)aper, or (s)cissors." << std::endl;
// Get player's move
char playerMove;
std::cout << "Your move: ";
std::cin >> playerMove;
// Generate computer's move
char computerMove;
int randomNum = rand() % 3;
if (randomNum == 0) {
computerMove = 'r';
} else if (randomNum == 1) {
computerMove = 'p';
} else {
computerMove = 's';
}
// Determine winner
if (playerMove == computerMove) {
std::cout << "It's a tie!" << std::endl;
} else if ((playerMove == 'r' && computerMove == 's') ||
(playerMove == 'p' && computerMove == 'r') ||
(playerMove == 's' && computerMove == 'p')) {
std::cout << "You win!" << std::endl;
} else {
std::cout << "Computer wins!" << std::endl;
}
return 0;
}
Cancel
Post
Random quote here...
nltr | Xaml & C# Developer | Former Fluxus Administrator
------------------------------------------------------------------------------------
💜 Developer of Kronos 💜
Senior Dev of Orbit
Random quote here...
Replied
bro literally God level codes
Cancel
Post
Thank you
Users viewing this thread:
( Members: 0, Guests: 1, Total: 1 )
Cancel
Post