Skip to content

Commit

Permalink
Changed Java Version from target 1.3 and source 1.5 (WTF?! its 2017 o…
Browse files Browse the repository at this point in the history
…0) to 1.8 for both.

Added Maven compile plugin to manage java versions.
Refactored some methods and fields.
Refactored directory layout and moved resources into resources folder.
Fixed a bug in level 3 AI where it would idle in 90% of the cases.
Fixed a bug in the level 3 AI which caused a kind of "shadow" moves.
  • Loading branch information
Laura Hartgers committed Jan 3, 2017
1 parent 8cdddfb commit e33a973
Show file tree
Hide file tree
Showing 15 changed files with 80 additions and 40 deletions.
9 changes: 7 additions & 2 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 3 additions & 11 deletions HoboOthello.iml
@@ -1,18 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5" inherit-compiler-output="false">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.8.0" level="project" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.8.0" level="project" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.8.0" level="project" />
<orderEntry type="module-library">
<library>
<CLASSES>
Expand All @@ -24,12 +22,6 @@
</orderEntry>
<orderEntry type="library" name="Maven: junit:junit:4.12" level="project" />
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
<orderEntry type="library" exported="" name="Maven: com.google.code.gson:gson:2.8.0" level="project" />
<orderEntry type="library" name="Maven: junit:junit:4.12" level="project" />
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.8.0" level="project" />
<orderEntry type="library" name="Maven: junit:junit:4.12" level="project" />
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.8.0" level="project" />
</component>
</module>
25 changes: 21 additions & 4 deletions pom.xml
Expand Up @@ -7,23 +7,40 @@
<groupId>de.htw-berlin.de.HoboOthello</groupId>
<artifactId>HoboOthello</artifactId>
<version>0.1</version>

<properties>
<junit.version>4.12</junit.version>
<gson.version>2.8.0</gson.version>
<java-version>1.8</java-version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<version>${junit.version}</version>
</dependency>

<!-- Gson: Java to Json conversion -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.0</version>
<scope>compile</scope>
<version>${gson.version}</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${java-version}</source>
<target>${java-version}</target>
</configuration>
</plugin>
</plugins>
</build>

</project>
2 changes: 1 addition & 1 deletion src/main/java/de/htw_berlin/HoboOthello/Core/Board.java
Expand Up @@ -258,7 +258,7 @@ public boolean getBoardIsFull() {
return boardIsFull();
}

public Field[][] isFields() {
public Field[][] getFields() {
return this.fields;
}

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/de/htw_berlin/HoboOthello/Core/Field.java
Expand Up @@ -39,6 +39,14 @@ public Field(int x, int y) {
this.y = y;
}

public Field(Field fieldToCopy) {
this.x = fieldToCopy.getX();
this.y = fieldToCopy.getY();
if (fieldToCopy.getStone() != null) {
this.stone = new Stone(fieldToCopy.getStone());
}
}

public void setStone(Stone stoneToSet) {
this.stone = stoneToSet;
}
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/de/htw_berlin/HoboOthello/Core/Game.java
Expand Up @@ -49,7 +49,7 @@ public void newGame(int boardSize, Player newPlayerBlack, Player newPlayerWhite)
this.gameState = GameState.RUNNING;

// set possible moves
GameRule move = new GameRule(this.gameBoard.isFields());
GameRule move = new GameRule(this.gameBoard.getFields());
move.changeAllPossibleFieldsToTrue(currentPlayer.getColor());
this.gameBoard.setFields(move.getFields());

Expand Down Expand Up @@ -90,7 +90,7 @@ public boolean setTurn(Field field) {
}

// return if the turn was successful (possible)
GameRule move = new GameRule(this.gameBoard.isFields());
GameRule move = new GameRule(this.gameBoard.getFields());

// todo remove debug code
//System.out.printf("Field: %d:%d%n", field.getX(), field.getY());
Expand Down Expand Up @@ -123,13 +123,12 @@ public boolean setTurn(Field field) {
// todo delete savegame.json
this.gameState = GameState.STOP;
}
} else {
}
if (currentPlayer instanceof KI) {
Field playerTurn = currentPlayer.setMove(this.gameBoard);
if (playerTurn != null) {
setTurn(playerTurn);
}
this.gameState = GameState.RUNNING;

}

