Commit 33aeb56c authored by erreiip's avatar erreiip
Browse files

Improve swap method

parent a9faa4ab
Loading
Loading
Loading
Loading
+2 −0
Changes for jeuAssemblage/build.xml: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -78,7 +78,9 @@
    </target>
    <!--  ==================== Run Target ==================================  -->
    <target name="run" depends="dist">
        <record name="log.txt" action="start"/> 
            <java jar="${dist.home}/${app.fullname}.jar" fork="true"/>
        <record name="log.txt" action="stop"/>
    </target>
    <!--  ==================== runWithoutComp Target ==================================  -->
    <target name="runWithoutComp">

jeuAssemblage/log.txt

0 → 100644
+267441 −0

File added.

Preview size limit exceeded, changes collapsed.

+72 −15
Changes for jeuAssemblage/src/bot/strategy/AbstractStrategyAI.java: 72 added lines, 15 removed lines.
Original line number Diff line number Diff line
@@ -3,6 +3,8 @@ package bot.strategy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import bot.interfaces.IStrategyBot;
@@ -11,43 +13,98 @@ import model.PlayBoard;
import piece.Piece;

public class AbstractStrategyAI implements IStrategyBot {
    protected int deep;
    protected LinkedList<Move> moves;
    private static final int POP_SIZE = 100; 

    protected List<Move> moves;
    protected PlayBoard model;

