can anybody help me out with an object orientated code for rock paper scissors, useing classes etc?? this is in c++
#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
Thank you very much
@peterotus1993 :)
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??
@peterotus1993 Can't understand. What's problem ? It's works fine on my pc.
it works fine on mine as well, but I need classes for players, how do you think i could implement this??
Oh
Do it in OOP way, make a class of player, then for game.
lol that's where I am stuck on, do you know any good tutorial sites??
Store things like score of player, no. of times played, name, a function, getPlayersMove that returns "rock, paper or scissor" in player class.
I got this http://www.dreamincode.net/forums/topic/43902-object-oriented-version-of-rock-paper-scissors-game/
Join our real-time social learning platform and learn together with your friends!