Labbcomplete

This commit is contained in:
Imbus 2023-09-20 16:14:09 +02:00
parent 62b47d8f8a
commit 88726b692c
6 changed files with 62 additions and 40 deletions

View file

@ -7,7 +7,8 @@ public class Board {
private String winner_id = null; private String winner_id = null;
public Optional<String> getWinnder() { public Optional<String> getWinnder() {
if (winner_id == null) return Optional.empty(); if (winner_id == null)
return Optional.empty();
return Optional.of(winner_id); return Optional.of(winner_id);
} }
@ -16,21 +17,16 @@ public class Board {
} }
public void takePins(int pins) { public void takePins(int pins) {
this.pins -= Math.min(2, pins); this.pins -= Math.min(this.pins, Math.min(2, pins));
this.pins = Math.max(0, this.pins);
} }
public void alert(String userId, int pins) { public int alert(String userId, int pins) {
if(this.pins <= 0) System.out.println(userId + String.format(" picks %d and wins!", pins)); if (this.pins <= 0)
else System.out.println(userId + " took " + pins + " pins."); UserInterface.printMessage(String.format("%s picks %d and wins!", userId, pins));
} else
UserInterface.printMessage(String.format(
public void printState() { "%s picks %d pins.\n%d pins remaining.", userId, pins, this.pins));
printState(false); return 0;
}
public void printState(boolean lineBreak) {
System.out.println("There are " + this.pins + " pins left.");
if (lineBreak) System.out.println();
} }
// GetPinsAmount // GetPinsAmount

View file

@ -8,10 +8,8 @@ public class ComputerPlayer extends Player {
@Override @Override
int takePins(Board board) { int takePins(Board board) {
int n = Math.min( int n = Math.min(
(int) (Math.random()*2 + 1), // Random int between 1 and 2 (int) (Math.random() * 2 + 1), // Random int between 1 and 2
board.getNoPins() board.getNoPins());
);
System.out.println(n);
board.takePins(n); board.takePins(n);
board.alert(this.getUserId(), n); board.alert(this.getUserId(), n);
return n; return n;

View file

@ -1,7 +1,5 @@
package labbar; package labbar;
import javax.swing.JOptionPane;
public class HumanPlayer extends Player { public class HumanPlayer extends Player {
HumanPlayer(String userId) { HumanPlayer(String userId) {
super(userId); super(userId);
@ -9,14 +7,10 @@ public class HumanPlayer extends Player {
@Override @Override
int takePins(Board board) { int takePins(Board board) {
// Filter all non-digits from the input int input = UserInterface.askForInt("How many pins do you want to take?");
String input = JOptionPane.showInputDialog("How many pins do you want to take?").chars() if(input < 0) return input; // Propagate input
.filter(Character::isDigit) int i = Math.min(board.getNoPins(), Math.min(2, input)); // Clamp input
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
.toString();
int i = Math.max(board.getNoPins(), Math.min(2, Integer.parseInt(input))); // "Should" never throw given input is sanitized
board.takePins(i); board.takePins(i);
board.alert(this.getUserId(), i); board.alert(this.getUserId(), i);
return i; return i;

View file

@ -1,18 +1,27 @@
package labbar; package labbar;
import java.util.ArrayList;
import java.util.List;
public class TakePinsGame { public class TakePinsGame {
static void abort(String message) {
System.out.println(message);
System.exit(0);
}
public static void main(String[] args) { public static void main(String[] args) {
Board board = new Board(); Board board = new Board();
board.setUp(10); board.setUp(10);
board.printState(); // board.printState();
Player player = new ComputerPlayer("Human");
Player cpu = new ComputerPlayer("Computer");
for(boolean playerTurn = true; board.getNoPins() > 0; playerTurn = !playerTurn) { List<Player> players = new ArrayList<Player>();
if(playerTurn) player.takePins(board); players.add(new HumanPlayer("Human"));
else cpu.takePins(board); players.add(new ComputerPlayer("Siri"));
board.printState(true); 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");
} }
} }
} }

View 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;
}
}
}

View file

@ -7,17 +7,15 @@ class PlayerTests{
@Test void fullCpuGameTest() { @Test void fullCpuGameTest() {
Board b = new Board(); Board b = new Board();
// Unreasonable amount of pins b.setUp(10);
b.setUp(500);
// Two computer players // Two computer players
Player player_a = new ComputerPlayer("Siri"); Player player_a = new ComputerPlayer("Cortana");
Player player_b = new ComputerPlayer("Hal"); Player player_b = new ComputerPlayer("Alexa");
for(boolean playerTurn = true; b.getNoPins() > 0; playerTurn = !playerTurn) { for(boolean playerTurn = true; b.getNoPins() > 0; playerTurn = !playerTurn) {
if(playerTurn) player_a.takePins(b); if(playerTurn) player_a.takePins(b);
else player_b.takePins(b); else player_b.takePins(b);
b.printState();
} }
assertEquals(0, b.getNoPins()); assertEquals(0, b.getNoPins());