Nim game

You are encouraged to solve this task according to the task description, using any language you may know.
Nim is a simple game where the second player ─── if they know the trick ─── will always win.



The game has only 3 rules:

   start with   12   tokens
   each player takes   1,  2,  or  3   tokens in turn
  the player who takes the last token wins.


To win every time,   the second player simply takes 4 minus the number the first player took.   So if the first player takes 1,   the second takes 3 ─── if the first player takes 2,   the second should take 2 ─── and if the first player takes 3,   the second player will take 1.



Task

Design a simple Nim game where the human player goes first, and the computer always wins. The game should enforce the rules.




 
BEGIN
 
PROCEDURE WELCOME;
BEGIN
  WRITE("THE GAME OF NIM");
  WRITE("");
  WRITE("WE BEGIN WITH 12 TOKENS. ON EACH TURN, A");
  WRITE("PLAYER MAY TAKE BETWEEN 1 AND 3 TOKENS.");
  WRITE("THE PLAYER WHO TAKES THE LAST TOKEN WINS.");
  WRITE("");
END;
 
PROCEDURE SHOW(N);
INTEGER N;
BEGIN
  WRITE("REMAINING TOKENS:",N);
END;
 
INTEGER FUNCTION GETNUM(LOWLIM, TOPLIM);
INTEGER LOWLIM, TOPLIM;
BEGIN
  INTEGER OK, N;
  OK := 0;
  WHILE OK = 0 DO
    BEGIN
      WRITE("YOU TAKE:");
      READ(N);
      IF N < LOWLIM OR N > TOPLIM THEN
        BEGIN
          WRITE("MUST TAKE BETWEEN",LOWLIM," AND",TOPLIM,".");
          WRITE("TRY AGAIN.");
        END
      ELSE
          OK := 1;
    END;
  GETNUM := N;
END;
 
INTEGER FUNCTION PLAY(PLAYER, TOKENS, TAKEN);
INTEGER PLAYER, TOKENS, TAKEN;
BEGIN
  IF PLAYER = 1 THEN      % HUMAN PLAYER'S MOVE %
    TAKEN := GETNUM(1,3)
  ELSE                    % MACHINE'S MOVE %
    TAKEN := 4 - TAKEN;
  PLAY := TAKEN;
END;
 
PROCEDURE REPORT(WINNER);
INTEGER WINNER; % MACHINE = 0, HUMAN = 1 %
BEGIN
  IF WINNER = 0 THEN
    WRITE("I TOOK THE LAST ONE, SO I WIN. SORRY ABOUT THAT.")
  ELSE
    WRITE("YOU TOOK THE LAST ONE. YOU WIN. CONGRATULATIONS!");
END;
 
% MAIN CODE BEGINS HERE %
 
INTEGER PLAYER, TOKENS, TAKEN, HUMAN, MACHINE;
 
MACHINE := 0;
HUMAN := 1;
TOKENS := 12;
TAKEN := 0;
PLAYER := HUMAN;
WELCOME;
WRITE("YOU GO FIRST.");
WHILE TOKENS > 0 DO
  BEGIN
    SHOW(TOKENS);
    TAKEN := PLAY(PLAYER, TOKENS, TAKEN);
    TOKENS := TOKENS - TAKEN;
    IF PLAYER = MACHINE THEN WRITE("I TOOK:",TAKEN);
    IF TOKENS > 0 THEN PLAYER := 1 - PLAYER;
  END;
REPORT(PLAYER);
WRITE("THANKS FOR PLAYING!");
 
END 