3
\$\begingroup\$

I have made a text-based Snakes and Ladders game in Java with only simple concepts like Scanner and Arrays. I don't know how to use different methods or to use inheritance to make the code shorter. Can anyone please advise me on how to write it efficiently?

Some specific things I want to do are:

  1. You will notice that the code for printing the board is repeated a lot. Can that be made into a method to avoid the repetition?
  2. After each player's turn, I am using a very long if-else-if ladder to check if the player has landed on a snake. Can that long ladder be shortened?

I have attached an image of the output:

Output

import java.util.Scanner;
public class Project
{
    public static void board()
    {
        /**This is a game of 4-player Snakes_&_Ladders which uses
         * Arrays to construct the board. For the dice, Math.random()
         * has been used.
         */
        Scanner sc=new Scanner(System.in);
        char retry='y';
        System.out.println("Enter \'Start\' to start the game:");
        do
        {
            String start=sc.nextLine();
            System.out.println("The game will start in :");
            for(short x=5; x>=1; x--)
            {
                System.out.println(x);
                for(long i=-9999999; i<=9999999; i++);//To add a delay
            }

            System.out.println("\f---------------------------------");
            System.out.println("Welcome to Java Snakes & Ladders!");
            System.out.println("---------------------------------\n");
            System.out.println("Rules:");
            System.out.println("--> This is similar to the Snakes & Ladders game played by many people.");
            System.out.println("--> This is a four-player game.");
            System.out.println("--> There will be a 10x10 board containing some snakes and ladders.");
            System.out.println("--> The players will take turns to roll a dice.");
            System.out.println("--> The player will move ahead according to the number rolled.");
            System.out.println("--> If a player lands on a ladder, he will be able to climb it and go ahead!!");
            System.out.println("--> But if a player lands on a snake, he will have to go back!!");
            System.out.println("--> The game will go on until a player reaches 100.\n");

            System.out.println("Enter Player 1's name:");
            String player1=sc.nextLine();//Input player 1's name
            System.out.println("Enter Player 2's name:");
            String player2=sc.nextLine();//Input player 2's name
            System.out.println("Enter Player 3's name:");
            String player3=sc.nextLine();//Input player 3's name
            System.out.println("Enter Player 4's name:");
            String player4=sc.nextLine();//Input player 4's name
            System.out.println("\f");

            int[] board=new int[100];//To store the numbers of the boxes
            String[] p1=new String[100];//To store player-1's position
            String[] p2=new String[100];//To store player-2's position
            String[] p3=new String[100];//To store player-3's position
            String[] p4=new String[100];//To store player-4's position
            String[] SnakesnLadders=new String[101];//To store the Snakes and Ladders
            int order=0;//To specify if the boxes in the row are descending or ascending
            int t=99;//Index of the numbers in board array
            int s_and_l=99;//Index of SnakesnLadders array
            int p1_val=0;//Position of player-1
            int p2_val=0;//Position of player-2
            int p3_val=0;//Position of player-3
            int p4_val=0;//Position of player-4

            for (int fill=99; fill>=0; fill--)
            {
                //This loop is for filling all the arrays
                SnakesnLadders[fill]=" ";
                //Snakes
                if(fill==99-1)SnakesnLadders[fill]="    Slip to 2";
                if(fill==91-1)SnakesnLadders[fill]="   Slip to 75";
                if(fill==87-1)SnakesnLadders[fill]="   Slip to 24";
                if(fill==49-1)SnakesnLadders[fill]="   Slip to 23";
                if(fill==17-1)SnakesnLadders[fill]="   Slip to 3";
                if(fill==51-1)SnakesnLadders[fill]="   Slip to 47";
                if(fill==37-1)SnakesnLadders[fill]="   Slip to 29";

                //Ladders
                if(fill==19-1)SnakesnLadders[fill]="  Climb to 80";
                if(fill==4-1)SnakesnLadders[fill]="  Climb to 14";
                if(fill==55-1)SnakesnLadders[fill]="  Climb to 63";
                if(fill==33-1)SnakesnLadders[fill]="  Climb to 83";
                if(fill==82-1)SnakesnLadders[fill]="  Climb to 97";
                if(fill==16-1)SnakesnLadders[fill]="  Climb to 25";
                if(fill==9-1)SnakesnLadders[fill]="  Climb to 39";

                p1[fill]="";
                p2[fill]="";
                p3[fill]="";
                p4[fill]="";
                board[fill]=fill+1;//Numbers of boxes
            }
            System.out.println("\u278A -->"+player1+"\t\t\u278B -->"+player2+"\t\t\u278C -->"+player3+"\t\t\u278D -->"+player4);
            for (int dash=1; dash<=161; dash++)
                System.out.print("-");
            System.out.println("");
            for (int boardi=1; boardi<=10; boardi++)
            {
                //This loop is for the rows

                /*The below if-elseif blocks are to check if
                 *the row numbers are in ascending or descending order.
                 */
                if(boardi%2==0)
                {
                    t=t-9;
                    s_and_l=s_and_l-9;
                    order=1;
                }
                else if(boardi!=1) {
                    t=t-11;
                    s_and_l=s_and_l-11;
                    order=0;
                }
                for (long boardj=1; boardj<=10; boardj++)
                {
                    System.out.print(p1[t]+p2[t]+"\t"+board[t]+p3[t]+p4[t]+"\t|");
                    if(order==1)t++;
                    else if(order==0)t--;
                }
                System.out.println("");
                for (long boardj=1; boardj<=10; boardj++)
                {
                    if(order==0)
                    {
                        if(boardj==1)
                        {
                            if(SnakesnLadders[s_and_l]!=" ")
                            System.out.print(SnakesnLadders[s_and_l]+"\t|");
                            else if(SnakesnLadders[s_and_l-1]!=" ")
                            System.out.print("\t"+SnakesnLadders[s_and_l]+"\t|");
                            else
                            System.out.print("\t"+SnakesnLadders[s_and_l]+"\t|\t");
                        }
                        else if(SnakesnLadders[s_and_l-1]!=" ")
                        System.out.print(SnakesnLadders[s_and_l]+"\t|");
                        else
                        System.out.print(SnakesnLadders[s_and_l]+"\t|\t");
                    }
                    if(order==1)
                    {
                        if(boardj==1)
                        {
                            if(SnakesnLadders[s_and_l]!=" ")
                            System.out.print(SnakesnLadders[s_and_l]+"\t|");
                            else if(SnakesnLadders[s_and_l+1]!=" ")
                            System.out.print("\t"+SnakesnLadders[s_and_l]+"\t|");
                            else
                            System.out.print("\t"+SnakesnLadders[s_and_l]+"\t|\t");
                        }
                        else if(SnakesnLadders[s_and_l+1]!=" ")
                        System.out.print(SnakesnLadders[s_and_l]+"\t|");
                        else
                        System.out.print(SnakesnLadders[s_and_l]+"\t|\t");
                    } 
                    if(order==1)s_and_l++;
                    else if(order==0)s_and_l--;
                }
                System.out.println("");
                for (int dash=1; dash<=161; dash++)
                System.out.print("-");
                System.out.println("");
            }
            for(long chance=1; chance>=0; chance++)
            {
                //Main loop which runs until a player reaches 100
                t=99;
                s_and_l=99;
                order=0;
                if((chance-1)%4==0)
                {
                    //Player 1's block
                    System.out.println("It is "+player1+"\'s turn:");
                    System.out.println("Enter \'Roll\' to roll the dice:");
                    char pause=sc.next().charAt(0);
                    System.out.println("\f");
                    int dice=(int)(Math.random()*6+1);//To generate a number betwween 1 & 6
                    p1_val=p1_val+dice;
                    if(p1_val==100)
                    {
                        System.out.println("==================");
                        System.out.println(player1+" WON!!");
                        System.out.println("==================");
                        System.out.println("It was a great game!!");
                        break;
                    }
                    else if(p1_val>100)
                    {
                        System.out.println("You rolled more than you require to reach 100!!");
                        System.out.println("You can't move ahead!!");
                        p1_val=p1_val-dice;
                    }
                    for (int pfill=0; pfill<100; pfill++)
                    {
                        p1[pfill]=" ";//To refill the board
                    }
                    p1[p1_val-1]=" \u278A";
                    System.out.println("\u278A -->"+player1+"\t\t\u278B -->"+player2+"\t\t\u278C -->"+player3+"\t\t\u278D -->"+player4);
                    for (int dash=1; dash<=161; dash++)
                    System.out.print("-");
                    System.out.println("");
                    for (int boardi=1; boardi<=10; boardi++)
                    {
                        if(boardi%2==0)
                        {
                            t=t-9;
                            s_and_l=s_and_l-9;
                            order=1;
                        }
                        else if(boardi!=1) {
                            t=t-11;
                            s_and_l=s_and_l-11;
                            order=0;
                        }
                        for (long boardj=1; boardj<=10; boardj++)
                        {
                            System.out.print(p1[t]+p2[t]+"\t"+board[t]+p3[t]+p4[t]+"\t|");
                            if(order==1)t++;
                            else if(order==0)t--;
                        }
                        System.out.println("");
                        for (long boardj=1; boardj<=10; boardj++)
                        {
                            if(order==0)
                            {
                                if(boardj==1)
                                {
                                    if(SnakesnLadders[s_and_l]!=" ")
                                    System.out.print(SnakesnLadders[s_and_l]+"\t|");
                                    else if(SnakesnLadders[s_and_l-1]!=" ")
                                    System.out.print("\t"+SnakesnLadders[s_and_l]+"\t|");
                                    else
                                    System.out.print("\t"+SnakesnLadders[s_and_l]+"\t|\t");
                                }
                                else if(SnakesnLadders[s_and_l-1]!=" ")
                                System.out.print(SnakesnLadders[s_and_l]+"\t|");
                                else
                                System.out.print(SnakesnLadders[s_and_l]+"\t|\t");
                            }
                            if(order==1)
                            {
                                if(boardj==1){
                                    if(SnakesnLadders[s_and_l]!=" ")
                                    System.out.print(SnakesnLadders[s_and_l]+"\t|");
                                    else if(SnakesnLadders[s_and_l+1]!=" ")
                                    System.out.print("\t"+SnakesnLadders[s_and_l]+"\t|");
                                    else
                                    System.out.print("\t"+SnakesnLadders[s_and_l]+"\t|\t");
                                }
                                else if(SnakesnLadders[s_and_l+1]!=" ")
                                System.out.print(SnakesnLadders[s_and_l]+"\t|");
                                else
                                System.out.print(SnakesnLadders[s_and_l]+"\t|\t");
                            } 
                            if(order==1)s_and_l++;
                            else if(order==0)s_and_l--;
                        }
                        System.out.println("");
                        for (int dash=1; dash<=161; dash++)
                        System.out.print("-");
                        System.out.println("");
                    }
                    System.out.println("Roll = "+dice);
                    if(SnakesnLadders[p1_val-1]!=" ")
                    {
                        //Snakes
                        if(p1_val==99)
                        {
                            System.out.println("Oh no You landed on a snake!!!");
                            System.out.println("You Slip to 2!!");
                            p1_val=2;
                        }
                        if(p1_val==91)
                        {
                            System.out.println("Oh no You landed on a snake!!!");
                            System.out.println("You Slip to 75!!");
                            p1_val=75;
                        }
                        if(p1_val==87)
                        {
                            System.out.println("Oh no You landed on a snake!!!");
                            System.out.println("You Slip to 24!!");
                            p1_val=24;
                        }
                        if(p1_val==51)
                        {
                            System.out.println("Oh no You landed on a snake!!!");
                            System.out.println("You Slip to 47!!");
                            p1_val=47;
                        }
                        if(p1_val==49)
                        {
                            System.out.println("Oh no You landed on a snake!!!");
                            System.out.println("You Slip to 23!!");
                            p1_val=23;
                        }
                        if(p1_val==37)
                        {
                            System.out.println("Oh no You landed on a snake!!!");
                            System.out.println("You Slip to 29!!");
                            p1_val=29;
                        }
                        if(p1_val==17)
                        {
                            System.out.println("Oh no You landed on a snake!!!");
                            System.out.println("You Slip to 3!!");
                            p1_val=3;
                        }
                        //Ladders
                        if(p1_val==82)
                        {
                            System.out.println("WOW! You landed on a ladder!!!");
                            System.out.println("You Climb to 97!!");
                            p1_val=97;
                        }
                        if(p1_val==55)
                        {
                            System.out.println("WOW! You landed on a ladder!!!");
                            System.out.println("You Climb to 63!!");
                            p1_val=63;
                        }
                        if(p1_val==33)
                        {
                            System.out.println("WOW! You landed on a ladder!!!");
                            System.out.println("You Climb to 83!!");
                            p1_val=83;
                        }
                        if(p1_val==19)
                        {
                            System.out.println("WOW! You landed on a ladder!!!");
                            System.out.println("You Climb to 80!!");
                            p1_val=80;
                        }
                        if(p1_val==16)
                        {
                            System.out.println("WOW! You landed on a ladder!!!");
                            System.out.println("You Climb to 25!!");
                            p1_val=25;
                        }
                        if(p1_val==9)
                        {
                            System.out.println("WOW! You landed on a ladder!!!");
                            System.out.println("You Climb to 39!!");
                            p1_val=39;
                        }
                        if(p1_val==4)
                        {
                            System.out.println("WOW! You landed on a ladder!!!");
                            System.out.println("You Climb to 14!!");
                            p1_val=14;
                        }
                        t=99;
                        s_and_l=99;
                        order=0;
                        for(long i=-99099999; i<=9999999; i++);
                        System.out.print("\f");
                        for (int pfill=0; pfill<100; pfill++){
                            p1[pfill]=" ";
                        }
                        p1[p1_val-1]=" \u278A";
                        System.out.println("\u278A -->"+player1+"\t\t\u278B -->"+player2+"\t\t\u278C -->"+player3+"\t\t\u278D -->"+player4);
                        for (int dash=1; dash<=161; dash++)
                        System.out.print("-");
                        System.out.println("");
                        for (int boardi=1; boardi<=10; boardi++){
                        if(boardi%2==0)
                        {
                            t=t-9;
                            s_and_l=s_and_l-9;
                            order=1;
                        }
                        else if(boardi!=1) {
                            t=t-11;
                            s_and_l=s_and_l-11;
                            order=0;
                        }
                        for (long boardj=1; boardj<=10; boardj++){
                            System.out.print(p1[t]+p2[t]+"\t"+board[t]+p3[t]+p4[t]+"\t|");
                            if(order==1)t++;
                            else if(order==0)t--;
                        }
                        System.out.println("");
                        for (long boardj=1; boardj<=10; boardj++){
                            if(order==0)
                            {
                                if(boardj==1){
                                    if(SnakesnLadders[s_and_l]!=" ")
                                    System.out.print(SnakesnLadders[s_and_l]+"\t|");
                                    else if(SnakesnLadders[s_and_l-1]!=" ")
                                    System.out.print("\t"+SnakesnLadders[s_and_l]+"\t|");
                                    else
                                    System.out.print("\t"+SnakesnLadders[s_and_l]+"\t|\t");
                                }
                                else if(SnakesnLadders[s_and_l-1]!=" ")
                                System.out.print(SnakesnLadders[s_and_l]+"\t|");
                                else
                                System.out.print(SnakesnLadders[s_and_l]+"\t|\t");
                            }
                            if(order==1)
                            {
                                if(boardj==1){
                                    if(SnakesnLadders[s_and_l]!=" ")
                                    System.out.print(SnakesnLadders[s_and_l]+"\t|");
                                    else if(SnakesnLadders[s_and_l+1]!=" ")
                                    System.out.print("\t"+SnakesnLadders[s_and_l]+"\t|");
                                    else
                                    System.out.print("\t"+SnakesnLadders[s_and_l]+"\t|\t");
                                }
                                else if(SnakesnLadders[s_and_l+1]!=" ")
                                System.out.print(SnakesnLadders[s_and_l]+"\t|");
                                else
                                System.out.print(SnakesnLadders[s_and_l]+"\t|\t");
                            } 
                            if(order==1)s_and_l++;
                            else if(order==0)s_and_l--;
                        }
                        System.out.println("");
                        for (int dash=1; dash<=161; dash++)
                        System.out.print("-");
                        System.out.println("");
                    }
                    }
                }
                else if(chance%2==0 && chance%4!=0)
                {
                    System.out.println("It is "+player2+"\'s turn:");
                    System.out.println("Enter \'Roll\' to roll the dice:");
                    char pause=sc.next().charAt(0);
                    System.out.println("\f");
                    int dice=(int)(Math. random()*6+1);
                    p2_val=p2_val+dice;
                    if(p2_val==100)
                    {
                        System.out.println("==================");
                        System.out.println(player2+" WON!!");
                        System.out.println("==================");
                        System.out.println("It was a great game!!");
                        break;
                    }
                    else if(p2_val>100)
                    {
                        System.out.println("You rolled more than you require to reach 100!!");
                        System.out.println("You can't move ahead!!");
                        p2_val=p2_val-dice;
                    }
                    for (int pfill=0; pfill<100; pfill++){
                        p2[pfill]=" ";
                    }
                    p2[p2_val-1]=" \u278B";
                    System.out.println("\u278A -->"+player1+"\t\t\u278B -->"+player2+"\t\t\u278C -->"+player3+"\t\t\u278D -->"+player4);
                    for (int dash=1; dash<=161; dash++)
                    System.out.print("-");
                    System.out.println("");
                    for (int boardi=1; boardi<=10; boardi++){
                        if(boardi%2==0)
                        {
                            t=t-9;
                            s_and_l=s_and_l-9;
                            order=1;
                        }
                        else if(boardi!=1) {
                            t=t-11;
                            s_and_l=s_and_l-11;
                            order=0;
                        }
                        for (long boardj=1; boardj<=10; boardj++){
                            System.out.print(p1[t]+p2[t]+"\t"+board[t]+p3[t]+p4[t]+"\t|");
                            if(order==1)t++;
                            else if(order==0)t--;
                        }
                        System.out.println("");
                        for (long boardj=1; boardj<=10; boardj++){
                            if(order==0)
                            {
                                if(boardj==1){
                                    if(SnakesnLadders[s_and_l]!=" ")
                                    System.out.print(SnakesnLadders[s_and_l]+"\t|");
                                    else if(SnakesnLadders[s_and_l-1]!=" ")
                                    System.out.print("\t"+SnakesnLadders[s_and_l]+"\t|");
                                    else
                                    System.out.print("\t"+SnakesnLadders[s_and_l]+"\t|\t");
                                }
                                else if(SnakesnLadders[s_and_l-1]!=" ")
                                System.out.print(SnakesnLadders[s_and_l]+"\t|");
                                else
                                System.out.print(SnakesnLadders[s_and_l]+"\t|\t");
                            }
                            if(order==1)
                            {
                                if(boardj==1){
                                    if(SnakesnLadders[s_and_l]!=" ")
                                    System.out.print(SnakesnLadders[s_and_l]+"\t|");
                                    else if(SnakesnLadders[s_and_l+1]!=" ")
                                    System.out.print("\t"+SnakesnLadders[s_and_l]+"\t|");
                                    else
                                    System.out.print("\t"+SnakesnLadders[s_and_l]+"\t|\t");
                                }
                                else if(SnakesnLadders[s_and_l+1]!=" ")
                                System.out.print(SnakesnLadders[s_and_l]+"\t|");
                                else
                                System.out.print(SnakesnLadders[s_and_l]+"\t|\t");
                            } 
                            if(order==1)s_and_l++;
                            else if(order==0)s_and_l--;
                        }
                        System.out.println("");
                        for (int dash=1; dash<=161; dash++)
                        System.out.print("-");
                        System.out.println("");
                    }
                    System.out.println("Roll = "+dice);
                    if(SnakesnLadders[p2_val-1]!=" ")
                    {
                        //Snakes
                        if(p2_val==99)
                        {
                            System.out.println("Oh no You landed on a snake!!!");
                            System.out.println("You Slip to 2!!");
                            p2_val=2;
                        }
                        if(p2_val==91)
                        {
                            System.out.println("Oh no You landed on a snake!!!");
                            System.out.println("You Slip to 75!!");
                            p2_val=75;
                        }
                        if(p2_val==87)
                        {
                            System.out.println("Oh no You landed on a snake!!!");
                            System.out.println("You Slip to 24!!");
                            p2_val=24;
                        }
                        if(p2_val==51)
                        {
                            System.out.println("Oh no You landed on a snake!!!");
                            System.out.println("You Slip to 47!!");
                            p2_val=47;
                        }
                        if(p2_val==49)
                        {
                            System.out.println("Oh no You landed on a snake!!!");
                            System.out.println("You Slip to 23!!");
                            p2_val=23;
                        }
                        if(p2_val==37)
                        {
                            System.out.println("Oh no You landed on a snake!!!");
                            System.out.println("You Slip to 29!!");
                            p2_val=29;
                        }
                        if(p2_val==17)
                        {
                            System.out.println("Oh no You landed on a snake!!!");
                            System.out.println("You Slip to 3!!");
                            p2_val=3;
                        }
                        //Ladders
                        if(p2_val==82)
                        {
                            System.out.println("WOW! You landed on a ladder!!!");
                            System.out.println("You Climb to 97!!");
                            p2_val=97;
                        }
                        if(p2_val==55)
                        {
                            System.out.println("WOW! You landed on a ladder!!!");
                            System.out.println("You Climb to 63!!");
                            p2_val=63;
                        }
                        if(p2_val==33)
                        {
                            System.out.println("WOW! You landed on a ladder!!!");
                            System.out.println("You Climb to 83!!");
                            p2_val=83;
                        }
                        if(p2_val==19)
                        {
                            System.out.println("WOW! You landed on a ladder!!!");
                            System.out.println("You Climb to 80!!");
                            p2_val=80;
                        }
                        if(p2_val==16)
                        {
                            System.out.println("WOW! You landed on a ladder!!!");
                            System.out.println("You Climb to 25!!");
                            p2_val=25;
                        }
                        if(p2_val==9)
                        {
                            System.out.println("WOW! You landed on a ladder!!!");
                            System.out.println("You Climb to 39!!");
                            p2_val=39;
                        }
                        if(p2_val==4)
                        {
                            System.out.println("WOW! You landed on a ladder!!!");
                            System.out.println("You Climb to 14!!");
                            p2_val=14;
                        }
                        t=99;
                        s_and_l=99;
                        order=0;
                        for(long i=-99099999; i<=9999999; i++);
                        System.out.print("\f");
                        for (int pfill=0; pfill<100; pfill++){
                            p2[pfill]=" ";
                        }
                        p2[p2_val-1]=" \u278B";
                        System.out.println("\u278A -->"+player1+"\t\t\u278B -->"+player2+"\t\t\u278C -->"+player3+"\t\t\u278D -->"+player4);
                        for (int dash=1; dash<=161; dash++)
                        System.out.print("-");
                        System.out.println("");
                        for (int boardi=1; boardi<=10; boardi++){
                            if(boardi%2==0)
                            {
                                t=t-9;
                                s_and_l=s_and_l-9;
                                order=1;
                            }
                            else if(boardi!=1) {
                                t=t-11;
                                s_and_l=s_and_l-11;
                                order=0;
                            }
                            for (long boardj=1; boardj<=10; boardj++){
                                System.out.print(p1[t]+p2[t]+"\t"+board[t]+p3[t]+p4[t]+"\t|");
                                if(order==1)t++;
                                else if(order==0)t--;
                            }
                            System.out.println("");
                            for (long boardj=1; boardj<=10; boardj++){
                                if(order==0)
                                {
                                    if(boardj==1){
                                        if(SnakesnLadders[s_and_l]!=" ")
                                        System.out.print(SnakesnLadders[s_and_l]+"\t|");
                                        else if(SnakesnLadders[s_and_l-1]!=" ")
                                        System.out.print("\t"+SnakesnLadders[s_and_l]+"\t|");
                                        else
                                        System.out.print("\t"+SnakesnLadders[s_and_l]+"\t|\t");
                                    }
                                    else if(SnakesnLadders[s_and_l-1]!=" ")
                                    System.out.print(SnakesnLadders[s_and_l]+"\t|");
                                    else
                                    System.out.print(SnakesnLadders[s_and_l]+"\t|\t");
                                }
                                if(order==1)
                                {
                                    if(boardj==1){
                                        if(SnakesnLadders[s_and_l]!=" ")
                                        System.out.print(SnakesnLadders[s_and_l]+"\t|");
                                        else if(SnakesnLadders[s_and_l+1]!=" ")
                                        System.out.print("\t"+SnakesnLadders[s_and_l]+"\t|");
                                        else
                                        System.out.print("\t"+SnakesnLadders[s_and_l]+"\t|\t");
                                    }
                                    else if(SnakesnLadders[s_and_l+1]!=" ")
                                    System.out.print(SnakesnLadders[s_and_l]+"\t|");
                                    else
                                    System.out.print(SnakesnLadders[s_and_l]+"\t|\t");
                                } 
                                if(order==1)s_and_l++;
                                else if(order==0)s_and_l--;
                            }
                            System.out.println("");
                            for (int dash=1; dash<=161; dash++)
                            System.out.print("-");
                            System.out.println("");
                        }
                    }
                }
                else if((chance+1)%4==0){
                    System.out.println("It is "+player3+"\'s turn:");
                    System.out.println("Enter \'Roll\' to roll the dice:");
                    char pause=sc.next().charAt(0);
                    System.out.println("\f");
                    int dice=(int)(Math. random()*6+1);
                    p3_val=p3_val+dice;
                    if(p3_val==100)
                    {
                        System.out.println("==================");
                        System.out.println(player3+" WON!!");
                        System.out.println("==================");
                        System.out.println("It was a great game!!");
                        break;
                    }
                    else if(p3_val>100)
                    {
                        System.out.println("You rolled more than you require to reach 100!!");
                        System.out.println("You can't move ahead!!");
                        p3_val=p3_val-dice;
                    }
                    for (int pfill=0; pfill<100; pfill++){
                        p3[pfill]=" ";
                    }
                    p3[p3_val-1]=" \u278C";
                    System.out.println("\u278A -->"+player1+"\t\t\u278B -->"+player2+"\t\t\u278C -->"+player3+"\t\t\u278D -->"+player4);
                    for (int dash=1; dash<=161; dash++)
                    System.out.print("-");
                    System.out.println("");
                    for (int boardi=1; boardi<=10; boardi++){
                        if(boardi%2==0)
                        {
                            t=t-9;
                            s_and_l=s_and_l-9;
                            order=1;
                        }
                        else if(boardi!=1) {
                            t=t-11;
                            s_and_l=s_and_l-11;
                            order=0;
                        }
                        for (long boardj=1; boardj<=10; boardj++){
                            System.out.print(p1[t]+p2[t]+"\t"+board[t]+p3[t]+p4[t]+"\t|");
                            if(order==1)t++;
                            else if(order==0)t--;
                        }
                        System.out.println("");
                        for (long boardj=1; boardj<=10; boardj++){
                            if(order==0)
                            {
                                if(boardj==1){
                                    if(SnakesnLadders[s_and_l]!=" ")
                                    System.out.print(SnakesnLadders[s_and_l]+"\t|");
                                    else if(SnakesnLadders[s_and_l-1]!=" ")
                                    System.out.print("\t"+SnakesnLadders[s_and_l]+"\t|");
                                    else
                                    System.out.print("\t"+SnakesnLadders[s_and_l]+"\t|\t");
                                }
                                else if(SnakesnLadders[s_and_l-1]!=" ")
                                System.out.print(SnakesnLadders[s_and_l]+"\t|");
                                else
                                System.out.print(SnakesnLadders[s_and_l]+"\t|\t");
                            }
                            if(order==1)
                            {
                                if(boardj==1){
                                    if(SnakesnLadders[s_and_l]!=" ")
                                    System.out.print(SnakesnLadders[s_and_l]+"\t|");
                                    else if(SnakesnLadders[s_and_l+1]!=" ")
                                    System.out.print("\t"+SnakesnLadders[s_and_l]+"\t|");
                                    else
                                    System.out.print("\t"+SnakesnLadders[s_and_l]+"\t|\t");
                                }
                                else if(SnakesnLadders[s_and_l+1]!=" ")
                                System.out.print(SnakesnLadders[s_and_l]+"\t|");
                                else
                                System.out.print(SnakesnLadders[s_and_l]+"\t|\t");
                            } 
                            if(order==1)s_and_l++;
                            else if(order==0)s_and_l--;
                        }
                        System.out.println("");
                        for (int dash=1; dash<=161; dash++)
                        System.out.print("-");
                        System.out.println("");
                    }
                    System.out.println("Roll = "+dice);
                    if(SnakesnLadders[p3_val-1]!=" ")
                    {
                        //Snakes
                        if(p3_val==99)
                        {
                            System.out.println("Oh no You landed on a snake!!!");
                            System.out.println("You Slip to 2!!");
                            p3_val=2;
                        }
                        if(p3_val==91)
                        {
                            System.out.println("Oh no You landed on a snake!!!");
                            System.out.println("You Slip to 75!!");
                            p3_val=75;
                        }
                        if(p3_val==87)
                        {
                            System.out.println("Oh no You landed on a snake!!!");
                            System.out.println("You Slip to 24!!");
                            p3_val=24;
                        }
                        if(p3_val==51)
                        {
                            System.out.println("Oh no You landed on a snake!!!");
                            System.out.println("You Slip to 47!!");
                            p3_val=47;
                        }
                        if(p3_val==49)
                        {
                            System.out.println("Oh no You landed on a snake!!!");
                            System.out.println("You Slip to 23!!");
                            p3_val=23;
                        }
                        if(p3_val==37)
                        {
                            System.out.println("Oh no You landed on a snake!!!");
                            System.out.println("You Slip to 29!!");
                            p3_val=29;
                        }
                        if(p3_val==17)
                        {
                            System.out.println("Oh no You landed on a snake!!!");
                            System.out.println("You Slip to 3!!");
                            p3_val=3;
                        }
                        //Ladders
                        if(p3_val==82)
                        {
                            System.out.println("WOW! You landed on a ladder!!!");
                            System.out.println("You Climb to 97!!");
                            p3_val=97;
                        }
                        if(p3_val==55)
                        {
                            System.out.println("WOW! You landed on a ladder!!!");
                            System.out.println("You Climb to 63!!");
                            p3_val=63;
                        }
                        if(p3_val==33)
                        {
                            System.out.println("WOW! You landed on a ladder!!!");
                            System.out.println("You Climb to 83!!");
                            p3_val=83;
                        }
                        if(p3_val==19)
                        {
                            System.out.println("WOW! You landed on a ladder!!!");
                            System.out.println("You Climb to 80!!");
                            p3_val=80;
                        }
                        if(p3_val==16)
                        {
                            System.out.println("WOW! You landed on a ladder!!!");
                            System.out.println("You Climb to 25!!");
                            p3_val=25;
                        }
                        if(p3_val==9)
                        {
                            System.out.println("WOW! You landed on a ladder!!!");
                            System.out.println("You Climb to 39!!");
                            p3_val=39;
                        }
                        if(p3_val==4)
                        {
                            System.out.println("WOW! You landed on a ladder!!!");
                            System.out.println("You Climb to 14!!");
                            p3_val=14;
                        }
                        t=99;
                        s_and_l=99;
                        order=0;
                        for(long i=-99099999; i<=9999999; i++);
                        System.out.print("\f");
                        for (int pfill=0; pfill<100; pfill++){
                            p3[pfill]=" ";
                        }
                        p3[p3_val-1]=" \u278C";
                        System.out.println("\u278A -->"+player1+"\t\t\u278B -->"+player2+"\t\t\u278C -->"+player3+"\t\t\u278D -->"+player4);
                        for (int dash=1; dash<=161; dash++)
                        System.out.print("-");
                        System.out.println("");
                        for (int boardi=1; boardi<=10; boardi++){
                            if(boardi%2==0)
                            {
                                t=t-9;
                                s_and_l=s_and_l-9;
                                order=1;
                            }
                            else if(boardi!=1) {
                                t=t-11;
                                s_and_l=s_and_l-11;
                                order=0;
                            }
                            for (long boardj=1; boardj<=10; boardj++){
                                System.out.print(p1[t]+p2[t]+"\t"+board[t]+p3[t]+p4[t]+"\t|");
                                if(order==1)t++;
                                else if(order==0)t--;
                            }
                            System.out.println("");
                            for (long boardj=1; boardj<=10; boardj++){
                                if(order==0)
                                {
                                    if(boardj==1){
                                        if(SnakesnLadders[s_and_l]!=" ")
                                        System.out.print(SnakesnLadders[s_and_l]+"\t|");
                                        else if(SnakesnLadders[s_and_l-1]!=" ")
                                        System.out.print("\t"+SnakesnLadders[s_and_l]+"\t|");
                                        else
                                        System.out.print("\t"+SnakesnLadders[s_and_l]+"\t|\t");
                                    }
                                    else if(SnakesnLadders[s_and_l-1]!=" ")
                                    System.out.print(SnakesnLadders[s_and_l]+"\t|");
                                    else
                                    System.out.print(SnakesnLadders[s_and_l]+"\t|\t");
                                }
                                if(order==1)
                                {
                                    if(boardj==1){
                                        if(SnakesnLadders[s_and_l]!=" ")
                                        System.out.print(SnakesnLadders[s_and_l]+"\t|");
                                        else if(SnakesnLadders[s_and_l+1]!=" ")
                                        System.out.print("\t"+SnakesnLadders[s_and_l]+"\t|");
                                        else
                                        System.out.print("\t"+SnakesnLadders[s_and_l]+"\t|\t");
                                    }
                                    else if(SnakesnLadders[s_and_l+1]!=" ")
                                    System.out.print(SnakesnLadders[s_and_l]+"\t|");
                                    else
                                    System.out.print(SnakesnLadders[s_and_l]+"\t|\t");
                                } 
                                if(order==1)s_and_l++;
                                else if(order==0)s_and_l--;
                            }
                            System.out.println("");
                            for (int dash=1; dash<=161; dash++)
                            System.out.print("-");
                            System.out.println("");
                        }
                    }
                }
                else if(chance%4==0)
                {
                    System.out.println("It is "+player4+"\'s turn:");
                    System.out.println("Enter \'Roll\' to roll the dice:");
                    char pause=sc.next().charAt(0);
                    System.out.println("\f");
                    int dice=(int)(Math. random()*6+1);
                    p4_val=p4_val+dice;
                    if(p4_val==100)
                    {
                        System.out.println("==================");
                        System.out.println(player4+" WON!!");
                        System.out.println("==================");
                        System.out.println("It was a great game!!");
                        break;
                    }
                    else if(p4_val>100)
                    {
                        System.out.println("You rolled more than you require to reach 100!!");
                        System.out.println("You can't move ahead!!");
                        p4_val=p4_val-dice;
                    }
                    for (int pfill=0; pfill<100; pfill++){
                        p4[pfill]=" ";
                    }
                    p4[p4_val-1]=" \u278D";
                    System.out.println("\u278A -->"+player1+"\t\t\u278B -->"+player2+"\t\t\u278C -->"+player3+"\t\t\u278D -->"+player4);
                    for (int dash=1; dash<=161; dash++)
                    System.out.print("-");
                    System.out.println("");
                    for (int boardi=1; boardi<=10; boardi++){
                        if(boardi%2==0)
                        {
                            t=t-9;
                            s_and_l=s_and_l-9;
                            order=1;
                        }
                        else if(boardi!=1) {
                            t=t-11;
                            s_and_l=s_and_l-11;
                            order=0;
                        }
                        for (long boardj=1; boardj<=10; boardj++){
                            System.out.print(p1[t]+p2[t]+"\t"+board[t]+p3[t]+p4[t]+"\t|");
                            if(order==1)t++;
                            else if(order==0)t--;
                        }
                        System.out.println("");
                        for (long boardj=1; boardj<=10; boardj++){
                            if(order==0)
                            {
                                if(boardj==1){
                                    if(SnakesnLadders[s_and_l]!=" ")
                                    System.out.print(SnakesnLadders[s_and_l]+"\t|");
                                    else if(SnakesnLadders[s_and_l-1]!=" ")
                                    System.out.print("\t"+SnakesnLadders[s_and_l]+"\t|");
                                    else
                                    System.out.print("\t"+SnakesnLadders[s_and_l]+"\t|\t");
                                }
                                else if(SnakesnLadders[s_and_l-1]!=" ")
                                System.out.print(SnakesnLadders[s_and_l]+"\t|");
                                else
                                System.out.print(SnakesnLadders[s_and_l]+"\t|\t");
                            }
                            if(order==1)
                            {
                                if(boardj==1){
                                    if(SnakesnLadders[s_and_l]!=" ")
                                    System.out.print(SnakesnLadders[s_and_l]+"\t|");
                                    else if(SnakesnLadders[s_and_l+1]!=" ")
                                    System.out.print("\t"+SnakesnLadders[s_and_l]+"\t|");
                                    else
                                    System.out.print("\t"+SnakesnLadders[s_and_l]+"\t|\t");
                                }
                                else if(SnakesnLadders[s_and_l+1]!=" ")
                                System.out.print(SnakesnLadders[s_and_l]+"\t|");
                                else
                                System.out.print(SnakesnLadders[s_and_l]+"\t|\t");
                            } 
                            if(order==1)s_and_l++;
                            else if(order==0)s_and_l--;
                        }
                        System.out.println("");
                        for (int dash=1; dash<=161; dash++)
                        System.out.print("-");
                        System.out.println("");
                    }
                    System.out.println("Roll = "+dice);
                    if(SnakesnLadders[p4_val-1]!=" ")
                    {
                        //Snakes
                        if(p4_val==99)
                        {
                            System.out.println("Oh no You landed on a snake!!!");
                            System.out.println("You Slip to 2!!");
                            p4_val=2;
                        }
                        if(p4_val==91)
                        {
                            System.out.println("Oh no You landed on a snake!!!");
                            System.out.println("You Slip to 75!!");
                            p4_val=75;
                        }
                        if(p4_val==87)
                        {
                            System.out.println("Oh no You landed on a snake!!!");
                            System.out.println("You Slip to 24!!");
                            p4_val=24;
                        }
                        if(p4_val==51)
                        {
                            System.out.println("Oh no You landed on a snake!!!");
                            System.out.println("You Slip to 47!!");
                            p4_val=47;
                        }
                        if(p4_val==49)
                        {
                            System.out.println("Oh no You landed on a snake!!!");
                            System.out.println("You Slip to 23!!");
                            p4_val=23;
                        }
                        if(p4_val==37)
                        {
                            System.out.println("Oh no You landed on a snake!!!");
                            System.out.println("You Slip to 29!!");
                            p4_val=29;
                        }
                        if(p4_val==17)
                        {
                            System.out.println("Oh no You landed on a snake!!!");
                            System.out.println("You Slip to 3!!");
                            p4_val=3;
                        }
                        //Ladders
                        if(p4_val==82)
                        {
                            System.out.println("WOW! You landed on a ladder!!!");
                            System.out.println("You Climb to 97!!");
                            p4_val=97;
                        }
                        if(p4_val==55)
                        {
                            System.out.println("WOW! You landed on a ladder!!!");
                            System.out.println("You Climb to 63!!");
                            p4_val=63;
                        }
                        if(p4_val==33)
                        {
                            System.out.println("WOW! You landed on a ladder!!!");
                            System.out.println("You Climb to 83!!");
                            p4_val=83;
                        }
                        if(p4_val==19)
                        {
                            System.out.println("WOW! You landed on a ladder!!!");
                            System.out.println("You Climb to 80!!");
                            p4_val=80;
                        }
                        if(p4_val==16)
                        {
                            System.out.println("WOW! You landed on a ladder!!!");
                            System.out.println("You Climb to 25!!");
                            p4_val=25;
                        }
                        if(p4_val==9)
                        {
                            System.out.println("WOW! You landed on a ladder!!!");
                            System.out.println("You Climb to 39!!");
                            p4_val=39;
                        }
                        if(p4_val==4)
                        {
                            System.out.println("WOW! You landed on a ladder!!!");
                            System.out.println("You Climb to 14!!");
                            p4_val=14;
                        }
                        t=99;
                        s_and_l=99;
                        order=0;
                        for(long i=-99099999; i<=9999999; i++);
                        System.out.print("\f");
                        for (int pfill=0; pfill<100; pfill++){
                            p4[pfill]=" ";
                        }
                        p4[p4_val-1]=" \u278D";
                        System.out.println("\u278A -->"+player1+"\t\t\u278B -->"+player2+"\t\t\u278C -->"+player3+"\t\t\u278D -->"+player4);
                        for (int dash=1; dash<=161; dash++)
                        System.out.print("-");
                        System.out.println("");
                        for (int boardi=1; boardi<=10; boardi++){
                            if(boardi%2==0)
                            {
                                t=t-9;
                                s_and_l=s_and_l-9;
                                order=1;
                            }
                            else if(boardi!=1) {
                                t=t-11;
                                s_and_l=s_and_l-11;
                                order=0;
                            }
                            for (long boardj=1; boardj<=10; boardj++){
                                System.out.print(p1[t]+p2[t]+"\t"+board[t]+p3[t]+p4[t]+"\t|");
                                if(order==1)t++;
                                else if(order==0)t--;
                            }
                            System.out.println("");
                            for (long boardj=1; boardj<=10; boardj++){
                                if(order==0)
                                {
                                    if(boardj==1){
                                        if(SnakesnLadders[s_and_l]!=" ")
                                        System.out.print(SnakesnLadders[s_and_l]+"\t|");
                                        else if(SnakesnLadders[s_and_l-1]!=" ")
                                        System.out.print("\t"+SnakesnLadders[s_and_l]+"\t|");
                                        else
                                        System.out.print("\t"+SnakesnLadders[s_and_l]+"\t|\t");
                                    }
                                    else if(SnakesnLadders[s_and_l-1]!=" ")
                                    System.out.print(SnakesnLadders[s_and_l]+"\t|");
                                    else
                                    System.out.print(SnakesnLadders[s_and_l]+"\t|\t");
                                }
                                if(order==1)
                                {
                                    if(boardj==1){
                                        if(SnakesnLadders[s_and_l]!=" ")
                                        System.out.print(SnakesnLadders[s_and_l]+"\t|");
                                        else if(SnakesnLadders[s_and_l+1]!=" ")
                                        System.out.print("\t"+SnakesnLadders[s_and_l]+"\t|");
                                        else
                                        System.out.print("\t"+SnakesnLadders[s_and_l]+"\t|\t");
                                    }
                                    else if(SnakesnLadders[s_and_l+1]!=" ")
                                    System.out.print(SnakesnLadders[s_and_l]+"\t|");
                                    else
                                    System.out.print(SnakesnLadders[s_and_l]+"\t|\t");
                                } 
                                if(order==1)s_and_l++;
                                else if(order==0)s_and_l--;
                            }
                            System.out.println("");
                            for (int dash=1; dash<=161; dash++)
                            System.out.print("-");
                            System.out.println("");
                        }
                    }
                }
            }

            System.out.println("");
            System.out.println("Press y or Y to retry.");
            System.out.println("Enter any other character to exit ");
            retry=sc.next().charAt(0);
            System.out.println("\f");
        }while(retry=='y' || retry=='Y');
        System.out.println("Thank You.");
    }
}
\$\endgroup\$
6
\$\begingroup\$

