From 403764edc61e0a2dcb0613fdc994cdbd420a4c3b Mon Sep 17 00:00:00 2001 From: James Boutaric Date: Thu, 9 Oct 2025 11:46:45 +0200 Subject: [PATCH] changement API --- TP3/fr/iut_fbleau/GameAPI/AbstractGame.class | Bin 0 -> 2148 bytes TP3/fr/iut_fbleau/GameAPI/AbstractGame.java | 61 ++++++++++++++++++ .../GameAPI/AbstractGamePlayer.java | 26 -------- TP3/fr/iut_fbleau/GameAPI/AbstractPly.class | Bin 268 -> 417 bytes TP3/fr/iut_fbleau/GameAPI/AbstractPly.java | 9 +++ TP3/fr/iut_fbleau/GameAPI/IBoard.class | Bin 525 -> 636 bytes TP3/fr/iut_fbleau/GameAPI/IBoard.java | 26 +++++--- TP3/fr/iut_fbleau/Nim/NimBoard.class | Bin 3776 -> 3776 bytes TP3/fr/iut_fbleau/Nim/NimBoard.java | 3 +- TP3/fr/iut_fbleau/Nim/NimPly.class | Bin 1203 -> 1201 bytes TP3/fr/iut_fbleau/Nim/NimPly.java | 29 +++++---- 11 files changed, 106 insertions(+), 48 deletions(-) create mode 100644 TP3/fr/iut_fbleau/GameAPI/AbstractGame.class create mode 100644 TP3/fr/iut_fbleau/GameAPI/AbstractGame.java delete mode 100644 TP3/fr/iut_fbleau/GameAPI/AbstractGamePlayer.java diff --git a/TP3/fr/iut_fbleau/GameAPI/AbstractGame.class b/TP3/fr/iut_fbleau/GameAPI/AbstractGame.class new file mode 100644 index 0000000000000000000000000000000000000000..9c63903f185feb1d7de32a42c7c025003f6eaaa5 GIT binary patch literal 2148 zcmX^0Z`VEs1_nolL@ov<24;2!79Ivx1~x_pfvm)`ME#t^ymWp4q^#8B5=I6#o6Nk- z5<5l)W)00SP6iGJPId+^9tLg(9!3VGv?Be?(vtYJq@2{mQhoQt+*HQ^PkqOv;*z4o z^1x5xz3e;3!T^OLfjKsKn=LXb5vL zC^Bg9FlaJpF*5L^XO^W#=9d=v=9i^1GB|7C@Gw*>uJC|4E+D58949(F47v<@j126V z#XhO&i881OI{G8i#3u%_e(kVFs--N3}a+qan4UkWn|#@$;?aj zE6q(xEec6Y%1LEps3X}tDtZUxGUlSvJVplI;F84TY~REJu(Q}1{23V(38rgC2Cm@z z(xT*4w@i>%1d)SG50u0h83dg2^GiU5W`S>NNk)E3F$Y5kBZC?wKouAjGK&=w6_RrD z^HVAnk~2~hOY{^x6+n4cp|n7cpN%1miy@pLf}J4}ln|pB8B{>VLdx;Xyt4f4RDDPS zf}~Bi#N?9vqDn@F8OTWtsu1D;kHox`oYZ0!pZxsn(gJHpbb<2~#2{o56pbO31*yoA zQ037`CSb`+P$QfZb8><+OH!>F8F)PN@=}YOa}tY-Q;XRdVi_4!G5mzhsq75#j0|%4 zl`=B0LHxy{$iTtC#K6G7#J~iq(ZDQz1_1^}1~)Lsgk%5)LgMo>`lYxQ37*t&{ zFfj0IZD(NA(%#CzuBEe;fp;SV0|O(27Xt%>5CaPX1A__!3xg^HCxaRTAA>s7C~pQv z1|J4j21c+!I~kb4Dl@dUFtG1r5M*SS#Sp5sg@G5s7iVPH#vpxwLD2`oS7v1J)z;a@ zpza6a>+WRGW@4DhAOPj)Gchb@5ZTRO9I3s7!E_gcH3P$T2K)WX|1Sk8*va6;$glzI z9gqvv7&sXi7z`LdrK|#j5rY mapPlayers; + + // constructeur à appeler dans le constructeur d'un fils concret avec super. + public AbstractGame(IBoard b, EnumMap m){ + this.currentBoard=b; + this.mapPlayers=m; + } + + /** + * boucle de contrôle du jeu. + * + * @throws IllegalStateException if a player proposes an IllegalMove at some point + * @return the Result of the game from the perspective of the first player. + */ + public Result run(){ + while(! currentBoard.isGameOver()) { + AbstractGamePlayer player = mapPlayers.get(currentBoard.getCurrentPlayer()); + IBoard board = currentBoard.safeCopy(); + AbstractPly ply = player.giveYourMove(board); + + if (currentBoard.isLegal(ply)) { + currentBoard.doPly(ply); + } + else throw new IllegalStateException("Player "+ player + " is a bloody cheat. I give up."); + } + + return currentBoard.getResult(); + } +} diff --git a/TP3/fr/iut_fbleau/GameAPI/AbstractGamePlayer.java b/TP3/fr/iut_fbleau/GameAPI/AbstractGamePlayer.java deleted file mode 100644 index be251ee..0000000 --- a/TP3/fr/iut_fbleau/GameAPI/AbstractGamePlayer.java +++ /dev/null @@ -1,26 +0,0 @@ -package fr.iut_fbleau.GameAPI; - -import java.util.Iterator; - -/** - * The abstract class for a game Player. - */ -public abstract class AbstractGamePlayer { - - // not a band, but which type of player I am in the game (PLAYER1 or PLAYER2). - private Player iAm; - - // Le joueur réel pourrait avoir besoin de connaître un constructeur de coup? - // pas pour l'instant. - - - /** - * - * - * @throws UnsupportedOperationException if the method is not yet implemented - * - * @throws IllegalStateException if the Situation is already in the bookmarks - */ - public abstract AbstractPly giveYourMove(IBoard p); - -} diff --git a/TP3/fr/iut_fbleau/GameAPI/AbstractPly.class b/TP3/fr/iut_fbleau/GameAPI/AbstractPly.class index d7e2a47f1bd1e1a2c6b0d1fcce138ee8e1ebd233..5a3a4d20f066e14f40c4519eb2abb9c8c4c97a13 100644 GIT binary patch delta 213 zcmeBSTFA_G>ff$?3=9m83?dV`^m#cMI2btD8Mt^DxEXjR+5|8wYWPgF7vl`bNvupQ zvepcn*d;B%nVwn#5nyCc(9i@aV2ALnCoWd5<6vN7U|?WmU;>%Jz`(%7z{tSLz{|kM zz{kMApvAxhmgmyi&cLXpy_JD|BUm**0|Nse12Y2y13Lo?$Q2Bn42%o{41!R7vJ8w2 cj0_A6tXf+b*f+qnuz>}67#JCZz*Y$Z06ff$?3=9m84Ez(h^qJWiI3~IUh_g87r=&76@cU%urTUfTCZ!gIBqrsg zPGVGcV`pGuU|?WmU}E58-~zK48CV(E7#JD285kI385kKD85kH?wYD=bZUjs7Fff3n R85o!u7#VmO7#Nrs_y7me5pMth diff --git a/TP3/fr/iut_fbleau/GameAPI/AbstractPly.java b/TP3/fr/iut_fbleau/GameAPI/AbstractPly.java index f831b22..211fc6f 100644 --- a/TP3/fr/iut_fbleau/GameAPI/AbstractPly.java +++ b/TP3/fr/iut_fbleau/GameAPI/AbstractPly.java @@ -2,4 +2,13 @@ package fr.iut_fbleau.GameAPI; public abstract class AbstractPly { private Player joueur; + + // constructeur à appeler dans le constructeur d'un fils concret avec super. + public AbstractPly(Player j){ + this.joueur=j; + } + + public Player getPlayer(){ + return this.joueur; + } } diff --git a/TP3/fr/iut_fbleau/GameAPI/IBoard.class b/TP3/fr/iut_fbleau/GameAPI/IBoard.class index 5728bcd51976c0ae8432880b3e8677d1d5010dcc..986c0b3a9039b9d0074485a663cb704015a548a2 100644 GIT binary patch delta 126 zcmeBW`NP6>>ff$?3=9m84AK+1>=>OldWJIca}+10r8?&qR5CIsXlVLO6p)d!W@KP- z&QD2YWZ?J7%uDqv%}q)z3Q0`LNu4~0QH_yf@?l0-T`>km25|-k1}O$c21W)31}3cy Z3=9m63=#|s3~XQ#Uj{}7NstVK6aWwl86W@v delta 40 wcmeyv(#yhi>ff$?3=9m84B`{H>==_bdWJGie#)rA$Ud2i$(5CXfr&v301W5~X8-^I diff --git a/TP3/fr/iut_fbleau/GameAPI/IBoard.java b/TP3/fr/iut_fbleau/GameAPI/IBoard.java index 2bf6d91..505e09b 100644 --- a/TP3/fr/iut_fbleau/GameAPI/IBoard.java +++ b/TP3/fr/iut_fbleau/GameAPI/IBoard.java @@ -11,7 +11,7 @@ public interface IBoard { /** * @return the current player */ - public Player getcurrentPlayer(); + public Player getCurrentPlayer(); /** * Returns the game status @@ -27,10 +27,9 @@ public interface IBoard { public Result getResult(); /** - * checker of legal moves from this position + * checker of the legality of a ply from this position * - * @throws NullPointerException if the game is over - * @return the iterator + * @return true iff the ply is legal */ public boolean isLegal(AbstractPly c); @@ -39,7 +38,6 @@ public interface IBoard { /** * constructor of Iterator over legal moves from this position * - * @throws NullPointerException if the game is over * @return the iterator */ public Iterator iterator(); @@ -48,7 +46,7 @@ public interface IBoard { /** * Plays a given move on the plateau. - * Should update history using + * * @throws IllegalArgumentException if the move is always illegal (say from the wrong game) * @throws IllegalStateException if the move is not legal in this position * @@ -60,10 +58,22 @@ public interface IBoard { /** * Resets the plateau to the position before the last move. * - * @throws IllegalStateException if nothing to undo in history + * @throws IllegalStateException if nothing to undo * */ public void undoPly(); - + /** + * Creates a safe copy of the board. + * + * Intended to prevent cheating from a human via some interface, + * or from a bot playing via the API, or from a badly coded bot. + * + * Beware that the default implantation is unsafe and returns this. + * + * @return the copy + */ + public default IBoard safeCopy(){ + return this; + } } diff --git a/TP3/fr/iut_fbleau/Nim/NimBoard.class b/TP3/fr/iut_fbleau/Nim/NimBoard.class index d690ef3763d7e625b2988780c6bfb686e5052a45..09ae5b82031bc6b1fa0dbe20a1b08de54d11a693 100644 GIT binary patch delta 14 VcmX>gdq8%BIR~TjW($rEHUK7j1aANU delta 14 VcmX>gdq8%BIR|6%W($rEHUK9(1djj! diff --git a/TP3/fr/iut_fbleau/Nim/NimBoard.java b/TP3/fr/iut_fbleau/Nim/NimBoard.java index 586adf3..c57392c 100644 --- a/TP3/fr/iut_fbleau/Nim/NimBoard.java +++ b/TP3/fr/iut_fbleau/Nim/NimBoard.java @@ -64,7 +64,7 @@ public class NimBoard extends AbstractBoard { } @Override - public Player getcurrentPlayer() { + public Player getCurrentPlayer() { return this.currentPlayer; } @@ -196,4 +196,3 @@ public class NimBoard extends AbstractBoard { return sb.toString(); } } - diff --git a/TP3/fr/iut_fbleau/Nim/NimPly.class b/TP3/fr/iut_fbleau/Nim/NimPly.class index b37ab2013ae386ce6061fb9b7157fafe3cf89b89..c74df15adac91c4435b502999d8053ff85b9bbf2 100644 GIT binary patch delta 414 zcmdnYxsj9W)W2Q(7#J8F8O$bf*)S<;Ostce=%F)lg{&|W10N3qKZ5`x17~__NkC3w zW$NU$j4H}p3>*wXJPg7x8L#}()Y2j@22lnvb_Q`C1_=hqi5I-sq0#l@h_pu^6f3(}`IxsB;Eqv7NzW-S(DkbSYta+8-a3)M3*Ffnj5 z@GvkiFo7&)U|?WjU}RurkY-?HkYQk8FlJz4U}j)o;MdyDz^J9Ym4RJLW-9~lMg|53 zMh0011_mJp7LYUp3xfayCxajZAA=ABBZC}+Jk$VL21W)(1_lOJtt|}f8{pd5z=9I> z42%p442oFP@~%Ns3o=lNK^dx4lYt3rf*{D)9Sk6fcNc>=0|UAh$_&g5Dq!!aGN>^y gFmNz1GH5a|GN?1?GcYi4b1^V77%&(ym@r5J0GF>Z_y7O^ diff --git a/TP3/fr/iut_fbleau/Nim/NimPly.java b/TP3/fr/iut_fbleau/Nim/NimPly.java index 7c6396f..4d70760 100644 --- a/TP3/fr/iut_fbleau/Nim/NimPly.java +++ b/TP3/fr/iut_fbleau/Nim/NimPly.java @@ -8,31 +8,36 @@ import fr.iut_fbleau.GameAPI.Player; * Un coup consiste à retirer un certain nombre d'allumettes. */ public class NimPly extends AbstractPly { - - private Player joueur; + private int nombreAllumettesPrises; - + + /** + * Constructeur du coup de Nim. + * + * @param joueur le joueur qui effectue le coup + * @param nombreAllumettesPrises le nombre d'allumettes retirées + */ public NimPly(Player joueur, int nombreAllumettesPrises) { - this.joueur = joueur; + super(joueur); this.nombreAllumettesPrises = nombreAllumettesPrises; } - + /** - * @return le joueur qui effectue le coup + * @return le joueur qui a joué ce coup */ public Player getJoueur() { - return this.joueur; + return super.getPlayer(); } - + /** - * @return le nombre d'allumettes prises + * @return le nombre d'allumettes retirées */ public int getNombreAllumettesPrises() { return this.nombreAllumettesPrises; } - + + @Override public String toString() { - return "Joueur " + joueur + " retire " + nombreAllumettesPrises + " allumette(s)"; + return "Le joueur " + getJoueur() + " retire " + nombreAllumettesPrises + " allumette(s)."; } } -