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). 


 
gamewon = false
running_total = 0
player = 1
opponent = 2
 
while not gamewon do
  num = 0
 
  if player == 1 then
    opponent = 2
    repeat
      print("Enter a number between 1 and 3 (0 to quit):")
      num = io.read("*n")
      if num == 0 then
          os.exit()
      end
    until (num > 0) and (num <=3)
  end
 
  if player == 2 and not (gamewon) then
      opponent = 1
      if (21 - running_total <= 3) then
        num = 21 - running_total
      else
        num = math.random(1,3)
      end
      print("Player 2 picks number "..num)
  end
 
  running_total = running_total + num
  print("Total: "..running_total)
 
  if running_total == 21 then
    print("Player "..player.." wins!")
    gamewon = true
  end
 
  if running_total > 21 then
    print("Player "..player.." lost...")
    print("Player "..opponent.." wins!")
    gamewon = true
  end
 
  if player == 1 then
    player = 2
  else player = 1 
  end
 
end
 