Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include "conio.h"
- using namespace std;
- bool EnsureValid(char v){
- switch (v){
- case 'r':
- case 'p':
- case 's':
- return true;
- default:
- return false;
- }
- }
- string FriendlyName(char v){
- switch (v){
- case 'r':
- return "Rock";
- case 'p':
- return "Paper";
- case 's':
- return "Scissors";
- }
- return "Invalid";
- }
- bool Is1Win(char a, char b) {
- return a=='r'&&b=='s'||a=='p'&&b=='r'||a=='s'&&b=='p';
- }
- int main(int, char*[])
- {
- char user[2] = {0,0};
- for (;;) {
- restart:
- // Read inputs
- cout << "Player 1: [R/P/S]" << endl;
- user[0]=_getch();//use _getch() because it doesn't require a Enter press
- cout << endl;
- cout << "Player 2: [R/P/S]" << endl;
- user[1]=_getch();
- cout << endl;
- // Lowercase
- user[0]|=0x20;
- user[1]|=0x20;
- // Validate
- for(int i=0;i<2;i++){
- if(!EnsureValid(user[i])){
- cout << "Player " << i+1 << ": That is not Rock, Paper or Scissors!" << endl;
- goto restart;//cant break/continue because no named loops
- }
- }
- if(user[0]==user[1]) {// Tie check
- cout << "Both players chose " << FriendlyName(user[0]) << "!" << endl;
- }else {
- if (Is1Win(user[0],user[1])){
- cout << "Player 1 wins with " << FriendlyName(user[0]) << "!" << endl;
- }else{// We have already checked the tie and p1 win, only other state is p2 win.
- cout << "Player 2 wins with " << FriendlyName(user[1]) << "!" << endl;
- }
- }
- cout << "Play again [Y/N]" << endl;
- char v=_getch();
- cout << endl;
- v|=0x20;
- if(v!='y'){
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement