21 game

You are encouraged to solve this task according to the task description, using any language you may know.
21 is a two player game, the game is played by choosing
a number (1, 2, or 3) to be added to the running total.

The game is won by the player whose chosen number causes the running total
to reach exactly 21.

The running total starts at zero.
One player will be the computer.

Players alternate supplying a number to be added to the running total.



Task

Write a computer program that will:

 do the prompting (or provide a button menu), 
 check for errors and display appropriate error messages, 
 do the additions (add a chosen number to the running total), 
 display the running total, 
 provide a mechanism for the player to quit/exit/halt/stop/close the program,
 issue a notification when there is a winner, and
 determine who goes first (maybe a random or user choice, or can be specified when the game begins). 


F select_count(game_count)
   ‘selects a random number if the game_count is less than 18. otherwise chooses the winning number’
   Int t
   I game_count < 18
      t = random:(1..3)
   E
      t = 21 - game_count
   print(‘The computer chooses #.’.format(t))
   R t
 
F request_count()
   ‘request user input between 1,2 and 3. It will continue till either quit(q) or one of those numbers is requested.’
   V t = ‘’
   L
      X.try
         t = input(‘Your choice 1 to 3 :’)
         I Int(t) C [1, 2, 3]
            R Int(t)
         E
            print(‘Out of range, try again’)
      X.catch
         I t == ‘q’
            R 0
         E
            print(‘Invalid Entry, try again’)
 
F start()
   V game_count = 0
   print("Enter q to quit at any time.\nThe computer will choose first.\nRunning total is now #.".format(game_count))
   V roundno = 1
   L
      print("\nROUND #.: \n".format(roundno))
      V t = select_count(game_count)
      game_count = game_count + t
      print("Running total is now #.\n".format(game_count))
      I game_count >= 21
         print(‘So, commiserations, the computer has won!’)
         R 0
      t = request_count()
      I t == 0
         print(‘OK,quitting the game’)
         R -1
      game_count = game_count + t
      print("Running total is now #.\n".format(game_count))
      I game_count >= 21
         print(‘So, congratulations, you've won!’)
         R 1
      roundno++
 
V c = 0
V m = 0
L
   V o = start()
   I o == -1
      L.break
   E
      c += I o == 0 {1} E 0
      m += I o == 1 {1} E 0
   print(‘Computer wins #. game, human wins #. games’.format(c, m))
   V t = input(‘Another game?(press y to continue):’)
   I t != ‘y’
      L.break