It took me several hours to rework your code. I admire how much effort you put into your code. I couldn't have written that application as one huge method.

The Unicode characters you used for player numbers were sans serif, which didn't work well with a non-proportional console font. I changed them to Unicode characters that were monospace.

When I create a Java application, I use the model / view / controller pattern. This pattern allows me to separate my concerns and focus on one part of the application at a time.

The first thing I did was to create a model of the game. I started by creating a Player class. This class holds the name, marker, and board position for each of the players.

public class Player {

    private int boardPosition;

    private final String name;
    private final String marker;

    public Player(String name, String marker) {
        this.name = name;
        this.marker = marker;
        this.boardPosition = 0;
    }

    public int getBoardPosition() {
        return boardPosition;
    }

    public void setBoardPosition(int boardPosition) {
        this.boardPosition = boardPosition;
    }

    public String getName() {
        return name;
    }

    public String getMarker() {
        return marker;
    }
}

Next, I created a BoardPosition class to hold the position from 1 - 100, a snake position, or a ladder position. If the snake position or ladder position is zero, it's a normal square. If not, it's either a snake or a ladder.

public class BoardPosition {

    private final int position;

    private int ladderPosition;
    private int snakePosition;

    public BoardPosition(int position) {
        this.position = position;
        this.ladderPosition = 0;
        this.snakePosition = 0;
    }

