[Solved] Carrom with Java

If Cricket is the most popular outdoor game in India, Carrom is not too behind as one of most played indoor games.
Let’s now make use of our knowlege in operators & conditional statements  to compute the points scored at the end of a round in Carrom Game.

Carrom is a board game where two participants (teams) play. It consists of 9 white coins, 9 black coins and a red coin. The first team that finishes all their coins wins (given that red has been pocketed by one of the teams). The points are awarded based on the number of left-over coins of the opposition (loser) in the board. If the winning team has pocketed the red, they get an additional 5 points. Write a program to compute the score of winner at the end of a round.

If the number of coins left on the board is either less than 1 or greater than 9 display “Invalid Input”.
Create a main class with the name “Main”.

Input Format:

Input consists of a single integer which corresponds to number of coins left on board and a character which corresponds to whether the winning team has pocketed red or not. 

Output Format:

Output corresponds to the total points won.
Print Invalid Input in case the no of coins is less than one or greater than nine.
[All text in bold corresponds to input and the rest corresponds to output] 

Sample Input and Output 1:

Enter number of lost team’s coins left on board

5

Has winning team pocketed red [y or n] ?

y

Points won : 10

Sample Input and Output 2:

Enter number of lost team’s coins left on board

-1

Invalid Input

Solution

import java.util.*;

class Main

{

   public static void main(String args[])

   {

       Scanner Sc = new Scanner(System.in);

       int coins;

       char red;

       System.out.print("Enter number of lost team's coins left on board\n");

       coins = Sc.nextInt();

       if(coins < 1 || coins > 9)

       {

           System.out.print("Invalid Input");

       }

       else

       {

           System.out.print("Has winning team pocketed red [y or n] ?\n");

           red = Sc.next().charAt(0);

           if(red == 'y')

           {

               System.out.print("Points won : " + (coins + 5));

           }

           else

           {

               System.out.print("Points won : " + coins);

           }

       }

   }

}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

πŸ“Œ Nodejs and Android 😎
πŸ“Œ Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *