This commit is contained in:
Imbus 2023-09-19 21:05:15 +02:00
commit 5e09d8a3a4
15 changed files with 609 additions and 0 deletions

43
app/build.gradle.kts Normal file
View file

@ -0,0 +1,43 @@
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java application project to get you started.
* For more details on building Java & JVM projects, please refer to https://docs.gradle.org/8.3/userguide/building_java_projects.html in the Gradle documentation.
*/
plugins {
// Apply the application plugin to add support for building a CLI application in Java.
application
}
repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}
dependencies {
// Use JUnit Jupiter for testing.
testImplementation("org.junit.jupiter:junit-jupiter:5.9.3")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
// This dependency is used by the application.
implementation("com.google.guava:guava:32.1.1-jre")
}
// Apply a specific Java toolchain to ease working on different environments.
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(17))
}
}
application {
// Define the main class for the application.
mainClass.set("labbar.TakePinsGame")
}
tasks.named<Test>("test") {
// Use JUnit Platform for unit tests.
useJUnitPlatform()
}

View file

@ -0,0 +1,40 @@
package labbar;
import java.util.Optional;
public class Board {
private int pins = 10;
private String winner_id = null;
public Optional<String> getWinnder() {
if (winner_id == null) return Optional.empty();
return Optional.of(winner_id);
}
public void setUp(int pins) {
this.pins = pins;
}
public void takePins(int pins) {
this.pins -= Math.min(2, pins);
this.pins = Math.max(0, this.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();
}
// GetPinsAmount
public int getNoPins() {
return pins;
}
}

View file

@ -0,0 +1,19 @@
package labbar;
public class ComputerPlayer extends Player {
ComputerPlayer(String userId) {
super(userId);
}
@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);
board.takePins(n);
board.alert(this.getUserId(), n);
return n;
}
}

View file

@ -0,0 +1,24 @@
package labbar;
import javax.swing.JOptionPane;
public class HumanPlayer extends Player {
HumanPlayer(String userId) {
super(userId);
}
@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 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

@ -0,0 +1,15 @@
package labbar;
public abstract class Player {
String userId;
Player(String userId) {
this.userId = userId;
}
String getUserId() {
return userId;
}
abstract int takePins(Board board);
}

View file

@ -0,0 +1,18 @@
package labbar;
public class TakePinsGame {
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");
for(boolean playerTurn = true; board.getNoPins() > 0; playerTurn = !playerTurn) {
if(playerTurn) player.takePins(board);
else cpu.takePins(board);
board.printState(true);
}
}
}

View file

@ -0,0 +1,13 @@
package labbar;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class BoardTest {
@Test void boardTest() {
Board b = new Board();
b.setUp(5);
assertEquals(5, b.getNoPins());
}
}

View file

@ -0,0 +1,25 @@
package labbar;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class PlayerTests{
@Test void fullCpuGameTest() {
Board b = new Board();
// Unreasonable amount of pins
b.setUp(500);
// Two computer players
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());
}
}