Exercise: Counting to 21

Make a fun game that requires the user to interact with the command-line interface.

We'll cover the following...

Counting to 21

Make a game with the following rules: You and the computer each take turns counting to 21, where each player increasing the number by either 1 or 2. Whoever says 21 loses.

You and me take turns say a number, the player forced to say '21' loses. At each turn, player can increase the number 1 or 2.
Do you want to go first? (y/n) y
You (1 or 2): 2
Me: 3
You (4 or 5): 4
Me: 5
You (6 or 7): 6
Me: 8
You (9 or 10): 10
Me: 11
You (12 or 13): 12
Me: 14
You (15 or 16): 16
Me: 17
You (18 or 19): 18
Me: 20
You (21 :-( ): 21
Demo of the "Counting to 21" game

Hints

  • Ruby’s modulo operator % returns the remainder of a number ...