Recreating "rock, paper, scissors" in C++

rockpaperscissors
cplusplus

#1

Fairly recently, I recreated the rock, paper, scissors JavaScript exercise in Java.

Now, let's do it in C++ :wink:

Since C++ is not a course on CC, I will not blur the code here.

main.cpp

#include <iostream>
#include <algorithm>
#include "Game.h"

using namespace std;

int main(int argc, char *argv[]) {
    string choice;
    if (argc == 2) {
        choice = argv[1];
    }
    while (choice.empty() || (choice != "rock" && choice != "paper" && choice != "scissors")) {
        cout << "Enter your choice as a word (rock, paper, scissors):\n> ";
        getline(cin, choice);
    }
    transform(choice.begin(), choice.end(), choice.begin(), ::tolower);
    Game *game = new Game(choice);
    cout << "You chose: " << game->getUserChoice() << "." << endl;
    cout << "The computer chose: " << game->getCompChoice() << "." << endl;
    cout << "The winner is: " << game->decideWinner() << "." << endl;
    delete game;
    return 0;
}

Game.cpp

//
// Created by simple on 2/17/17.
//

#include "Game.h"

Game::Game(std::string user_choice) {
    this->user_choice = user_choice;
    this->comp_choice = this->compMakeChoice();
}

std::string Game::getUserChoice() {
    return this->user_choice;
}

std::string Game::getCompChoice() {
    return this->comp_choice;
}

std::string Game::decideWinner() {
    if (this->user_choice == this->comp_choice) return resultToString(TIE);
    else {
        if (this->user_choice == "rock") {
            if (this->comp_choice == "scissors") return resultToString(USER);
            else if (this->comp_choice == "paper") return resultToString(COMPUTER);
        }
        else if (this->user_choice == "paper") {
            if (this->comp_choice == "rock") return resultToString(USER);
            else if (this->comp_choice == "scissors") return resultToString(COMPUTER);
        }
        else if (this->user_choice == "scissors") {
            if (this->comp_choice == "paper") return resultToString(USER);
            else if (this->comp_choice == "rock") return resultToString(COMPUTER);
        }
    }
    return resultToString(INCONCLUSIVE);
}

std::string Game::compMakeChoice() {
    std::string choices[3] = { "rock", "paper", "scissors" };
    return choices[rand()%3];
}

const std::string Game::resultToString(Game::Result r) {
    switch (r) {
        case TIE:           return "tie";
        case USER:          return "user";
        case COMPUTER:      return "computer";
        case INCONCLUSIVE:
        default:            return "inconclusive";
    }
}

Game.h

//
// Created by simple on 2/17/17.
//
#include <string>

#ifndef ROCKPAPERSCISSORS_GAME_H
#define ROCKPAPERSCISSORS_GAME_H


class Game {
public:
    enum Result { TIE, USER, COMPUTER, INCONCLUSIVE };
    Game(std::string user_choice);
    std::string getUserChoice();
    std::string getCompChoice();
    std::string decideWinner();
    const std::string resultToString(Result r);
private:
    std::string user_choice;
    std::string comp_choice;
    std::string compMakeChoice();
};


#endif //ROCKPAPERSCISSORS_GAME_H

This has been a learning experience to say the least :wink: Now on to other things!