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.




$ include "seed7_05.s7i";
$ include "keybd.s7i";
 
const array string: rockPaperScissors is [] ("Rock", "Paper", "Scissors");
 
const proc: main is func
  local
    var char: command is ' ';
    var integer: user is 0;
    var integer: comp is 0;
  begin
    writeln("Hello, Welcome to rock-paper-scissors");
    repeat
      write("Please type in 1 for Rock, 2 for Paper, 3 for Scissors, q to quit ");
      flush(OUT);
      repeat
        command := lower(getc(KEYBOARD));
      until command in {'1', '2', '3', 'q'};
      writeln(command);
      if command <> 'q' then
        user := integer parse str(command);
        comp := rand(1, 3);
        writeln("You Played: " <& rockPaperScissors[user]);
        writeln("Computer Played: " <& rockPaperScissors[comp]);
        if comp = user then
          writeln("You Tied");
        elsif succ(comp) = user or user + 2 = comp then
          writeln("Yay, You Won!");
        else
          writeln("Sorry, You Lost!");
        end if;
      end if;
    until command = 'q';
    writeln("Goodbye! Thanks for playing!");
  end func;