This repository has been archived by the owner on Nov 29, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from GoSuji/GameTree
Game tree
- Loading branch information
Showing
10 changed files
with
410 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,6 @@ build/ | |
out/ | ||
|
||
#Local Settings | ||
.idea/workspace.xml | ||
.idea/workspace.xml* | ||
.idea/dictionaries | ||
.gradle/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.