TD2
This commit is contained in:
BIN
testraté/AbreNim.class
Normal file
BIN
testraté/AbreNim.class
Normal file
Binary file not shown.
13
testraté/AbreNim.java
Normal file
13
testraté/AbreNim.java
Normal file
@@ -0,0 +1,13 @@
|
||||
public class AbreNim {
|
||||
|
||||
private Noeud root;
|
||||
|
||||
public AbreNim(int valeur) {
|
||||
this.root = new Noeud(valeur);
|
||||
}
|
||||
|
||||
public Noeud getRoot() {
|
||||
return root;
|
||||
}
|
||||
|
||||
}
|
||||
17
testraté/Etat.java
Normal file
17
testraté/Etat.java
Normal file
@@ -0,0 +1,17 @@
|
||||
public class Etat {
|
||||
private int nb_alumettes;
|
||||
private int statut;
|
||||
|
||||
public Etat(int nb,int statut){
|
||||
this.nb_alumettes = nb;
|
||||
this.statut = statut;
|
||||
}
|
||||
|
||||
public int getNb_alumettes() {
|
||||
return nb_alumettes;
|
||||
}
|
||||
|
||||
public int getStatut() {
|
||||
return statut;
|
||||
}
|
||||
}
|
||||
BIN
testraté/Noeud.class
Normal file
BIN
testraté/Noeud.class
Normal file
Binary file not shown.
22
testraté/Noeud.java
Normal file
22
testraté/Noeud.java
Normal file
@@ -0,0 +1,22 @@
|
||||
public class Noeud {
|
||||
private Integer valeur;
|
||||
public Noeud[] child = new Noeud[3];
|
||||
private int Etat;
|
||||
|
||||
public Noeud(Integer etiquette) {
|
||||
this.valeur = etiquette;
|
||||
}
|
||||
|
||||
public int getValeur()
|
||||
{
|
||||
return valeur;
|
||||
}
|
||||
|
||||
public int getEtat(){
|
||||
return Etat;
|
||||
}
|
||||
|
||||
public void setEtat(int newEtat){
|
||||
this.Etat = newEtat;
|
||||
}
|
||||
}
|
||||
82
testraté/nim.java
Normal file
82
testraté/nim.java
Normal file
@@ -0,0 +1,82 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import AbreNim;
|
||||
import Noeud;
|
||||
|
||||
public class nim {
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
AbreNim AN = AbreNim(5);
|
||||
System.out.println(exploreMax(AN.getRoot()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine si le joueur gagne ou perd grace a exploreMin.
|
||||
* @param allumette le nombre d'allumette au tour du joueur
|
||||
* @return -1 si perdant ou 1 si gagnant
|
||||
*/
|
||||
public static boolean exploreMax(Noeud root) {
|
||||
allumette = root.getValeur();
|
||||
allumette -= 3;
|
||||
for(int i=0;allumette>1&&i<3;i++) {
|
||||
root.child[i] = new Noeud(allumette);
|
||||
if (checkEtat(root.child[i])==1) {
|
||||
return true;
|
||||
}
|
||||
allumette++;
|
||||
}
|
||||
boolean childtest;
|
||||
for (int i = 2; i >=0; i--) {
|
||||
if (root.child[i].getEtat()==0) {
|
||||
childtest = exploreMin(root.child[i]);
|
||||
if (childtest) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Determine si le joueur gagne ou perd grace a exploreMax.
|
||||
* @param allumette le nombre d'allumette au tour du joueur
|
||||
* @return -1 si gagnant ou 1 si perdant
|
||||
*/
|
||||
public static boolean exploreMin(Noeud root){
|
||||
allumette = root.getValeur();
|
||||
allumette -= 3;
|
||||
for(int i=0;allumette>1&&i<3;i++) {
|
||||
root.child[i] = new Noeud(allumette);
|
||||
if (checkEtat(root.child[i])==-1) {
|
||||
return true;
|
||||
}
|
||||
allumette++;
|
||||
}
|
||||
boolean childtest = false;
|
||||
for (int i = 2; i >=0; i--) {
|
||||
if (root.child[i].getEtat()==0) {
|
||||
childtest = exploreMax(root.child[i]);
|
||||
if (childtest) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static int checkEtat(Noeud node){
|
||||
if(node.getValeur()<1){
|
||||
node.setEtat(-1);
|
||||
return -1;
|
||||
}
|
||||
else if (node.getValeur()==1) {
|
||||
node.setEtat(1);
|
||||
return 1;
|
||||
}
|
||||
node.setEtat(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user