projet fini achromate marche yesssssssssssss
This commit is contained in:
@@ -1,10 +1,12 @@
|
|||||||
package com.example.flow_free;
|
package com.example.flow_free;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
import android.graphics.Canvas;
|
import android.graphics.Canvas;
|
||||||
import android.graphics.Color;
|
import android.graphics.Color;
|
||||||
import android.graphics.Paint;
|
import android.graphics.Paint;
|
||||||
import android.graphics.RectF;
|
import android.graphics.RectF;
|
||||||
|
import android.preference.PreferenceManager;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.MotionEvent;
|
import android.view.MotionEvent;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
@@ -22,9 +24,10 @@ public class FlowFreeView extends View {
|
|||||||
private final int[][] board;
|
private final int[][] board;
|
||||||
private final Map<Integer, List<int[]>> paths = new HashMap<>();
|
private final Map<Integer, List<int[]>> paths = new HashMap<>();
|
||||||
|
|
||||||
private final Map<Integer, Integer> colorMap = new HashMap<>();//test pour la couleur
|
private final Map<Integer, Integer> colorMap = new HashMap<>();
|
||||||
private final Set<Integer> completedColors = new HashSet<>();// liste des chemin fini
|
private final Set<Integer> completedColors = new HashSet<>();// liste des chemin fini
|
||||||
private boolean win = false;
|
private boolean win = false;
|
||||||
|
private boolean isAchromate ;
|
||||||
|
|
||||||
|
|
||||||
private static final int[] DISTINCT_COLORS = {
|
private static final int[] DISTINCT_COLORS = {
|
||||||
@@ -35,7 +38,6 @@ public class FlowFreeView extends View {
|
|||||||
Color.rgb(255, 0, 255), // Fuchsia / Magenta
|
Color.rgb(255, 0, 255), // Fuchsia / Magenta
|
||||||
Color.rgb(0, 255, 255), // Cyan
|
Color.rgb(0, 255, 255), // Cyan
|
||||||
Color.rgb(255, 165, 0), // Orange vif
|
Color.rgb(255, 165, 0), // Orange vif
|
||||||
|
|
||||||
Color.rgb(128, 0, 0), // Bordeaux
|
Color.rgb(128, 0, 0), // Bordeaux
|
||||||
Color.rgb(0, 128, 0), // Vert foncé
|
Color.rgb(0, 128, 0), // Vert foncé
|
||||||
Color.rgb(0, 0, 128), // Bleu foncé
|
Color.rgb(0, 0, 128), // Bleu foncé
|
||||||
@@ -50,11 +52,28 @@ public class FlowFreeView extends View {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
private static final int[] DISTINCT_GRIS = {
|
||||||
|
Color.rgb(0, 0, 0), // Noir
|
||||||
|
Color.rgb(40, 40, 40), // Gris très foncé
|
||||||
|
Color.rgb(80, 80, 80), // Gris foncé
|
||||||
|
Color.rgb(120, 120, 120), // Gris moyen foncé
|
||||||
|
Color.rgb(160, 160, 160), // Gris moyen
|
||||||
|
Color.rgb(200, 200, 200), // Gris moyen clair
|
||||||
|
Color.rgb(220, 220, 220), // Gris clair
|
||||||
|
Color.rgb(240, 240, 240), // Gris très clair
|
||||||
|
Color.rgb(255, 255, 255), // Blanc
|
||||||
|
Color.rgb(25, 25, 25),
|
||||||
|
Color.rgb(55, 55, 55),
|
||||||
|
Color.rgb(95, 95, 95),
|
||||||
|
Color.rgb(135, 135, 135),
|
||||||
|
Color.rgb(175, 175, 175)
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private int colorId=0;
|
private int colorId=0;
|
||||||
private int selectedColor = 0;
|
private int selectedColor = 0;
|
||||||
private int cellSize;
|
private int cellSize;
|
||||||
|
|
||||||
public FlowFreeView(Context context, Puzzle puzzle) {
|
public FlowFreeView(Context context, Puzzle puzzle) {
|
||||||
super(context);
|
super(context);
|
||||||
this.puzzle = puzzle;
|
this.puzzle = puzzle;
|
||||||
@@ -66,7 +85,6 @@ public class FlowFreeView extends View {
|
|||||||
int row1 = pair[1];
|
int row1 = pair[1];
|
||||||
int col2 = pair[2];
|
int col2 = pair[2];
|
||||||
int row2 = pair[3];
|
int row2 = pair[3];
|
||||||
|
|
||||||
// Crée une couleur id unique pour chaque paire (1, 2, 3, ...)
|
// Crée une couleur id unique pour chaque paire (1, 2, 3, ...)
|
||||||
colorId = colorId+ 1;
|
colorId = colorId+ 1;
|
||||||
board[row1][col1] = colorId;
|
board[row1][col1] = colorId;
|
||||||
@@ -74,10 +92,7 @@ public class FlowFreeView extends View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void printBoardToLog() {
|
||||||
|
|
||||||
//test
|
|
||||||
private void printBoardToLog() {
|
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
for (int[] row : board) {
|
for (int[] row : board) {
|
||||||
for (int cell : row) {
|
for (int cell : row) {
|
||||||
@@ -85,9 +100,7 @@ private void printBoardToLog() {
|
|||||||
}
|
}
|
||||||
sb.append("\n");
|
sb.append("\n");
|
||||||
}
|
}
|
||||||
Log.d("DEBUG_FLOW", "Contenu du board :\n" + sb.toString());
|
|
||||||
}
|
}
|
||||||
//test
|
|
||||||
|
|
||||||
|
|
||||||
@Override//test pour calculer cellsize
|
@Override//test pour calculer cellsize
|
||||||
@@ -95,9 +108,8 @@ private void printBoardToLog() {
|
|||||||
super.onSizeChanged(w, h, oldw, oldh);
|
super.onSizeChanged(w, h, oldw, oldh);
|
||||||
if (puzzle != null) {
|
if (puzzle != null) {
|
||||||
cellSize = Math.min(w, h) / puzzle.getSize();
|
cellSize = Math.min(w, h) / puzzle.getSize();
|
||||||
Log.d("DEBUG_FLOW", "cellSize calculé : " + cellSize);
|
|
||||||
}
|
}
|
||||||
}//fin test cellsize
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -106,18 +118,8 @@ private void printBoardToLog() {
|
|||||||
@Override
|
@Override
|
||||||
protected void onDraw(Canvas canvas) {
|
protected void onDraw(Canvas canvas) {
|
||||||
super.onDraw(canvas);
|
super.onDraw(canvas);
|
||||||
Log.d("DEBUG_FLOW", "onDraw() a été appelé !");//debug
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
|
||||||
// --- DEBUG : Affiche le contenu du board dans la console ---
|
isAchromate = preferences.getBoolean("achromate_mode", false);
|
||||||
StringBuilder boardLog = new StringBuilder("Contenu du board :\n");
|
|
||||||
for (int row = 0; row < puzzle.getSize(); row++) {
|
|
||||||
for (int col = 0; col < puzzle.getSize(); col++) {
|
|
||||||
boardLog.append(String.format("%3d ", board[row][col]));
|
|
||||||
}
|
|
||||||
boardLog.append("\n");
|
|
||||||
}
|
|
||||||
Log.d("DEBUG_FLOW", boardLog.toString());
|
|
||||||
|
|
||||||
|
|
||||||
paint.setStrokeWidth(4);
|
paint.setStrokeWidth(4);
|
||||||
paint.setStyle(Paint.Style.STROKE);
|
paint.setStyle(Paint.Style.STROKE);
|
||||||
paint.setColor(Color.BLACK);
|
paint.setColor(Color.BLACK);
|
||||||
@@ -135,7 +137,7 @@ private void printBoardToLog() {
|
|||||||
for (int col = 0; col < puzzle.getSize(); col++) {
|
for (int col = 0; col < puzzle.getSize(); col++) {
|
||||||
int colorId = board[row][col];
|
int colorId = board[row][col];
|
||||||
if (colorId > 0) { // permet de ne pas dessiner des cercle quand je fais un trait
|
if (colorId > 0) { // permet de ne pas dessiner des cercle quand je fais un trait
|
||||||
paint.setColor(getColorForId(colorId));
|
paint.setColor(getColorForId(colorId,isAchromate));
|
||||||
float cx = col * cellSize + cellSize / 2f;
|
float cx = col * cellSize + cellSize / 2f;
|
||||||
float cy = row * cellSize + cellSize / 2f;
|
float cy = row * cellSize + cellSize / 2f;
|
||||||
canvas.drawCircle(cx, cy, cellSize / 4f, paint);
|
canvas.drawCircle(cx, cy, cellSize / 4f, paint);
|
||||||
@@ -147,7 +149,7 @@ private void printBoardToLog() {
|
|||||||
paint.setStrokeWidth(cellSize / 4f);
|
paint.setStrokeWidth(cellSize / 4f);
|
||||||
paint.setStyle(Paint.Style.STROKE);
|
paint.setStyle(Paint.Style.STROKE);
|
||||||
for (Map.Entry<Integer, List<int[]>> entry : paths.entrySet()) {
|
for (Map.Entry<Integer, List<int[]>> entry : paths.entrySet()) {
|
||||||
paint.setColor(getColorForId(Math.abs(entry.getKey())));
|
paint.setColor(getColorForId(Math.abs(entry.getKey()),isAchromate));
|
||||||
List<int[]> path = entry.getValue();
|
List<int[]> path = entry.getValue();
|
||||||
for (int i = 0; i < path.size() - 1; i++) {
|
for (int i = 0; i < path.size() - 1; i++) {
|
||||||
int[] p1 = path.get(i);
|
int[] p1 = path.get(i);
|
||||||
@@ -227,7 +229,6 @@ private void printBoardToLog() {
|
|||||||
|
|
||||||
// 2. Vérifie si toutes les paires sont terminées
|
// 2. Vérifie si toutes les paires sont terminées
|
||||||
if (completedColors.size() == puzzle.getPairs().size()) {
|
if (completedColors.size() == puzzle.getPairs().size()) {
|
||||||
Log.d("DEBUG_FLOW", "🎉 Puzzle résolu !");
|
|
||||||
win = true;
|
win = true;
|
||||||
invalidate(); // Redessine pour afficher le message
|
invalidate(); // Redessine pour afficher le message
|
||||||
}
|
}
|
||||||
@@ -239,28 +240,18 @@ private void printBoardToLog() {
|
|||||||
public boolean onTouchEvent(MotionEvent event) {
|
public boolean onTouchEvent(MotionEvent event) {
|
||||||
int col = (int) (event.getX() / cellSize);
|
int col = (int) (event.getX() / cellSize);
|
||||||
int row = (int) (event.getY() / cellSize);
|
int row = (int) (event.getY() / cellSize);
|
||||||
|
|
||||||
if (col < 0 || row < 0 || col >= puzzle.getSize() || row >= puzzle.getSize())
|
if (col < 0 || row < 0 || col >= puzzle.getSize() || row >= puzzle.getSize())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
switch (event.getAction()) {
|
switch (event.getAction()) {
|
||||||
case MotionEvent.ACTION_DOWN:
|
case MotionEvent.ACTION_DOWN:
|
||||||
|
|
||||||
int colorAtCell = board[row][col];
|
int colorAtCell = board[row][col];
|
||||||
if (colorAtCell > 0) {
|
if (colorAtCell > 0) {
|
||||||
|
|
||||||
//test
|
|
||||||
clearPath(colorAtCell); // 👈 même fonction de nettoyage
|
clearPath(colorAtCell); // 👈 même fonction de nettoyage
|
||||||
selectedColor = colorAtCell;
|
selectedColor = colorAtCell;
|
||||||
paths.put(selectedColor, new ArrayList<>());
|
paths.put(selectedColor, new ArrayList<>());
|
||||||
paths.get(selectedColor).add(new int[]{row, col});
|
paths.get(selectedColor).add(new int[]{row, col});
|
||||||
//test
|
|
||||||
|
|
||||||
if (completedColors.contains(colorAtCell)) {// regarde si le chemin qu'on veut tracer est fini
|
if (completedColors.contains(colorAtCell)) {// regarde si le chemin qu'on veut tracer est fini
|
||||||
// 🔒 Ne pas toucher à un chemin terminé
|
// 🔒 Ne pas toucher à un chemin terminé
|
||||||
Log.d("DEBUG_FLOW", "Chemin déjà terminé, on ne fait rien.");
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
selectedColor = colorAtCell;
|
selectedColor = colorAtCell;
|
||||||
@@ -275,24 +266,6 @@ private void printBoardToLog() {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* case MotionEvent.ACTION_MOVE:
|
|
||||||
if (selectedColor != 0) {
|
|
||||||
List<int[]> path = paths.get(selectedColor);
|
|
||||||
int[] last = path.get(path.size() - 1);
|
|
||||||
if (last[0] != row || last[1] != col) {
|
|
||||||
path.add(new int[]{row, col});
|
|
||||||
board[row][col] = selectedColor; // ✅ Met à jour le tableau
|
|
||||||
|
|
||||||
printBoardToLog(); // 🔍 Affiche la grille pendant le tracé
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
break;*/
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//version chat
|
|
||||||
case MotionEvent.ACTION_MOVE:
|
case MotionEvent.ACTION_MOVE:
|
||||||
if (selectedColor != 0) {
|
if (selectedColor != 0) {
|
||||||
List<int[]> path = paths.get(selectedColor);
|
List<int[]> path = paths.get(selectedColor);
|
||||||
@@ -317,7 +290,6 @@ private void printBoardToLog() {
|
|||||||
// ⚠️ Ne pas revenir au point de départ
|
// ⚠️ Ne pas revenir au point de départ
|
||||||
int[] start = path.get(0);
|
int[] start = path.get(0);
|
||||||
if (row == start[0] && col == start[1]) {
|
if (row == start[0] && col == start[1]) {
|
||||||
Log.d("DEBUG_FLOW", "Tentative de retour au point de départ — refusé.");
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -327,7 +299,6 @@ private void printBoardToLog() {
|
|||||||
selectedColor = 0;
|
selectedColor = 0;
|
||||||
checkWin();
|
checkWin();
|
||||||
invalidate();
|
invalidate();
|
||||||
Log.d("DEBUG_FLOW", "Paire complétée !");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3️⃣ Reculer sur une case du chemin
|
// 3️⃣ Reculer sur une case du chemin
|
||||||
@@ -338,19 +309,12 @@ private void printBoardToLog() {
|
|||||||
int[] removed = path.remove(path.size() - 1);
|
int[] removed = path.remove(path.size() - 1);
|
||||||
board[removed[0]][removed[1]] = 0;
|
board[removed[0]][removed[1]] = 0;
|
||||||
invalidate();
|
invalidate();
|
||||||
Log.d("DEBUG_FLOW", "Recul d’une case sur le chemin.");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
||||||
//version chat
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
case MotionEvent.ACTION_UP:
|
case MotionEvent.ACTION_UP:
|
||||||
selectedColor = 0; // Permet de reprendre à la prochaine touche
|
selectedColor = 0; // Permet de reprendre à la prochaine touche
|
||||||
break;
|
break;
|
||||||
@@ -361,12 +325,23 @@ private void printBoardToLog() {
|
|||||||
|
|
||||||
|
|
||||||
// Attribue une couleur vive et contrastée à chaque identifiant de point
|
// Attribue une couleur vive et contrastée à chaque identifiant de point
|
||||||
private int getColorForId(int id) {
|
private int getColorForId(int id,boolean achromate) {
|
||||||
if (!colorMap.containsKey(id)) {
|
|
||||||
int index = colorMap.size() % DISTINCT_COLORS.length;
|
|
||||||
colorMap.put(id, DISTINCT_COLORS[index]);
|
if(achromate) {
|
||||||
|
if (!colorMap.containsKey(id)) {
|
||||||
|
int index = colorMap.size() % DISTINCT_GRIS.length;
|
||||||
|
colorMap.put(id, DISTINCT_GRIS[index]);
|
||||||
|
}
|
||||||
|
return colorMap.get(id);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (!colorMap.containsKey(id)) {
|
||||||
|
int index = colorMap.size() % DISTINCT_COLORS.length;
|
||||||
|
colorMap.put(id, DISTINCT_COLORS[index]);
|
||||||
|
}
|
||||||
|
return colorMap.get(id);
|
||||||
}
|
}
|
||||||
return colorMap.get(id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -46,16 +46,8 @@ public class MainActivity extends Activity {
|
|||||||
puzzleListView.setAdapter(adapter);
|
puzzleListView.setAdapter(adapter);
|
||||||
|
|
||||||
// Click sur un puzzle
|
// Click sur un puzzle
|
||||||
puzzleListView.setOnItemClickListener((parent, view, position, id) -> {
|
// Utilisation de la nouvelle classe pour la gestion des clics sur la liste des puzzles
|
||||||
String selectedPuzzle = puzzleList.get(position);
|
puzzleListView.setOnItemClickListener(new PuzzleClickListener(this, puzzleList));
|
||||||
if (!selectedPuzzle.endsWith(".xml")) {
|
|
||||||
Toast.makeText(this, "Puzzle invalide", Toast.LENGTH_SHORT).show();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Intent intent = new Intent(MainActivity.this, GameActivity.class);
|
|
||||||
intent.putExtra("PUZZLE_FILE", selectedPuzzle);
|
|
||||||
startActivity(intent);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Bouton paramètres
|
// Bouton paramètres
|
||||||
settingsButton.setOnClickListener(new listener(this,SettingsActivity.class));
|
settingsButton.setOnClickListener(new listener(this,SettingsActivity.class));
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
package com.example.flow_free;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.view.View;
|
||||||
|
import android.widget.AdapterView;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class PuzzleClickListener implements AdapterView.OnItemClickListener {
|
||||||
|
|
||||||
|
private final Context context;
|
||||||
|
private final List<String> puzzleList;
|
||||||
|
|
||||||
|
public PuzzleClickListener(Context context, List<String> puzzleList) {
|
||||||
|
this.context = context;
|
||||||
|
this.puzzleList = puzzleList;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
||||||
|
String selectedPuzzle = puzzleList.get(position);
|
||||||
|
|
||||||
|
if (!selectedPuzzle.endsWith(".xml")) {
|
||||||
|
Toast.makeText(context, "Puzzle invalide", Toast.LENGTH_SHORT).show();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Intent intent = new Intent(context, GameActivity.class);
|
||||||
|
intent.putExtra("PUZZLE_FILE", selectedPuzzle);
|
||||||
|
context.startActivity(intent);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user