Advertisement
Guest User

rock paper shit

a guest
Nov 15th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.59 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include "conio.h"
  4.  
  5. using namespace std;
  6.  
  7. bool EnsureValid(char v){
  8.     switch (v){
  9.         case 'r':
  10.         case 'p':
  11.         case 's':
  12.             return true;
  13.         default:
  14.             return false;
  15.     }
  16. }
  17.  
  18. string FriendlyName(char v){
  19.     switch (v){
  20.         case 'r':
  21.             return "Rock";
  22.         case 'p':
  23.             return "Paper";
  24.         case 's':
  25.             return "Scissors";
  26.     }
  27.     return "Invalid";
  28. }
  29.  
  30. bool Is1Win(char a, char b) {
  31.         return a=='r'&&b=='s'||a=='p'&&b=='r'||a=='s'&&b=='p';
  32. }
  33.  
  34. int main(int, char*[])
  35. {
  36.     char user[2] = {0,0};
  37.  
  38.     for (;;) {
  39.         restart:
  40.         // Read inputs
  41.         cout << "Player 1: [R/P/S]" << endl;
  42.         user[0]=_getch();//use _getch() because it doesn't require a Enter press
  43.         cout << endl;
  44.         cout << "Player 2:  [R/P/S]" << endl;
  45.         user[1]=_getch();
  46.         cout << endl;
  47.         // Lowercase
  48.         user[0]|=0x20;
  49.         user[1]|=0x20;
  50.         // Validate
  51.         for(int i=0;i<2;i++){
  52.             if(!EnsureValid(user[i])){
  53.                     cout << "Player " << i+1 << ": That is not Rock, Paper or Scissors!" << endl;
  54.                     goto restart;//cant break/continue because no named loops
  55.             }
  56.         }
  57.         if(user[0]==user[1]) {// Tie check
  58.                 cout << "Both players chose " << FriendlyName(user[0]) << "!" << endl;
  59.         }else {
  60.             if (Is1Win(user[0],user[1])){
  61.                     cout << "Player 1 wins with " << FriendlyName(user[0]) << "!" << endl;
  62.             }else{// We have already checked the tie and p1 win, only other state is p2 win.
  63.                     cout << "Player 2 wins with " << FriendlyName(user[1]) << "!" << endl;
  64.             }
  65.         }
  66.  
  67.         cout << "Play again [Y/N]" << endl;
  68.         char v=_getch();
  69.         cout << endl;
  70.         v|=0x20;
  71.         if(v!='y'){
  72.             break;
  73.         }
  74.     }
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement