#!/bin/bash

p1='[^^^^^]'
p2=' \   / '
p3='  | |  '

s1=' \\ // '
s2=' _ X _ '
s3='/_/ \_\'

r1='{^----\'
r2='{     |'
r3='{_____|'

echo "*** Paper, Scissors, Rock !!! ***"
echo ""

ok=0

while [ $ok -eq 0 ]; do
    echo -n "Paper(P), Scissors(S), Rock(R), Quit(Q) ? "
    read ans

    if [ "$ans" = 'q' ] || [ "$ans" = 'Q' ]; then
        echo ""
        echo "Bye !"
        ok=1
    fi

    un=-1

    if [ "$ans" = 'p' ] || [ "$ans" = 'P' ]; then
        u1="$p1"
        u2="$p2"
        u3="$p3"
        un=0
    fi

    if [ "$ans" = 's' ] || [ "$ans" = 'S' ]; then
        u1="$s1"
        u2="$s2"
        u3="$s3"
        un=1
    fi

    if [ "$ans" = 'r' ] || [ "$ans" = 'R' ]; then
        u1="$r1"
        u2="$r2"
        u3="$r3"
        un=2
    fi

    cn=$[$RANDOM % 3]

    if [ $cn -eq 0 ]; then
        c1="$p1"
        c2="$p2"
        c3="$p3"
    fi

    if [ $cn -eq 1 ]; then
        c1="$s1"
        c2="$s2"
        c3="$s3"
    fi

    if [ $cn -eq 2 ]; then
        c1="$r1"
        c2="$r2"
        c3="$r3"
    fi

    if [ $un -ge 0 ]; then
        echo ""
        echo "You:       Com:"
        echo ""
        echo "$u1    $c1"
        echo "$u2    $c2"
        echo "$u3    $c3"
        echo ""

        if [ $un -eq 0 ] && [ $cn -eq 1 ]; then
            win=-1
        elif [ $un -eq 1 ] && [ $cn -eq 2 ]; then
            win=-1
        elif [ $un -eq 2 ] && [ $cn -eq 0 ]; then
            win=-1
        elif [ $un -eq 0 ] && [ $cn -eq 2 ]; then
            win=1
        elif [ $un -eq 1 ] && [ $cn -eq 0 ]; then
            win=1
        elif [ $un -eq 2 ] && [ $cn -eq 1 ]; then
            win=1
        else
            win=0
        fi

        if [ $win -eq 1 ]; then
            echo "You win."
        elif [ $win -eq -1 ]; then
            echo "You lose."
        else
            echo "Fair."
        fi

        echo ""
    fi
done
