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.




Enumeration
  ;choices are in listed according to their cycle, weaker followed by stronger
  #rock
  #paper
  #scissors
  #numChoices ;this comes after all possible choices
EndEnumeration
 
;give names to each of the choices
Dim choices.s(#numChoices - 1)
choices(#rock) = "rock"
choices(#paper) = "paper"
choices(#scissors) = "scissors"
 
Define gameCount
Dim playerChoiceHistory(#numChoices - 1)
 
Procedure weightedRandomChoice()
  Shared gameCount, playerChoiceHistory()
  Protected x = Random(gameCount - 1), t, c
 
  For i = 0 To #numChoices - 1
    t + playerChoiceHistory(i)
    If t >= x
      c = i
      Break
    EndIf
  Next
 
  ProcedureReturn (c + 1) % #numChoices
EndProcedure
 
If OpenConsole()
  PrintN("Welcome to the game of rock-paper-scissors")
  PrintN("Each player guesses one of these three, and reveals it at the same time.")
  PrintN("Rock blunts scissors, which cut paper, which wraps stone.")
  PrintN("If both players choose the same, it is a draw!")
  PrintN("When you've had enough, choose Q.")
 
  Define computerChoice, playerChoice, response.s
  Define playerWins, computerWins, draw, quit
 
  computerChoice = Random(#numChoices - 1)
  Repeat
    Print(#CRLF$ + "What is your move (press R, P, or S)?")
    Repeat
      response = LCase(Input())
    Until FindString("rpsq", response) > 0
 
    If response = "q":
      quit = 1
    Else
      gameCount + 1
      playerChoice = FindString("rps", response) - 1
 
      result = (playerChoice - computerChoice + #numChoices) % #numChoices
      Print("You chose " + choices(playerChoice) + " and I chose " + choices(computerChoice))
      Select result
        Case 0
          PrintN(". It's a draw.")
          draw + 1
        Case 1
          PrintN(". You win!")
          playerWins + 1
        Case 2
          PrintN(". I win!")
          computerWins + 1
      EndSelect
      playerChoiceHistory(playerChoice) + 1
      computerChoice = weightedRandomChoice()
    EndIf 
  Until quit
 
  Print(#CRLF$ + "You chose: ")
  For i = 0 To #numChoices - 1
    Print(choices(i) + " " + StrF(playerChoiceHistory(i) * 100 / gameCount, 1) + "%; ")
  Next
  PrintN("")
  PrintN("You won " + Str(playerWins) + ", and I won " + Str(computerWins) + ". There were " + Str(draw) + " draws.")
  PrintN("Thanks for playing!")
 
  Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
  CloseConsole()
EndIf