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 ..