## Sunday, December 20, 2015

### Snake and Ladder using graph in Java

Hello guys, today I want to talk about the well known game of all times. Snake and ladders.

Let us think of creating the game in Java using Graph.

Before starting I have to clear some of the points.

1. I didn't entertained edge cases to the maximum.
2. Also I have created 2 different graph. one for ladders addresses and one for snakes addresses.
3. There are different approaches of solving this problem as well but I have used graph in order to do so.
4. Now lets see the program.
Full code is available here

Example code
```

import java.util.ArrayList;
import java.util.List;

import org.jgrapht.DirectedGraph;
import org.jgrapht.graph.DefaultDirectedGraph;
import org.jgrapht.graph.DefaultEdge;

public static void main(String[] args) {

Integer board[] = new Integer[101];

for(int i=1; i<100; i++){
board[i] = new Integer(i);
}

DirectedGraph<Integer, DefaultEdge> ladders = new DefaultDirectedGraph<Integer, DefaultEdge>(DefaultEdge.class);
/*
* 2 -> 32
* 14 -> 57
* 27 -> 78
* 34 -> 44
* 7 -> 98
*/
}

List<GroupInfo> snakesInfo = new ArrayList<GroupInfo>();

//Snake Configuration
DirectedGraph<Integer, DefaultEdge> snakes = new DefaultDirectedGraph<Integer, DefaultEdge>(DefaultEdge.class);
/*
* Snakes are at
* 99 -> 10
* 77 -> 22
* 62 -> 13
* 54 -> 8
* 42 -> 3
*/
for(GroupInfo gi : snakesInfo){
}

/*
* These are the current positions of the player, I am considering 1 players
*/
int player1 = 1;

//The game goes till he reaches 100
while(player1  != 100){
//get the value of dice and
int nextValue = getNextDiceValue();
System.out.println("Player is in " + player1 + " and got " + nextValue + " so next position is " + (player1 + nextValue));

if((player1 + nextValue) <= 100){

if((player1 + nextValue) == 100){
System.out.println("!!!Congratulations you won!!! ");
System.out.println("Bye, have a nice day");
break;
}

//Now this is the current position of the player
player1 += nextValue;

Integer boardCurrentValue = board[player1];

System.out.println("Congratulations, you got ladder to " + boardCurrentValue);
player1 = boardCurrentValue;
boardCurrentValue = snakes.getEdgeTarget(snakes.edgesOf(boardCurrentValue).iterator().next());
System.out.println("Oops, you get stung by snake and now you came to " + boardCurrentValue);
player1 = boardCurrentValue;
}
}
}
}

private static int getNextDiceValue(){
//here randomize the dice for value between 1 & 6 and return the value that comes back
int result = 0;

while(result == 0){
result = (int)(Math.random() * 10);

if(result > 6){
result -= 6;
}
}
return result;
}
}

class GroupInfo{
private Integer startIndex;
private Integer endIndex;

public GroupInfo(Integer startIndex, Integer endIndex) {
super();
this.startIndex = startIndex;
this.endIndex = endIndex;
}

public Integer getStartIndex() {
return startIndex;
}

public void setStartIndex(Integer startIndex) {
this.startIndex = startIndex;
}

public Integer getEndIndex() {
return endIndex;
}

public void setEndIndex(Integer endIndex) {
this.endIndex = endIndex;
}
}

```