-
Nov 8th, 2013, 03:55 PM
#1
[C++]Rock-Paper-Scissors
This is just the source code.
Notes:
I wrote it as a win32 console application.
c++ Code:
#include "stdafx.h" //For the console application
#include <iostream> //For cout and cin - aka: Input/Output
#include <string> //For string - aka: text
using namespace std;
string AI()
{
/*
0 - Rock
1 - Paper
2 - Scissors
*/
int r = rand() % 3; //returns 0 - 2
if (r == 0)
{
return "rock";
}
else if(r == 1)
{
return "paper";
}
else
{
return "scissors";
}
}
int _tmain(int argc, _TCHAR* argv[])
{
//Set up a random seed
srand (0);
//Loop indefinately
while (true == true)
{
string user_input = "";
//Loop until user_input isn't empty
while (user_input == "")
{
cout << "Please type in: rock, paper, or scissors" << "\n";
cin >> user_input;
if (user_input != "rock" && user_input != "paper" && user_input != "scissors")
{
user_input = "";
cout << "Invalid input." << "\n" << "\n";
}
}
string comp_input = AI();
//Dish out the results
cout << "You picked: " << user_input << "\n" << "The computer picked: " << comp_input << "\n";
if (user_input == comp_input)
{
cout << "It's a draw." << "\n" << "\n";
}
else if (user_input == "rock" && comp_input == "paper" || user_input == "paper" && comp_input == "scissors" || user_input == "scissors" && comp_input == "rock")
{
cout << "You lose..." << "\n" << "\n";
}
else if (user_input == "rock" && comp_input == "scissors" || user_input == "paper" && comp_input == "rock" || user_input == "scissors" && comp_input == "paper")
{
cout << "You won!" << "\n" << "\n";
}
}
return 0;
}
Last edited by dday9; Nov 8th, 2013 at 03:59 PM.
-
Nov 28th, 2013, 08:01 PM
#2
Re: [C++]Rock-Paper-Scissors
A few things:
Code:
while (true == true)
You would only need this, if you're doing it that way:
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:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <time.h> #include <stdbool.h> int getmove(const size_t len) { char *user = malloc(sizeof(char)*len); printf("Choose:\n\t0 - Rock\n\t1 - Paper\n\t2 - Scissors\nInput: "); if (fgets(user, len, stdin) != NULL) { while ((user[0] < '0' || user[0] > '2') || strlen(user) != 2) { printf("Invalid choice, try again: "); fgets(user, len, stdin); } int n = user[0] - '0'; free(user); return n; } else return 0; } int main() { char ch; do { int choice = getmove(256); printf("\nYou chose: %d\n", choice); srand(time(NULL)); char cpu = rand() % 3; if (cpu) { bool b = cpu & 1; printf("You %s\n", b ? "Win!" : "Lose..."); cpu = b ? (choice == 0 ? 2 : choice - 1) : (choice +1) % 3; } else { printf("Draw.\n"); cpu = choice; } printf("Computer chose: %d\n\n", cpu); printf("Press [Q] to quit, otherwise any other key to continue...\n"); } while ((ch = getchar()) != 'Q' && ch != 'q'); return 0; }
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Nov 28th, 2013, 11:08 PM
#3
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|