From 5f85ae96ed030b383d6dc6571a38c220e786914a Mon Sep 17 00:00:00 2001 From: harshitchauhan01 <44221075+harshitchauhan01@users.noreply.github.com> Date: Tue, 31 Mar 2020 14:58:17 +0530 Subject: [PATCH] Add files via upload --- .../snakes and ladder game.txt | 120 ++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 G group Project evaluation 1/snakes and ladder game.txt diff --git a/G group Project evaluation 1/snakes and ladder game.txt b/G group Project evaluation 1/snakes and ladder game.txt new file mode 100644 index 0000000..4ab09b0 --- /dev/null +++ b/G group Project evaluation 1/snakes and ladder game.txt @@ -0,0 +1,120 @@ +import java.util.HashMap; + import java.util.Map; + import java.util.Random; + import java.util.Scanner; + +public class Harshit171304 { + + public static void main(String[] args) { + // TODO Auto-generated method stub + SnakeNLadder s = new SnakeNLadder(); + s.startGame(); + }} + + class SnakeNLadder + { + + final static int WINPOINT = 100; + + + static Map < Integer , Integer > snake = new HashMap < Integer , Integer >(); + static Map < Integer , Integer > ladder = new HashMap< Integer , Integer >(); + + { + snake.put(99,54); + snake.put(70,55); + snake.put(52,42); + snake.put(25,2); + snake.put(95,72); + + ladder.put(6,25); + ladder.put(11,40); + ladder.put(60,85); + ladder.put(46,90); + ladder.put(17,69); + } + public int rollDice() + { + int n = 0; + Random r = new Random(); + n=r.nextInt(7); + return (n==0?1:n); + } + public int calculatePlayerValue(int player, int diceValue) + { + player = player + diceValue; + + if(player > WINPOINT) + { + player = player - diceValue; + return player; + } + + if(null!=snake.get(player)) + { + System.out.println("swallowed by snake"); + player= snake.get(player); + } + + if(null!=ladder.get(player)) + { + System.out.println("climb up the ladder"); + player= ladder.get(player); + } + return player; + } + public boolean isWin(int player) + { + return WINPOINT == player; + } + public void startGame() + { + int player1 =0, player2=0; + int currentPlayer=-1; + Scanner s = new Scanner(System.in); + String str; + int diceValue =0; + do + { + System.out.println(currentPlayer==-1?"\n\nFIRST PLAYER TURN":"\n\nSECOND PLAYER TURN"); + System.out.println("Press r to roll Dice"); + str = s.next(); + diceValue = rollDice(); + + + if(currentPlayer == -1) + { + player1 = calculatePlayerValue(player1,diceValue); + System.out.println("First Player :: " + player1); + System.out.println("Second Player :: " + player2); + System.out.println("------------------"); + if(isWin(player1)) + { + System.out.println("First player wins"); + return; + } + } + else + { + player2 = calculatePlayerValue(player2,diceValue); + System.out.println("First Player :: " + player1); + System.out.println("Second Player :: " + player2); + System.out.println("------------------"); + if(isWin(player2)) + { + System.out.println("Second player wins"); + return; + } + } + + currentPlayer= -currentPlayer; + + + + }while("r".equals(str)); + } + + + + + }