Develop 21 Matchstick Game in Java

21 Matchstick Game:

It is a simple console-based gaming application developed in Java. It starts with a collection of 21 one-sticks and prompts the user to pick either one or two sticks in one attempt. Based on the user’s choice, the computer makes its own choice. Eventually, whosoever picks the last stick loses the game. The user is prompted at the beginning of the game to choose whether he wants to play first.

The various variables used in this program are:
1. TwentyOneSticks: It is the main class which contains the entire code.
2. no_of_sticks: It is the variable that stores the number of sticks currently available. Initially, it is assigned the value of 21.
3. input_num: It stores the number of sticks chosen by the user.

21 Matchstick Game in Java:

import java.util.Scanner;
public class TwentyOneSticks
{
public static void main(String[] args)
{
int no_of_sticks = 21;
System.out.println("Do you want to play first?");
Scanner input =new Scanner(System.in);
String first_attempt=input.nextLine();
Scanner read_next=new Scanner(System.in);
int input_num=0;
while(no_of_sticks>0)
{
if(first_attempt.equals("y") || first_attempt.equals("Y"))
{
System.out.println("Currently " +no_of_sticks +" sticks are available");
System.out.println("Pick your sticks (1 or 2)");
input_num=read_next.nextInt();
if(input_num>2)
input_num=2;
else if(input_num<1)
input_num=1;
no_of_sticks=no_of_sticks-input_num;
if(no_of_sticks<=0)
System.out.println("You have lost the game!");
else
{
if((no_of_sticks-2) % 3 == 0 || (no_of_sticks-2)==0)
input_num=1;
else
input_num=2;
System.out.println("Your opponent picks " + input_num+ " sticks");
no_of_sticks=no_of_sticks-input_num;
if(no_of_sticks<=0)
System.out.println("You have won the game!");
}
}
else
{
if((no_of_sticks-2) % 3 == 0 || (no_of_sticks-2) == 0)
input_num=1;
else
input_num=2;
System.out.println("Your opponent picks " + input_num+ " sticks");
no_of_sticks=no_of_sticks-input_num;
if(no_of_sticks<=0) System.out.println("You have won the game!"); else { System.out.println("Currently " + no_of_sticks + " sticks are available"); System.out.println("Pick your sticks (1 or 2)"); input_num=read_next.nextInt(); if(input_num>2)
input_num=2;
else if(input_num<1)
input_num=1;
no_of_sticks=no_of_sticks-input_num;
if(no_of_sticks<=0)
System.out.println("You have lost the game!");
}
}
}
}
}

Output:

Do you want to play first?
y
Currently 21 sticks are available
Pick your sticks (1 or 2)
2
Your opponent picks 2 sticks
Currently 17 sticks are available
Pick your sticks (1 or 2)
2
Your opponent picks 2 sticks
Currently 13 sticks are available
Pick your sticks (1 or 2)
2
Your opponent picks 1 sticks
Currently 10 sticks are available
Pick your sticks (1 or 2)
2
Your opponent picks 1 sticks
Currently 7 sticks are available
Pick your sticks (1 or 2)
2
Your opponent picks 1 sticks
Currently 4 sticks are available
Pick your sticks (1 or 2)
2
Your opponent picks 1 sticks
Currently 1 sticks are available
Pick your sticks (1 or 2)
1
You have lost the game!