Rules for **Snake and Ladder** are:
1. There are two players in this game and board size is 100.(10 X 10)
2. Possible outcomes by throwing a dice are 1,2,3,4,5,6. 3. If output is 6 then current player will get a chance again to throw the Dice.
4. If outcome of the Dice is 1,2,3,4,5,6 and player positioned on mouth of snake then his current position will change to tail of snake, and he will not get other chance until he throws a dice which has value 6.
5. If outcome of the Dice is 1,2,3,4,5,6 and player positioned at below of ladder then His current position will change to topmost position of ladder and he will get another chance to throw the dice again.
6. If player's current position+ roll >100 then take the following considerations i. if(roll==6) current player will get the chance again ,otherwise other player will get.
7. Any player reach 100 earlier than other player will be the winner and game will end. 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
1. There are two players in this game and board size is 100.(10 X 10)
2. Possible outcomes by throwing a dice are 1,2,3,4,5,6. 3. If output is 6 then current player will get a chance again to throw the Dice.
4. If outcome of the Dice is 1,2,3,4,5,6 and player positioned on mouth of snake then his current position will change to tail of snake, and he will not get other chance until he throws a dice which has value 6.
5. If outcome of the Dice is 1,2,3,4,5,6 and player positioned at below of ladder then His current position will change to topmost position of ladder and he will get another chance to throw the dice again.
6. If player's current position+ roll >100 then take the following considerations i. if(roll==6) current player will get the chance again ,otherwise other player will get.
7. Any player reach 100 earlier than other player will be the winner and game will end. 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(HashMaphashMap){ 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 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 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)){ 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 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; } } } } }
No comments:
Post a Comment