From 044674afc71600308e50836d13af56ca40cc6699 Mon Sep 17 00:00:00 2001 From: Maxime Date: Sun, 30 Mar 2025 21:46:34 +0200 Subject: [PATCH] projet fini achromate marche yesssssssssssss --- .../com/example/flow_free/FlowFreeView.java | 113 +++++++----------- .../com/example/flow_free/MainActivity.java | 12 +- .../flow_free/PuzzleClickListener.java | 34 ++++++ 3 files changed, 80 insertions(+), 79 deletions(-) create mode 100644 test_sae_contenue_juste_le_main/src/main/java/com/example/flow_free/PuzzleClickListener.java diff --git a/test_sae_contenue_juste_le_main/src/main/java/com/example/flow_free/FlowFreeView.java b/test_sae_contenue_juste_le_main/src/main/java/com/example/flow_free/FlowFreeView.java index dc76791..0bce640 100644 --- a/test_sae_contenue_juste_le_main/src/main/java/com/example/flow_free/FlowFreeView.java +++ b/test_sae_contenue_juste_le_main/src/main/java/com/example/flow_free/FlowFreeView.java @@ -1,10 +1,12 @@ package com.example.flow_free; import android.content.Context; +import android.content.SharedPreferences; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; +import android.preference.PreferenceManager; import android.util.Log; import android.view.MotionEvent; import android.view.View; @@ -22,9 +24,10 @@ public class FlowFreeView extends View { private final int[][] board; private final Map> paths = new HashMap<>(); - private final Map colorMap = new HashMap<>();//test pour la couleur + private final Map colorMap = new HashMap<>(); private final Set completedColors = new HashSet<>();// liste des chemin fini private boolean win = false; + private boolean isAchromate ; private static final int[] DISTINCT_COLORS = { @@ -35,7 +38,6 @@ public class FlowFreeView extends View { Color.rgb(255, 0, 255), // Fuchsia / Magenta Color.rgb(0, 255, 255), // Cyan Color.rgb(255, 165, 0), // Orange vif - Color.rgb(128, 0, 0), // Bordeaux Color.rgb(0, 128, 0), // Vert 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 selectedColor = 0; private int cellSize; - public FlowFreeView(Context context, Puzzle puzzle) { super(context); this.puzzle = puzzle; @@ -66,7 +85,6 @@ public class FlowFreeView extends View { int row1 = pair[1]; int col2 = pair[2]; int row2 = pair[3]; - // Crée une couleur id unique pour chaque paire (1, 2, 3, ...) colorId = colorId+ 1; board[row1][col1] = colorId; @@ -74,10 +92,7 @@ public class FlowFreeView extends View { } } - - -//test -private void printBoardToLog() { + private void printBoardToLog() { StringBuilder sb = new StringBuilder(); for (int[] row : board) { for (int cell : row) { @@ -85,9 +100,7 @@ private void printBoardToLog() { } sb.append("\n"); } - Log.d("DEBUG_FLOW", "Contenu du board :\n" + sb.toString()); } - //test @Override//test pour calculer cellsize @@ -95,9 +108,8 @@ private void printBoardToLog() { super.onSizeChanged(w, h, oldw, oldh); if (puzzle != null) { 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 protected void onDraw(Canvas canvas) { super.onDraw(canvas); - Log.d("DEBUG_FLOW", "onDraw() a été appelé !");//debug - // --- DEBUG : Affiche le contenu du board dans la console --- - 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()); - - + SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext()); + isAchromate = preferences.getBoolean("achromate_mode", false); paint.setStrokeWidth(4); paint.setStyle(Paint.Style.STROKE); paint.setColor(Color.BLACK); @@ -135,7 +137,7 @@ private void printBoardToLog() { for (int col = 0; col < puzzle.getSize(); col++) { int colorId = board[row][col]; 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 cy = row * cellSize + cellSize / 2f; canvas.drawCircle(cx, cy, cellSize / 4f, paint); @@ -147,7 +149,7 @@ private void printBoardToLog() { paint.setStrokeWidth(cellSize / 4f); paint.setStyle(Paint.Style.STROKE); for (Map.Entry> entry : paths.entrySet()) { - paint.setColor(getColorForId(Math.abs(entry.getKey()))); + paint.setColor(getColorForId(Math.abs(entry.getKey()),isAchromate)); List path = entry.getValue(); for (int i = 0; i < path.size() - 1; i++) { int[] p1 = path.get(i); @@ -227,7 +229,6 @@ private void printBoardToLog() { // 2. Vérifie si toutes les paires sont terminées if (completedColors.size() == puzzle.getPairs().size()) { - Log.d("DEBUG_FLOW", "🎉 Puzzle résolu !"); win = true; invalidate(); // Redessine pour afficher le message } @@ -239,28 +240,18 @@ private void printBoardToLog() { public boolean onTouchEvent(MotionEvent event) { int col = (int) (event.getX() / cellSize); int row = (int) (event.getY() / cellSize); - if (col < 0 || row < 0 || col >= puzzle.getSize() || row >= puzzle.getSize()) return false; - - - switch (event.getAction()) { case MotionEvent.ACTION_DOWN: - int colorAtCell = board[row][col]; if (colorAtCell > 0) { - - //test clearPath(colorAtCell); // 👈 même fonction de nettoyage selectedColor = colorAtCell; paths.put(selectedColor, new ArrayList<>()); paths.get(selectedColor).add(new int[]{row, col}); - //test - if (completedColors.contains(colorAtCell)) {// regarde si le chemin qu'on veut tracer est fini // 🔒 Ne pas toucher à un chemin terminé - Log.d("DEBUG_FLOW", "Chemin déjà terminé, on ne fait rien."); return true; } selectedColor = colorAtCell; @@ -275,24 +266,6 @@ private void printBoardToLog() { } break; - /* case MotionEvent.ACTION_MOVE: - if (selectedColor != 0) { - List 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: if (selectedColor != 0) { List path = paths.get(selectedColor); @@ -317,7 +290,6 @@ private void printBoardToLog() { // ⚠️ Ne pas revenir au point de départ int[] start = path.get(0); if (row == start[0] && col == start[1]) { - Log.d("DEBUG_FLOW", "Tentative de retour au point de départ — refusé."); break; } @@ -327,7 +299,6 @@ private void printBoardToLog() { selectedColor = 0; checkWin(); invalidate(); - Log.d("DEBUG_FLOW", "Paire complétée !"); } // 3️⃣ Reculer sur une case du chemin @@ -338,19 +309,12 @@ private void printBoardToLog() { int[] removed = path.remove(path.size() - 1); board[removed[0]][removed[1]] = 0; invalidate(); - Log.d("DEBUG_FLOW", "Recul d’une case sur le chemin."); } } } } break; - - //version chat - - - - case MotionEvent.ACTION_UP: selectedColor = 0; // Permet de reprendre à la prochaine touche break; @@ -361,12 +325,23 @@ private void printBoardToLog() { // Attribue une couleur vive et contrastée à chaque identifiant de point - private int getColorForId(int id) { - if (!colorMap.containsKey(id)) { - int index = colorMap.size() % DISTINCT_COLORS.length; - colorMap.put(id, DISTINCT_COLORS[index]); + private int getColorForId(int id,boolean achromate) { + + + 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); } diff --git a/test_sae_contenue_juste_le_main/src/main/java/com/example/flow_free/MainActivity.java b/test_sae_contenue_juste_le_main/src/main/java/com/example/flow_free/MainActivity.java index 22b6d30..94514c7 100644 --- a/test_sae_contenue_juste_le_main/src/main/java/com/example/flow_free/MainActivity.java +++ b/test_sae_contenue_juste_le_main/src/main/java/com/example/flow_free/MainActivity.java @@ -46,16 +46,8 @@ public class MainActivity extends Activity { puzzleListView.setAdapter(adapter); // Click sur un puzzle - puzzleListView.setOnItemClickListener((parent, view, position, id) -> { - String selectedPuzzle = puzzleList.get(position); - 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); - }); + // Utilisation de la nouvelle classe pour la gestion des clics sur la liste des puzzles + puzzleListView.setOnItemClickListener(new PuzzleClickListener(this, puzzleList)); // Bouton paramètres settingsButton.setOnClickListener(new listener(this,SettingsActivity.class)); diff --git a/test_sae_contenue_juste_le_main/src/main/java/com/example/flow_free/PuzzleClickListener.java b/test_sae_contenue_juste_le_main/src/main/java/com/example/flow_free/PuzzleClickListener.java new file mode 100644 index 0000000..236a9af --- /dev/null +++ b/test_sae_contenue_juste_le_main/src/main/java/com/example/flow_free/PuzzleClickListener.java @@ -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 puzzleList; + + public PuzzleClickListener(Context context, List 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); + } +} \ No newline at end of file