    public int getLadderPosition() {
        return ladderPosition;
    }

    public String getLadderPositionText() {
        return "Climb to " + ladderPosition;
    }

    public void setLadderPosition(int ladderPosition) {
        this.ladderPosition = ladderPosition;
    }

    public int getSnakePosition() {
        return snakePosition;
    }

    public String getSnakePositionText() {
        return "Slip to " + snakePosition;
    }

    public void setSnakePosition(int snakePosition) {
        this.snakePosition = snakePosition;
    }

    public boolean isLadder() {
        return ladderPosition > 0;
    }

    public boolean isSnake() {
        return snakePosition > 0;
    }

    public int getPosition() {
        return position;
    }
}

These two classes are plain Java classes that hold the information for a player and a board position, respectively. By creating classes, you don't have to manage multiple parallel arrays.

Finally, I created a Board class to hold 100 board positions. This class uses a factory pattern to create each of the board positions and put them in one board array.

public class Board {

    private int cellCount;

    private BoardPosition[] board;

    public Board() {
        this.cellCount = 100;
        board = new BoardPosition[cellCount];
        generateBoard();
    }

    private void generateBoard() {
        initalizeBoard();
        setupSnakes();
        setupLadders();
    }

    private void initalizeBoard() {
        for (int i = 0; i < cellCount; i++) {
            BoardPosition boardPosition = 
                    new BoardPosition(i + 1);
            board[i] = boardPosition;
        }
    }

