Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions snakeAndLadder/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.machinecoding</groupId>
<artifactId>snakeAndLadder</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>23</maven.compiler.source>
<maven.compiler.target>23</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.0.0</version> <!-- Use the latest stable version -->
<scope>test</scope>
</dependency>
</dependencies>
</project>
49 changes: 49 additions & 0 deletions snakeAndLadder/src/main/java/org/machinecoding/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.machinecoding;

import org.machinecoding.models.Board;
import org.machinecoding.models.BoardEntity;
import org.machinecoding.models.Dice;
import org.machinecoding.models.Player;
import org.machinecoding.models.teleports.Ladder;
import org.machinecoding.models.teleports.Snake;
import org.machinecoding.models.teleports.TeleporterEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int targetCell = 100;

int numSnakes = scan.nextInt();
List<TeleporterEntity> teleporterList = new ArrayList<>();
for (int i = 0; i < numSnakes; i++)
teleporterList.add(new Snake(scan.nextInt(), scan.nextInt()));

int numLadder = scan.nextInt();
for (int i = 0; i < numLadder; i++)
teleporterList.add(new Ladder(scan.nextInt(), scan.nextInt()));

int numPlayers = scan.nextInt();
List<Player> playerList = new ArrayList<>();
for (int i = 0; i < numPlayers; i++)
playerList.add(new Player(scan.next(), 0, targetCell));

// debug
// for (TeleporterEntity teleporter : teleporterList)
// System.out.println("start: " + teleporter.getStartCell() +
// ", end: " + teleporter.getEndCell());
//
// for (Player player : playerList)
// System.out.println(player.getName() + " is at " + player.getPosition());

List<Dice> diceList = new ArrayList<>();
diceList.add(new Dice());

BoardEntity boardEntity = new BoardEntity(new Board(targetCell),
teleporterList, playerList, diceList);
boardEntity.start();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.machinecoding.models;

public class Board {
private int cells;

public Board(int cells) { this.cells = cells; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.machinecoding.models;

import org.machinecoding.models.teleports.TeleporterEntity;
import org.machinecoding.strategy.DiceMovementStrategy;
import org.machinecoding.strategy.GameMovementStrategy;
import org.machinecoding.strategy.TeleporterMovementStrategy;

import java.util.List;

public class BoardEntity {
private final Board board;
private final List<TeleporterEntity> teleports;
private final List<Player> players;
private final List<Dice> dices;

private GameMovementStrategy movementStrategy;

public BoardEntity(Board board, List<TeleporterEntity> teleports, List<Player> players, List<Dice> dices) {
this.board = board;
this.teleports = teleports;
this.players = players;
this.dices = dices;

this.movementStrategy = new GameMovementStrategy(new DiceMovementStrategy(dices), new TeleporterMovementStrategy(teleports));
}

public void start() {
while(shouldPerformRound()){
performOneRound();
}
}

private boolean shouldPerformRound() {
return players.stream().filter(p -> !p.isWinner()).count() > 1;
}

public void performOneRound(){
for (Player player : players) {
if(!shouldPerformRound()) return;

int currentPosition = player.getPosition();
int newPosition = movementStrategy.move(currentPosition);
player.move(newPosition);

System.out.println(player.getName() + " rolled a " + movementStrategy.getDiceValue() + " and moved from " +
currentPosition + " to " + player.getPosition());

if (player.isWinner()) {
System.out.println(player.getName() + " wins the game");
}
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.machinecoding.models;

public class Dice {
public int roll() {
return (int) (Math.random() * 6) + 1;
}
}
29 changes: 29 additions & 0 deletions snakeAndLadder/src/main/java/org/machinecoding/models/Player.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.machinecoding.models;

public class Player {
private final String name;
private int position;
private final int targetPosition;

public Player(String name, int startPosition, int targetPosition){
this.name = name;
this.position = startPosition;
this.targetPosition = targetPosition;
}

public void move(int newPosition){
this.position = (newPosition > targetPosition) ? position : newPosition;
}

public boolean isWinner(){
return (position == targetPosition);
}

public int getPosition(){
return position;
}

public String getName(){
return name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.machinecoding.models.teleports;

public class Ladder extends TeleporterEntity {
public Ladder(int startCell, int endCell) {
super(startCell, endCell);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.machinecoding.models.teleports;

public class Snake extends TeleporterEntity {
public Snake(int startCell, int endCell) {
super(startCell, endCell);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.machinecoding.models.teleports;

public interface Teleporter {
boolean canBeUsed(int currentPosition);
int teleport();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.machinecoding.models.teleports;

public class TeleporterEntity implements Teleporter {
private final int startCell;
private final int endCell;

public TeleporterEntity(int startCell, int endCell){
this.startCell = startCell;
this.endCell = endCell;
}

@Override
public boolean canBeUsed(int currentPosition) {
return (startCell == currentPosition);
}

@Override
public int teleport() {
return endCell;
}

public int getStartCell() {
return startCell;
}

public int getEndCell() {
return endCell;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.machinecoding.strategy;

import org.machinecoding.models.Dice;

import java.util.List;

public class DiceMovementStrategy implements MovementStrategy {
private final List<Dice> dices;

public DiceMovementStrategy(List<Dice> dices) {
this.dices = dices;
}

@Override
public int move(int pos) {
int steps = 0;
for (Dice dice : dices) {
steps += dice.roll();
}
return pos + steps;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.machinecoding.strategy;

public class GameMovementStrategy implements MovementStrategy {
final DiceMovementStrategy diceMovementStrategy;
final TeleporterMovementStrategy teleporterMovementStrategy;

private int diceValue = 0;
private int teleporterValue = 0;

public GameMovementStrategy(DiceMovementStrategy diceMovementStrategy,
TeleporterMovementStrategy teleporterMovementStrategy) {
this.diceMovementStrategy = diceMovementStrategy;
this.teleporterMovementStrategy = teleporterMovementStrategy;
}

@Override
public int move(int pos) {
int diceMovedPosition = diceMovementStrategy.move(pos);
this.diceValue = diceMovedPosition - pos;

int teleporterMovedPosition = teleporterMovementStrategy.move(diceMovedPosition);
this.teleporterValue = teleporterMovedPosition - diceMovedPosition;

return teleporterMovedPosition;
}

public int getDiceValue() {
return diceValue;
}
public int getTeleporterValue() {
return teleporterValue;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.machinecoding.strategy;

public interface MovementStrategy {
int move(int pos);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.machinecoding.strategy;

import org.machinecoding.models.teleports.TeleporterEntity;

import java.util.List;

public class TeleporterMovementStrategy implements MovementStrategy {
private final List<TeleporterEntity> teleports;

public TeleporterMovementStrategy(List<TeleporterEntity> teleports) {
this.teleports = teleports;
}

@Override
public int move(int pos) {
int currentPosition = pos;

while (true) {
boolean teleported = false;

for (TeleporterEntity teleport : teleports) {
if (teleport.canBeUsed(currentPosition)) {
currentPosition = teleport.teleport();
teleported = true;
break;
}
}

if (!teleported) {
break;
}
}

return currentPosition;
}

}
Loading