diff --git a/.classpath b/.classpath new file mode 100644 index 0000000000000000000000000000000000000000..f2588a185d4bb0737001732ddd8ff60c30de1a5c --- /dev/null +++ b/.classpath @@ -0,0 +1,9 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> + <classpathentry kind="src" path="example-implementation"/> + <classpathentry kind="src" path="src"/> + <classpathentry kind="lib" path="FlowerWarsPP-Display.jar"/> + <classpathentry kind="lib" path="FlowerWarsPP-Tester.jar"/> + <classpathentry kind="output" path="bin"/> +</classpath> diff --git a/.gitignore b/.gitignore index df2239187cb9d2a461816f2d300d0b6ff8b63acd..3b7b3283917246196ce03673919769f1b9358c2c 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ doc/ *.class *.jar +/bin/ diff --git a/.project b/.project new file mode 100644 index 0000000000000000000000000000000000000000..59447ea5d1f0691344fa67d5b5c7f020ab767855 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>floragnomewars</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.jdt.core.javanature</nature> + </natures> +</projectDescription> diff --git a/example-implementation/flowerwarspp/MyBoard.java b/example-implementation/flowerwarspp/MyBoard.java index 8c3fd99176312334f34ea75df29dbe2c41859a0a..6c45c0ff3a0d55aa0cd88373e93e8f052758faf4 100644 --- a/example-implementation/flowerwarspp/MyBoard.java +++ b/example-implementation/flowerwarspp/MyBoard.java @@ -59,4 +59,8 @@ public class MyBoard implements Board { } }; } + + public boolean legalMove(Move nextMove, PlayerColor color) { + return false; + } } diff --git a/src/flowerwarspp/Main.java b/src/flowerwarspp/Main.java index 1930c66c1357dd9f06a5a0b4cd5fe75dee6bcaeb..c63a267fd2e6e083abb40ee0b81be345e405ec38 100644 --- a/src/flowerwarspp/Main.java +++ b/src/flowerwarspp/Main.java @@ -1,12 +1,15 @@ package flowerwarspp; import flowerwarspp.boarddisplay.BoardDisplay; +import flowerwarspp.boardmechanics.FGWBoard; +import flowerwarspp.boardmechanics.FGWBoardViewer; +import flowerwarspp.player.HumanPlayer; import flowerwarspp.preset.*; public class Main { public static void main(String[] args) { - Board board = new MyBoard(6); - Viewer viewer = board.viewer(); + FGWBoard board = new FGWBoard(6); + FGWBoardViewer viewer = board.viewer(); BoardDisplay boardDisplay = new BoardDisplay(); boardDisplay.setViewer(viewer); @@ -14,5 +17,7 @@ public class Main { board.make(move); boardDisplay.update(move); boardDisplay.showStatus(viewer.getStatus()); - } + + HumanPlayer human = new HumanPlayer(viewer); + } } diff --git a/src/flowerwarspp/boardmechanics/ColoredDitch.java b/src/flowerwarspp/boardmechanics/ColoredDitch.java new file mode 100644 index 0000000000000000000000000000000000000000..99063067fc966bc491ccba63b1ecf968185201da --- /dev/null +++ b/src/flowerwarspp/boardmechanics/ColoredDitch.java @@ -0,0 +1,61 @@ +package flowerwarspp.boardmechanics; + +import java.util.*; + +import flowerwarspp.preset.*; + + +/** +* Extends the ditch class. Adds extra functionality: <br> +* -Status Variable (if Flower is Red, Blue, uncolored, fallow) <br> <br> +* erweitern!!! +* @author Charlotte Ackva, Felix Spuehler, Martin Heide +**/ +public class ColoredDitch extends Ditch { + /** Holds neighbored flowers */ + private Vector<ColoredFlower> flowerNeighbors; + /** Holds neighbored ditches */ + private Vector<ColoredDitch> ditchNeighbors; + /** Holds an information about the status of the ColoredDitch */ + private DitchType ditchType; + + public ColoredDitch(final Position first, final Position second) { + super(first, second); + ditchType = DitchType.UNCOLORED; + } + + /** returns the flowerNeighbors of the Ditch + * @return vector of Flowers + **/ + public Collection<ColoredFlower> getFlowerNeighbors() { + return flowerNeighbors; + } + + /** returns the edgeNeighbors of the Flower + * @return vector of Ditches + **/ + public Collection<ColoredDitch> getDitchNeighbors() { + return ditchNeighbors; + } + + /** sets the flowerNeighbors of the Ditch + * @param vectorFlowers of Flowers + **/ + public void setNodeNeighbors(Vector<ColoredFlower> vectorFlowers) { + flowerNeighbors = vectorFlowers; + } + + /** sets the ditchNeighbors of the Ditch + * @param vectorDitches of Ditches + **/ + public void setEdgeNeighbors(Vector<ColoredDitch> vectorDitches) { + ditchNeighbors = vectorDitches; + } + + /** sets the type of the Ditch + * @param type DitchType - the new type of the Ditch + **/ + public void setFlowerType(DitchType type) { + ditchType = type; + } +} \ No newline at end of file diff --git a/src/flowerwarspp/boardmechanics/ColoredFlower.java b/src/flowerwarspp/boardmechanics/ColoredFlower.java new file mode 100644 index 0000000000000000000000000000000000000000..20981c8ab117f37e24f43f8f8202b88cc6b24914 --- /dev/null +++ b/src/flowerwarspp/boardmechanics/ColoredFlower.java @@ -0,0 +1,96 @@ +package flowerwarspp.boardmechanics; + +import flowerwarspp.preset.*; +import java.util.*; + +/** +* Extends the flower class. Adds extra functionality: <br> +* -Status Variable (if Flower is Red, Blue, uncolored, fallow) +* -vector of EdgeNeighbors +* -vector of NodeNeighbors +* -information about the parentFlowerSet +* +* @author Charlotte Ackva, Felix Spuehler, Martin Heide +**/ + +public class ColoredFlower extends Flower { + /** Holds the edge neighbored flowers */ + private Vector<ColoredFlower> edgeNeighbors; + /** Holds the node neighbored flowers */ + private Vector<ColoredFlower> nodeNeighbors; + /** Holds neighbored ditches */ + private Vector<ColoredDitch> ditchNeighbors; + /** Holds an information about the status of the ColoredFlower */ + private FlowerType flowerType; + /** Holds the information about the parent flower set where the flower is contained in */ + private FlowerSet parentFlowerSet; + + /** Constructs a ColoredFlower **/ + public ColoredFlower (final Position first, final Position second, final Position third) { + super(first,second,third); + flowerType = FlowerType.UNCOLORED; + } + + /** returns the nodeNeighbors of the Flower + * @return vector of Nodeneighbored Flowers + **/ + public Collection<ColoredFlower> getNodeNeighbors() { + return nodeNeighbors; + } + + /** returns the edgeNeighbors of the Flower + * @return vector of Edgeneighbored Flowers + **/ + public Collection<ColoredFlower> getEdgeNeighbors() { + return edgeNeighbors; + } + + /** returns the ditchNeighbors of the Flower + * @return vector of Ditches + **/ + public Collection<ColoredDitch> getDitchNeighbors() { + return ditchNeighbors; + } + + /** sets the nodeNeighbors of the Flower + * @param vectorNodes of Flowers + **/ + public void setNodeNeighbors(Vector<ColoredFlower> vectorNodes) { + nodeNeighbors = vectorNodes; + } + + /** sets the edgeNeighbors of the Flower + * @param vectorEdges of Flowers + **/ + public void setEdgeNeighbors(Vector<ColoredFlower> vectorEdges) { + edgeNeighbors = vectorEdges; + } + + /** sets the ditchNeighbors of the Flower + * @param vectorDitches of Ditches + **/ + public void setDitchNeighbors(Vector<ColoredDitch> vectorDitches) { + ditchNeighbors = vectorDitches; + } + + /** sets the type of the Flower + * @param type FlowerType - the new type of the Flower + **/ + public void setFlowerType(FlowerType type) { + flowerType = type; + } + + /** sets the parent Flower Set of the flower + * @param parent FlowerSet + **/ + public void setParentFlowerSet(FlowerSet parent) { + parentFlowerSet = parent; + } + + /** returns the parent Flower Set of the flower + * @return parent FlowerSet + **/ + public FlowerSet getParentFlowerSet() { + return parentFlowerSet; + } +} diff --git a/src/flowerwarspp/boardmechanics/Compound.java b/src/flowerwarspp/boardmechanics/Compound.java new file mode 100644 index 0000000000000000000000000000000000000000..8c8bd400bd68ae35c87c173bd2d4f76d433a3024 --- /dev/null +++ b/src/flowerwarspp/boardmechanics/Compound.java @@ -0,0 +1,30 @@ +package flowerwarspp.boardmechanics; + +import java.util.*; + +/** +* Contains FlowerSets which are connected over Ditches. Ditches are not included. +* @author Charlotte Ackva, Felix Spuehler, Martin Heide +**/ +public class Compound { + /** Contains the FlowerSets where all Flowers are safed in.*/ + private Collection<FlowerSet> flowerSets; + + /** + * Constructs a Component based on an initial ColoredFlower. + * @param firstFlower ColoredFlower the inital ColoredFlower + **/ + public Compound(ColoredFlower firstFlower) { + flowerSets = new Vector<FlowerSet>(1); + flowerSets.add(new FlowerSet(firstFlower)); + } + + /** + * Adds a Flower + **/ + public void addFlower(ColoredFlower furtherFlower) { + FlowerSet dummy = new FlowerSet(furtherFlower); + dummy.setParentCompound(this); + flowerSets.add(dummy); + } +} diff --git a/src/flowerwarspp/boardmechanics/DitchType.java b/src/flowerwarspp/boardmechanics/DitchType.java new file mode 100644 index 0000000000000000000000000000000000000000..9883c0da89426aa748b7c17d54b029f624ca6777 --- /dev/null +++ b/src/flowerwarspp/boardmechanics/DitchType.java @@ -0,0 +1,15 @@ +package flowerwarspp.boardmechanics; + +/** +* Defines possible Status Types for ColoredDitch <br> +* +* implements Serializable missing?! +* @author Charlotte Ackva, Felix Spuehler, Martin Heide +**/ + +enum DitchType { + RED, + BLUE, + FALLOW, + UNCOLORED +} diff --git a/src/flowerwarspp/boardmechanics/FGWBoard.java b/src/flowerwarspp/boardmechanics/FGWBoard.java new file mode 100644 index 0000000000000000000000000000000000000000..5e551602b461f89ac202e4edebf2df73328b17f1 --- /dev/null +++ b/src/flowerwarspp/boardmechanics/FGWBoard.java @@ -0,0 +1,265 @@ +package flowerwarspp.boardmechanics; + +import java.awt.Color; +import java.util.*; +import flowerwarspp.preset.*; + + +/** + * Board for the board and game mechanics + * @author Charlotte Ackva, Felix Spuehler, Martin Heide +**/ +public class FGWBoard implements Board { + public static final int MAX_VALUE = 31; + private final int boardSize; + private Status status; + private PlayerColor currentPlayerColor; + private int pointsRed; + private int pointsBlue; + + private HashMap<Integer, Position> allPositions; + + private HashMap<Integer, ColoredFlower> uncoloredFlowers; + private Collection<Compound> redFlowers; + private Collection<Compound> blueFlowers; + private Collection<ColoredFlower> fallowFlowers; + + private HashMap<Integer, ColoredDitch> uncoloredDitches; + private Collection<ColoredDitch> redDitches; + private Collection<ColoredDitch> blueDitches; + private Collection<ColoredDitch> fallowDitches; + + + + public FGWBoard(int boardSize) { + this.boardSize = boardSize; + status = Status.Ok; + currentPlayerColor = PlayerColor.Red; + pointsRed = 0; + pointsBlue = 0; + + initializePositions(); + initizializeFlowers(); + initalizeDitches(); + } + + public FGWBoardViewer viewer() { + return new FGWBoardViewer(this); + } + + public int getBoardSize() { + return boardSize; + } + + public Status getStatus() { + return status; + } + + public PlayerColor getCurrentPlayerColor() { + return currentPlayerColor; + } + + public int getPoints( PlayerColor color ) { + if( color == PlayerColor.Red) { + return pointsRed; + } else { + return pointsBlue; + } + } + + public Collection<Flower> getFlowers(PlayerColor color) { + Collection<Compound> coloredFlowers; + if( color == PlayerColor.Red) { + coloredFlowers = redFlowers; + } else { + coloredFlowers = blueFlowers; + } + + // hier wirklich eine Collection von Flowers zuerueckgeben nicht von Compound!!! + Vector<Flower> flowers = new Vector<Flower>(); + return flowers; + } + + public Collection<Ditch> getDitches( PlayerColor color ) { + Collection<ColoredDitch> coloredDitches; + if( color == PlayerColor.Red ) { + coloredDitches = redDitches; + } else { + coloredDitches = blueDitches; + } + // ColoredDitches in Ditches umwandeln + Vector<Ditch> ditches = new Vector<Ditch>(); + return ditches; + } + + public void make(final Move move) throws IllegalStateException { + while(true) + System.out.println("I like to move it, move it, move"); + } + + /** + * initialize all Positions into the Hashmap {@link FGWBoard#allPositions} + */ + public void initializePositions() { + allPositions = new HashMap<Integer, Position>(); + for( int c = 1; c <= boardSize + 1; c++) { + for( int r = 1; c + r <= boardSize + 2; r++) { + Position dummyPosition = new Position(c, r); + allPositions.put(dummyPosition.hashCode(), dummyPosition); + } + } + + /* Ueberpreufung, ob alle Positions (richtig) erstellt wurden + System.out.println(allPositions.size()); + Set set = allPositions.entrySet(); + Iterator iterator = set.iterator(); + while(iterator.hasNext()) { + Map.Entry mentry = (Map.Entry)iterator.next(); + System.out.print("key is: "+ mentry.getKey() + " & Value is: "); + System.out.println(mentry.getValue()); + } + */ + } + + /** + * Calculates Hashcode of Position + * @param column column number + * @param row row number + * @return Hashcode + */ + public int calculatePositionHashCode(int column, int row) { + if( column < 1 || column > MAX_VALUE || row < 1 || row > MAX_VALUE ) { + throw new IllegalArgumentException(); + } + return column * MAX_VALUE + row; + } + + /** + * initialize all ColoredFlowers into the Hashmap {@link FGWBoard#uncoloredFlowers} + */ + public void initizializeFlowers() { + if( allPositions == null ) { + throw new UnsupportedOperationException(); + } + uncoloredFlowers = new HashMap<Integer, ColoredFlower>(); + + Position first; + Position second; + Position third; + ColoredFlower dummyColoredFlower; + + for( int c = 1; c <= boardSize; c++ ) { + for( int r = 1; c + r <= boardSize + 1; r++) { + first = allPositions.get( calculatePositionHashCode(c,r) ); + second = allPositions.get( calculatePositionHashCode(c+1,r) ); + third = allPositions.get( calculatePositionHashCode(c,r+1) ); + dummyColoredFlower = new ColoredFlower(first, second, third); + uncoloredFlowers.put( dummyColoredFlower.hashCode(), dummyColoredFlower); + + if( c > 1) { + first = allPositions.get( calculatePositionHashCode(c,r) ); + second = allPositions.get( calculatePositionHashCode(c,r+1) ); + third = allPositions.get( calculatePositionHashCode(c-1,r+1) ); + dummyColoredFlower = new ColoredFlower(first, second, third); + uncoloredFlowers.put( dummyColoredFlower.hashCode(), dummyColoredFlower); + } + } + } + + /*Ueberpreufung, ob alle Positions (richtig) erstellt wurden + System.out.println(uncoloredFlowers.size()); + Set set = uncoloredFlowers.entrySet(); + Iterator iterator = set.iterator(); + while(iterator.hasNext()) { + Map.Entry mentry = (Map.Entry)iterator.next(); + System.out.print("key is: "+ mentry.getKey() + " & Value is: "); + System.out.println(mentry.getValue()); + } + */ + } + + /** + * Calculates Hashcode of a Flower + * @param first one Position + * @param second another Position + * @param third last Position + * @return Hashcode + */ + public int calculateFlowerHashCode( Position first, Position second, Position third) { + // Keine Pruefung, ob Blume ueberhaupt im Feld... + Flower dummyFlower = new Flower(first, second, third); + return dummyFlower.hashCode(); + + } + + /** + * initialize all ColoredFlowers into the Hashmap {@link FGWBoard#uncoloredDitches} + */ + public void initalizeDitches() { + if( allPositions == null ) { + throw new UnsupportedOperationException(); + } + + uncoloredDitches = new HashMap<Integer, ColoredDitch>(); + + + Position first; + Position second; + ColoredDitch dummyColoredDitch; + for( int c = 1; c <= boardSize+1; c++ ) { + for( int r = 1; c + r <= boardSize + 2; r++) { + if( c + r < boardSize + 2) { + first = allPositions.get( calculatePositionHashCode(c,r) ); + second = allPositions.get( calculatePositionHashCode(c+1,r) ); + dummyColoredDitch = new ColoredDitch(first, second); + uncoloredDitches.put(dummyColoredDitch.hashCode(), dummyColoredDitch); + + first = allPositions.get( calculatePositionHashCode(c,r) ); + second = allPositions.get( calculatePositionHashCode(c,r+1) ); + dummyColoredDitch = new ColoredDitch(first, second); + uncoloredDitches.put(dummyColoredDitch.hashCode(), dummyColoredDitch); + } + if( r > 1) { + first = allPositions.get( calculatePositionHashCode(c,r) ); + second = allPositions.get( calculatePositionHashCode(c+1,r-1) ); + dummyColoredDitch = new ColoredDitch(first, second); + uncoloredDitches.put(dummyColoredDitch.hashCode(), dummyColoredDitch); + } + } + } + + /*Ueberpreufung, ob alle Positions (richtig) erstellt wurden + System.out.println(uncoloredDitches.size()); + Set set = uncoloredDitches.entrySet(); + Iterator iterator = set.iterator(); + while(iterator.hasNext()) { + Map.Entry mentry = (Map.Entry)iterator.next(); + System.out.print("key is: "+ mentry.getKey() + " & Value is: "); + System.out.println(mentry.getValue()); + } + */ + } + + /** + * Calculates Hashcode of a Flower + * @param first one Position + * @param second other Position + * @return Hashcode + */ + public int calculateDitchHashCode(Position first, Position second) { + // Keine Pruefung, ob Graben ueberhaupt im Feld... + Ditch dummyDitch = new Ditch(first, second); + return dummyDitch.hashCode(); + } + + /** + * Main Methode, um einzelne Funktionen beim Implementieren zu testen <br> + * spaeter loeschen!!! <br> + * Aufruf mit {@code java -cp build flowerwarspp.boardmechanics.FGWBoard} + */ + public static void main(String[] args) { + FGWBoard MainBoard = new FGWBoard(6); + //int d = MainBoard.calculateDitchHashCode(new Position(3,3), new Position(3,4)); + //System.out.println(d); + } +} diff --git a/src/flowerwarspp/boardmechanics/FGWBoardViewer.java b/src/flowerwarspp/boardmechanics/FGWBoardViewer.java new file mode 100644 index 0000000000000000000000000000000000000000..b3f041efe802ed19eacdbc1a7f7e3c86d7602e67 --- /dev/null +++ b/src/flowerwarspp/boardmechanics/FGWBoardViewer.java @@ -0,0 +1,53 @@ +package flowerwarspp.boardmechanics; + +import java.util.*; +import flowerwarspp.preset.*; + +/** +* Viewer for the FGWBoard +* @author Charlotte Ackva, Felix Spuehler, Martin Heide +**/ +public class FGWBoardViewer implements Viewer { + private FGWBoard board; + + public FGWBoardViewer(FGWBoard board){ + super(); + this.board = board; + } + + public PlayerColor getTurn() { + return board.getCurrentPlayerColor(); + } + + public int getSize() { + return board.getBoardSize(); + } + + public Status getStatus() { + return board.getStatus(); + } + + public Collection<Flower> getFlowers(final PlayerColor color) { + return board.getFlowers(color); + } + + public Collection<Ditch> getDitches(final PlayerColor color) { + return board.getDitches(color); + } + + public Collection<Move> getPossibleMoves() { + throw new UnsupportedOperationException(); + } + + public int getPoints(final PlayerColor color) { + return board.getPoints(color); + } + + public Move deliverNextMove() { + return null; + } + + + + +} diff --git a/src/flowerwarspp/boardmechanics/FlowerSet.java b/src/flowerwarspp/boardmechanics/FlowerSet.java new file mode 100644 index 0000000000000000000000000000000000000000..d63fb51e4e87ef47c316739a8db8459f441c78c8 --- /dev/null +++ b/src/flowerwarspp/boardmechanics/FlowerSet.java @@ -0,0 +1,57 @@ +package flowerwarspp.boardmechanics; + +import java.util.*; +/** +* Contains edge connected ColoredFlowers +* @author Charlotte Ackva, Felix Spuehler, Martin Heide +**/ +public class FlowerSet { + // auesser ColoredFlowers Referenzen + + /** Contains the flowers in the FlowerSet */ + private Collection<ColoredFlower> flowers; + + /** Holds the information about the parent Compound where the set is contained in */ + private Compound parentCompound; + + /** + * Constructs the set of Flowers + * @param firstFlower of type ColoredFlower which is the first entry in the set. + **/ + public FlowerSet(ColoredFlower firstFlower ) { + flowers = new Vector<ColoredFlower>(1); + add(firstFlower); + } + + /** + * Adds a further Flower to the set of Flowers + * @param furtherFlower of type ColoredFlower to be added + **/ + public void add(ColoredFlower furtherFlower) { + if(flowers.size() == 4) { + throw new IllegalArgumentException(); + } + furtherFlower.setParentFlowerSet(this); + flowers.add(furtherFlower); + // referenzen nach aussen anpassen + } + + /** + * Sets the parent Compound where the set is contained in. + * @param parent Compound + **/ + public void setParentCompound(Compound parent) { + parentCompound = parent; + } + + /** + * Return the parent Compound where the set is contained in. + * @return parent Compound + **/ + public Compound getParentCompound() { + return parentCompound; + } + + // merge + // split +} diff --git a/src/flowerwarspp/boardmechanics/FlowerType.java b/src/flowerwarspp/boardmechanics/FlowerType.java new file mode 100644 index 0000000000000000000000000000000000000000..466a0fadfea42596764a16fbcb0e1881c0ef6661 --- /dev/null +++ b/src/flowerwarspp/boardmechanics/FlowerType.java @@ -0,0 +1,15 @@ +package flowerwarspp.boardmechanics; + +/** +* Defines possible Status Types for ColoredFlower +* +* implements Serializable missing?! +* @author Charlotte Ackva, Felix Spuehler, Martin Heide +**/ + +enum FlowerType { + RED, + BLUE, + FALLOW, + UNCOLORED +} diff --git a/src/flowerwarspp/player/HumanPlayer.java b/src/flowerwarspp/player/HumanPlayer.java new file mode 100644 index 0000000000000000000000000000000000000000..085da0d5115a04d4bc95d09f2a81d94ec2e01b81 --- /dev/null +++ b/src/flowerwarspp/player/HumanPlayer.java @@ -0,0 +1,167 @@ +package flowerwarspp.player; + +import flowerwarspp.preset.Move; +import flowerwarspp.preset.PlayerColor; +import flowerwarspp.preset.PlayerType; +import flowerwarspp.preset.Status; +import flowerwarspp.boardmechanics.FGWBoard; +import flowerwarspp.boardmechanics.FGWBoardViewer; +import java.rmi.RemoteException; + +/** + * Class for an interactive human player + * @author Felix Strnad + * @version 1 + * + */ +public class HumanPlayer implements flowerwarspp.preset.Player { + + /** + * type of the player + */ + private final PlayerType type; + + /** + * Status in which this player is + * @see #PlayerStatus + */ + private PlayerStatus status; + + /** + * The color of this player + */ + private PlayerColor color; + + /** + * Each player has a copy of the current board that controls the game. + */ + private FGWBoard myBoard; + + /** + * Viewer delivers the move the player can take. + */ + private FGWBoardViewer viewer=null; + + /** + * Needed next move, given by the Viewer. + * It is an auxiliary variable, thus no getter and setter needed. + */ + private Move nextMove; + + /** + * Defines specialized Player constructor + */ + public HumanPlayer(FGWBoardViewer viewer) { + this.type = PlayerType.HUMAN; + this.status = PlayerStatus.NOTEXISTING; + this.viewer = viewer; + + } + + //*********************** Get & Set Functions ************************** + /** + * Get the type of this player + * @return PlayerType type + */ + public PlayerType getType(){ + return this.type; + } + + + + //*********************** Function implementation ****************** + + + /** + * Inititializes the Player and tells him/her the size of the board. + * This function is called first by the game. + * @param boardSize int + * @param Color PlayerColor + */ + @Override + public void init(int boardSize, PlayerColor color) { + this.myBoard = new FGWBoard(boardSize); + this.color = color; + if(viewer==null) { + this.viewer = new FGWBoardViewer(myBoard) ; + } + this.status=PlayerStatus.INITIALIZED; + } + + /** + * Requests a new draw of the player. + * @return + * @throws Exception + * @throws RemoteException + */ + @Override + public Move request() throws Exception{ + if(this.color == PlayerColor.Red){ //check the right order, red starts with request + if(this.status == PlayerStatus.NOTEXISTING){ + throw new Exception("Call init() before using request()"); + } + else if(this.status != PlayerStatus.UPDATE && this.status != PlayerStatus.INITIALIZED){ + throw new Exception("Update() cannot be called before request()"); + } + } + else if(this.status != PlayerStatus.UPDATE){ //Blue starts with update BEFORE request + throw new Exception("call update() before request()"); + } + while(this.status != PlayerStatus.REQUEST){ + this.nextMove = viewer.deliverNextMove(); + + //if(this.nextMove == null || myBoard.legalMove(this.nextMove, this.color)){ + if(this.nextMove == null ){ + this.status = PlayerStatus.REQUEST; + } + } + return this.nextMove; + } + + + + /** + * Confirms that own Status status is in agreement with the status on the MainBoard. + * @param status Status + * @throws Exception + * @throws RemoteException + */ + @Override + public void confirm(Status boardStatus) throws Exception, RemoteException { + if(this.status != PlayerStatus.REQUEST){ //check the correct order. Red player starts with request() + throw new Exception("call request() before confirm()"); + } + this.myBoard.make(this.nextMove); + if(boardStatus != this.myBoard.getStatus()){ + throw new Exception("Confusion! Board of Player not in the same state as Control Board!"); + } + this.status = PlayerStatus.CONFIRM; + } + + /** + * + * @param opponentMove Move + * @param status Status + * @throws Exception + * @throws RemoteException + */ + @Override + public void update(Move opponentMove, Status boardStatus) throws Exception, RemoteException { + if(this.color == PlayerColor.Blue){ //check the correct order, blue player starts with update() + if(this.status == PlayerStatus.NOTEXISTING){ + throw new Exception("Call init() before using request()"); + } + else if(this.status != PlayerStatus.CONFIRM && this.status != PlayerStatus.INITIALIZED){ + throw new Exception("call confirm() before update()"); + } + } + else if(this.status != PlayerStatus.CONFIRM){ + throw new Exception("call confirm() before update()"); + } + this.myBoard.make(opponentMove); + if(boardStatus != this.myBoard.getStatus()){ + throw new Exception("confusion at boardstatus"); + } + this.status = PlayerStatus.UPDATE; + } +} diff --git a/src/flowerwarspp/player/PlayerStatus.java b/src/flowerwarspp/player/PlayerStatus.java new file mode 100644 index 0000000000000000000000000000000000000000..8130d06e44c4dfc7f4c0cb14b057ac5a542d1c58 --- /dev/null +++ b/src/flowerwarspp/player/PlayerStatus.java @@ -0,0 +1,12 @@ +package flowerwarspp.player; +/** +* Enum for the different states that the player objects can have +* @version 1 +*/ +public enum PlayerStatus { + NOTEXISTING, + INITIALIZED, + REQUEST, + CONFIRM, + UPDATE +} \ No newline at end of file diff --git a/src/flowerwarspp/preset/Player.java b/src/flowerwarspp/preset/Player.java index 15ada75b9592f7d525a792d89efdea7f93cf8569..94b1db1d3bb82dce2c9ccff3b6e2524353366736 100644 --- a/src/flowerwarspp/preset/Player.java +++ b/src/flowerwarspp/preset/Player.java @@ -1,7 +1,7 @@ package flowerwarspp.preset; import java.rmi.*; - +import flowerwarspp.MyBoard; /** * <h1>Spieler-Schnittstelle</h1> * <p> @@ -68,7 +68,7 @@ public interface Player extends Remote { * @throws RemoteException * falls bei der Netzwerkkommunikation etwas schief gelaufen ist */ - void confirm(Status status) throws Exception, RemoteException; + void confirm(Status boardStatus) throws Exception, RemoteException; /** * Diese Funktion uebermittelt dem Spieler den Zug und Status des Zuges des Gegenspielers. Im Parameter {@code @@ -88,7 +88,7 @@ public interface Player extends Remote { * @throws RemoteException * falls bei der Netzwerkkommunikation etwas schief gelaufen ist */ - void update(Move opponentMove, Status status) throws Exception, RemoteException; + void update(Move opponentMove, Status boardStatus) throws Exception, RemoteException; /** * Initialisiere den Spieler und teile ihm die Groesse des Spielbretts und seine Spielerfarbe mit. Diese Funktion @@ -107,3 +107,4 @@ public interface Player extends Remote { */ void init(int boardSize, PlayerColor color) throws Exception, RemoteException; } + diff --git a/src/flowerwarspp/preset/Viewer.java b/src/flowerwarspp/preset/Viewer.java index fbce475e773b25730df7ad1c36bec906b26c5328..bccd818e41a700c1f222984534d3877075ea8e75 100644 --- a/src/flowerwarspp/preset/Viewer.java +++ b/src/flowerwarspp/preset/Viewer.java @@ -75,6 +75,12 @@ public interface Viewer { // ******************************************************************** // Hier koennen weitere Funktionen ergaenzt werden... // ******************************************************************** - + + /** + * Delivers the next move of an player + * @return Move nextMove + */ + Move deliverNextMove(); + }