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.




DIM pPLchoice(1 TO 3) AS INTEGER, pCMchoice(1 TO 3) AS INTEGER
DIM choices(1 TO 3) AS STRING
DIM playerwins(1 TO 3) AS INTEGER
DIM playerchoice AS INTEGER, compchoice AS INTEGER
DIM playerwon AS INTEGER, compwon AS INTEGER, tie AS INTEGER
DIM tmp AS INTEGER
 
' Do it this way for QBasic; FreeBASIC supports direct array assignment.
DATA "rock", "paper", "scissors"
FOR tmp = 1 TO 3
    READ choices(tmp)
NEXT
DATA 3, 1, 2
FOR tmp = 1 TO 3
    READ playerwins(tmp)
NEXT
 
RANDOMIZE TIMER
 
DO
    ' Computer chooses first to ensure there's no "cheating".
    compchoice = INT(RND * (pPLchoice(1) + pPLchoice(2) + pPLchoice(3) + 3))
    SELECT CASE compchoice
        CASE 0 TO (pPLchoice(1))
            ' Player past choice: rock; choose paper.
            compchoice = 2
        CASE (pPLchoice(1) + 1) TO (pPLchoice(1) + pPLchoice(2) + 1)
            ' Player past choice: paper; choose scissors.
            compchoice = 3
        CASE (pPLchoice(1) + pPLchoice(2) + 2) TO (pPLchoice(1) + pPLchoice(2) + pPLchoice(3) + 2)
            ' Player past choice: scissors; choose rock.
            compchoice = 1
    END SELECT
 
    PRINT "Rock, paper, or scissors ";
    DO
        PRINT "[1 = rock, 2 = paper, 3 = scissors, 0 to quit]";
        INPUT playerchoice
    LOOP WHILE (playerchoice < 0) OR (playerchoice > 3)
 
    IF 0 = playerchoice THEN EXIT DO
 
    pCMchoice(compchoice) = pCMchoice(compchoice) + 1
    pPLchoice(playerchoice) = pPLchoice(playerchoice) + 1
    PRINT "You chose "; choices(playerchoice); " and I chose "; choices(compchoice); ". ";
    IF (playerchoice) = compchoice THEN
        PRINT "Tie!"
        tie = tie + 1
    ELSEIF (compchoice) = playerwins(playerchoice) THEN
        PRINT "You won!"
        playerwon = playerwon + 1
    ELSE
        PRINT "I won!"
        compwon = compwon + 1
    END IF
LOOP
 
PRINT "Some useless statistics:"
PRINT "You won "; STR$(playerwon); " times, and I won "; STR$(compwon); " times; "; STR$(tie); " ties."
PRINT             ,   choices(1),   choices(2),   choices(3)
PRINT "You chose:", pPLchoice(1), pPLchoice(2), pPLchoice(3)
PRINT "  I chose:", pCMchoice(1), pCMchoice(2), pCMchoice(3)