Results 1 to 3 of 3

Thread: [C++]Rock-Paper-Scissors

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    [C++]Rock-Paper-Scissors

    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. }
    Last edited by dday9; Nov 8th, 2013 at 03:59 PM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  2. #2
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: [C++]Rock-Paper-Scissors

    A few things:
    Code:
    while (true == true)
    You would only need this, if you're doing it that way:
    Code:
    while (true)
    Instead of evaluating strings, you should be evaluating a more simplistic and less bulky datatype like an integer (byte, char, int, ...) instead. I would use an enum for this.

    There are more effective ways of validation though... Here's the rock paper scissors game I wrote in C (some minor issues still, but it's alright):
    c Code:
    1. #include <stdio.h>
    2. #include <string.h>
    3. #include <stdlib.h>
    4. #include <time.h>
    5. #include <stdbool.h>
    6.  
    7. int getmove(const size_t len)
    8. {
    9.     char *user = malloc(sizeof(char)*len);
    10.     printf("Choose:\n\t0 - Rock\n\t1 - Paper\n\t2 - Scissors\nInput: ");
    11.     if (fgets(user, len, stdin) != NULL)
    12.     {
    13.         while ((user[0] < '0' || user[0] > '2') || strlen(user) != 2)
    14.         {
    15.             printf("Invalid choice, try again: ");
    16.             fgets(user, len, stdin);
    17.         }
    18.         int n = user[0] - '0';
    19.         free(user);
    20.         return n;
    21.     } else return 0;
    22. }
    23.  
    24. int main()
    25. {
    26.     char ch;
    27.     do
    28.     {
    29.         int choice = getmove(256);
    30.         printf("\nYou chose: %d\n", choice);
    31.         srand(time(NULL));
    32.         char cpu = rand() % 3;
    33.         if (cpu)
    34.         {
    35.             bool b = cpu & 1;
    36.             printf("You %s\n", b ? "Win!" : "Lose...");
    37.             cpu = b ? (choice == 0 ? 2 : choice - 1) : (choice +1) % 3;
    38.         }
    39.         else
    40.         {
    41.             printf("Draw.\n");
    42.             cpu = choice;
    43.         }
    44.         printf("Computer chose: %d\n\n", cpu);
    45.  
    46.         printf("Press [Q] to quit, otherwise any other key to continue...\n");
    47.     } while ((ch = getchar()) != 'Q' && ch != 'q');
    48.     return 0;
    49. }
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  3. #3

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: [C++]Rock-Paper-Scissors

    Thanks for the suggestions. Whenever I get the opportunity I'll be sure to make some changes and update it.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width