From 9b4d4c8d3fa747743f3d8aca251e55639f387266 Mon Sep 17 00:00:00 2001 From: Faiyaz Khira Date: Thu, 31 Jul 2025 23:36:53 +0530 Subject: [PATCH] Snake and Ladder Solution --- src/Board.java | 42 ++++++++++++++++++ src/Dice.java | 19 ++++++++ src/Game.java | 113 ++++++++++++++++++++++++++++++++++++++++++++++++ src/Ladder.java | 17 ++++++++ src/Player.java | 21 +++++++++ src/Snake.java | 17 ++++++++ 6 files changed, 229 insertions(+) create mode 100644 src/Board.java create mode 100644 src/Dice.java create mode 100644 src/Game.java create mode 100644 src/Ladder.java create mode 100644 src/Player.java create mode 100644 src/Snake.java diff --git a/src/Board.java b/src/Board.java new file mode 100644 index 00000000..cfd8d513 --- /dev/null +++ b/src/Board.java @@ -0,0 +1,42 @@ +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class Board { + private final Map snakes = new HashMap<>(); + private final Map ladders = new HashMap<>(); + private final int boardSize; + + public Board(List snakeList, List ladderList, int boardSize){ + this.boardSize = boardSize; + for(Snake s : snakeList){ + snakes.put(s.getHead(), s.getTail()); + } + for(Ladder l : ladderList){ + ladders.put(l.getStart(), l.getEnd()); + } + } + + public int getBoardSize() { + return boardSize; + } + + public int getNextPosition(int currentPosition) { + int newPos = currentPosition; + + //handle snake and ladder logic + boolean moved; + do { + moved = false; + if(ladders.containsKey(newPos)){ + newPos = ladders.get(newPos); + moved = true; + } else if(snakes.containsKey(newPos)){ + newPos = snakes.get(newPos); + moved = true; + } + } while (moved); + + return newPos; + } +} diff --git a/src/Dice.java b/src/Dice.java new file mode 100644 index 00000000..298d4e4f --- /dev/null +++ b/src/Dice.java @@ -0,0 +1,19 @@ +import java.util.Random; + +public class Dice { + private final Random random = new Random(); + private final int diceCount; + + public Dice(int diceCount) { + this.diceCount = diceCount; + } + + + public int rollDice(){ + int total = 0; + for(int i=0; i 3){ + System.out.println("Invalid dice count. Must be between 1 and 3."); + return; + } + + Dice dice = new Dice(diceCount); + + + // Read Board + System.out.println("Enter board dimension N (you will get an NxN board):"); + int dimension = Integer.parseInt(scanner.nextLine().trim()); + int boardSize = dimension * dimension; + + if(dimension < 3 || dimension > 15){ + System.out.println("Invalid dimension. Must be between 3 and 15."); + return; + } + System.out.println("Your board size is: " + boardSize); + + + // Read Snakes + System.out.println("Enter the number of snakes:"); + int snakeCount = Integer.parseInt(scanner.nextLine().trim()); + List snakeList = new ArrayList<>(); + for(int i=0; i boardSize || tail >= boardSize || head == boardSize){ + System.out.println("Invalid snake: head must be > tail and within board size"); + return; + } + + snakeList.add(new Snake(head, tail)); + } + + // Read ladders + System.out.println("Enter the number of ladders:"); + int ladderCount = Integer.parseInt(scanner.nextLine().trim()); + List ladderList = new ArrayList<>(); + for(int i=0; i= end || start <= 0 || end > boardSize) { + System.out.println("Invalid ladder: start must be < end and within board size"); + return; + } + + ladderList.add(new Ladder(start, end)); + } + + // Read players + System.out.println("Enter the number of players:"); + int playerCount = Integer.parseInt(scanner.nextLine().trim()); + List playerList = new ArrayList<>(); + for(int i=0; i board.getBoardSize()){ + nextPosition = currentPosition; + } else { + nextPosition = board.getNextPosition(nextPosition); + } + + currentPlayer.setPosition(nextPosition); + + System.out.printf("%s rolled %d and moved from %d to %d%n", + currentPlayer.getName(), diceRoll, currentPosition, nextPosition); + + if(nextPosition == 100){ + System.out.println(currentPlayer.getName() + " wins the game"); + gameWon = true; + break; + } + + currentPlayerIndex = (currentPlayerIndex + 1) % playerList.size(); + } + scanner.close(); + } +} diff --git a/src/Ladder.java b/src/Ladder.java new file mode 100644 index 00000000..e7328685 --- /dev/null +++ b/src/Ladder.java @@ -0,0 +1,17 @@ +public class Ladder { + private final int start; + private final int end; + + public Ladder(int start, int end) { + this.start = start; + this.end = end; + } + + public int getStart() { + return start; + } + + public int getEnd() { + return end; + } +} diff --git a/src/Player.java b/src/Player.java new file mode 100644 index 00000000..eba13fc5 --- /dev/null +++ b/src/Player.java @@ -0,0 +1,21 @@ +public class Player { + private final String name; + private int position; + + public Player(String name) { + this.name = name; + this.position = 0; + } + + public String getName() { + return name; + } + + public int getPosition() { + return position; + } + + public void setPosition(int position) { + this.position = position; + } +} diff --git a/src/Snake.java b/src/Snake.java new file mode 100644 index 00000000..54f6f243 --- /dev/null +++ b/src/Snake.java @@ -0,0 +1,17 @@ +public class Snake { + private final int head; + private final int tail; + + public Snake(int head, int tail) { + this.head = head; + this.tail = tail; + } + + public int getHead() { + return head; + } + + public int getTail() { + return tail; + } +}