parent
64f1e42691
commit
300353ffc4
@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="11" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/tictactoe.iml" filepath="$PROJECT_DIR$/tictactoe.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
@ -0,0 +1,7 @@
|
||||
package model;
|
||||
|
||||
public class IllegalGameStateException extends Exception{
|
||||
public IllegalGameStateException(){
|
||||
super("This gamestate is illegal");
|
||||
}
|
||||
}
|
@ -1,4 +1,11 @@
|
||||
package model;
|
||||
|
||||
public class WinChecker {
|
||||
public boolean isGameOver(Board b) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public Player getWinner(Board b) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -1,2 +1,171 @@
|
||||
import model.Board;
|
||||
import model.IllegalGameStateException;
|
||||
import model.Player;
|
||||
import model.WinChecker;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class WinCheckerTests {
|
||||
private static Random rng = new Random();
|
||||
|
||||
public static Stream<Arguments> isGameOverTestData() {
|
||||
return Stream.of(
|
||||
Arguments.of(new Board(), false),
|
||||
Arguments.of(topRowXWins(), true),
|
||||
Arguments.of(midRowOWins(), true),
|
||||
Arguments.of(bottomRow(), true),
|
||||
Arguments.of(leftColumn(), true),
|
||||
Arguments.of(midColumn(), true),
|
||||
Arguments.of(rightColumn(), true),
|
||||
Arguments.of(diagUp(), true),
|
||||
Arguments.of(diagDown(), true),
|
||||
Arguments.of(stalemate(), true)
|
||||
);
|
||||
}
|
||||
|
||||
private static Board stalemate() {
|
||||
Board b = new Board();
|
||||
b.setBox(1, 1, Player.X);
|
||||
b.setBox(1, 2, Player.O);
|
||||
b.setBox(1, 3, Player.X);
|
||||
b.setBox(2, 1, Player.O);
|
||||
b.setBox(2, 2, Player.O);
|
||||
b.setBox(2, 3, Player.X);
|
||||
b.setBox(3, 1, Player.X);
|
||||
b.setBox(3, 2, Player.X);
|
||||
b.setBox(3, 3, Player.O);
|
||||
return b;
|
||||
}
|
||||
|
||||
private static Board diagDown() {
|
||||
Board b = new Board();
|
||||
b.setBox(3, 1, Player.O);
|
||||
b.setBox(2, 2, Player.O);
|
||||
b.setBox(1, 3, Player.O);
|
||||
return b;
|
||||
}
|
||||
|
||||
private static Board diagUp() {
|
||||
Board b = new Board();
|
||||
b.setBox(1, 1, Player.O);
|
||||
b.setBox(2, 2, Player.O);
|
||||
b.setBox(3, 3, Player.O);
|
||||
return b;
|
||||
}
|
||||
|
||||
private static Board rightColumn() {
|
||||
Board b = new Board();
|
||||
b.setBox(3, 1, Player.X);
|
||||
b.setBox(3, 2, Player.X);
|
||||
b.setBox(3, 3, Player.X);
|
||||
return b;
|
||||
}
|
||||
|
||||
private static Board midColumn() {
|
||||
Board b = new Board();
|
||||
b.setBox(2, 1, Player.O);
|
||||
b.setBox(2, 2, Player.O);
|
||||
b.setBox(2, 3, Player.O);
|
||||
return b;
|
||||
}
|
||||
|
||||
private static Board leftColumn() {
|
||||
Board b = new Board();
|
||||
b.setBox(1, 1, Player.O);
|
||||
b.setBox(1, 2, Player.O);
|
||||
b.setBox(1, 3, Player.O);
|
||||
return b;
|
||||
}
|
||||
|
||||
private static Board bottomRow() {
|
||||
Board b = new Board();
|
||||
b.setBox(1, 3, Player.X);
|
||||
b.setBox(2, 3, Player.X);
|
||||
b.setBox(3, 3, Player.X);
|
||||
return b;
|
||||
}
|
||||
|
||||
private static Board midRowOWins() {
|
||||
Board b = new Board();
|
||||
b.setBox(1, 2, Player.O);
|
||||
b.setBox(2, 2, Player.O);
|
||||
b.setBox(3, 2, Player.O);
|
||||
return b;
|
||||
}
|
||||
|
||||
private static Board topRowXWins() {
|
||||
Board b = new Board();
|
||||
b.setBox(1, 1, Player.X);
|
||||
b.setBox(2, 1, Player.X);
|
||||
b.setBox(3, 1, Player.X);
|
||||
return b;
|
||||
}
|
||||
|
||||
public static Stream<Arguments> getWinnerTestNormalData() {
|
||||
return Stream.of(
|
||||
Arguments.of(new Board(), null),
|
||||
Arguments.of(stalemate(), null),
|
||||
Arguments.of(topRowXWins(), Player.X),
|
||||
Arguments.of(midRowOWins(), Player.O)
|
||||
);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("isGameOverTestData")
|
||||
public void isGameOverTest(Object[] args){
|
||||
Board b = (Board) args[0];
|
||||
boolean expected = (boolean) args[1];
|
||||
boolean actual = new WinChecker().isGameOver(b);
|
||||
Assertions.assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("getWinnerTestNormalData")
|
||||
public void getWinnerTestNormal(Object[] args){
|
||||
Board b = (Board) args[0];
|
||||
Player expected = (Player) args[1];
|
||||
Player actual = new WinChecker().getWinner(b);
|
||||
Assertions.assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getWinnerTestThrows(){
|
||||
Board b = new Board();
|
||||
b.setBox(1, 1, Player.X);
|
||||
b.setBox(1, 2, Player.X);
|
||||
b.setBox(1, 3, Player.X);
|
||||
b.setBox(2, 1, Player.O);
|
||||
b.setBox(2, 2, Player.O);
|
||||
b.setBox(2, 3, Player.O);
|
||||
Assertions.assertThrows(IllegalGameStateException.class, () -> new WinChecker().getWinner(b));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getWinnerTestRandom(){
|
||||
Player currentBox;
|
||||
Board b = new Board();
|
||||
Player expected = null;
|
||||
boolean shouldThrow = false;
|
||||
for (int i = 1; i <= 3; ++i)
|
||||
for (int j = 1; j <= 3; ++j) {
|
||||
int rand = rng.nextInt(3);
|
||||
Player p = switch (rand){
|
||||
case 1 -> Player.X;
|
||||
case 2 -> Player.O;
|
||||
default -> null;
|
||||
};
|
||||
b.setBox(i, j, p);
|
||||
}
|
||||
|
||||
currentBox = b.getBox(1, 1);
|
||||
if(currentBox != null){
|
||||
//if (currentBox == b.getBox(1, 2) && currentBox == b.getBox(1, 3))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" name="junit.jupiter" level="project" />
|
||||
</component>
|
||||
</module>
|
Loading…
Reference in new issue