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

Commit

Permalink
Merge beb19c9 into 0e6602c
Browse files Browse the repository at this point in the history
  • Loading branch information
vapour101 committed Aug 19, 2017
2 parents 0e6602c + beb19c9 commit 5afd91e
Show file tree
Hide file tree
Showing 18 changed files with 957 additions and 33 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Suji
[![Build Status](https://travis-ci.org/GoSuji/Suji.svg?branch=master)](https://travis-ci.org/GoSuji/Suji) [![Coverage Status](https://coveralls.io/repos/github/GoSuji/Suji/badge.svg?branch=master)](https://coveralls.io/github/GoSuji/Suji?branch=master)

The Suji Go Client
7 changes: 5 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ dependencies {
}

jfx {
mainClass = 'Main'
mainClass = 'ui.Main'
}

cobertura.coverageFormats = ['html', 'xml']
cobertura {
coverageFormats = ['html', 'xml']
coverageExcludes = ['.*instrumented_classes.ui.*']
}

version '0.1'

Expand Down
2 changes: 0 additions & 2 deletions src/main/java/Controller.java

This file was deleted.

21 changes: 0 additions & 21 deletions src/main/java/Main.java

This file was deleted.

70 changes: 70 additions & 0 deletions src/main/java/logic/Board.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (C) 2017 Vincent Varkevisser
*
* 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 java.util.Set;

public class Board {
private ChainSet blackStones;
private ChainSet whiteStones;

public Board() {
blackStones = new ChainSet();
whiteStones = new ChainSet();
}

public final Set<Coords> getBlackStones() {
return blackStones.getStones();
}

public final Set<Coords> getWhiteStones() {
return whiteStones.getStones();
}

public void playBlackStone(Coords coords) {
throwIfOccupied(coords);

blackStones.add(coords);
}

public void playWhiteStone(Coords coords) {
throwIfOccupied(coords);

whiteStones.add(coords);
}

public boolean isLegalWhiteMove(Coords coords) {
return !isOccupied(coords);
}

public boolean isLegalBlackMove(Coords coords) {
return !isOccupied(coords);
}


private void throwIfOccupied(Coords coords) {
if (isOccupied(coords))
throw new IllegalArgumentException(coords.toString() + " is already occupied.");
}

private boolean isOccupied(Coords coords) {
return blackStones.contains(coords) || whiteStones.contains(coords);
}
}
95 changes: 95 additions & 0 deletions src/main/java/logic/Chain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright (C) 2017 Vincent Varkevisser
*
* 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 java.util.HashSet;
import java.util.Set;

public class Chain {
private HashSet<Coords> stones;
private HashSet<Coords> liberties;

Chain(Coords coords) {
stones = new HashSet<>();

stones.add(coords);
recalculateLiberties();
}

private void recalculateLiberties() {
liberties = new HashSet<>();

for (Coords stone : stones) {
Set<Coords> neighbours = stone.getNeighbours();

for (Coords c : neighbours)
if (!this.contains(c))
liberties.add(c);
}
}

protected boolean contains(Coords stone) {
return stones.contains(stone);
}

protected HashSet<Coords> getLiberties() {
return liberties;
}

protected boolean isAdjacentTo(Coords coords) {
return liberties.contains(coords);
}

protected boolean isAdjacentTo(Chain other) {
for (Coords lib : liberties)
if (other.contains(lib))
return true;

return false;
}

protected int size() {
return stones.size();
}

private void clear() {
stones.clear();
liberties.clear();
}

protected int countLiberties() {
return liberties.size();
}

protected void mergeChain(Chain other) {
if (!isAdjacentTo(other))
throw new IllegalArgumentException("Chains are not adjacent and cannot be merged.");

stones.addAll(other.stones);

other.clear();

recalculateLiberties();
}

protected Set<Coords> getStones() {
return stones;
}
}
64 changes: 64 additions & 0 deletions src/main/java/logic/ChainSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (C) 2017 Vincent Varkevisser
*
* 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 java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

public class ChainSet {
private HashMap<Chain, Integer> chains;

public ChainSet() {
chains = new HashMap<>();
}

public boolean contains(Coords stone) {
for (Chain chain : chains.keySet())
if (chain.contains(stone))
return true;

return false;
}

public Set<Coords> getStones() {
Set<Coords> stones = new HashSet<>();

for (Chain chain : chains.keySet())
stones.addAll(chain.getStones());

return stones;
}

public void add(Coords stone) {
addChain(new Chain(stone));
}

private void addChain(Chain chain) {
for (Chain existing : chains.keySet())
if (existing.isAdjacentTo(chain)) {
existing.mergeChain(chain);
return;
}

chains.put(chain, chain.countLiberties());

}
}
115 changes: 115 additions & 0 deletions src/main/java/ui/BoardController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright (C) 2017 Vincent Varkevisser
*
* 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 ui;

import javafx.beans.value.ChangeListener;
import javafx.fxml.Initializable;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.util.Pair;
import logic.Board;

import java.net.URL;
import java.util.ResourceBundle;

public class BoardController implements Initializable {
public Canvas boardCanvas;
public Pane pane;
private Board board;

public BoardController() {
board = new Board();
}

public void canvasClicked(MouseEvent mouseEvent) {
drawBoard();
}

private void resizeCanvas() {
boardCanvas.setHeight(pane.getHeight());
boardCanvas.setWidth(pane.getWidth());
}

private void drawBoard() {
drawBackground();
drawBoardTexture(getTopLeftCorner());
drawBoardLines();
drawStones();
}

private void drawBoardTexture(Pair<Double, Double> topLeft) {
GraphicsContext context = boardCanvas.getGraphicsContext2D();
double length = getBoardLength();

context.setFill(Color.web("0xB78600"));
context.fillRect(topLeft.getKey(), topLeft.getValue(), length, length);
}

private void drawBackground() {
GraphicsContext context = boardCanvas.getGraphicsContext2D();

context.setFill(Color.GREEN);
context.fillRect(0, 0, boardCanvas.getWidth(), boardCanvas.getHeight());
}

private void drawBoardLines() {

}

private void drawStones() {

}

private double getBoardLength() {
double canvasWidth = boardCanvas.getWidth();
double canvasHeight = boardCanvas.getHeight();

return Math.min(canvasHeight, canvasWidth);
}

private Pair<Double, Double> getTopLeftCorner() {
double length = getBoardLength();
double canvasWidth = boardCanvas.getWidth();
double canvasHeight = boardCanvas.getHeight();

double x = 0;
double y = 0;

if (canvasWidth > length)
x = (canvasWidth - length) / 2;
else
y = (canvasHeight - length) / 2;

return new Pair<>(x, y);
}

@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
ChangeListener<Number> paneChangeListener = (observableValue, number, t1) -> {
resizeCanvas();
drawBoard();
};
pane.widthProperty().addListener(paneChangeListener);
pane.heightProperty().addListener(paneChangeListener);

drawBoard();
}
}
Loading

0 comments on commit 5afd91e

Please sign in to comment.