    private void setupSnakes() {
        int[] position = 
            { 99, 91, 87, 51, 49, 37, 17 };
        int[] snakePosition = 
            {  2, 75, 24, 47, 23, 29,  3 };

        for (int i = 0; i < position.length; i++) {
            BoardPosition boardPosition = 
                    board[position[i] - 1];
            boardPosition.setSnakePosition(
                    snakePosition[i]);
        }
    }

    private void setupLadders() {
        int[] position = 
            { 82, 55, 33, 19, 16,  9,  4 };
        int[] ladderPosition = 
            { 97, 63, 83, 80, 25, 39, 14 };

        for (int i = 0; i < position.length; i++) {
            BoardPosition boardPosition = 
                    board[position[i] - 1];
            boardPosition.setLadderPosition(
                    ladderPosition[i]);
        }
    }

    public BoardPosition[] getBoard() {
        return board;
    }

    public int getCellCount() {
        return cellCount;
    }
}

I reworked your game code to use many methods that each (hopefully) do one thing and do it correctly.

Here's the entire, runnable, code for your game. All together, it's about half the size of your original code. Any duplication of code is deliberate for documentation purposes.

import java.util.Scanner;

public class SnakesAndLadders {

    public static void main(String[] args) {
        SnakesAndLadders sl = new SnakesAndLadders();
        sl.playGame();
    }