// todo remove debug code
Expand Down Expand Up @@ -173,7 +172,7 @@ public Player getCurrentPlayer() {
/**
* update the player Typ after load a Game from a savegame
*/
public void updatePlayerTyp () {
public void updatePlayerTyp() {
// todo update for network
switch (this.playerBlack.getPlayerType()) {
case KI_LEVEL1:
Expand Down Expand Up @@ -207,7 +206,7 @@ private void saveFieldToJson() {
File file = new File("field.json");

Gson gson = new GsonBuilder().create();
String content = gson.toJson(gameBoard.isFields());
String content = gson.toJson(gameBoard.getFields());

try {
FileWriter fw = new FileWriter(file.getAbsoluteFile());
Expand Down Expand Up @@ -250,6 +249,7 @@ public Player getPlayerWhite() {

/**
* Use KI Level 3 to generate the best move for the player
*
* @return the Field with the best move
*/
public Field showHint() {
Expand All @@ -265,10 +265,11 @@ public HobeModeType getLastHobeModeType() {

/**
* activate Random Hobomode action
*
* @return Start field of destruction
*/
public Field activateHobeMode() {
HoboMode hoboMode = new HoboMode(gameBoard.isFields());
HoboMode hoboMode = new HoboMode(gameBoard.getFields());
hoboMode.changeAllPossibleFieldsToTrue(currentPlayer.getColor());
lastHobeModeType = hoboMode.getHobeModeType();

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/de/htw_berlin/HoboOthello/Core/Stone.java
Expand Up @@ -21,6 +21,12 @@ public Stone(Color stoneColor) {
this.stoneColor = stoneColor;
}

public Stone(Stone stoneToCopy) {
if (stoneToCopy.getColor() != null) {
this.stoneColor = stoneToCopy.getColor();
}
}

public Color getColor() {
return this.stoneColor;
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/de/htw_berlin/HoboOthello/GUI/Gameview.java
Expand Up @@ -194,10 +194,10 @@ public Gameview(int boardSize) {
/*
* initialise all stones
*/
grey = new ImageIcon(this.getClass().getResource("greybutton.png"));
white = new ImageIcon(this.getClass().getResource("whitebutton.png"));
black = new ImageIcon(this.getClass().getResource("blackbutton.png"));
hint = new ImageIcon(this.getClass().getResource("hint.png"));
grey = new ImageIcon(this.getClass().getResource("/greybutton.png"));
white = new ImageIcon(this.getClass().getResource("/whitebutton.png"));
black = new ImageIcon(this.getClass().getResource("/blackbutton.png"));
hint = new ImageIcon(this.getClass().getResource("/hint.png"));

setupScaleFactors(fieldView.length);
setupBlackImageIcon(var);
Expand Down
25 changes: 18 additions & 7 deletions src/main/java/de/htw_berlin/HoboOthello/KI/KI.java
Expand Up @@ -45,7 +45,7 @@ public KI(Color color, Level level) {
*/
public Field setMove(Board board) {
this.board = board;
this.gameRule = new GameRule(this.board.isFields());
this.gameRule = new GameRule(this.board.getFields());
Field fieldToSetMove;

if (level == level.LEVEL1) {
Expand Down Expand Up @@ -80,7 +80,7 @@ private List<Field> listPossibleMoves() {
List<Field> listOfPossibleMoves = new ArrayList<Field>();
List<Field> listOfAllFields = board.iterateThroughAllFields();

if (listOfAllFields != null) {
if (listOfAllFields != null && listOfAllFields.size() != 0) {
for (Field field : listOfAllFields) {
if (gameRule.isMoveAllowed(field, kiColor)) {
listOfPossibleMoves.add(field);
Expand Down Expand Up @@ -206,25 +206,25 @@ private Field pickTacticalField() {

//If there are corner fields available, these are a very good and tactical pick!
List<Field> listOfCornerFields = listPossibleCornerFields();
if (listOfCornerFields != null) {
if (listOfCornerFields != null && listOfCornerFields.size() != 0) {
return pickTacticalFieldFromList(listOfCornerFields);
}

//Side fields are also very good and tactical spots
List<Field> listOfSideFields = listPossibleSideFields();
if (listOfSideFields != null) {
if (listOfSideFields != null && listOfSideFields.size() != 0) {
return pickTacticalFieldFromList(listOfSideFields);
}

//It is smart to make a move on a field not too close to the border,
//so the other player can't get a stone in a corner of on a border field.
List<Field> listOfFieldsNotTooCloseToBorder = listPossibleFieldsNotTooCloseToBorder();
if (listOfFieldsNotTooCloseToBorder != null) {
if (listOfFieldsNotTooCloseToBorder != null && listOfFieldsNotTooCloseToBorder.size() != 0) {
return pickTacticalFieldFromList(listOfFieldsNotTooCloseToBorder);
}

List<Field> listOfAllPossibleMoves = listPossibleMoves();
if (listOfAllPossibleMoves != null) {
if (listOfAllPossibleMoves != null && listOfAllPossibleMoves.size() != 0) {
return pickTacticalFieldFromList(listOfAllPossibleMoves);
} else {
return null;
Expand Down Expand Up @@ -262,7 +262,8 @@ private int testHowManyStonesAreFlipped(Field field) {
// useage gamerule neu erstellen -> mit felder füllen -> testen -> auswerten -> löschen
// solange wiederholen wie dir spass macht

GameRule newGameRule = new GameRule(board.isFields());
Field[][] copiedFields = copyFields(board.getFields());
GameRule newGameRule = new GameRule(copiedFields);
newGameRule.setMove(field, kiColor);

Board newBoard = new Board(board.getBoardSize());
Expand All @@ -273,6 +274,16 @@ private int testHowManyStonesAreFlipped(Field field) {
return numberOfStonesFlipped;
}

private Field[][] copyFields(Field[][] originalFields) {
Field[][] copiedFields = new Field[originalFields.length][originalFields[0].length];
for (int i = 0; i < originalFields.length; i++) {
for (int j = 0; j < originalFields[i].length; j++) {
copiedFields[i][j] = new Field(originalFields[i][j]);
}
}
return copiedFields;
}

/*
int randomNumber = (int) (Math.random() * listOfFieldsNotCloseToBorder.size()); // picks random index of field in list
fieldToSet = listOfFieldsNotCloseToBorder.get(randomNumber);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/de/htw_berlin/HoboOthello/Network/Network.java
Expand Up @@ -70,8 +70,8 @@ private Field getTurn(Board boardOriginal, Board boardNetwork) {
// todo return error if the size differ
for (int x = 0; x < boardOriginal.getBoardSize(); x++) {
for (int y = 0; y < boardOriginal.getBoardSize(); y++) {
if (boardOriginal.isFields()[x][y].isEmpty() != boardNetwork.isFields()[x][y].isEmpty()) {
return boardNetwork.isFields()[x][y];
if (boardOriginal.getFields()[x][y].isEmpty() != boardNetwork.getFields()[x][y].isEmpty()) {
return boardNetwork.getFields()[x][y];
}
}
}
Expand Down
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes

0 comments on commit e33a973

Please sign in to comment.