package edu.bloomu.ch5a;

import java.util.concurrent.ThreadLocalRandom;

/**
 * Plays the game of Quagga.  The game is played with three dice. You roll the dice and
 * win if you roll a quagga, otherwise you lose. A quagga arises when it is possible to 
 * concatenate two of the dice values to obtain a number that is a multiple of the other 
 * die value. For example, if you roll 4, 6, and 3, that is a quagga because you can 
 * concatenate 3 and 6 to get 36, which is a multiple of 4. 
 * 
 * One small catch: Ones do not count. Any roll that contains a one is a losing roll.
 * 
 * @author Drue Coles
 */
public class Quagga { 

    public static void main(String[] args) {
        
        // roll three dice
        ThreadLocalRandom rand = ThreadLocalRandom.current();
        int die1 = rand.nextInt(1, 7);
        int die2 = rand.nextInt(1, 7);
        int die3 = rand.nextInt(1, 7);
        System.out.println(die1 + " " + die2 + " " + die3);
        
        // ouput win/lose message
        if (isQuagga(die1, die2, die3)) {
            System.out.println("You have a QUAGGA!");
        } else {
            System.out.println("Sorry, no QUAGGA for you.");
        }
    }   

    /**
     * Returns true if three given dice values form a quagga, false otherwise.
     */
    private static boolean isQuagga(int x, int y, int z) {
        
        // Any roll that contains a one is a losing roll.
        if (x == 1 || y == 1 || z == 1) {
            return false;
        }
        
        // There are 6 ways to concatenate two of the digits. For each, see if the
        // resulting number is a multiple of the other digit.
        if ((10 * y + z) % x == 0) { 
            return true;
        }
        if ((10 * z + y) % x == 0) { 
            return true;
        }
        if ((10 * x + z) % y == 0) {
            return true;
        }
        if ((10 * z + x) % y == 0) {
            return true;
        }
        if ((10 * x + y) % z == 0) {
            return true;
        }
        return (10 * y + x) % z == 0;
    }
}