    public void playGame() {
        Board board = new Board();
        Scanner sc = new Scanner(System.in);

        char retry = 'y';
        do {
            displayIntroduction();
            Player[] players = getPlayers(sc);
            boolean isGameActive = true;
            while (isGameActive) {
                waitForDisplay(sc);
                displayPlayers(players);
                displayBoard(board, players);
                isGameActive = playRound(sc, 
                        board, players);
            }
            retry = getPlayAgainResponse(sc);
        } while (retry == 'y');

        System.out.println();
        System.out.println("Thank you for playing.");
        sc.close();
    }

    private void displayIntroduction() {
        System.out.println("-----------------------"
                + "----------");
        System.out.println("Welcome to Java Snakes "
                + "& Ladders!");
        System.out.println("------------------------"
                + "---------\n");
        System.out.println("Rules:");
        System.out.println("--> This is similar to the "
                + "Snakes & Ladders game played by many "
                + "people.");
        System.out.println("--> This is a four-player "
                + "game.\n");
        System.out.println("--> There will be a 10x10 "
                + "board containing some snakes and "
                + "ladders.\n");
        System.out.println("--> The players will take "
                + "turns rolling one six-sided die.");
        System.out.println("--> The player will move "
                + "ahead according to the "
                + "number rolled.\n");
        System.out.println("--> If a player lands on "
                + "a ladder, he will be able to climb "
                + "it and go ahead!!");
        System.out.println("--> But if a player lands "
                + "on a snake, he will have to go back!!\n");
        System.out.println("--> The players start at 0.");
        System.out.println("--> The game continues "
                + "until one player reaches 100.\n");
    }

