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.




100 PROGRAM "Rock.bas"
110 RANDOMIZE
120 STRING CH$(1 TO 3)*8,K$*1
130 NUMERIC PLWINS(1 TO 3),SCORE(1 TO 3),PLSTAT(1 TO 3),CMSTAT(1 TO 3),PLCHOICE,CMCHOICE
140 CALL INIC
150 DO
160   CALL GUESS
170   PRINT :PRINT "Rock, paper, or scissors (1 = rock, 2 = paper, 3 = scissors, ESC = quit)"
180   DO
190     LET K$=INKEY$
200   LOOP UNTIL K$>="1" AND K$<="3" OR K$=CHR$(27)
210   IF K$=CHR$(27) THEN EXIT DO
220   LET PLCHOICE=VAL(K$)
230   LET CMSTAT(CMCHOICE)=CMSTAT(CMCHOICE)+1:LET PLSTAT(PLCHOICE)=PLSTAT(PLCHOICE)+1
240   PRINT "You chose ";CH$(PLCHOICE);" and I chose ";CH$(CMCHOICE);"."
250   SET #102:INK 3
260   IF PLCHOICE=CMCHOICE THEN
270     PRINT "Tie!"
280     LET SCORE(3)=SCORE(3)+1
290   ELSE IF CMCHOICE=PLWINS(PLCHOICE) THEN
300     PRINT "You won!"
310     LET SCORE(1)=SCORE(1)+1
320   ELSE
330     PRINT "I won!"
340     LET SCORE(2)=SCORE(2)+1
350   END IF 
360   SET #102:INK 1
370 LOOP
380 PRINT :PRINT "Some useless statistics:"
390 PRINT "You won";SCORE(1);"times, and I won";SCORE(2);"times;";SCORE(3);"ties."
400 PRINT :PRINT ,,CH$(1),CH$(2),CH$(3)
410 PRINT "You chose:",PLSTAT(1),PLSTAT(2),PLSTAT(3)
420 PRINT "  I chose:",CMSTAT(1),CMSTAT(2),CMSTAT(3)
430 END
440 DEF INIC
450   LET CH$(1)="rock":LET CH$(2)="paper":LET CH$(3)="scissors"
460   LET PLWINS(1)=3:LET PLWINS(2)=1:LET PLWINS(3)=2
470   FOR I=1 TO 3
480     LET PLSTAT(I),CMSTAT(I),SCORE(I)=0
490   NEXT
500   TEXT 80
510 END DEF
520 DEF GUESS
530   LET CMCHOICE=INT(RND*(PLSTAT(1)+PLSTAT(2)+PLSTAT(3)+3))
540   SELECT CASE CMCHOICE
550   CASE 0 TO PLSTAT(1)
560     LET CMCHOICE=2
570   CASE PLSTAT(1)+1 TO PLSTAT(1)+PLSTAT(2)+1
580     LET CMCHOICE=3
590   CASE ELSE
600     LET CMCHOICE=1
610   END SELECT
620 END DEF