Master java skills

Java console based gaming app

In this tutorial, we will build a gaming app in which two player will play. Every player will throw dice for three times. player with maximum score will win the match.

Gaming App description

In this gaming app, consider following points

  1. Two players can play
  2. Toss will decide which player will play first
  3. After winning the toss, the player will throw the dice and then second player will throw the dice.
  4. Each player will throw the dice three times alternatively
  5. Total of 3 moves will be counted and the player will max total will be the winner
package com.javatrainingschool;

import java.util.Random;
import java.util.Scanner;

public class GamingMain {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		Random random = new Random();

		System.out.println("Welcome to Java Training School gaming app:");
		System.out.println("Enter player1 name : ");
		String player1 = sc.nextLine();

		System.out.println("Enter player2 name : ");
		String player2 = sc.nextLine();

		System.out.println("Let's have the toss. Player one will toss the coin. What is your"
				+ " call player two ? Enter H for Head. T for Tail");

		String tossCall = sc.nextLine();
		int tossOutput = random.nextInt(2) + 1;

		String tossWinner = null;
		String tossLoser = null;

		if (tossOutput == 1 && tossCall.equalsIgnoreCase("H")) {
			tossWinner = player2;
			tossLoser = player1;
		} else {
			tossWinner = player1;
			tossLoser = player2;
		}
		System.out.println(tossWinner + " has won the toss.");

		int totalOfTossWinner = 0;
		int totalOfTossLoser = 0;

		for (int i = 1; i <= 3; i++) {

			System.out.println(tossWinner + " press enter key to throw the dice");
			String enterKey = sc.nextLine();
			int diceOutput1 = 0;
			if ("".equalsIgnoreCase(enterKey)) {
				diceOutput1 = random.nextInt(6) + 1;
			}
			totalOfTossWinner += diceOutput1;
			System.out.println(tossWinner + " : chance " + i + " .Dice output is : " + diceOutput1);
			System.out.println("");
			System.out.println(tossLoser + " press enter key to throw the dice");
			enterKey = sc.nextLine();
			int diceOutput2 = 0;
			if ("".equalsIgnoreCase(enterKey)) {
				diceOutput2 = random.nextInt(6) + 1;
			}
			totalOfTossLoser += diceOutput2;
			System.out.println(tossLoser + " : chance " + i + " .Dice output is : " + diceOutput2);

			System.out.println("__________________________________________________");

		}

		System.out.println("Total score for " + tossWinner + " = " + totalOfTossWinner);
		System.out.println("Total score for " + tossLoser + " = " + totalOfTossLoser);

		if (totalOfTossWinner == totalOfTossLoser) {
			System.out.println("The match is a tie");
		} else {

			String winner = (totalOfTossWinner > totalOfTossLoser) ? tossWinner : tossLoser;

			System.out.println("Hence the winner is : " + winner);
		}
	}
}
Output :

Welcome to Java Training School gaming app:
Enter player1 name : 
Ram
Enter player2 name : 
Shyam
Let's have the toss. Player one will toss the coin. What is your call player two ? Enter H for Head. T for Tail
H
Shyam has won the toss.
Shyam press enter key to throw the dice

Shyam : chance 1 .Dice output is : 5

Ram press enter key to throw the dice

Ram : chance 1 .Dice output is : 3
__________________________________________________
Shyam press enter key to throw the dice

Shyam : chance 2 .Dice output is : 5

Ram press enter key to throw the dice

Ram : chance 2 .Dice output is : 5
__________________________________________________
Shyam press enter key to throw the dice

Shyam : chance 3 .Dice output is : 1

Ram press enter key to throw the dice

Ram : chance 3 .Dice output is : 1
__________________________________________________
Total score for Shyam = 11
Total score for Ram = 9
Hence the winner is : Shyam