Ask your own question, for FREE!
Computer Science 9 Online
OpenStudy (anonymous):

can anybody help me out with an object orientated code for rock paper scissors, useing classes etc?? this is in c++

OpenStudy (anonymous):

#include <iostream> #include <string> #include <cassert> #include <ctime> #include <cstdlib> std::string numberToWeapon(int n) { switch (n) { case 0: return "rock"; case 1: return "paper"; case 2: return "scissors"; } assert(!"Invalid parameter"); } int main() { std::cout << "Rock-Paper-Scissors v1.0\n"; int playerScore = 0; int ourScore = 0; std::srand(static_cast<unsigned int>(time(0))); while (true) { int playerMove; std::cout << "Please enter 1 for rock, 2 for paper, or 3 for scissors.\n"; if (!(std::cin >> playerMove)) return 0; --playerMove; if (playerMove < 0 || playerMove > 2) { std::cout << "Invalid input.\n"; continue; } int ourMove = rand()%3; std::cout << "You played: " << numberToWeapon(playerMove) << ". "; std::cout << "We played: " << numberToWeapon(ourMove) << ".\n"; switch ((3 + playerMove - ourMove) % 3) { case 0: std::cout << "The game is a tie.\n"; break; case 1: std::cout << "You won!\n"; ++playerScore; break; case 2: std::cout << "We won!\n"; ++ourScore; break; } std::cout << "The score is currently:\n"; std::cout << "You - " << playerScore << ".\n"; std::cout << "We - " << ourScore << ".\n"; } } Source: http://codereview.stackexchange.com/a/9604/9895

OpenStudy (anonymous):

Thank you very much

OpenStudy (anonymous):

@peterotus1993 :)

OpenStudy (anonymous):

how do you reckon i could make this with adding a player class with private attributes representing the player’s choice of ‘weapon’ and current score, along with methods to get and set the values of these attributes??

OpenStudy (anonymous):

@peterotus1993 Can't understand. What's problem ? It's works fine on my pc.

OpenStudy (anonymous):

it works fine on mine as well, but I need classes for players, how do you think i could implement this??

OpenStudy (anonymous):

Here's a sceenshot too http://i.imgur.com/FHiAR.jpg

OpenStudy (anonymous):

Oh

OpenStudy (anonymous):

Do it in OOP way, make a class of player, then for game.

OpenStudy (anonymous):

lol that's where I am stuck on, do you know any good tutorial sites??

OpenStudy (anonymous):

Store things like score of player, no. of times played, name, a function, getPlayersMove that returns "rock, paper or scissor" in player class.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!