diff --git a/app/src/main/java/labbar/Board.java b/app/src/main/java/labbar/Board.java index 9bf17a8..79fcab5 100644 --- a/app/src/main/java/labbar/Board.java +++ b/app/src/main/java/labbar/Board.java @@ -7,7 +7,8 @@ public class Board { private String winner_id = null; public Optional getWinnder() { - if (winner_id == null) return Optional.empty(); + if (winner_id == null) + return Optional.empty(); return Optional.of(winner_id); } @@ -16,21 +17,16 @@ public class Board { } public void takePins(int pins) { - this.pins -= Math.min(2, pins); - this.pins = Math.max(0, this.pins); + this.pins -= Math.min(this.pins, Math.min(2, pins)); } - public void alert(String userId, int pins) { - if(this.pins <= 0) System.out.println(userId + String.format(" picks %d and wins!", pins)); - else System.out.println(userId + " took " + pins + " pins."); - } - - public void printState() { - printState(false); - } - public void printState(boolean lineBreak) { - System.out.println("There are " + this.pins + " pins left."); - if (lineBreak) System.out.println(); + public int alert(String userId, int pins) { + if (this.pins <= 0) + UserInterface.printMessage(String.format("%s picks %d and wins!", userId, pins)); + else + UserInterface.printMessage(String.format( + "%s picks %d pins.\n%d pins remaining.", userId, pins, this.pins)); + return 0; } // GetPinsAmount diff --git a/app/src/main/java/labbar/ComputerPlayer.java b/app/src/main/java/labbar/ComputerPlayer.java index 7e631d6..2d5a910 100644 --- a/app/src/main/java/labbar/ComputerPlayer.java +++ b/app/src/main/java/labbar/ComputerPlayer.java @@ -8,10 +8,8 @@ public class ComputerPlayer extends Player { @Override int takePins(Board board) { int n = Math.min( - (int) (Math.random()*2 + 1), // Random int between 1 and 2 - board.getNoPins() - ); - System.out.println(n); + (int) (Math.random() * 2 + 1), // Random int between 1 and 2 + board.getNoPins()); board.takePins(n); board.alert(this.getUserId(), n); return n; diff --git a/app/src/main/java/labbar/HumanPlayer.java b/app/src/main/java/labbar/HumanPlayer.java index 1a69221..aec2980 100644 --- a/app/src/main/java/labbar/HumanPlayer.java +++ b/app/src/main/java/labbar/HumanPlayer.java @@ -1,7 +1,5 @@ package labbar; -import javax.swing.JOptionPane; - public class HumanPlayer extends Player { HumanPlayer(String userId) { super(userId); @@ -9,14 +7,10 @@ public class HumanPlayer extends Player { @Override int takePins(Board board) { - // Filter all non-digits from the input - String input = JOptionPane.showInputDialog("How many pins do you want to take?").chars() - .filter(Character::isDigit) - .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append) - .toString(); + int input = UserInterface.askForInt("How many pins do you want to take?"); + if(input < 0) return input; // Propagate input + int i = Math.min(board.getNoPins(), Math.min(2, input)); // Clamp input - - int i = Math.max(board.getNoPins(), Math.min(2, Integer.parseInt(input))); // "Should" never throw given input is sanitized board.takePins(i); board.alert(this.getUserId(), i); return i; diff --git a/app/src/main/java/labbar/TakePinsGame.java b/app/src/main/java/labbar/TakePinsGame.java index c3c417b..c7eac3a 100644 --- a/app/src/main/java/labbar/TakePinsGame.java +++ b/app/src/main/java/labbar/TakePinsGame.java @@ -1,18 +1,27 @@ package labbar; +import java.util.ArrayList; +import java.util.List; public class TakePinsGame { + static void abort(String message) { + System.out.println(message); + System.exit(0); + } + public static void main(String[] args) { Board board = new Board(); board.setUp(10); - board.printState(); - - Player player = new ComputerPlayer("Human"); - Player cpu = new ComputerPlayer("Computer"); + // board.printState(); - for(boolean playerTurn = true; board.getNoPins() > 0; playerTurn = !playerTurn) { - if(playerTurn) player.takePins(board); - else cpu.takePins(board); - board.printState(true); + List players = new ArrayList(); + players.add(new HumanPlayer("Human")); + players.add(new ComputerPlayer("Siri")); + players.add(new ComputerPlayer("Hal")); + + if(players.size() < 2) abort("Not enough players"); + + for (int index = 0; board.getNoPins() > 0; index = (index + 1) % players.size()) { + if(players.get(index).takePins(board) < 0) abort("User aborted game"); } } } diff --git a/app/src/main/java/labbar/UserInterface.java b/app/src/main/java/labbar/UserInterface.java new file mode 100644 index 0000000..0059280 --- /dev/null +++ b/app/src/main/java/labbar/UserInterface.java @@ -0,0 +1,27 @@ +package labbar; + +import javax.swing.JOptionPane; + + +public class UserInterface { + public static void printMessage(String message) { + JOptionPane.showMessageDialog(null, message); + } + + public static int askForInt(String message) { + String input = JOptionPane.showInputDialog(message); + if (input == null) return -2; + + // Shadow input + input = input.chars() + .filter(Character::isDigit) + .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append) + .toString(); + + try { + return Integer.parseInt(input); + } catch (NumberFormatException e) { + return -1; + } + } +} diff --git a/app/src/test/java/labbar/PlayerTests.java b/app/src/test/java/labbar/PlayerTests.java index 048b2e2..35493fc 100644 --- a/app/src/test/java/labbar/PlayerTests.java +++ b/app/src/test/java/labbar/PlayerTests.java @@ -7,17 +7,15 @@ class PlayerTests{ @Test void fullCpuGameTest() { Board b = new Board(); - // Unreasonable amount of pins - b.setUp(500); + b.setUp(10); // Two computer players - Player player_a = new ComputerPlayer("Siri"); - Player player_b = new ComputerPlayer("Hal"); + Player player_a = new ComputerPlayer("Cortana"); + Player player_b = new ComputerPlayer("Alexa"); for(boolean playerTurn = true; b.getNoPins() > 0; playerTurn = !playerTurn) { if(playerTurn) player_a.takePins(b); else player_b.takePins(b); - b.printState(); } assertEquals(0, b.getNoPins());