#include <array>
#include <cctype>
#include <iostream>
#include <optional>
#include <random>
#include <string>

enum class Move { rock, paper, scissors };

std::string lower(std::string text) {
    for (char& letter : text) letter = static_cast<char>(std::tolower(static_cast<unsigned char>(letter)));
    return text;
}

std::optional<Move> parseMove(const std::string& text) {
    if (text == "rock") return Move::rock;
    if (text == "paper") return Move::paper;
    if (text == "scissors") return Move::scissors;
    return std::nullopt;
}

int outcome(Move player, Move computer) {
    if (player == computer) return 0;
    if ((player == Move::rock && computer == Move::scissors)
        || (player == Move::paper && computer == Move::rock)
        || (player == Move::scissors && computer == Move::paper)) return 1;
    return -1;
}

int main(int argc, char* argv[]) {
    bool demo = false;
    if (argc == 2 && std::string(argv[1]) == "--demo") demo = true;
    else if (argc != 1) { std::cerr << "Usage: program [--demo]\n"; return 1; }
    const std::array<std::string, 3> names{{"rock", "paper", "scissors"}};
    std::mt19937 generator(12345); // demo bypasses randomness entirely
    if (!demo) generator.seed(std::random_device{}());
    std::uniform_int_distribution<int> pick(0, 2);
    int wins = 0, losses = 0, ties = 0, rounds = 0;
    std::string line;
    while (std::cout << "rock/paper/scissors (quit to finish): " && std::getline(std::cin, line)) {
        line = lower(line);
        if (line == "quit") break;
        const auto player = parseMove(line);
        if (!player) { std::cout << "Invalid move; no round counted.\n"; continue; }
        if (rounds == 10000) { std::cerr << "Round limit reached.\n"; return 1; }
        const int computerIndex = demo ? rounds % 3 : pick(generator);
        const Move computer = static_cast<Move>(computerIndex);
        const int result = outcome(*player, computer);
        ++rounds;
        if (result > 0) ++wins;
        else if (result < 0) ++losses;
        else ++ties;
        std::cout << "Computer: " << names.at(static_cast<std::size_t>(computerIndex)) << '\n'
                  << "Result: " << (result > 0 ? "win" : result < 0 ? "loss" : "tie") << '\n';
    }
    std::cout << "Score wins/losses/ties: " << wins << '/' << losses << '/' << ties << '\n';
}
