Skip to content
This repository has been archived by the owner on Nov 29, 2017. It is now read-only.

Commit

Permalink
Merge pull request #24 from GoSuji/GameTree
Browse files Browse the repository at this point in the history
Game tree
  • Loading branch information
vapour101 committed Aug 31, 2017
2 parents d613948 + 5766f11 commit f0cc0ea
Show file tree
Hide file tree
Showing 10 changed files with 410 additions and 82 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ build/
out/

#Local Settings
.idea/workspace.xml
.idea/workspace.xml*
.idea/dictionaries
.gradle/
55 changes: 32 additions & 23 deletions src/main/java/logic/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,8 @@ public Board() {
}
}

public Collection<Coords> getStones(StoneColour colour) {
return getChainSet(colour).getStones();
}

private ChainSet getChainSet(StoneColour colour) {
return stones[colour.ordinal()];
}

public void playStone(Coords coords, StoneColour colour) {
if ( !isLegalMove(coords, colour) )
if ( isOccupied(coords) )
throwIllegalMove(coords);

ChainSet stones = getChainSet(colour);
Expand All @@ -67,29 +59,20 @@ private void addCaptures(int number, StoneColour colour) {
captures[colour.ordinal()] += number;
}

public boolean isLegalMove(Coords coords, StoneColour colour) {
boolean isLegal;

isLegal = !isOccupied(coords);
isLegal &= !isSuicide(colour, coords);

return isLegal;
}

private boolean isOccupied(Coords coords) {
public boolean isOccupied(Coords coords) {
return getChainSet(StoneColour.BLACK).contains(coords) || getChainSet(StoneColour.WHITE).contains(coords);
}

private boolean isSuicide(StoneColour colour, Coords coords) {
return getChainSet(colour).isSuicide(coords, getChainSet(colour.other()));
private ChainSet getChainSet(StoneColour colour) {
return stones[colour.ordinal()];
}

private void throwIllegalMove(Coords coords) {
throw new IllegalArgumentException(coords.toString() + " is an illegal move.");
}

public int getCaptures(StoneColour colour) {
return captures[colour.ordinal()];
public boolean isSuicide(Coords coords, StoneColour colour) {
return getChainSet(colour).isSuicide(coords, getChainSet(colour.other()));
}

Chain getChainAtCoords(Coords coords) {
Expand All @@ -99,4 +82,30 @@ Chain getChainAtCoords(Coords coords) {

return null;
}

@Override
public boolean equals(Object other) {
if ( this == other )
return true;
else if ( !(other instanceof Board) )
return false;
else {
Board compare = (Board) other;
boolean equals = this.getStones(StoneColour.BLACK).containsAll(compare.getStones(StoneColour.BLACK));
equals &= compare.getStones(StoneColour.BLACK).containsAll(this.getStones(StoneColour.BLACK));

equals &= this.getStones(StoneColour.WHITE).containsAll(compare.getStones(StoneColour.WHITE));
equals &= compare.getStones(StoneColour.WHITE).containsAll(this.getStones(StoneColour.WHITE));

return equals;
}
}

public Collection<Coords> getStones(StoneColour colour) {
return getChainSet(colour).getStones();
}

public int getCaptures(StoneColour colour) {
return captures[colour.ordinal()];
}
}
36 changes: 36 additions & 0 deletions src/main/java/logic/GameHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2017
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package logic;

import util.Coords;
import util.StoneColour;

import java.util.Collection;

public abstract class GameHandler {

public abstract boolean isLegalMove(Coords move, StoneColour colour);

public abstract void playStone(Coords move, StoneColour colour);

public final Collection<Coords> getStones(StoneColour colour) {
return getBoard().getStones(colour);
}

public abstract Board getBoard();
}
74 changes: 74 additions & 0 deletions src/main/java/logic/GameTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2017
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package logic;

import util.Coords;
import util.StoneColour;

import java.util.LinkedList;

public class GameTree {

LinkedList<Move> moveList;

GameTree() {
moveList = new LinkedList<>();
}

public void playMove(Coords coords, StoneColour colour) {
Move move = new Move(coords, colour);
moveList.add(move);
}

public Board getPosition() {
return getPositionAt(moveList.size());
}

public Board getPositionAt(int moveNumber) {
Board board = new Board();

for (int i = 0; i < moveNumber; ++i) {
board.playStone(moveList.get(i).coords, moveList.get(i).colour);
}

return board;
}

public void stepBack() {
moveList.removeLast();
}

public int getNumberOfMoves() {
return moveList.size();
}

public Board getLastPosition() {
return getPositionAt(moveList.size() - 1);
}

private class Move {

public Coords coords;
public StoneColour colour;

Move(Coords coords, StoneColour colour) {
this.coords = coords;
this.colour = colour;
}
}
}
58 changes: 58 additions & 0 deletions src/main/java/logic/LocalGameHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2017
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package logic;

import util.Coords;
import util.StoneColour;

public class LocalGameHandler extends GameHandler {

private GameTree gameTree;

public LocalGameHandler() {
gameTree = new GameTree();
}

@Override
public boolean isLegalMove(Coords move, StoneColour colour) {
boolean isLegal;

isLegal = !gameTree.getPosition().isOccupied(move);
isLegal &= !gameTree.getPosition().isSuicide(move, colour);

if ( isLegal && gameTree.getNumberOfMoves() > 2 ) {
Board previous = gameTree.getLastPosition();
Board future = gameTree.getPosition();
future.playStone(move, colour);

isLegal = !previous.equals(future);
}

return isLegal;
}

@Override
public void playStone(Coords move, StoneColour colour) {
gameTree.playMove(move, colour);
}

@Override
public Board getBoard() {
return gameTree.getPosition();
}
}
31 changes: 16 additions & 15 deletions src/main/java/ui/controller/BoardController.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import logic.Board;
import logic.BoardScorer;
import logic.GameHandler;
import logic.LocalGameHandler;
import ui.drawer.BoardDrawer;
import ui.drawer.BoardScoreDrawer;
import util.*;
Expand All @@ -52,7 +53,7 @@ public class BoardController implements Initializable {
public Button whiteDone;

private Canvas boardCanvas;
private Board board;
private GameHandler game;
private BoardScorer boardScorer;
private BoardDrawer boardDrawer;

Expand All @@ -62,7 +63,7 @@ public class BoardController implements Initializable {
private double komi;

public BoardController() {
board = new Board();
game = new LocalGameHandler();
blackMove = true;
pass = false;
gameState = GameState.PLAYING;
Expand Down Expand Up @@ -141,7 +142,7 @@ private void constructCanvas() {
}

private void drawBoard() {
boardDrawer.draw(board);
boardDrawer.draw(game.getBoard());

updateScore();
}
Expand All @@ -153,19 +154,19 @@ private void updateScore() {
}
}

void setKomi(double komi) {
this.komi = komi;
}

void setHandicap(int handicap) {
if ( board.getStones(StoneColour.BLACK).size() > 0 || board.getStones(StoneColour.WHITE).size() > 0 )
board = new Board();
if ( game.getStones(StoneColour.BLACK).size() > 0 || game.getStones(StoneColour.WHITE).size() > 0 )
game = new LocalGameHandler();

blackMove = (handicap == 0);

if ( handicap > 0 )
for (Coords stone : HandicapHelper.getHandicapStones(handicap))
board.playStone(stone, StoneColour.BLACK);
game.playStone(stone, StoneColour.BLACK);
}

void setKomi(double komi) {
this.komi = komi;
}

private void canvasClicked(MouseEvent mouseEvent) {
Expand All @@ -183,8 +184,8 @@ else if ( mouseEvent.getButton() == MouseButton.SECONDARY )
boardScorer.unmarkGroupDead(boardPos);
}
else if ( gameState == GameState.PLAYING ) {
if ( board.isLegalMove(boardPos, getTurnPlayer()) ) {
board.playStone(boardPos, getTurnPlayer());
if ( game.isLegalMove(boardPos, getTurnPlayer()) ) {
game.playStone(boardPos, getTurnPlayer());
blackMove = !blackMove;
pass = false;
}
Expand All @@ -209,7 +210,7 @@ private void canvasHover(MouseEvent mouseEvent) {

StoneColour turnPlayer = blackMove ? StoneColour.BLACK : StoneColour.WHITE;

boardDrawer.drawGhostStone(board, mousePosition, turnPlayer);
boardDrawer.drawGhostStone(game, mousePosition, turnPlayer);
}

private void resizeScore(ObservableValue<? extends Number> observableValue, Number number, Number t1) {
Expand Down Expand Up @@ -248,7 +249,7 @@ private void pass(ActionEvent event) {

if ( pass ) {
gameState = GameState.SCORING;
boardScorer = new BoardScorer(board, komi);
boardScorer = new BoardScorer(game.getBoard(), komi);
passButton.setVisible(false);
scorePane.setVisible(true);

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/ui/drawer/BoardDrawer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import logic.Board;
import logic.GameHandler;
import util.CoordProjector;
import util.Coords;
import util.DrawCoords;
Expand Down Expand Up @@ -49,7 +50,7 @@ public void draw(Board board) {
drawStones(board);
}

public void drawGhostStone(Board board, DrawCoords position, StoneColour colour) {
public void drawGhostStone(GameHandler board, DrawCoords position, StoneColour colour) {
double radius = getStoneRadius();
CoordProjector projector = new CoordProjector(getBoardLength(canvas), getTopLeftCorner(canvas));
GraphicsContext context = getGraphicsContext();
Expand Down
Loading

0 comments on commit f0cc0ea

Please sign in to comment.