Friday, November 1, 2019

Rock Paper Scissors in C++ (Dev C++)

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

int main()
{ srand(time(NULL));
  int CChoice, PChoice, CScore=0, PScore=0;
  char C[9], P[9], set[][9] = { { "Rock" }, { "Paper" }, { "Scissors" } };
  int GameNo=0;
  
  while(++GameNo <= 10)
  { CChoice = rand() % 3 + 1;
    cout << "Game " << GameNo << "\n";
    cout << "Enter 1 (ROCK), 2 (PAPER), 3 (SCISSORS), 0 (EXIT): ";
cin >> PChoice;
    if(PChoice == 0)
      return -1;
   
    cout << "C chose " << CChoice << "  (" << set[CChoice-1] << "). You chose " << PChoice << "  (" << set[PChoice-1] << ")\n";
    
    if(CChoice == 1)
    { if(PChoice == 1)
      { cout << "Both chose rock, draw\nC Score = " << CScore << " Your Score = " << PScore << "\n\n\n";  }
      else if(PChoice == 2)
      { PScore++;  cout << "Paper covers rock, you win\nC Score = " << CScore << " Your Score = " << PScore << "\n\n\n";  }
      else if(PChoice == 3)
      { CScore++;  cout << "Rock smashes scissors, you lose\nC Score = " << CScore << " Your Score = " << PScore << "\n\n\n";  }
    }
  
    if(CChoice == 2)
    { if(PChoice == 1)
      { CScore++;  cout << "Paper covers rock, you lose\nC Score = " << CScore << " Your Score = " << PScore << "\n\n\n";  }
      else if(PChoice == 2)
      { cout << "Both chose paper, draw\nC Score = " << CScore << " Your Score = " << PScore << "\n\n\n";  }
      else if(PChoice == 3)
      { PScore++;  cout << "Scissors cuts paper, you win\nC Score = " << CScore << " Your Score = " << PScore << "\n\n\n";  }
    }
  
    if(CChoice == 3)
    { if(PChoice == 1)
      { PScore++;  cout << "Rock smashes scissors, you win\nC Score = " << CScore << " Your Score = " << PScore << "\n\n\n";  }
      else if(PChoice == 2)
      { CScore++;  cout << "Scissors cuts paper, you lose\nC Score = " << CScore << " Your Score = " << PScore << "\n\n\n";  }
      else if(PChoice == 3)
      { cout << "Both chose scissors, draw\nC Score = " << CScore << " Your Score = " << PScore << "\n\n\n";  }
    }
  }
  
  cout << "Computer Score = " << CScore << " Your Score = " << PScore << " Draws = " << GameNo-1-(PScore + CScore);
  getch();
}

No comments:

Post a Comment