Commit a9a3363e authored by erreiip's avatar erreiip
Browse files

First piece of an generation algo

parent 0062d721
Loading
Loading
Loading
Loading
+15 −10
Changes for jeuAssemblage/src/bot/strategy/AbstractStrategyAI.java: 15 added lines, 10 removed lines.
Original line number Diff line number Diff line
@@ -6,10 +6,13 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Collections;


import bot.interfaces.IStrategyBot;
import bot.Move;
import model.PlayBoard;
import model.SeedUtils;
import piece.Piece;

public class AbstractStrategyAI implements IStrategyBot {
@@ -21,12 +24,14 @@ public class AbstractStrategyAI implements IStrategyBot {
    protected long seed;
    
    public AbstractStrategyAI(PlayBoard model) {
        this(model, generateRandomSeed());
        this(model, SeedUtils.generateRandomSeed());
    }

    protected AbstractStrategyAI(PlayBoard model, long seed) {
        this.model = model;
        System.out.println(model);
        System.out.println(model.getArea());

        this.moves = getMoves();
        this.seed = seed;
    }
@@ -41,6 +46,10 @@ public class AbstractStrategyAI implements IStrategyBot {
            population = mutation(population, rand, 1d);
        }

        System.out.println(population.get(0));
        System.out.println(population.get(0).getArea());


        return ret;
    }

@@ -55,6 +64,10 @@ public class AbstractStrategyAI implements IStrategyBot {
            }
        }

        Collections.sort(postMutation);

        postMutation = postMutation.subList(0, POP_SIZE);

        return postMutation;
    }
    
@@ -64,8 +77,6 @@ public class AbstractStrategyAI implements IStrategyBot {
        
        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) {
@@ -97,14 +108,8 @@ public class AbstractStrategyAI implements IStrategyBot {
        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));
        new AbstractStrategyAI(PlayBoard.constructPlayBoard(SeedUtils.generateRandomSeed(), 8, 8, 4));
    }
    
}
+2 −1
Changes for jeuAssemblage/src/main/App.java: 2 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -6,12 +6,13 @@ import javax.swing.SwingUtilities;

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

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

        //PlayBoard p = PlayBoard.constructPlayBoard(AbstractStrategyAI.generateRandomSeed(), 8, 8, 4);
        //PlayBoard p = PlayBoard.constructPlayBoard(SeedUtils.generateRandomSeed(), 8, 8, 4);
        //new AbstractStrategyAI(p);
        SwingUtilities.invokeLater(() -> {
            
+20 −10
Changes for jeuAssemblage/src/model/PlayBoard.java: 20 added lines, 10 removed lines.
Original line number Diff line number Diff line
@@ -29,7 +29,7 @@ import utils.EDifficulty;
import utils.ETypeListen;
import view.utils.PieceRenderUtils;

public class PlayBoard extends AbstractListenableHM implements Listener, IPlayBoardListenable {
public class PlayBoard extends AbstractListenableHM implements Listener, IPlayBoardListenable, Comparable<PlayBoard> {

	private static final int EMPTY = 0;

@@ -192,7 +192,6 @@ 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);

@@ -211,8 +210,6 @@ public class PlayBoard extends AbstractListenableHM implements Listener, IPlayBo
			}
		}
		
		System.out.println("pas bon");
		System.out.println(this);
		this.board = rollback;
		piecesMap.put(id1, p1);
		piecesMap.put(id2, p2);
@@ -298,7 +295,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) ? "  " : this.board[i][j] + " ");
				sb.append((this.board[i][j] == EMPTY) ? "0 " : this.board[i][j] + " ");
				sb.append(" ");
			}

@@ -355,8 +352,6 @@ public class PlayBoard extends AbstractListenableHM implements Listener, IPlayBo
		throw new IllegalStateException("No piece found");
	}

	public Point getUpperLeftPieceCorner() { return new Point(getLowerPieceX(), getLowerPieceY()); }

	public int getUpperPieceX() {

		for (int x = this.board[0].length - 1; x >= 0; x--) {
@@ -421,12 +416,15 @@ public class PlayBoard extends AbstractListenableHM implements Listener, IPlayBo
		throw new IllegalStateException("Piece not found");
	}

	public Map<Integer, Piece> getPieces() { 

		return new HashMap<>(piecesMap);
	public int getArea() {
		return (getLowerPieceX() - getUpperPieceX()) * (getLowerPieceY() - getUpperPieceY());
	}

	public Map<Integer, Piece> getPieces() { return new HashMap<>(piecesMap); }

	public Point getLowerRightPieceCorner() { return new Point(getUpperPieceX(), getUpperPieceY()); }		
	public Point getUpperLeftPieceCorner() { return new Point(getLowerPieceX(), getLowerPieceY()); }

	public Point getUpperLeftPieceCornerById(int pieceId) { return new Point(getLowerPieceXById(pieceId), getLowerPieceYById(pieceId)); }
	
	public int getPieceIdAt(int x, int y) { return this.board[y][x]; }
@@ -439,8 +437,20 @@ public class PlayBoard extends AbstractListenableHM implements Listener, IPlayBo


	public int getWidth() { return this.sizeX; }

	public int getHeight() { return this.sizeY; }
	
	
	// -----------------
	// STATIC METHODS
	// -----------------
	
	@Override
	public int compareTo(PlayBoard o) {
		return this.getArea() - o.getArea();
	}


	// -----------------
	// STATIC METHODS
	// -----------------