Labbcomplete
This commit is contained in:
parent
62b47d8f8a
commit
88726b692c
6 changed files with 62 additions and 40 deletions
|
@ -7,7 +7,8 @@ public class Board {
|
|||
private String winner_id = null;
|
||||
|
||||
public Optional<String> 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
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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<Player> players = new ArrayList<Player>();
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
27
app/src/main/java/labbar/UserInterface.java
Normal file
27
app/src/main/java/labbar/UserInterface.java
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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());
|
||||
|
|
Loading…
Reference in a new issue