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.




import "random" for Random
import "/str" for Str
import "/ioutil" for Input
 
var choices = "rpsq"
var rand = Random.new()
 
var pWins  = 0          // player wins
var cWins  = 0          // computer wins
var draws  = 0          // neither wins
var games  = 0          // games played
var pFreqs = [0, 0, 0]  // player frequencies for each choice (rps)
 
var printScore = Fn.new { 
    System.print("Wins: You %(pWins), Computer %(cWins), Neither %(draws)\n")
}
 
var getComputerChoice = Fn.new {
    // make a completely random choice until 3 games have been played
    if (games < 3) return choices[rand.int(3)]
    var num = rand.int(games)
    return (num < pFreqs[0]) ? "p" :
           (num < pFreqs[0] + pFreqs[1]) ? "s" : "r"
}
 
System.print("Enter: (r)ock, (p)aper, (s)cissors or (q)uit\n")
while (true) {
    printScore.call()
    var pChoice = Str.lower(Input.option("Your choice r/p/s/q : ", "rpsqRPSQ"))
    if (pChoice == "q") {
        System.print("OK, quitting")
        return
    }
    var cChoice = getComputerChoice.call()
    System.print("Computer's choice   : %(cChoice)")
    if (pChoice == "r" && cChoice == "s") {
        System.print("Rock breaks scissors - You win!")
        pWins = pWins + 1
    } else if (pChoice == "p" && cChoice == "r") {
        System.print("Paper covers rock - You win!")
        pWins = pWins + 1
    } else if (pChoice == "s" && cChoice == "p") {
        System.print("Scissors cut paper - You win!")
        pWins = pWins + 1
    } else if (pChoice == "s" && cChoice == "r") {
        System.print("Rock breaks scissors - Computer wins!")
        cWins = cWins + 1
    } else if (pChoice == "r" && cChoice == "p") {
        System.print("Paper covers rock - Computer wins!")
        cWins = cWins + 1
    } else if (pChoice == "p" && cChoice == "s") {
        System.print("Scissors cut paper - Computer wins!")
        cWins = cWins + 1
    } else {
        System.print("It's a draw!")
        draws = draws + 1
    }
    var pf = pFreqs[choices.indexOf(pChoice)]
    pFreqs[choices.indexOf(pChoice)] = pf + 1
    games = games + 1
    System.print()
}