Compare commits

..

No commits in common. "88726b692cc05edd640192823335a49ee7f8d747" and "cca4201ad7bdc0f974a5e2647f93d3141f4ad22a" have entirely different histories.

7 changed files with 40 additions and 62 deletions

View file

@ -7,8 +7,7 @@ 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);
}
@ -17,16 +16,21 @@ public class Board {
}
public void takePins(int pins) {
this.pins -= Math.min(this.pins, Math.min(2, pins));
this.pins -= Math.min(2, pins);
this.pins = Math.max(0, this.pins);
}
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;
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();
}
// GetPinsAmount

View file

@ -8,8 +8,10 @@ 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());
(int) (Math.random()*2 + 1), // Random int between 1 and 2
board.getNoPins()
);
System.out.println(n);
board.takePins(n);
board.alert(this.getUserId(), n);
return n;

View file

@ -1,5 +1,7 @@
package labbar;
import javax.swing.JOptionPane;
public class HumanPlayer extends Player {
HumanPlayer(String userId) {
super(userId);
@ -7,10 +9,14 @@ public class HumanPlayer extends Player {
@Override
int takePins(Board board) {
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
// 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 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;

View file

@ -1,27 +1,18 @@
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();
board.printState();
Player player = new ComputerPlayer("Human");
Player cpu = new ComputerPlayer("Computer");
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");
for(boolean playerTurn = true; board.getNoPins() > 0; playerTurn = !playerTurn) {
if(playerTurn) player.takePins(board);
else cpu.takePins(board);
board.printState(true);
}
}
}

View file

@ -1,27 +0,0 @@
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,15 +7,17 @@ class PlayerTests{
@Test void fullCpuGameTest() {
Board b = new Board();
b.setUp(10);
// Unreasonable amount of pins
b.setUp(500);
// Two computer players
Player player_a = new ComputerPlayer("Cortana");
Player player_b = new ComputerPlayer("Alexa");
Player player_a = new ComputerPlayer("Siri");
Player player_b = new ComputerPlayer("Hal");
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());

Binary file not shown.