minor changes in Abstract Board+added a getter to AbstractPly

This commit is contained in:
2025-09-19 15:22:44 +02:00
parent ad7821e5a3
commit c3c4609d62
2 changed files with 57 additions and 14 deletions

View File

@@ -1,5 +1,6 @@
package fr.iut_fbleau.GameAPI; package fr.iut_fbleau.GameAPI;
import java.util.NoSuchElementException;
import java.util.Iterator; import java.util.Iterator;
import java.util.Deque; import java.util.Deque;
@@ -16,11 +17,17 @@ import java.util.Deque;
*/ */
public abstract class AbstractBoard implements IBoard{ public abstract class AbstractBoard implements IBoard{
// the player, the turn of which it is to play.
private Player currentPlayer ; private Player currentPlayer ;
// used as a stack.
private Deque<AbstractPly> history; private Deque<AbstractPly> history;
private void setNextPlayer(){ /**
* set current player to the other player.
*
*/
protected void setNextPlayer(){
if (this.currentPlayer==Player.PLAYER1){ if (this.currentPlayer==Player.PLAYER1){
this.currentPlayer= Player.PLAYER2; this.currentPlayer= Player.PLAYER2;
} }
@@ -28,16 +35,30 @@ public abstract class AbstractBoard implements IBoard{
this.currentPlayer= Player.PLAYER1; this.currentPlayer= Player.PLAYER1;
} }
// Beware not checking if move is legal. /**
// To be used in do(AbstractPly c) * add the given ply to history so that it becomes the last one.
private void addPlyToHistory(AbstractPly c){ *
this.history.addLast(c); */
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(){ * removes the last ply from history.
this.history.removeLast(); *
*@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();
} }
@@ -81,7 +102,8 @@ public abstract class AbstractBoard implements IBoard{
/** /**
* Plays a given move on the plateau. * 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 IllegalArgumentException if the move is always illegal (say from the wrong game)
* @throws IllegalStateException if the move is not legal in this position * @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 * @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. * 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 * @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. * creates a safe copy of the board.

View File

@@ -2,4 +2,8 @@ package fr.iut_fbleau.GameAPI;
public abstract class AbstractPly { public abstract class AbstractPly {
private Player joueur; private Player joueur;
public Player getPlayer(){
return this.joueur;
}
} }