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.




@echo off
setlocal enabledelayedexpansion
 
set choice1=rock
set choice2=paper
set choice3=scissors
set freq1=0
set freq2=0
set freq3=0
set games=0
set won=0
set lost=0
set tie=0
 
:start
cls
echo Games - %games% : Won - %won% : Lost - %lost% : Ties - %tie%
choice /c RPS /n /m "[R]ock, [P]aper or [S]cissors? "
 
set choice=%errorlevel%
rem used [1,1000] as interval for random
if %games% equ 0 (
    rem at the beginning, there's no bias for each choice
    set /a "rocklimit=1000 / 3"
    set /a "scissorslimit=1000 * 2 / 3"
) else (
    set /a "rocklimit=1000 * %freq3% / %games%"
    set /a "scissorslimit=1000 * (%freq1% + %freq3%) / %games%"
)
set /a "randchoice=%random% %% 1000 + 1"
set compchoice=2
if %randchoice% geq %scissorslimit% set compchoice=3
if %randchoice% leq %rocklimit% set compchoice=1
 
cls
echo Player: !choice%choice%! ^|vs^| Computer: !choice%compchoice%!
goto %compchoice%
 
:1 
if %choice%==1 goto tie
if %choice%==2 goto win
if %choice%==3 goto loss
 
:2
if %choice%==1 goto loss
if %choice%==2 goto tie
if %choice%==3 goto win
 
:3 
if %choice%==1 goto win
if %choice%==2 goto loss
if %choice%==3 goto tie
 
:win
set /a "won+=1"
echo Player wins^!
goto end
 
:loss
set /a "lost+=1"
echo Computer Wins^!
goto end
 
:tie
set /a "tie+=1"
echo Tie^!
goto end
 
:end
set /a "games+=1"
set /a "freq%choice%+=1"
pause
goto start