0

I want to add timer functionality to my game, can you tell me how to add a 15 second timer to the program? After the time is over I want the output to be displayed as Try Again or something. Code is below:

import java.util.Scanner;

public class Main {

 public static void main(String [] args)
    {
        rules();

        Scanner s = new Scanner(System.in);
        boolean[] cantCut = new boolean[6];

        /* 0 - white
           1 - red
           2 - purple
           3 - black
           4 - green
           5 - orange */

        boolean exploded = false;

        for(int i = 0; i <= 5 ; i++)
        {
            String input;
            input = s.nextLine();
            switch(input)
            {
            case "white":
                if(cantCut[0])
                    exploded = true;
                else 
                {
                    cantCut[0] = true; //white
                    cantCut[1] = true; //red
                    cantCut[2] = false; //purple
                    cantCut[3] = false; //black
                    cantCut[4] = false; //green
                    cantCut[5] = false; //orange
                }
                break;
            case "red":
                if(cantCut[1])
                    exploded = true;
                else 
                {
                    cantCut[0] = true; //white
                    cantCut[1] = false; //red
                    cantCut[2] = false; //purple
                    cantCut[3] = false; //black
                    cantCut[4] = true; //green
                    cantCut[5] = true; //orange
                }
                break;
            case "purple":
                if(cantCut[2])
                    exploded = true;
                else
                {
                    cantCut[0] = true; //white
                    cantCut[1] = false; //red
                    cantCut[2] = true; //purple
                    cantCut[3] = false; //black
                    cantCut[4] = true; //green
                    cantCut[5] = true; //orange
                }
                break;
            case "black":
                if(cantCut[3])
                    exploded = true;
                else 
                {
                    cantCut[0] = true; //white
                    cantCut[1] = true; //red
                    cantCut[2] = true; //purple
                    cantCut[3] = true; //black
                    cantCut[4] = false; //green
                    cantCut[5] = true; //orange
                }
                break;
            case "green":
                if(cantCut[4])
                    exploded = true;
                else 
                {
                    cantCut[0] = false; //white
                    cantCut[1] = true; //red
                    cantCut[2] = true; //purple
                    cantCut[3] = true; //red
                    cantCut[4] = true; //black
                    cantCut[5] = false; //orange
                }
                break;
            case "orange":
                if(cantCut[5])
                    exploded = true;
                else 
                {
                    cantCut[0] = true; //white
                    cantCut[1] = false; //red
                    cantCut[2] = true; //purple
                    cantCut[3] = false; //black
                    cantCut[4] = true; //green
                    cantCut[5] = true; //orange
                }
                break;
            default:
                System.out.println("Please enter a correct choice");
            }
        }


        if(exploded)
            System.out.println("BOOM!, Terrorists Win");
        else
            System.out.println("Bomb Defused, Counter-Terrorists Win.");
        s.close();
}

 public static void rules() {
        System.out.println("If you cut a white cable you can't cut white or red cable.");
        System.out.println("If you cut a red cable it is not allowed to cut a white, green or orange one.");
        System.out.println("If you cut a purple cable you can't cut a purple, green, orange or white cable");
        System.out.println("If you cut a black cable you have to cut a green one.");
        System.out.println("If you cut a green one you have to cut an orange or white one.");
        System.out.println("If you cut an orange cable you should cut a red or black one.");
        System.out.println("Enter the 6 wires to be cut in the correct order to defuse the bomb:");
}

}
  • Ah, sorry, you are expected to do some initial research yourself first ? Did you even trythat? Like just using a search engine with the terms "java timer"? – GhostCat Dec 26 '16 at 20:36
  • Yes I did but I didnt find what I was looking for. No one wanted to stop the program after a time. – Overdrive141 Dec 26 '16 at 20:42

0 Answers0