Quantcast
Channel: VBForums - Game Demos
Viewing all articles
Browse latest Browse all 56

[C++]Rock-Paper-Scissors

$
0
0
This is just the source code.

Notes:
I wrote it as a win32 console application.

c++ Code:
  1. #include "stdafx.h" //For the console application
  2. #include <iostream> //For cout and cin - aka: Input/Output
  3. #include <string> //For string - aka: text
  4.  
  5. using namespace std;
  6.  
  7. string AI()
  8. {
  9.     /*
  10.     0 - Rock
  11.     1 - Paper
  12.     2 - Scissors
  13.     */
  14.  
  15.     int r = rand() % 3; //returns 0 - 2
  16.     if (r == 0)
  17.     {
  18.         return "rock";
  19.     }
  20.     else if(r == 1)
  21.     {
  22.         return "paper";
  23.     }
  24.     else
  25.     {
  26.         return "scissors";
  27.     }
  28. }
  29.  
  30. int _tmain(int argc, _TCHAR* argv[])
  31. {
  32.     //Set up a random seed
  33.     srand (0);
  34.  
  35.     //Loop indefinately
  36.     while (true == true)
  37.     {
  38.         string user_input = "";
  39.  
  40.         //Loop until user_input isn't empty
  41.         while (user_input == "")
  42.         {
  43.             cout << "Please type in: rock, paper, or scissors" << "\n";
  44.             cin >> user_input;
  45.             if (user_input != "rock" && user_input != "paper" && user_input != "scissors")
  46.             {
  47.                 user_input = "";
  48.                 cout << "Invalid input." << "\n" << "\n";
  49.             }
  50.         }
  51.  
  52.         string comp_input = AI();
  53.  
  54.         //Dish out the results
  55.         cout << "You picked: " << user_input << "\n" << "The computer picked: " << comp_input << "\n";
  56.         if (user_input == comp_input)
  57.         {
  58.             cout << "It's a draw." << "\n" << "\n";
  59.         }
  60.         else if (user_input == "rock" && comp_input == "paper" || user_input == "paper" && comp_input == "scissors" || user_input == "scissors" && comp_input == "rock")
  61.         {
  62.             cout << "You lose..." << "\n" << "\n";
  63.         }
  64.         else if (user_input == "rock" && comp_input == "scissors" || user_input == "paper" && comp_input == "rock" || user_input == "scissors" && comp_input == "paper")
  65.         {
  66.             cout << "You won!" << "\n" << "\n";
  67.         }
  68.     }
  69.     return 0;
  70. }

Viewing all articles
Browse latest Browse all 56

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>