    private Player[] getPlayers(Scanner sc) {
        int numberOfPlayers = 4;
        Player[] players = new Player[numberOfPlayers];
        String[] markers = { "\u2460", "\u2461", 
                "\u2462", "\u2463" };

        for (int i = 0; i < numberOfPlayers; i++) {
            System.out.print("Enter Player ");
            System.out.print(i + 1);
            System.out.print("'s name: ");
            String name = sc.nextLine().trim();
            Player player = new Player(name, markers[i]);
            players[i] = player;
        }

        return players;
    }

    private boolean playRound(Scanner sc, Board board, 
            Player[] players) {
        for (int turn = 0; turn < players.length; turn++) {
            Player player = players[turn];
            int die = getDieRoll(sc, player, turn);

            int position = player.getBoardPosition();
            position += die;

            int finalPosition = board.getCellCount();
            if (position == finalPosition) {
                declareWinner(player);
                return false;
            } else if (position > finalPosition) {
                displayOvershoot();
            } else {
                movePlayer(board, player, position);
            }
        }
        return true;
    }

    private void declareWinner(Player player) {
        System.out.println("==================");
        System.out.println(player.getName() + " Won!!");
        System.out.println("==================");
        System.out.println("It was a great game!!");
        System.out.println();
    }

    private void displayOvershoot() {
        System.out.println("You rolled more than "
                + "you require to reach 100!!");
        System.out.println("You can't move ahead!!");
    }

