I came across a interview question "Suggest data structures you would use for snake & ladder game? "
I would use a 2D array (same as we do in chess ) to design each block of game. But is it possible to design it in 1D array ? Many people has suggested this but no one has explained how to do it.
Rules for Snake and Ladder are:
We are passing only one HashMap, which contains the current position as key and next position as value. I think one HashMap will complete all the requirement of the Ladder and Snake,
int playSnakeAndLadder(HashMap<Integer, Integer> hashMap){
int player1=1, player2=1;// Initial position of players
int chance=0;// This value show the change of players if chance is odd then player1 will play
// if chance is even then player2 will play
while(1){
if((player1==100)||(player2==100))// if any of player's position is 100 return chance;
return chance;// Here chance shows who win the game, if chance is even player1 wins other //wise player2 wins
int roll=Randon(6);// this will generate random number from 1 to 6.
if(chance%2==0){
int key=roll+player1;// new position of player1
boolean isLadder=false;// This is for checking the turn current player if againTurn is ture
// then the current player will player again.
if(hashMap.contains(Key)){
player1=hashMap.getValue(Key);
// Here player current position will automatically update according to the hashMap.
// if there is a snake the key value is greater than it mapping value.
// if there is a ladder then key value is less than it mapping value.
if(Key<hashMap.getValue(Key))
isLadder=true;// Here player gets ladder.
if(isLadder==true && roll==6 || isLadder==true)
chance=chance;
else
chance=(chance+1)%2;
}
else if(player1+roll>100 && roll!=6)
chance=(chance+1)%2;
else if(player1+roll>100 && roll==6)
chance=chance;
else if(roll==6){
player1=player1+roll;
chance=chance;
}
else{
player1=player1+roll;
chance1=(chance1+1)%2;
}
}
else{// Now similarly for player2
{
int key=roll+player2;// new position of player2
boolean isLadder=false;// This is for checking the turn current player if againTurn is ture
// then the current player will player again.
if(hashMap.contains(Key)){
player2=hashMap.getValue(Key);
// Here player current position will automatically update according to the hashMap.
// if there is snake the key value is greater than it mapping value.
// if there is ladder then key value is less than it mapping value.
if(Key<hashMap.getValue(Key))
isLadder=true;// Here player gets ladder.
if(isLadder==true && roll==6 || isLadder==true)
chance=chance;
else
chance=(chance+1)%2;
}
else if(player2+roll>100 && roll!=6)
chance=(chance+1)%2;
else if(player2+roll>100 && roll==6)
chance=chance;
else if(roll==6){
player2=player2+roll;
chance=chance;
}
else{
player2=player2+roll;
chance=(chance+1)%2;
}
}
}
}
}
Vakh is correct. "Yes it is possible: every 2D array can be represented as a 1D array."
The array
int board[100] =
{
0, 0, 0, 10, 0, 0, 0, 0, 22, 0,
0, 0, 0, 0, 0, 0,-10, 0, 0, 18,
0, 0, 0, 0, 0, 0, 0, 56, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 19,
17, 0, 0,-20, 0, 0, 0, 0, 0, 0,
0,-43, 18, -4, 0, 0, 0, 0, 0, 0,
20, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,-63, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0,-20, 0,-20, 0, 0, 0,-21, 0
};
can hold ladders 4->14, 9->13, 20->38, 28->84, 40->59, 51->67, 63->81, 71->91 and snakes 17->7, 54->34, 62->19, 64->60, 87->24, 93->73, 95->75, 99->78
if red is at position 2 (i.e. r=2) and scores 2 (i.e. s=2) then new position of red is
2+2+board[2+2-1] = 14
i.e.
r = r + s + board[r+s-1])
@Jan Dvorak, "jagged arrays are not 2D array"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With