import java.util.*;

public class Main {
    static final int LARGEUR = 15;
    static final int HAUTEUR = 10;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        LinkedList<int[]> serpent = new LinkedList<>();
        serpent.add(new int[]{HAUTEUR/2, LARGEUR/2});

        int dirL = 0, dirC = 1; // direction initiale : droite
        Random rand = new Random();
        int[] pomme = placerPomme(serpent, rand);
        int score = 0;
        boolean fini = false;

        System.out.println("=== SNAKE ===");
        System.out.println("Commandes : z=haut  s=bas  q=gauche  d=droite  puis Entrée");

        while (!fini) {
            afficherPlateau(serpent, pomme);
            System.out.print("Score: " + score + " | Direction (z/q/s/d) : ");
            String saisie = sc.nextLine().trim().toLowerCase();

            if (!saisie.isEmpty()) {
                char c = saisie.charAt(0);
                if (c == 'z' && dirL != 1) { dirL = -1; dirC = 0; }
                else if (c == 's' && dirL != -1) { dirL = 1; dirC = 0; }
                else if (c == 'q' && dirC != 1) { dirL = 0; dirC = -1; }
                else if (c == 'd' && dirC != -1) { dirL = 0; dirC = 1; }
            }

            int[] tete = serpent.getFirst();
            int nouvL = tete[0] + dirL;
            int nouvC = tete[1] + dirC;

            // Collision mur
            if (nouvL < 0 || nouvL >= HAUTEUR || nouvC < 0 || nouvC >= LARGEUR) {
                fini = true;
                break;
            }

            // Collision avec soi-même
            for (int[] partie : serpent) {
                if (partie[0] == nouvL && partie[1] == nouvC) {
                    fini = true;
                    break;
                }
            }
            if (fini) break;

            int[] nouvelleTete = new int[]{nouvL, nouvC};
            serpent.addFirst(nouvelleTete);

            if (nouvL == pomme[0] && nouvC == pomme[1]) {
                score += 10;
                pomme = placerPomme(serpent, rand);
            } else {
                serpent.removeLast();
            }
        }

        System.out.println("\n💀 GAME OVER ! Score final : " + score);
        sc.close();
    }

    static void afficherPlateau(LinkedList<int[]> serpent, int[] pomme) {
        System.out.println();
        char[][] grille = new char[HAUTEUR][LARGEUR];
        for (char[] ligne : grille) Arrays.fill(ligne, '.');

        for (int[] partie : serpent) grille[partie[0]][partie[1]] = 'o';
        grille[serpent.getFirst()[0]][serpent.getFirst()[1]] = 'O';
        grille[pomme[0]][pomme[1]] = '*';

        StringBuilder sb = new StringBuilder();
        for (char[] ligne : grille) {
            for (char c : ligne) sb.append(c).append(' ');
            sb.append('\n');
        }
        System.out.print(sb);
    }

    static int[] placerPomme(LinkedList<int[]> serpent, Random rand) {
        int l, c;
        boolean surSerpent;
        do {
            l = rand.nextInt(HAUTEUR);
            c = rand.nextInt(LARGEUR);
            surSerpent = false;
            for (int[] partie : serpent) {
                if (partie[0] == l && partie[1] == c) { surSerpent = true; break; }
            }
        } while (surSerpent);
        return new int[]{l, c};
    }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: