21 Stick Game in C#

21 Stick Game  

Image-1.jpg
 
If there are 21 sticks and two players - user & computer.
 
Rules for the game are as follows:

  1. There are 21 matchsticks.
  2. First turn is of user and computer asks the player to pick 1, 2, 3 or 4 matchsticks.
  3. After the person picks, the computer does its picking. Computer will pick up maximum 4 sticks.
  4. Turns will be one by one.
  5. Now who ever picks up the last key will lose the game and we have to make sure in our program that Computer must win.

Logic Implemented
 
The computer asks the player to pick 1, 2, 3 or 4 matchsticks.

So if we reduce the total by 5 each round, the sequence will go

21 16 11 6 1

In effect, whatever number the user picks (n), the computer picks 5-n
 
Program for 21 stick game

using System;

 

namespace _21stick_game

{

    class Program

    {

        static void Main(string[] args)

        {

            int stick = 21, user, computer;

            while (stick > 0)

            {

 

                Console.WriteLine("There are {0} Sticks Choose Either 1,2,3 or 4 stick", stick);

                user = Convert.ToInt32(Console.ReadLine());

                stick = stick - user;

                if (stick <= 0)

                {

                    Console.WriteLine("You Lose");

                }

                else

                {

                    computer = 5 - user;

                    Console.WriteLine("Computer picks {0} stick", computer);

                    stick = stick - computer;

                    if (stick <= 0)

                    {

                        Console.WriteLine("You Win");

                    }

                }

            }

            Console.ReadLine();

        }

    }

}