Search code examples
javaweblogic

The Logic of code is not being executed


Question :This a program for snake and ladder.the user enters the number of players and players name.

--The program is not executing from the positon where the throwdice() method is called.

package practical1;

import java.util.ArrayList;
import java.util.Scanner;

public class snakeandladder1 {

int totalpos=100;
int v;
int[] scores = new int[100];
int score=0;
int l=0;

public int throwdice() //to calculate the dice value
{
    int i=0;
    {
        while(i==0)
        {
            i=(int)Math.random()*100;
            i=i%13;
        }
        return i;
    }
}

public int ladder(int score) //to check if the user has reached a ladder
{
    if(score==15)
        score=30;
    else if(score == 45)
        score=71;
    else if(score == 25)
        score=62;
    else if(score == 81)
        score=91;
    else if(score == 9)
        score=39;
    scores[l]=score;
    return score;
}

public int snake(int score) //to check if the user has reached a snake
{
    if(score == 29)
        score=11;
    else if(score == 81)
        score=48;
    else if(score == 92)
        score=71;
    else if(score == 30)
        score=6;
    else if(score == 58)
        score=19;
    scores[l]=score;
    return score;
}

void start() 
{
    System.out.println("Enter the number of players:");
    Scanner in=new Scanner(System.in);
    int n=in.nextInt();
    System.out.println("Enter Players names in order:");
    ArrayList<String> name1=new ArrayList<String>(); //an array to store players names
    for (int i=0;i<n;i++)
    {
        Scanner in1=new Scanner(System.in);
        String name2=in1.nextLine();
        name1.add(name2);
    }
    while(true)
    {
        while(l<n) //for each player
        {
            System.out.println("Click y to roll dice");
            Scanner in2=new Scanner(System.in);
            String yes=in2.nextLine();
            if (yes == "y")             //------!!!This part is not getting executed
            {                           // It is taking y but not going further.It does not print               
                v=throwdice();          //anything also
                System.out.println("dice value:"+v);
            }
            score = scores[l]+v;        //the value of dice is added to individual score 
            if(score==totalpos)         //checking if reached 100
            {
                System.out.println("User:"+name1.get(l)+" got "+v+".Winner!!!");
                break;
            }
            else if(score > totalpos) //checking if dice value is more than required to 100
            {
                scores[l]=scores[l];
            }
            int s1;
            s1=ladder(score); //ladder is called, if true valuue is added to score[i] in the function itself
            if(s1==score)
            {
                snake(score); //if not ladder then snake is called
            }
            System.out.println("Current score of "+name1.get(l)+" is:"+scores[l]);  //to display result
            l++;
        }
        if(l==n)
        {
            l=0;
        }
    }
}
public static void main(String[] args) throws Exception{
    // TODO Auto-generated method stub
    snakeandladder1 sal=new snakeandladder1();
    sal.start();
}

}

The output I am getting:

Enter the number of players: 2 Enter Players names in order: rag kis Click y to roll dice y Current score of rag is:0 Click y to roll dice y Current score of kis is:0 Click y to roll dice y Current score of rag is:0 Click y to roll dice

Please help ..


Solution

  • you have some issues in your code... First I would like to suggest you to get adhere with the correct Java usage, best practices, naming conventions, exception handling ..start it from your class name..that is up to you..

    Regarding your issue, 1:

    if (yes == "y") {}

    should be changed to :

    if (yes.equalsIgnoreCase("y")) {}

    I accpet the above answer of "rert588" in this case.

    2: there are couple of issues in your throwdice() method. I saw your current method goes in a never ending while loop as i is always '0'. I am not that clear what you are gonna achieve but I believe it should be like below.

    should be changed to :

    public int throwdice() //to calculate the dice value
      {
        int i = (int)(Math.random() * 100); // make note on your Casting.
        i = i % 13;
        return i;
      }

    Have a try, hope this will help you.