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, strutils, tables
 
type
  Choice {.pure.} = enum Rock, Paper, Scissors
  History = tuple[total: int; counts: CountTable[Choice]]
 
const Successor: array[Choice, Choice] = [Paper, Scissors, Rock]
 
func `>`(a, b: Choice): bool =
  ## By construction, only the successor is greater than the choice.
  a == Successor[b]
 
proc choose(history: History): Choice =
  ## Make a weighted random choice using the player counts
  ## then select the choice likely to beat it.
  var value = rand(1..history.total)
  for choice, count in history.counts.pairs:
    if value <= count:
      return Successor[choice]
    dec value, count
 
 
randomize()
 
# Initialize history with one for each choice in order to avoid special case.
var history: History = (3, [Rock, Paper, Scissors].toCountTable)
 
echo "To quit game, type 'q' when asked for your choice."
 
var myChoice, yourChoice: Choice
var myWins, yourWins = 0
 
while true:
 
  # Get player choice.
  try:
    stdout.write "Rock(1), paper(2), scissors(3). Your choice? "
    let answer = stdin.readLine().strip()
    if answer == "q":
      quit "Quitting game.", QuitSuccess
    if answer notin ["1", "2", "3"]:
      echo "Invalid choice."
      continue
    yourChoice = Choice(ord(answer[0]) - ord('1'))
  except EOFError:
    quit "Quitting game.", QuitFailure
 
  # Make my choice.
  myChoice = history.choose()
  echo "I choosed ", myChoice, '.'
  history.counts.inc yourChoice
  inc history.total
 
  # Display result of round.
  if myChoice == yourChoice:
    echo "It’s a tie."
  elif myChoice > yourChoice:
    echo "I win."
    inc myWins
  else:
    echo "You win."
    inc yourWins
  echo "Total wins. You: ", yourWins, "  Me: ", myWins