    private void movePlayer(Board board, Player player, 
            int position) {
        BoardPosition[] squares = board.getBoard();
        BoardPosition square = squares[position - 1];
        if (square.isSnake()) {
            displayPosition(position);
            displaySnakePosition(square);
            player.setBoardPosition(
                    square.getSnakePosition());
        } else if (square.isLadder()) {
            displayPosition(position);
            displayLadderPosition(square);
            player.setBoardPosition(
                    square.getLadderPosition());
        } else {
            displayPosition(position);
            player.setBoardPosition(position);
        }
    }

    private void displaySnakePosition(BoardPosition square) {
        System.out.println("Oh no. You landed "
                + "on a snake!!!");
        System.out.print("You slip to ");
        System.out.print(square.getSnakePosition());
        System.out.println("!!");
    }

    private void displayLadderPosition(BoardPosition square) {
        System.out.println("Wow! You landed "
                + "on a ladder!!!");
        System.out.print("You climb to ");
        System.out.print(square.getLadderPosition());
        System.out.println("!!");
    }

    private void displayPosition(int position) {
        System.out.print("You landed on square ");
        System.out.print(position);
        System.out.println(".");
    }

    private int getDieRoll(Scanner sc, Player player, 
            int turn) {
        System.out.println();
        System.out.print("It is ");
        System.out.print(addApostrophe(player.getName()));
        System.out.println(" turn.");
        System.out.print("Press Enter to "
                + "roll the dice:");
        sc.nextLine();

        // Generate a number between 1 & 6
        int die = (int) (Math.random() * 6 + 1);
        System.out.println("You rolled a " + die + ".");
        return die;
    }

