diff --git a/Readme b/Readme new file mode 100644 index 00000000..5ab2f8a4 --- /dev/null +++ b/Readme @@ -0,0 +1 @@ +Hello \ No newline at end of file diff --git a/snake_ladder/Main.java b/snake_ladder/Main.java new file mode 100644 index 00000000..4af17a04 --- /dev/null +++ b/snake_ladder/Main.java @@ -0,0 +1,78 @@ +package snake_ladder; + +import snake_ladder.behaviours.SimpleDiceRollBehaviour; +import snake_ladder.entities.Game; +import snake_ladder.entities.GameElement; +import snake_ladder.entities.Ladder; +import snake_ladder.entities.Player; +import snake_ladder.entities.Snake; + +import java.io.BufferedInputStream; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.Scanner; + +public class Main { + + private record GameConfig(Map gameElementMap, List players) { + } + + private static GameConfig loadGameConfig(String inputPath) { + Map gameElementMap = new HashMap<>(); + List players = new ArrayList<>(); + + try (Scanner sc = new Scanner(new BufferedInputStream(new FileInputStream(inputPath)))) { + int snakeCount = sc.nextInt(); + for (int i = 0; i < snakeCount; i++) { + int head = sc.nextInt(); + int tail = sc.nextInt(); + gameElementMap.put(head, new Snake(head, tail)); + } + + int ladderCount = sc.nextInt(); + for (int i = 0; i < ladderCount; i++) { + int start = sc.nextInt(); + int end = sc.nextInt(); + gameElementMap.put(start, new Ladder(start, end)); + } + + int playerCount = sc.nextInt(); + for (int i = 0; i < playerCount; i++) { + String name = sc.next(); + players.add(new Player(name, 0)); + } + } catch (IOException e) { + throw new RuntimeException("Failed to read input file: " + inputPath, e); + } + + return new GameConfig(gameElementMap, players); + } + + + public static void main(String[] args) { + int gameSize = 100; + + String inputPath = args.length > 0 ? args[0] : "input/Input1.txt"; + GameConfig config = loadGameConfig(inputPath); + Game game = new Game(gameSize, config.gameElementMap, config.players, new SimpleDiceRollBehaviour()); + + Random randomNumber = new Random(); + int min = 1, max = 6; + while (true) { + for (Player player : config.players) { + int diceRoll = randomNumber.nextInt(max - min + 1) + min; + game.rollDice(player, diceRoll); + + if (player.getPosition() == gameSize) { + System.out.println(player.getName() + " wins the game"); + return; + } + } + } + } +} diff --git a/snake_ladder/behaviours/DiceRollBehaviour.java b/snake_ladder/behaviours/DiceRollBehaviour.java new file mode 100644 index 00000000..27925ba3 --- /dev/null +++ b/snake_ladder/behaviours/DiceRollBehaviour.java @@ -0,0 +1,10 @@ +package snake_ladder.behaviours; + +import snake_ladder.entities.Game; +import snake_ladder.entities.Player; + +public interface DiceRollBehaviour { + + void rollDice(Game game, Player p, int number); + +} diff --git a/snake_ladder/behaviours/SimpleDiceRollBehaviour.java b/snake_ladder/behaviours/SimpleDiceRollBehaviour.java new file mode 100644 index 00000000..cdd34dd5 --- /dev/null +++ b/snake_ladder/behaviours/SimpleDiceRollBehaviour.java @@ -0,0 +1,31 @@ +package snake_ladder.behaviours; + +import snake_ladder.entities.Game; +import snake_ladder.entities.GameElement; +import snake_ladder.entities.Player; + +import java.util.Map; + +public class SimpleDiceRollBehaviour implements DiceRollBehaviour{ + + public void rollDice(Game game, Player player, int number) { + int gameSize = game.getGameSize(); + + int playerPosition = player.getPosition(); + int endPosition = playerPosition + number; + + if (endPosition > gameSize) { + return; + } + + Map gameElementMap = game.getElementsMap(); + + while (gameElementMap.containsKey(endPosition)) { + GameElement gameElement = gameElementMap.get(endPosition); + endPosition = gameElement.act(); + } + + player.setPosition(endPosition); + } + +} diff --git a/snake_ladder/entities/Game.java b/snake_ladder/entities/Game.java new file mode 100644 index 00000000..a6c0a077 --- /dev/null +++ b/snake_ladder/entities/Game.java @@ -0,0 +1,41 @@ +package snake_ladder.entities; + +import snake_ladder.behaviours.DiceRollBehaviour; + +import java.util.List; +import java.util.Map; + +public class Game { + + private final int gameSize; + private final Map elementsMap; + private final List players; + private final DiceRollBehaviour diceRollBehaviour; + + public Game(int gameSize, Map elementsMap, List players, DiceRollBehaviour diceRollBehaviour) { + this.gameSize = gameSize; + this.elementsMap = elementsMap; + this.players = players; + this.diceRollBehaviour = diceRollBehaviour; + } + + public int getGameSize() { + return this.gameSize; + } + + public List getPlayers() { + return this.players; + } + + public Map getElementsMap() { + return this.elementsMap; + } + + public void rollDice(Player p, int number) { + int playerStartPosition = p.getPosition(); + this.diceRollBehaviour.rollDice(this, p, number); + int playerEndPosition = p.getPosition(); + System.out.println(p.getName() + " rolled a " + number + " and moved from " + playerStartPosition + " to " + playerEndPosition); + } + +} diff --git a/snake_ladder/entities/GameElement.java b/snake_ladder/entities/GameElement.java new file mode 100644 index 00000000..88d2d2e5 --- /dev/null +++ b/snake_ladder/entities/GameElement.java @@ -0,0 +1,23 @@ +package snake_ladder.entities; + +public abstract class GameElement { + + private final int startPosition; + private final int endPosition; + + GameElement(int startPosition, int endPosition) { + this.startPosition = startPosition; + this.endPosition = endPosition; + } + + public int getStartPosition() { + return this.startPosition; + } + + public int getEndPosition() { + return this.endPosition; + } + + public abstract int act(); + +} diff --git a/snake_ladder/entities/Ladder.java b/snake_ladder/entities/Ladder.java new file mode 100644 index 00000000..e2644fa8 --- /dev/null +++ b/snake_ladder/entities/Ladder.java @@ -0,0 +1,13 @@ +package snake_ladder.entities; + +public class Ladder extends GameElement{ + + public Ladder(int startPosition, int endPosition) { + super(startPosition, endPosition); + } + + public int act() { + return this.getEndPosition(); + } + +} diff --git a/snake_ladder/entities/Player.java b/snake_ladder/entities/Player.java new file mode 100644 index 00000000..4511cede --- /dev/null +++ b/snake_ladder/entities/Player.java @@ -0,0 +1,29 @@ +package snake_ladder.entities; + +public class Player { + + private final String name; + private int position; + + public Player(String name) { + this.name = name; + } + + public Player(String name, int startPosition) { + this.name = name; + this.position = startPosition; + } + + public String getName() { + return this.name; + } + + public int getPosition() { + return this.position; + } + + public void setPosition(int position) { + this.position = position; + } + +} diff --git a/snake_ladder/entities/Snake.java b/snake_ladder/entities/Snake.java new file mode 100644 index 00000000..68071738 --- /dev/null +++ b/snake_ladder/entities/Snake.java @@ -0,0 +1,13 @@ +package snake_ladder.entities; + +public class Snake extends GameElement{ + + public Snake(int startPosition, int endPosition) { + super(startPosition, endPosition); + } + + public int act() { + return this.getEndPosition(); + } + +} diff --git a/snake_ladder/input/Input1.txt b/snake_ladder/input/Input1.txt new file mode 100644 index 00000000..e69de29b diff --git a/snake_ladder/planning b/snake_ladder/planning new file mode 100644 index 00000000..e69de29b