forked from fauvet/API_BUT5.5
Refaire Undo
This commit is contained in:
parent
ee380deb0e
commit
8d5bc6b0a7
@ -1,5 +1,26 @@
|
||||
import java.util.Iterator;
|
||||
|
||||
import fr.iut_fbleau.raw_api_body.entity.*;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello world!");
|
||||
PlateauNim jeu = new PlateauNim(5, Player.JOUEUR1);
|
||||
jeu.doPly(new NimPly(2));
|
||||
jeu.doPly(new NimPly(1));
|
||||
jeu.doPly(new NimPly(1));
|
||||
jeu.doPly(new NimPly(1));
|
||||
if (jeu.isFinished()) {
|
||||
int resultat = jeu.getResult();
|
||||
if (resultat == Result.PERDU) {
|
||||
System.out.println("Le joueur 1 a perdu !");
|
||||
} else if (resultat == Result.GAGNE) {
|
||||
System.out.println("Le joueur 1 a gagné !");
|
||||
} else {
|
||||
System.out.println("Égalité !");
|
||||
}
|
||||
}
|
||||
else {
|
||||
System.out.println("C'est pas fini : " + jeu.isFinished());
|
||||
}
|
||||
}
|
||||
}
|
24
src/fr/iut_fbleau/raw_api_body/entity/NimPly.java
Normal file
24
src/fr/iut_fbleau/raw_api_body/entity/NimPly.java
Normal file
@ -0,0 +1,24 @@
|
||||
package fr.iut_fbleau.raw_api_body.entity;
|
||||
|
||||
/**
|
||||
* Represents a move in the Nim game.
|
||||
*/
|
||||
public class NimPly extends Ply {
|
||||
private final int allumettes;
|
||||
|
||||
// Constructor to initialize the move with a number of allumettes to remove
|
||||
public NimPly(int allumettes) {
|
||||
this.allumettes = allumettes;
|
||||
}
|
||||
|
||||
// Returns the number of allumettes removed
|
||||
public int getallumettes() {
|
||||
return allumettes;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Remove " + allumettes + " allumettes.";
|
||||
}
|
||||
}
|
108
src/fr/iut_fbleau/raw_api_body/entity/PlateauNim.java
Normal file
108
src/fr/iut_fbleau/raw_api_body/entity/PlateauNim.java
Normal file
@ -0,0 +1,108 @@
|
||||
package fr.iut_fbleau.raw_api_body.entity;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Implements the Plateau interface for the game of Nim.
|
||||
*/
|
||||
public class PlateauNim implements Plateau {
|
||||
|
||||
private int allumettesRestantes; // Number of remaining allumettes
|
||||
private Player currentPlayer; // Current player
|
||||
private boolean gameFinished; // Game status
|
||||
private int result; // Game result: 1 (win), 0 (draw), -1 (loss)
|
||||
private List<Ply> playHistory; // History of moves
|
||||
|
||||
// Constructor initializes the game with a given number of allumettes and a starting player
|
||||
public PlateauNim(int initialallumettes, Player startingPlayer) {
|
||||
this.allumettesRestantes = initialallumettes;
|
||||
this.currentPlayer = startingPlayer;
|
||||
this.gameFinished = false;
|
||||
this.result = 0;
|
||||
this.playHistory = new ArrayList<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Player getPlayer() {
|
||||
if (gameFinished) {
|
||||
throw new NullPointerException("Game is over.");
|
||||
}
|
||||
return this.currentPlayer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFinished() {
|
||||
return this.gameFinished;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getResult() {
|
||||
if (!gameFinished) {
|
||||
throw new NullPointerException("Game is not over.");
|
||||
}
|
||||
return this.result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Ply> givePlies() {
|
||||
if (gameFinished) {
|
||||
throw new NullPointerException("Game is over.");
|
||||
}
|
||||
// Generate legal moves (remove 1, 2, or 3 allumettes, but not more than remaining allumettes)
|
||||
List<Ply> legalPlies = new ArrayList<>();
|
||||
for (int i = 1; i <= Math.min(3, allumettesRestantes); i++) {
|
||||
legalPlies.add(new NimPly(i));
|
||||
}
|
||||
return legalPlies.iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doPly(Ply ply) {
|
||||
if (!(ply instanceof NimPly)) {
|
||||
throw new IllegalArgumentException("Invalid move type.");
|
||||
}
|
||||
NimPly nimPly = (NimPly) ply;
|
||||
|
||||
if (nimPly.getallumettes() < 1 || nimPly.getallumettes() > Math.min(3, allumettesRestantes)) {
|
||||
throw new IllegalArgumentException("Invalid move.");
|
||||
}
|
||||
|
||||
// Update the state
|
||||
allumettesRestantes -= nimPly.getallumettes();
|
||||
playHistory.add(nimPly);
|
||||
|
||||
// Check if game is finished
|
||||
if (allumettesRestantes == 0) {
|
||||
gameFinished = true;
|
||||
if (this.currentPlayer == Player.JOUEUR1){
|
||||
result = -1; // Player 2 Win
|
||||
}
|
||||
else{
|
||||
result = 1; // Player 1 Win
|
||||
}
|
||||
} else {
|
||||
// Switch players if game is not finished
|
||||
currentPlayer = (currentPlayer == Player.JOUEUR1) ? Player.JOUEUR2 : Player.JOUEUR1;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void undoPly(Ply ply) {
|
||||
if (playHistory.isEmpty()) {
|
||||
throw new IllegalStateException("No moves to undo.");
|
||||
}
|
||||
if (!playHistory.get(playHistory.size() - 1).equals(ply)) {
|
||||
throw new IllegalStateException("Cannot undo a non-last move.");
|
||||
}
|
||||
|
||||
// Revert the move
|
||||
NimPly lastMove = (NimPly) playHistory.remove(playHistory.size() - 1);
|
||||
allumettesRestantes += lastMove.getallumettes();
|
||||
|
||||
// Switch back to previous player
|
||||
currentPlayer = (currentPlayer == Player.JOUEUR1) ? Player.JOUEUR2 : Player.JOUEUR1;
|
||||
gameFinished = false; // If game was finished, it's no longer
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user