Task

Implement the classic children's game Rock-paper-scissors, as well as a simple predictive   AI   (artificial intelligence)   player.

Rock Paper Scissors is a two player game.

Each player chooses one of rock, paper or scissors, without knowing the other player's choice.

The winner is decided by a set of rules:

   Rock beats scissors
   Scissors beat paper
   Paper beats rock


If both players choose the same thing, there is no winner for that round.

For this task, the computer will be one of the players.

The operator will select Rock, Paper or Scissors and the computer will keep a record of the choice frequency, and use that information to make a weighted random choice in an attempt to defeat its opponent.



Extra credit

Support additional choices   additional weapons.




--standard game
constant rule3 = {"rock blunts scissors",
                  "paper wraps rock",
                  "scissors cut paper"}
--extended version
constant rule5 = {"rock blunts scissors",
                  "rock crushes lizard",
                  "paper wraps rock",
                  "paper disproves spock",
                  "scissors cut paper",
                  "scissors decapitate lizard",
                  "lizard eats paper",
                  "lizard poisons spock",
                  "spock smashes scissors",
                  "spock vaporizes rock"}
 
constant rules = iff(rand(2)=1?rule3:rule5)
 
sequence what = {}
sequence beats = {}
string wkeys = ""
string question = "What is your move "
integer choices, hsum
sequence history, cplays, pplays
 
object x, verb, y
 
    for i=1 to length(rules) do
        {x} = split(rules[i])
        if not find(x,what) then
            what = append(what,x)
            if find(x[1],wkeys) then
                wkeys = append(wkeys,x[$])
                question &= x[1..-2]&"("&x[$]&"), "
            else
                wkeys = append(wkeys,x[1])
                question &= "("&x[1]&")"&x[2..$]&", "
            end if
        end if
    end for
    choices = length(wkeys)
    history = repeat(1,choices)
    hsum = 3
    cplays = repeat(0,choices)
    pplays = repeat(0,choices)
    beats = repeat(repeat(0,choices),choices)
    question[-2] = '?'
    for i=1 to length(rules) do
        {x,verb,y} = split(rules[i])
        beats[find(x,what)][find(y,what)] = verb
    end for
 
integer cmove, pmove, draws = 0, pwins = 0, cwins = 0
    while 1 do
        cmove = rand(hsum)
        for i=1 to choices do
            cmove -= history[i]
            if cmove<=0 then
                -- predicted user choice of i, find whatever beats it
                for j=1 to choices do
                    if string(beats[j][i]) then
                        cmove = j
                        exit
                    end if
                end for
                exit
            end if
        end for
        puts(1,question)
        while 1 do
            pmove = lower(wait_key())
            if pmove='q' then exit end if
            pmove = find(pmove,wkeys)
            if pmove!=0 then exit end if
        end while
        if pmove='q' then exit end if
 
        printf(1,"you: %s, me: %s, ",{what[pmove],what[cmove]})
        cplays[cmove] += 1
        pplays[pmove] += 1
        if cmove=pmove then
            printf(1,"a draw.\n")
            draws += 1
        else
            if string(beats[cmove][pmove]) then
                printf(1,"%s %s %s. I win.\n",{what[cmove],beats[cmove][pmove],what[pmove]})
                cwins += 1
            elsif string(beats[pmove][cmove]) then
                printf(1,"%s %s %s. You win.\n",{what[pmove],beats[pmove][cmove],what[cmove]})
                pwins += 1
            else
                ?9/0    -- sanity check
            end if
        end if
        history[pmove] += 1
        hsum += 1
    end while
    printf(1,"\n\nYour wins:%d, My wins:%d, Draws:%d\n",{pwins,cwins,draws})
    printf(1,"\n\nYour wins:%d, My wins:%d, Draws:%d\n",{pwins,cwins,draws})
    printf(1,"       ") for i=1 to choices do   printf(1,"%9s",what[i])     end for
    printf(1,"\nyou: ") for i=1 to choices do   printf(1,"%9d",pplays[i])   end for
    printf(1,"\n me: ") for i=1 to choices do   printf(1,"%9d",cplays[i])   end for