Simple & Fully working Dice game in Java Programming Language

Avatar

 BY TEJAS




As we see in the last post Faces On Dice java example, we declare there that we come with "Dice game in java" program."

In this example, we will show a dice game in java, which works fully as android dice game, or other simple game which works fully automatic. This is simple game which gives output randomly (every time new).
And this example is fully working on any IDE, As you saw in this output of this program, I used "IntelliJ IDE" which is very beautiful and best IDE for Java Developement.

Also Check:-
  • Addition of two numbers in Java



  • This example understands just diamond level, java programmer. It's meaning is not that begineers do not see this example. For begineers, this example gives confidence, an idea bout what java can do?. As we know, Java is a compelling programming language.

    Dice Game

    Turn in into our "Dice game in java" program.
    First we started with our simple java framework which we all knows.
    In this example we assign every function in private keyword, which declares members access as an individual.
    We added arrays in this program for returning the functions.
    Also, we added a Switch....casestatement in this.
    We used boolean keyword for making program in just two answers like as we know that boolean work as a True or False, Yes or No, & Right or Wrong.

    Also Check:-
  • Faces on Dice Java Program


  • The dice game gives every time different faces on dice on the output screen every time the new winner is there, such as in some time player wins computer loss or vice versa.

    In below video shows program how works


    below here is a Java code and output of Java program.


    Remember, In java file name of java program is same as public class name. Be sure you are right in your program.


    Demo.java :-
        
     
    import java.util.Random;
    
    public class Demo {
    
        private boolean[][] y, p;
        private char in, out, w;        
                    
        public static void main(String[] args) {
            new Demo().start(
                new Random().nextInt(6) + 1, 
                new Random().nextInt(6) + 1);
        }
        
        private void start(int you, int pc) {
            in = (char) 9617;
            out = (char) 9618;
            w = (char) 9608;
            y = new boolean[12][19];
            p = new boolean[12][19];    
            numbers(p, pc); // generate Array
            numbers(y, you);
            for(int i= 0; i < p.length; i++) {
                write(i, y[i], p[i]);
            }
            System.out.printf(
            "%sYou% pc ? "Won" : 
            you == pc ? "  Draw" : "Lost");
        }
        
        private void numbers(boolean [][] b, int num) {
            switch(num){
                case 1:  set(b, new int[] {5, 8}); break;
                case 3:  set(b, new int[] {5, 8});
                case 2:  set(b, new int[] {2, 3}, 
                                new int[] {8, 13}); break;    
                default: set(b, new int[] {2, 3, 13},
                                new int[] {8, 3, 13});   
                switch(num) { // switch default 
                case 6: set(b, new int[] {5, 3,13}); break;
                case 5: set(b, new int[] {5, 8}); break;
                } 
            }
        }    
        
        private void set(boolean[][] b, int[]... x) {
            for(int i = 0; i < x.length; i++){
                for(int j = 1; j < x[i].length; j++){
                    for(int k = 0; k < 3; k++){
                        b[x[i][0]][x[i][j] + k] = true;
                        b[x[i][0] + 1][x[i][j] + k] = true;
                    }
                }
            }
        }
        
        private void write(int i, boolean[]... b) {
            for(int x =0; x < b.length; x++){
                for(int j = 0; j < b[x].length; j++) {
                    System.out.print(
                        (i == 0 || i == y.length - 1) ||
                        (j == 0 || j == b[x].length - 1) &&
                        (i != 0 || i != y.length - 1) ?
                        in : !b[x][j] ? out : w);
                }
                System.out.print(" ");
            }
            System.out.println();
        }
    }
        
    


    output :-
        
         
        ░░░░░░░░░░░░░░░░░░░ ░░░░░░░░░░░░░░░░░░░ 
        ░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░ ░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░ 
        ░▒▒███▒▒▒▒▒▒▒▒▒▒▒▒░ ░▒▒███▒▒▒▒▒▒▒███▒▒░ 
        ░▒▒███▒▒▒▒▒▒▒▒▒▒▒▒░ ░▒▒███▒▒▒▒▒▒▒███▒▒░ 
        ░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░ ░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░ 
        ░▒▒▒▒▒▒▒███▒▒▒▒▒▒▒░ ░▒▒▒▒▒▒▒███▒▒▒▒▒▒▒░ 
        ░▒▒▒▒▒▒▒███▒▒▒▒▒▒▒░ ░▒▒▒▒▒▒▒███▒▒▒▒▒▒▒░ 
        ░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░ ░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░ 
        ░▒▒▒▒▒▒▒▒▒▒▒▒███▒▒░ ░▒▒███▒▒▒▒▒▒▒███▒▒░ 
        ░▒▒▒▒▒▒▒▒▒▒▒▒███▒▒░ ░▒▒███▒▒▒▒▒▒▒███▒▒░ 
        ░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░ ░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░ 
        ░░░░░░░░░░░░░░░░░░░ ░░░░░░░░░░░░░░░░░░░ 
                 You                  Player
                          Lost!
        Process finished with exit code 0
            
        
      
        
            import java.util.Random;
    
            public class Demo {
            
                private boolean[][] y, p;
                private char in, out, w;        
                            
                public static void main(String[] args) {
                    new Demo().start(
                        new Random().nextInt(6) + 1, 
                        new Random().nextInt(6) + 1);
                }
                
                private void start(int you, int pc) {
                    in = (char) 9617;
                    out = (char) 9618;
                    w = (char) 9608;
                    y = new boolean[12][19];
                    p = new boolean[12][19];    
                    numbers(p, pc); // generate Array
                    numbers(y, you);
                    for(int i= 0; i < p.length; i++) {
                        write(i, y[i], p[i]);
                    }
                    System.out.printf(
                    "%sYou% pc ? "Won" : 
                    you == pc ? "  Draw" : "Lost");
                }
                
                private void numbers(boolean [][] b, int num) {
                    switch(num){
                        case 1:  set(b, new int[] {5, 8}); break;
                        case 3:  set(b, new int[] {5, 8});
                        case 2:  set(b, new int[] {2, 3}, 
                                        new int[] {8, 13}); break;    
                        default: set(b, new int[] {2, 3, 13},
                                        new int[] {8, 3, 13});   
                        switch(num) { // switch default 
                        case 6: set(b, new int[] {5, 3,13}); break;
                        case 5: set(b, new int[] {5, 8}); break;
                        } 
                    }
                }    
                
                private void set(boolean[][] b, int[]... x) {
                    for(int i = 0; i < x.length; i++){
                        for(int j = 1; j < x[i].length; j++){
                            for(int k = 0; k < 3; k++){
                                b[x[i][0]][x[i][j] + k] = true;
                                b[x[i][0] + 1][x[i][j] + k] = true;
                            }
                        }
                    }
                }
                
                private void write(int i, boolean[]... b) {
                    for(int x =0; x < b.length; x++){
                        for(int j = 0; j < b[x].length; j++) {
                            System.out.print(
                                (i == 0 || i == y.length - 1) ||
                                (j == 0 || j == b[x].length - 1) &&
                                (i != 0 || i != y.length - 1) ?
                                in : !b[x][j] ? out : w);
                        }
                        System.out.print(" ");
                    }
                    System.out.println();
                }
            }
        
    


    output :-
        
         
                 
        ░░░░░░░░░░░░░░░░░░░ ░░░░░░░░░░░░░░░░░░░ 
        ░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░ ░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░ 
        ░▒▒███▒▒▒▒▒▒▒▒▒▒▒▒░ ░▒▒███▒▒▒▒▒▒▒███▒▒░ 
        ░▒▒███▒▒▒▒▒▒▒▒▒▒▒▒░ ░▒▒███▒▒▒▒▒▒▒███▒▒░ 
        ░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░ ░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░ 
        ░▒▒▒▒▒▒▒███▒▒▒▒▒▒▒░ ░▒▒▒▒▒▒▒███▒▒▒▒▒▒▒░ 
        ░▒▒▒▒▒▒▒███▒▒▒▒▒▒▒░ ░▒▒▒▒▒▒▒███▒▒▒▒▒▒▒░ 
        ░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░ ░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░ 
        ░▒▒▒▒▒▒▒▒▒▒▒▒███▒▒░ ░▒▒███▒▒▒▒▒▒▒███▒▒░ 
        ░▒▒▒▒▒▒▒▒▒▒▒▒███▒▒░ ░▒▒███▒▒▒▒▒▒▒███▒▒░ 
        ░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░ ░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░ 
        ░░░░░░░░░░░░░░░░░░░ ░░░░░░░░░░░░░░░░░░░ 
                 You                  Player
                          Lost!
        Process finished with exit code 0