    protected AbstractStrategyAI(PlayBoard model) {
        this.deep = -1;
        this.moves = new LinkedList<Move>();
    protected long seed;
    
    public AbstractStrategyAI(PlayBoard model) {
        this(model, generateRandomSeed());
    }

    protected AbstractStrategyAI(PlayBoard model, long seed) {
        this.model = model;
        System.out.println(model);
        this.moves = getMoves();
        this.seed = seed;
    }
    
    private LinkedList<Move> getMoves() {
        return null;
    private List<Move> getMoves() {
        List<Move> ret = new LinkedList<Move>();
        
        Random rand = new Random(this.seed);
        List<PlayBoard> population = createPopulation(this.model, POP_SIZE, rand);

        for (int i = 0; i < 10; i++) {
            population = mutation(population, rand, 1d);
        }

    private ArrayList<PlayBoard> mutation(ArrayList<PlayBoard> toMutate, Random rand, double mutationRate) {
        return ret;
    }

        ArrayList<PlayBoard> postMutation = new ArrayList<PlayBoard>();
    private List<PlayBoard> mutation(List<PlayBoard> toMutate, Random rand, double mutationRate) {

        List<PlayBoard> postMutation = new ArrayList<PlayBoard>();
        for (PlayBoard parent : toMutate) {
            postMutation.add(parent);

            if (rand.nextDouble() > mutationRate) {
                postMutation.add(individualMutation(parent));
            if (rand.nextDouble(1) < mutationRate) {
                postMutation.add(individualMutation(parent, rand));
            }
        }

        return postMutation;
    }
    
    private PlayBoard individualMutation(PlayBoard parent) {
        PlayBoard son = PlayBoard.constructEmptyPlayBoardCopy(parent);
    private PlayBoard individualMutation(PlayBoard parent1, Random rand) {
        
        PlayBoard son = PlayBoard.constructCopyPlayBoard(parent1);
        
        Map<Integer, Piece> piecesParent1 = parent1.getPieces();

        System.out.println("PAREZNTTTTTTTTTTTTTTTTTTTTT");
        System.out.println(parent1);
        for (int cpt = 0; cpt < piecesParent1.size(); cpt++) {
            int toSwapId = cpt;
            while (toSwapId == cpt) {
                toSwapId = rand.nextInt(piecesParent1.size());
            }

            son.swap(son.getPieceById(cpt+1), son.getPieceById(toSwapId + 1));
        }

        return null;
        return son;
    }
    
    @Override
    public Move nextMove() {
        return this.moves.pop();
        return this.moves.remove(0);
    }

    private static List<PlayBoard> createPopulation(PlayBoard model, int size, Random rand) {
        
        List<PlayBoard> population = new ArrayList<PlayBoard>();

        int pieceCount = model.getPieces().size();
        for (int i = 0; i < size; i++) {

            population.add(PlayBoard.constructPlayBoard(rand.nextLong(), model.getBoardWidth(), model.getBoardHeight(),
                    pieceCount));
        }

        return population;
    }

    // REFACTO FAIRE UNE CLASSE SEED AVEC TOUQ LES TRUCS SUR LES SEEDS
    public static long generateRandomSeed() {

        return (long) (Math.random() * Long.MAX_VALUE);
    }

    public static void main(String args) {
        new AbstractStrategyAI(PlayBoard.constructPlayBoard(generateRandomSeed(), 8, 8, 4));
    }
    
}
+6 −0
Changes for jeuAssemblage/src/main/App.java: 6 added lines, 0 removed lines.
Original line number Diff line number Diff line
package main;

import java.io.File;

import javax.swing.SwingUtilities;

import bot.strategy.AbstractStrategyAI;
import model.PlayBoard;
import view.MainFrame;

public class App {
    public static void main(String[] args) throws Exception {

        //PlayBoard p = PlayBoard.constructPlayBoard(AbstractStrategyAI.generateRandomSeed(), 8, 8, 4);
        //new AbstractStrategyAI(p);
        SwingUtilities.invokeLater(() -> {
            
            new MainFrame();
+22 −7
Changes for jeuAssemblage/src/model/PlayBoard.java: 22 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@ import java.awt.Point;
import java.awt.Color;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@@ -32,7 +33,7 @@ public class PlayBoard extends AbstractListenableHM implements Listener, IPlayBo

	private static final int EMPTY = 0;

	private final int[][] board;
	private int[][] board;

	private final Map<Integer, Piece> piecesMap;
	private final Map<Integer, Image> piecesImagesMap;
@@ -165,7 +166,7 @@ public class PlayBoard extends AbstractListenableHM implements Listener, IPlayBo
		this.unregisterPiece(piece);
	}

	public boolean movePiece(Piece piece, int x, int y) {
	public boolean movePiece(int x, int y, Piece piece) {

		if (canBePlaced(x, y, piece)) {

@@ -191,15 +192,30 @@ public class PlayBoard extends AbstractListenableHM implements Listener, IPlayBo
		int id1 = getPieceId(p1);
		int id2 = getPieceId(p2);

		System.out.println(this);
		Point pointP1 = getUpperLeftPieceCornerById(id1);
		Point pointP2 = getUpperLeftPieceCornerById(id2);

		int[][] rollback = new int[this.board.length][this.board[0].length];
		for(int i = 0; i < this.board.length; i++)
    		rollback[i] = this.board[i].clone();


		if (canBeSwapped(pointP2.x, pointP2.y, p1, id2) && canBeSwapped(pointP1.x, pointP1.y, p2, id1)) {

			removePieceFromBoardWithoutRegistration(p1);
			placePieceAsId(pointP1.x, pointP1.y, p2, id2);
			placePieceAsId(pointP2.x, pointP2.y, p1, id1);
			removePieceFromBoardWithoutRegistration(p2);

			if (placePieceAsId(pointP1.x, pointP1.y, p2, id2) && placePieceAsId(pointP2.x, pointP2.y, p1, id1)) {
				return true;
			}
		}
		
		System.out.println("pas bon");
		System.out.println(this);
		this.board = rollback;
		piecesMap.put(id1, p1);
		piecesMap.put(id2, p2);

		return false;
	}
@@ -237,11 +253,10 @@ public class PlayBoard extends AbstractListenableHM implements Listener, IPlayBo
			for (int j = 0; j < piece.getWidth() && canBePlaced; j++) {

				if (pieceMatrix[i][j] == true) {
					boolean isEmpty = this.board[y + i][x + j] == EMPTY || this.board[y + i][x + j] == ignoreId;
					canBePlaced = (
						y + i < this.board.length && 
						x + j < this.board[0].length && 
						isEmpty
						(this.board[y + i][x + j] == EMPTY || this.board[y + i][x + j] == ignoreId)
					);
				}	
			}
@@ -283,7 +298,7 @@ public class PlayBoard extends AbstractListenableHM implements Listener, IPlayBo

			for (int j = 0; j < this.board[i].length; j++) {

				sb.append((this.board[i][j] == EMPTY) ? "0 " : this.board[i][j] + " ");
				sb.append((this.board[i][j] == EMPTY) ? "  " : this.board[i][j] + " ");
				sb.append(" ");
			}