    private char getPlayAgainResponse(Scanner sc) {
        char retry;
        System.out.println();
        System.out.println("Enter y to replay game.");
        System.out.println("Enter any other character "
                + "to exit.");
        retry = sc.nextLine().charAt(0);
        return Character.toLowerCase(retry);
    }

    private void waitForDisplay(Scanner sc) {
        System.out.println();
        System.out.print("Press Enter to display board: ");
        sc.nextLine();
    }

    private void displayPlayers(Player[] players) {
        System.out.println();
        for (int i = 0; i < players.length; i++) {
            Player player = players[i];
            String marker = player.getMarker();
            String name = player.getName();
            System.out.println("Player " + marker 
                    + " --> " + name); 
        }
    }

    private void displayBoard(Board board, 
            Player[] players) {
        int cellWidth = 16;
        int cellCount = board.getCellCount();
        int cells = (int) Math.sqrt(cellCount);
        displayDashLine(cellWidth, cells);

        for (int i = 0; i < cells; i += 2) {
            cellCount = displayCells(board, players, 
                    cells, cellWidth, cellCount, -1);
            displayDashLine(cellWidth, cells);
            cellCount = displayCells(board, players, 
                    cells, cellWidth, cellCount, +1);
            displayDashLine(cellWidth, cells);
        }
    }

    private void displayDashLine(int cellWidth, int cells) {
        int width = cellWidth * cells + 1;
        for (int dash = 1; dash <= width; dash++) {
            System.out.print("-");
        }
        System.out.println();
    }

    private int displayCells(Board board, Player[] players,
            int cells, int cellWidth, int cellCount, 
            int increment) {
        int temp = calculateStartingCell(cells, 
                cellCount, increment);
        displayPositionNumber(board, cells, cellWidth, 
                increment, temp);

        temp = calculateStartingCell(cells, 
                cellCount, increment);
        displayPositionText(board, cells, cellWidth, 
                increment, temp);

        temp = calculateStartingCell(cells, 
                cellCount, increment);
        displayPositionPlayer(board, players, cells, 
                cellWidth, increment, temp);

        return cellCount - cells;   
    }

    private void displayPositionNumber(Board board, 
            int cells, int cellWidth, int increment, 
            int temp) {
        for (int i = 0; i < cells; i++) {
            temp += increment;
            BoardPosition boardPosition = 
                    board.getBoard()[temp];

            if (i == 0) {
                System.out.print("|");
            }

            int position = boardPosition.getPosition();
            String text = Integer.toString(position);
            String s = generateTextLine(text, cellWidth);
            System.out.print(s);
        }
        System.out.println();
    }

    private void displayPositionText(Board board, 
            int cells, int cellWidth, int increment, 
            int temp) {
        for (int i = 0; i < cells; i++) {
            temp += increment;
            BoardPosition boardPosition = 
                    board.getBoard()[temp];

            if (i == 0) {
                System.out.print("|");
            }

            String text = "";
            if (boardPosition.isSnake()) {
                text = boardPosition.getSnakePositionText();
            } else if (boardPosition.isLadder()) {
                text = boardPosition.getLadderPositionText();
            }
            String s = generateTextLine(text, cellWidth);
            System.out.print(s);
        }
        System.out.println();
    }

    private void displayPositionPlayer(Board board, 
            Player[] players, int cells, 
            int cellWidth, int increment, int temp) {
        for (int i = 0; i < cells; i++) {
            temp += increment;

            if (i == 0) {
                System.out.print("|");
            }

            String text = "";
            for (int j = 0; j < players.length; j++) {
                Player player = players[j];
                if (player.getBoardPosition() == (temp + 1)) {
                    text += player.getMarker() + "  ";
                }
            }
            text = text.trim();
            String s = generateTextLine(text, cellWidth);
            System.out.print(s);
        }
        System.out.println();
    }


    private int calculateStartingCell(int cells, 
            int cellCount, int increment) {
        int temp = cellCount;
        if (increment > 0) {
            temp -= cells + 1;
        }
        return temp;
    }

    private String generateTextLine(String text, int cellWidth) {
        String output = "";

        int spaces = (cellWidth - text.length()) / 2;
        output += createBlankString(spaces);

        output += text;

        int width = cellWidth - spaces - text.length() - 1;
        output += createBlankString(width);

        output += "|";
        return output;
    }

    private String createBlankString(int width) {
        String output = "";
        for (int i = 0; i < width; i++) {
            output += " ";
        }
        return output;
    }

    private String addApostrophe(String name) {
        char last = name.charAt(name.length() - 1);
        if (last == 's') {
            return name + "'";
        } else {
            return name + "'s";
        }
    }

    public class Board {

        private int cellCount;

        private BoardPosition[] board;

        public Board() {
            this.cellCount = 100;
            board = new BoardPosition[cellCount];
            generateBoard();
        }

        private void generateBoard() {
            initalizeBoard();
            setupSnakes();
            setupLadders();
        }

        private void initalizeBoard() {
            for (int i = 0; i < cellCount; i++) {
                BoardPosition boardPosition = 
                        new BoardPosition(i + 1);
                board[i] = boardPosition;
            }
        }

        private void setupSnakes() {
            int[] position = 
                { 99, 91, 87, 51, 49, 37, 17 };
            int[] snakePosition = 
                {  2, 75, 24, 47, 23, 29,  3 };

            for (int i = 0; i < position.length; i++) {
                BoardPosition boardPosition = 
                        board[position[i] - 1];
                boardPosition.setSnakePosition(
                        snakePosition[i]);
            }
        }

        private void setupLadders() {
            int[] position = 
                { 82, 55, 33, 19, 16,  9,  4 };
            int[] ladderPosition = 
                { 97, 63, 83, 80, 25, 39, 14 };

            for (int i = 0; i < position.length; i++) {
                BoardPosition boardPosition = 
                        board[position[i] - 1];
                boardPosition.setLadderPosition(
                        ladderPosition[i]);
            }
        }

        public BoardPosition[] getBoard() {
            return board;
        }

        public int getCellCount() {
            return cellCount;
        }
    }

    public class BoardPosition {

        private final int position;

        private int ladderPosition;
        private int snakePosition;

        public BoardPosition(int position) {
            this.position = position;
            this.ladderPosition = 0;
            this.snakePosition = 0;
        }

        public int getLadderPosition() {
            return ladderPosition;
        }

        public String getLadderPositionText() {
            return "Climb to " + ladderPosition;
        }

        public void setLadderPosition(int ladderPosition) {
            this.ladderPosition = ladderPosition;
        }

        public int getSnakePosition() {
            return snakePosition;
        }

        public String getSnakePositionText() {
            return "Slip to " + snakePosition;
        }

        public void setSnakePosition(int snakePosition) {
            this.snakePosition = snakePosition;
        }

        public boolean isLadder() {
            return ladderPosition > 0;
        }

        public boolean isSnake() {
            return snakePosition > 0;
        }

        public int getPosition() {
            return position;
        }
    }

    public class Player {

        private int boardPosition;

        private final String name;
        private final String marker;

        public Player(String name, String marker) {
            this.name = name;
            this.marker = marker;
            this.boardPosition = 0;
        }

        public int getBoardPosition() {
            return boardPosition;
        }

        public void setBoardPosition(int boardPosition) {
            this.boardPosition = boardPosition;
        }

        public String getName() {
            return name;
        }

        public String getMarker() {
            return marker;
        }
    }
}
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.