#!/bin/bash

# Function to display game options
function display_options() {
    echo "1. Stone"
    echo "2. Paper"
    echo "3. Scissors"
    echo "4. Quit"
}

# Function to get user choice
function get_user_choice() {
    read -p "Enter your choice (1-4): " choice

    while [[ ! "$choice" =~ ^[1-4]$ ]]; do
        echo "Invalid choice! Please try again."
        read -p "Enter your choice (1-4): " choice
    done

    return "$choice"
}

# Function to generate computer choice
function get_computer_choice() {
    local choices=("Stone" "Paper" "Scissors")
    local random_index=$(( RANDOM % 3 ))
    local computer_choice="${choices[$random_index]}"
    echo "$computer_choice"
}

# Function to determine the game result
function get_game_result() {
    local user_choice=$1
    local computer_choice=$2

    if [ "$user_choice" == "$computer_choice" ]; then
        echo "It's a tie!"
    elif [ "$user_choice" == "Stone" ]; then
        if [ "$computer_choice" == "Paper" ]; then
            echo "You lose!"
        else
            echo "You win!"
        fi
    elif [ "$user_choice" == "Paper" ]; then
        if [ "$computer_choice" == "Scissors" ]; then
            echo "You lose!"
        else
            echo "You win!"
        fi
    elif [ "$user_choice" == "Scissors" ]; then
        if [ "$computer_choice" == "Stone" ]; then
            echo "You lose!"
        else
            echo "You win!"
        fi
    fi
}

# Main game loop
function main() {
    echo "Welcome to Stone-Paper-Scissors game!"

    while true; do
        echo ""
        display_options
        get_user_choice
        choice=$?

        case $choice in
            1)
                user_choice="Stone"
                ;;
            2)
                user_choice="Paper"
                ;;
            3)
                user_choice="Scissors"
                ;;
            4)
                echo "Thanks for playing. Goodbye!"
                exit
                ;;
        esac

        echo "You chose: $user_choice"
        computer_choice=$(get_computer_choice)
        echo "Computer chose: $computer_choice"

        result=$(get_game_result "$user_choice" "$computer_choice")
        echo "$result"
    done
}

# Start the game
main
