Skip to content

Commit

Permalink
Fix final
Browse files Browse the repository at this point in the history
  • Loading branch information
antoineguillory authored Jan 19, 2018
1 parent 78761fd commit 440fdd1
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 69 deletions.
6 changes: 5 additions & 1 deletion ihm/GridJButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ public class GridJButton extends JButton {
*/
private static final long serialVersionUID = 1L;

//ID par rapport a l'IHM
private Integer numerotation;

//Etat courant du bouton
private IHMState state;


//Contructeur
public GridJButton(ImageIcon icn, Integer numerotation){
super(icn);
this.setOpaque(true);
Expand All @@ -30,6 +33,7 @@ public GridJButton(ImageIcon icn, Integer numerotation){
this.repaint();
}

//Change l'icone courante
public void setIcon(ImageIcon icn){
super.setIcon(icn);
Color col = new Color(255,128,0);
Expand Down
2 changes: 1 addition & 1 deletion ihm/Home.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public Home(){

mainPanel.setVisible(true);
mainPanel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JButton type1 = new JButton("Type 1");
JButton type2 = new JButton("Type 2");

Expand Down
23 changes: 16 additions & 7 deletions model/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Set;

import util.BoardsTypes;
import util.Contract;

public class Board implements IBoard {
Expand All @@ -19,11 +20,7 @@ public Board() {
public Board(int i) {
Contract.checkCondition(1 <= i && i <= IBoard.LAST_TAB);

switch (i) {
case IBoard.CLASSIC_TAB_NB:
setHole = HoleFactory.generateBoardHoles(IBoard.BAD_POS_PRIMITIVE);
break;
}
setHole = HoleFactory.generateBoardHoles(convert(BoardsTypes.badpositions(i)));
}

//METHODES
Expand All @@ -32,8 +29,8 @@ public Set<IHole> getHoleSet() {
return setHole;
}

public IBoard copie(){
IBoard b = new Board();
public IBoard copie(int i){
IBoard b = new Board(i);
for(IHole h : this.setHole) {
for(IHole nh : b.getHoleSet()) {
if(nh.getXPos() == h.getXPos() && nh.getYPos() == h.getYPos()) {
Expand All @@ -55,5 +52,17 @@ public boolean equals(Object o) {
}
return true;
}

//OUTIL
//Pour passer d'un tableau d'Integer à un int[]
private int [] convert (Integer[] t) {
int [] ret = new int[49];
int k = 0;
for(Integer i : t) {
ret[k] = i;
k++;
}
return ret;
}

}
10 changes: 6 additions & 4 deletions model/HeurPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ public class HeurPath implements IHeurPath {
private Set futurPool; //PAREIL
private Map <IBoard, Double> futurPoolHeur;
private Map <IBoard, StringBuffer> moves;
private Integer type;

//CONSTRUCTEURS

public HeurPath(int i, String start, String end) {
IBoard b = new Board(i);
type = i;
for (IHole h: b.getHoleSet()) {
if (h.getPosition().compareTo(start) == 0) {
h.takePeg();
Expand Down Expand Up @@ -76,7 +78,7 @@ public void integrate (IBoard b, StringBuffer s) {
}
//Si on a de la place on ajoute juste le plateau sans plus de test.
if (this.futurPool.size() < this.nbBoard) {
IBoard cpy = b.copie();
IBoard cpy = b.copie(type);
this.futurPoolHeur.put(cpy, heuri);
this.moves.put(cpy, new StringBuffer(s));
this.futurPool.add(cpy);
Expand All @@ -86,7 +88,7 @@ public void integrate (IBoard b, StringBuffer s) {
//Si c'est le cas on ajoute b et on retire le board à l'heuristique la moins bonne.
for(Double d : futurPoolHeur.values()) {
if (heuri < d) {
IBoard cpy = b.copie();
IBoard cpy = b.copie(type);
this.futurPoolHeur.put(cpy, heuri);
this.moves.put(cpy, new StringBuffer(s));
this.futurPool.add(cpy);
Expand All @@ -108,7 +110,7 @@ public void integrate (IBoard b, StringBuffer s) {
moves.remove(bmax);

} else {
IBoard cpy = b.copie();
IBoard cpy = b.copie(type);
this.futurPoolHeur.put(cpy, heuri);
this.moves.put(cpy, new StringBuffer(s));
this.futurPool.add(cpy);
Expand Down Expand Up @@ -235,7 +237,7 @@ public void calculPath() {
}
System.out.println("i = " + i +" size futurpool " + futurPool.size());
i++;
} while(i < 28);
} while(!currentPool.isEmpty());
}

//OUTILS
Expand Down
4 changes: 2 additions & 2 deletions model/IBoard.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ public interface IBoard {
public static final int[] BAD_POS_PRIMITIVE = {0,1,5,6,7,8,12,13,35,36,40,41,42,43,47,48};


public static final int LAST_TAB = 1;
public static final int LAST_TAB = 2;

/**
* getHoleSet renvoi l'ensemble des pegs du IBoard.
* @inv : getHoleSet() != null
*/
public Set<IHole> getHoleSet();

public IBoard copie();
public IBoard copie(int i);



Expand Down
25 changes: 25 additions & 0 deletions model/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,31 @@ public static void main(String[] args) {
p.calculPath();
System.out.println(p.getBestMoves());

// int i = 0;
// for (IHole h : p.getBoard().getHoleSet()) {
// i++;
// if (h.pegIn()) {
// System.out.println(i + " " + h.getPosition());
// } else {
// System.out.println(i + " " + h.getPosition() + " out");
// }
// for (int j = 1; j <= 4; j++) {
// if (h.nearHoleHere(j)) {
// System.out.println(" " + h.getNearHole(j).getPosition());
// }
// }
// }


// p.computePath();
// System.out.println("" + p.getBestNb() +"\n"
// + p.getNb() +"\n"
// + p.getPegOutNb() +"\n"
// + p.getBestMoves() +"\n"
// + p.getBoard() +"\n"
// + p.getCurrentHole() +"\n"
// + p.getMoves());

}

}
22 changes: 5 additions & 17 deletions util/BoardsTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@

import java.util.HashMap;
import java.util.Map;
import java.util.TreeSet;
/*
* Représente les différents types de plateau utilisable, avec ce qu'il faut pour les utiliser.
* Il est facilement étendable à d'autre plateaux. (mais uniquement 7x7 maximum)
*
*/

//Représente les différents types de plateau, on peut facilement rajouter des plateaux en modifiant badpositions et en rajoutant deux variables.
public class BoardsTypes {
public final static int PLATEAU1 = 1;
public final static int PLATEAU2 = 2;
Expand All @@ -25,29 +19,23 @@ public static Integer[] badpositions(Integer type) {
return null;
}
}
// Permet de faire le lien avec l'ihm, puisqu'on passe d'une string à une liste d'entiers.
public static Map<String, Integer> bijection (Integer type) {
int k = 1;
Integer b[] = badpositions(type);
Map<String,Integer> m = new HashMap<String, Integer>();
TreeSet<String> set = new TreeSet<String>();
for(int i = 0; i < 7; i++) {
for(int j = 0; j < 7; j++) {
if (!inArrayint((i * 7) + j, b)){
char y = (char) ('a' + i);
char x = (char) ('1' + j);
String s = "" + y + x;
set.add(s);
m.put(s, k);
System.out.println(s);
k++;
}
}
}
int k = 1;
while(!set.isEmpty()) {
String s = set.pollFirst();
m.put(s, k);
k++;
}
return m;
}

Expand All @@ -58,7 +46,7 @@ public static Map<Integer, String> bijection2 (Integer type) {
ret.put(m.get(s), s);
}
return ret;
}
}


private static boolean inArrayint(Integer a, Integer t[]) {
Expand Down
6 changes: 1 addition & 5 deletions util/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ public static void main(String[] args) {
while(l.size() != 0) {
if(l.getFirst() != null) {
Move m = (Move) l.getLast();
System.out.print(m.getStart());
for(Integer i : m.getRemoved()) {
System.out.print(" " + i);
}
System.out.print(" " + m.getEnd() + '\n');
System.out.println(m.getStart() + " " + m.getEnd());
l.removeLast();
}
}
Expand Down
58 changes: 26 additions & 32 deletions util/Wrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,32 @@
import java.util.Set;
import java.util.StringTokenizer;

/*
* Wrapper utilise Move pour générer, à partir d'une chaine formatée, une liste de coups (Move).
* On détaillera toute l'utilisation du wrapper dans le dossier.
*
*/

public class Wrapper {

public static String sample = "d6Wd4;b5Sd5;c7Wc5;e7Nc7;d4Ed6;b4Sd4;c2Ec4;a3Sc3;a5Wa3;d3Nb3;c4Ec6;f3Nd3;e1Ee3;c1Se1;e4We2;e6We4;g5Ne5;g3Eg5;c7Wc5;a3Sc3;e1Ee3;d3Ed5Sf5;g5Ne5;e4Ee6Nc6;c6Wc4Wc2;c2Se2Ee4;f4Nd4;";

public static LinkedList getMoves (String moves, int typeofBoard) {
// utilise ça pour tes tests, tant que le model craint

public static LinkedList<Move> getMoves(String moves, int typeofBoard) {
StringTokenizer st = new StringTokenizer(moves, ";");
LinkedList<Move> l = new LinkedList<Move>();
Map<String, Integer> m = BoardsTypes.bijection(typeofBoard);
while (st.hasMoreTokens()) {

String s = st.nextToken();
if(s.length() > 6) { //Cas du double coup
String start = s.substring(0, 2);
String dir1 = s.substring(2, 3);
String mid = s.substring(3, 5);
String dir2 = s.substring(5, 6);
String end = s.substring(6, 8);
Set<Integer> set = new HashSet<Integer>();
set.add(m.get(start));
set.add(m.get(dirmove(start, dir1)));
set.add(m.get(dirmove(mid, dir2)));
int sta = m.get(start);
int en = m.get(end);
Move entry = new Move(sta, en, set);
l.addFirst(entry);
} else { //Cas du coup simple
String s = st.nextToken();
if(s.length() > 6) {
String start = s.substring(0, 2);
String dir1 = s.substring(2, 3);
String mid = s.substring(3, 5);
String dir2 = s.substring(5, 6);
String end = s.substring(6, 8);
Set<Integer> set = new HashSet<Integer>();
set.add(m.get(start));
set.add(m.get(dirmove(start, dir1)));
set.add(m.get(dirmove(mid, dir2)));
int sta = m.get(start);
int en = m.get(end);
Move entry = new Move(sta, en, set);
l.addFirst(entry);
} else {
String start = s.substring(0, 2);
String dir1 = s.substring(2, 3);
String end = s.substring(3, 5);
Expand All @@ -61,8 +54,8 @@ private static String dirmove(String s, String dir) {
Character c1 = (char) ic1;
Character c2 = (char) ic2;
String ret;
switch(dir) {
case "N" :
switch (dir) {
case "N":
ic1--;
c1 = (char) ic1;
ret = new String(c1.toString() + c2.toString() + "");
Expand All @@ -72,18 +65,19 @@ private static String dirmove(String s, String dir) {
c2 = (char) ic2;
ret = new String(c1.toString() + c2.toString() + "");
return ret;
case "W" :
case "W":
ic2--;
c2 = (char) ic2;
ret = new String(c1.toString() + c2.toString() + "");
return ret;
case "S" :
case "S":
ic1++;
c1 = (char) ic1;
ret = new String(c1.toString() + c2.toString() + "");
return ret;
default :
return ret;
default:
return "";
}
}

}

0 comments on commit 440fdd1

Please sign in to comment.