From c3c4609d6213dbef69dc301dca9164217a82a098 Mon Sep 17 00:00:00 2001 From: Florent Madelaine Date: Fri, 19 Sep 2025 15:22:44 +0200 Subject: [PATCH] minor changes in Abstract Board+added a getter to AbstractPly --- .../fr/iut_fbleau/GameAPI/AbstractBoard.java | 67 +++++++++++++++---- .../fr/iut_fbleau/GameAPI/AbstractPly.java | 4 ++ 2 files changed, 57 insertions(+), 14 deletions(-) diff --git a/2024-25/javaAPI/fr/iut_fbleau/GameAPI/AbstractBoard.java b/2024-25/javaAPI/fr/iut_fbleau/GameAPI/AbstractBoard.java index 26136e8..8448b7a 100644 --- a/2024-25/javaAPI/fr/iut_fbleau/GameAPI/AbstractBoard.java +++ b/2024-25/javaAPI/fr/iut_fbleau/GameAPI/AbstractBoard.java @@ -1,5 +1,6 @@ package fr.iut_fbleau.GameAPI; +import java.util.NoSuchElementException; import java.util.Iterator; import java.util.Deque; @@ -16,11 +17,17 @@ import java.util.Deque; */ public abstract class AbstractBoard implements IBoard{ + // the player, the turn of which it is to play. private Player currentPlayer ; + // used as a stack. private Deque history; - - private void setNextPlayer(){ + + /** + * set current player to the other player. + * + */ + protected void setNextPlayer(){ if (this.currentPlayer==Player.PLAYER1){ this.currentPlayer= Player.PLAYER2; } @@ -28,19 +35,33 @@ public abstract class AbstractBoard implements IBoard{ this.currentPlayer= Player.PLAYER1; } - // Beware not checking if move is legal. - // To be used in do(AbstractPly c) - private void addPlyToHistory(AbstractPly c){ - this.history.addLast(c); + /** + * add the given ply to history so that it becomes the last one. + * + */ + protected void addPlyToHistory(AbstractPly c){ + this.history.addFirst(c); } - // Beware not checking if history is not empty - // To be used in undo() - private void removePlyFromHistory(){ - this.history.removeLast(); + + /** + * removes the last ply from history. + * + *@throws NoSuchElementException if empty history. + * @return the last ply from history. + */ + protected AbstractPly removePlyFromHistory(){ + return this.history.removeFirst(); } - + /** + * @return the last ply from history or null if none. + */ + protected AbstractPly getLastPlyFromHistory(){ + return this.history.peekFirst(); + } + + /** * @return the current player */ @@ -81,7 +102,8 @@ public abstract class AbstractBoard implements IBoard{ /** * Plays a given move on the plateau. - * Should update history using removePlyFromHistory + * + * A daughter class should call super of this last if the move has been done. * * @throws IllegalArgumentException if the move is always illegal (say from the wrong game) * @throws IllegalStateException if the move is not legal in this position @@ -89,15 +111,32 @@ public abstract class AbstractBoard implements IBoard{ * @param AbstractPly to be played * */ - public abstract void doPly(AbstractPly c); + public void doPly(AbstractPly c){ + // Should update history + this.addPlyToHistory(c); + // swap players + this.setNextPlayer(); + } /** * Resets the plateau to the position before the last move. * + * A daughter class should probably overwrite this class, but use this code as a base, as it will need to know the ply that needs to be undone. + * * @throws IllegalStateException if nothing to undo in history * */ - public abstract void undoPly(); + public void undoPly(){ + AbstractPly p; + try{ + p=this.removePlyFromHistory(); + } + catch(NoSuchElementException e) { + throw new IllegalStateException("No ply in history, nothing can be undone"); + } + // swap players (works for two players, fairly dangerous if more players, great for a test exam). + this.setNextPlayer(); + } /** * creates a safe copy of the board. diff --git a/2024-25/javaAPI/fr/iut_fbleau/GameAPI/AbstractPly.java b/2024-25/javaAPI/fr/iut_fbleau/GameAPI/AbstractPly.java index f831b22..27c5984 100644 --- a/2024-25/javaAPI/fr/iut_fbleau/GameAPI/AbstractPly.java +++ b/2024-25/javaAPI/fr/iut_fbleau/GameAPI/AbstractPly.java @@ -2,4 +2,8 @@ package fr.iut_fbleau.GameAPI; public abstract class AbstractPly { private Player joueur; + + public Player getPlayer(){ + return this.joueur; + } }