Modification de la gestion de la grille

This commit is contained in:
Lyanis SOUIDI 2023-04-27 17:57:02 +02:00
parent a123c1f8e6
commit e9651316bf
Signed by: Lyanis SOUIDI
GPG Key ID: 251ADD56CFE6A854
17 changed files with 504 additions and 454 deletions

30
.gitignore vendored
View File

@ -1,32 +1,2 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
### Java ###
*.class

View File

@ -1,55 +0,0 @@
import javax.swing.*;
import java.awt.*;
public class Accueil extends JPanel {
public static void main(String[] args) {
// Appel de la méthode fenetreAccueil pour créer la fenêtre d'accueil
fenetreAccueil();
}
public static void fenetreAccueil() {
// Création de la fenêtre d'accueil
Fenetre fenetre = new Fenetre();
// Création de l'accueil
Accueil accueil = new Accueil();
fenetre.setContentPane(accueil);
// Création du texte
JLabel texte = new JLabel("Bon jeu !", SwingConstants.CENTER);
texte.setPreferredSize(new Dimension(500, 50));
texte.setFont(new Font("Arial", Font.BOLD, 30));
texte.setForeground(new Color(0, 0, 0));
// Création des boutons
Boutons boutons = new Boutons(fenetre);
//Récupération des boutons créés dans la classe Boutons
JButton choisirGrille = boutons.getChoisirGrille();
JButton importerGrille = boutons.getImporterGrille();
// Création du panel pour le texte
JPanel panelTexte = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
panelTexte.add(texte);
// Création du panel pour les boutons
JPanel panelBoutons = new JPanel(new FlowLayout(FlowLayout.CENTER, 100, 200));
panelBoutons.add(choisirGrille);
panelBoutons.add(importerGrille);
panelBoutons.setOpaque(false);
// Ajout des composants au JPanel principal
accueil.setLayout(new BorderLayout());
accueil.add(panelTexte, BorderLayout.NORTH);
accueil.add(panelBoutons, BorderLayout.CENTER);
fenetre.setVisible(true);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(new ImageIcon("background.jpg").getImage(), 0, 0, getWidth(), getHeight(), null);
}
}

View File

@ -3,91 +3,83 @@ import javax.swing.*;
import java.io.File;
public class Boutons extends JPanel {
private JButton choisirGrille;
private JButton importerGrille;
private JFrame fenetrePrincipale;
/*private int tailleGrille; // variable pour stocker la taille de la grille choisie par l'utilisateur */
public Boutons(JFrame fenetrePrincipale) {
// Création des boutons
choisirGrille = new JButton("Générer une grille");
public static JButton ChoisirGrille(JFrame window) {
JPanel panel = new JPanel();
JButton choisirGrille = new JButton("Générer une grille");
choisirGrille.setPreferredSize(new Dimension(250, 50));
choisirGrille.setFont(new Font("Arial", Font.BOLD, 20));
choisirGrille.setBackground(Color.GRAY);
importerGrille = new JButton("Importer une grille");
importerGrille.setPreferredSize(new Dimension(250, 50));
importerGrille.setFont(new Font("Arial", Font.BOLD, 20));
importerGrille.setBackground(Color.GRAY);
this.fenetrePrincipale = fenetrePrincipale;
// Ajout des listeners aux boutons + gestion des erreurs
choisirGrille.addActionListener(e -> {
String strTaille = JOptionPane.showInputDialog(this, "Entrez la taille de la grille :", "Taille de la grille", JOptionPane.PLAIN_MESSAGE);
String strTaille = JOptionPane.showInputDialog(panel, "Entrez la taille de la grille :", "Taille de la grille", JOptionPane.PLAIN_MESSAGE);
if (strTaille != null && !strTaille.isEmpty()) {
if (!Character.isDigit(strTaille.charAt(0))) {
JOptionPane.showMessageDialog(this, "Le premier caractère doit être un chiffre ou nombre.", "Erreur", JOptionPane.ERROR_MESSAGE);
JOptionPane.showMessageDialog(panel, "Le premier caractère doit être un chiffre ou nombre.", "Erreur", JOptionPane.ERROR_MESSAGE);
return;
}
try {
int taille = Integer.parseInt(strTaille);
if (taille > 3 && taille < 21) {
// stocker la taille de la grille choisie
/*this.tailleGrille = taille;*/
// afficher la taille choisir dans la console
System.out.println("Les dimensions de la grille : " + taille + "x" + taille);
// Afficher la boîte de dialogue pour le choix du remplissage de la grille
String[] options = {"Remplir aléatoirement", "Partir d'une grille vide"};
int choix = JOptionPane.showOptionDialog(this, "Choisissez comment remplir la grille :", "Remplissage de la grille", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
switch (choix) {
if (taille > 3 && taille < 21) {
// stocker la taille de la grille choisie
/*this.tailleGrille = taille;*/
// afficher la taille choisir dans la console
System.out.println("Les dimensions de la grille : " + taille + "x" + taille);
// Afficher la boîte de dialogue pour le choix du remplissage de la grille
String[] options = {"Remplir aléatoirement", "Partir d'une grille vide"};
int choix = JOptionPane.showOptionDialog(panel, "Choisissez comment remplir la grille :", "Remplissage de la grille", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
GridController grille = new GridController(new Grid(taille), new GridView());
switch (choix) {
case 0:
// afficher la grille aléatoirement
GridPanel grille = new GridPanel(taille);
this.fenetrePrincipale.add(grille);
this.fenetrePrincipale.validate();
grille.random();
window.add(grille.getView());
window.validate();
break;
case 1:
// TODO: partir d'une grille vide
window.add(grille.getView());
window.validate();
break;
default:
// gérer le cas aucun choix n'a été fait
JOptionPane.showMessageDialog(this, "Aucun choix n'a été fait.", "Attention", JOptionPane.WARNING_MESSAGE);
JOptionPane.showMessageDialog(panel, "Aucun choix n'a été fait.", "Attention", JOptionPane.WARNING_MESSAGE);
return;
}
} else {
String errorMessage = "La taille doit être au moins de 4.";
if (taille >= 21) {
errorMessage = "La taille ne doit pas dépasser 20.";
}
JOptionPane.showMessageDialog(this, errorMessage, "Erreur", JOptionPane.ERROR_MESSAGE);
JOptionPane.showMessageDialog(panel, errorMessage, "Erreur", JOptionPane.ERROR_MESSAGE);
}
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "Tapez " + strTaille.charAt(0) + " pour une grille " + strTaille.charAt(0) +"x"+ strTaille.charAt(0) +".", "Erreur", JOptionPane.ERROR_MESSAGE);
JOptionPane.showMessageDialog(panel, "Tapez " + strTaille.charAt(0) + " pour une grille " + strTaille.charAt(0) +"x"+ strTaille.charAt(0) +".", "Erreur", JOptionPane.ERROR_MESSAGE);
}
}
});
return choisirGrille;
}
public static JButton ImporterGrille(JFrame window) {
JPanel panel = new JPanel();
JButton importerGrille = new JButton("Importer une grille");
importerGrille.setPreferredSize(new Dimension(250, 50));
importerGrille.setFont(new Font("Arial", Font.BOLD, 20));
importerGrille.setBackground(Color.GRAY);
importerGrille.addActionListener(e -> {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Selectionnez le fichier ou se trouve votre grille");
int choix = fileChooser.showOpenDialog(this);
int choix = fileChooser.showOpenDialog(panel);
if (choix == JFileChooser.APPROVE_OPTION) {
File fichier = fileChooser.getSelectedFile();
// TODO: charger la grille depuis le fichier
}
});
}
public JButton getChoisirGrille() {
return choisirGrille;
}
public JButton getImporterGrille() {
return importerGrille;
}
}

View File

@ -1,14 +0,0 @@
import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.Toolkit;
public class Fenetre extends JFrame {
public Fenetre() {
super("Accueil");
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension d = new Dimension(screenSize.width, screenSize.height);
this.setSize(d);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

View File

@ -1,43 +1,41 @@
import java.util.Arrays;
public class Grid {
private Square[][] squares;
private int squareSize;
private Thesee thesee = new Thesee();
public class Grid {
private Square[][] grid;
public Grid(int taille) {
this.grid = new Square[taille][taille];
for (int i = 0; i < taille; i++) {
for (int j = 0; j < taille; j++) {
this.grid[i][j] = new Square(i, j);
public Grid(int size) {
this.squares = new Square[size][size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
this.squares[i][j] = new Square(this, i, j);
}
}
}
public void setWall(int x, int y) throws ArrayIndexOutOfBoundsException {
this.grid[x][y].setWall();
/**
* Get the grid's size (number of squares on a row/column)
* @return The grid's size
*/
public int getSize() {
return this.squares.length;
}
public void setExit(int x, int y) throws ArrayIndexOutOfBoundsException {
for (Square[] line : this.grid) {
for (Square square : line) {
if (square.isExit()) square.setEmpty();
}
/**
* Get a square from the grid
* @param row The row of the square
* @param column The column of the square
* @return The square at the given position
* @throws Exception If the position is out of grid's bounds
*/
public Square getSquare(int row, int column) throws Exception {
try {
return this.squares[row][column];
} catch (ArrayIndexOutOfBoundsException e) {
throw new Exception("No square found at position (" + row + ", " + column + ")");
}
this.grid[x][y].setExit();
}
public int getRows() {
return this.grid.length;
}
public int getColumns() {
return this.grid[0].length;
}
public boolean isWall(int x, int y) {
return this.grid[x][y].isWall();
}
public boolean isExit(int x, int y) {
return this.grid[x][y].isEmpty();
public Thesee getThesee() {
return this.thesee;
}
}

View File

@ -1,29 +1,130 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
public class GridController {
private Grid model;
private GridView view;
private enum Mode { DISABLED, WALL, THESEE, EXIT }
private Mode editMode = Mode.DISABLED;
private JButton editTheseeButton;
private JButton editExitButton;
private JButton editWallButton;
public GridController(Grid model, GridView view) {
this.model = model;
this.view = view;
this.view.setGrid(this.model);
TheseeController theseeController = new TheseeController(this.model.getThesee(), this.view);
this.view.setPreferredSize(new Dimension(700, 500));
this.view.setBackground(Color.WHITE);
this.view.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
Square square = view.click(e);
if (square != null) {
if (editMode == Mode.WALL) {
if (square.isWall()) {
square.setEmpty();
} else {
square.setWall();
}
view.repaint();
} else if (editMode == Mode.THESEE) {
model.getThesee().setSquare(square);
view.repaint();
} else if (editMode == Mode.EXIT) {
square.setExit();
view.repaint();
}
}
}
});
editWallButton = new JButton("Enlever/Ajouter Murs");
editWallButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (editMode == Mode.DISABLED) {
editMode = Mode.WALL;
editWallButton.setText("Mode Dessin");
editTheseeButton.setEnabled(false);
editExitButton.setEnabled(false);
} else {
editMode = Mode.DISABLED;
editWallButton.setText("Enlever/Ajouter Murs");
editTheseeButton.setEnabled(true);
editExitButton.setEnabled(true);
}
}
});
this.view.add(editWallButton);
editTheseeButton = new JButton("Placer Joueur");
editTheseeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (editMode == Mode.DISABLED) {
editMode = Mode.THESEE;
editTheseeButton.setText("Mode Dessin");
editWallButton.setEnabled(false);
editExitButton.setEnabled(false);
} else {
editMode = Mode.DISABLED;
editTheseeButton.setText("Placer Joueur");
editWallButton.setEnabled(true);
editExitButton.setEnabled(true);
}
}
});
this.view.add(editTheseeButton);
editExitButton = new JButton("Placer Sortie");
editExitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (editMode == Mode.DISABLED) {
editMode = Mode.EXIT;
editExitButton.setText("Mode Dessin");
editWallButton.setEnabled(false);
editTheseeButton.setEnabled(false);
} else {
editMode = Mode.DISABLED;
editExitButton.setText("Placer Sortie");
editWallButton.setEnabled(true);
editTheseeButton.setEnabled(true);
}
}
});
this.view.add(editExitButton);
}
public void setGridExit(int x, int y) {
this.model.setExit(x, y);
/**
* Randomly fills the grid with walls, an exit and Thésée
*/
public void random() {
try {
Random rand = new Random();
for (int i = 0; i < this.model.getSize(); i++) {
for (int j = 0; j < this.model.getSize(); j++) {
if (rand.nextInt(3) == 0) this.model.getSquare(i, j).setWall();
}
}
this.model.getSquare(rand.nextInt(this.model.getSize()), rand.nextInt(this.model.getSize())).setExit();
this.model.getThesee().setSquare(this.model.getSquare(rand.nextInt(this.model.getSize()), rand.nextInt(this.model.getSize())));
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public void setGridWall(int x, int y) {
this.model.setWall(x, y);
}
public boolean isGridWall(int x, int y) {
return this.model.isWall(x, y);
}
public boolean isGridExit(int x, int y) {
return this.model.isExit(x, y);
}
public void updateView() {
this.view.printGridInfo(this.model.getRows(), this.model.getColumns());
public GridView getView() {
return this.view;
}
}

View File

@ -1,149 +0,0 @@
import javax.swing.*;
import java.awt.*;
import java.util.Random;
import java.awt.event.*;
public class GridPanel extends JPanel {
private int gridSize;
private int cellSize = 50;
private Color[][] gridColors;
private boolean editMode = false;
private boolean placeJoueurMode = false;
private boolean placeTMode = false;
private Point joueurPos = null;
private Point tPos = null;
private JButton placeJoueurButton;
private JButton placeTButton;
private JButton editButton;
public GridPanel(int gs) {
this.gridSize = gs;
setPreferredSize(new Dimension(gridSize * cellSize, gridSize * cellSize));
setBackground(Color.WHITE);
gridColors = new Color[gridSize][gridSize];
Random rand = new Random();
for (int i = 0; i < gridSize; i++) {
for (int j = 0; j < gridSize; j++) {
gridColors[i][j] = rand.nextBoolean() ? Color.BLACK : Color.WHITE;
}
}
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int row = e.getY() / cellSize;
int col = e.getX() / cellSize;
if (editMode) {
gridColors[row][col] = gridColors[row][col] == Color.BLACK ? Color.WHITE : Color.BLACK;
repaint();
} else if (placeJoueurMode) {
joueurPos = new Point(col, row);
repaint();
} else if (placeTMode) {
tPos = new Point(col, row);
repaint();
}
}
});
editButton = new JButton("Enlever/Ajouter Murs");
editButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
editMode = !editMode;
if (editMode) {
editButton.setText("Mode Editeur");
placeJoueurButton.setEnabled(false);
placeTButton.setEnabled(false);
} else {
editButton.setText("Enlever/Ajouter Murs");
placeJoueurButton.setEnabled(true);
placeTButton.setEnabled(true);
}
}
});
add(editButton);
placeJoueurButton = new JButton("Placer Thésee");
placeJoueurButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
placeJoueurMode = !placeJoueurMode;
if (placeJoueurMode) {
placeJoueurButton.setText("Mode Editeur");
editButton.setEnabled(false);
placeTButton.setEnabled(false);
} else {
placeJoueurButton.setText("Placer Thésée");
editButton.setEnabled(true);
placeTButton.setEnabled(true);
}
}
});
add(placeJoueurButton);
placeTButton = new JButton("Placer Sortie");
placeTButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
placeTMode = !placeTMode;
if (placeTMode) {
placeTButton.setText("Mode Editeur");
editButton.setEnabled(false);
placeJoueurButton.setEnabled(false);
} else {
placeTButton.setText("Placer Sortie");
editButton.setEnabled(true);
placeJoueurButton.setEnabled(true);
}
}
});
add(placeTButton);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
// Draw grid cells
for (int i = 0; i < gridSize; i++) {
for (int j = 0; j < gridSize; j++) {
g.setColor(gridColors[i][j]);
g.fillRect(j * cellSize, i * cellSize, cellSize, cellSize);
}
}
// Draw horizontal lines
g.setColor(Color.BLACK);
for (int i = 1; i < gridSize; i++) {
int y = i * cellSize;
g.drawLine(0, y, getWidth(), y);
}
// Draw vertical lines
for (int j = 1; j < gridSize; j++) {
int x = j * cellSize;
g.drawLine(x, 0, x, getHeight());
}
// Draw player and T
if (joueurPos != null){
g.setColor(Color.BLUE);
g.fillOval(joueurPos.x * cellSize, joueurPos.y * cellSize, cellSize, cellSize);
}
if (tPos != null) {
g.setColor(Color.RED);
g.fillRect(tPos.x * cellSize, tPos.y * cellSize, cellSize, cellSize);
}
}
/*public static void main(String[] args) {
JFrame frame = new JFrame("GridPanel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout());
GridPanel gridPanel = new GridPanel();
frame.getContentPane().add(gridPanel, BorderLayout.CENTER);
frame.getContentPane().add(gridPanel.getComponents()[0], BorderLayout.NORTH);
frame.pack();
frame.setVisible(true);
}*/
}

View File

@ -1,5 +1,83 @@
public class GridView {
public void printGridInfo(int rows, int columns) {
System.out.println("Grid size: " + rows + "x" + columns);
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
public class GridView extends JPanel {
private Grid model;
private int gridSize;
private int gridStartX;
private int gridStartY;
private int squareSize;
private Font font;
private final String exit = "";
private final String thesee = "Θ";
/**
* Manages the display of the grid
*/
public GridView() {
super();
}
public void setGrid(Grid model) {
this.model = model;
}
public Square click(MouseEvent e) {
// TODO: Détection du clic sur une case
return null;
}
private void calculateProportions() {
this.gridSize = Math.min((getHeight() - 50), getWidth()) - 10;
this.gridStartX = (getWidth() - this.gridSize) / 2;
this.gridStartY = (((getHeight() - 50) - this.gridSize) / 2) + 50;
this.squareSize = this.gridSize / this.model.getSize();
this.font = new Font("Arial", Font.PLAIN, (int) (this.squareSize * 0.75));
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (this.model != null) {
calculateProportions();
// Draw squares
for (int i = 0; i < this.model.getSize(); i++) {
for (int j = 0; j < this.model.getSize(); j++) {
try {
if (this.model.getSquare(i, j).isWall()) g.setColor(Color.BLACK);
else g.setColor(Color.WHITE);
g.fillRect(this.gridStartX + (this.squareSize * j), this.gridStartY + (this.squareSize * i), this.squareSize, this.squareSize);
g.setColor(Color.BLACK);
FontMetrics metrics = g.getFontMetrics(this.font);
g.setFont(this.font);
// Draw exit
if (this.model.getSquare(i, j).isExit()) {
g.drawString(this.exit, (this.gridStartX + (this.squareSize * j)) + ((this.squareSize - metrics.stringWidth(this.exit)) / 2), (this.gridStartY + (this.squareSize * i)) + (((this.squareSize - metrics.getHeight()) / 2) + metrics.getAscent()));
}
// Draw Thésée
if (this.model.getThesee().getSquare() == this.model.getSquare(i, j)) {
g.drawString(this.thesee, (this.gridStartX + (this.squareSize * j)) + ((this.squareSize - metrics.stringWidth(this.thesee)) / 2), (this.gridStartY + (this.squareSize * i)) + (((this.squareSize - metrics.getHeight()) / 2) + metrics.getAscent()));
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
// Draw horizontal lines
g.setColor(Color.BLACK);
for (int i = 0; i < this.model.getSize()+1; i++) {
g.drawLine(this.gridStartX, this.gridStartY + (this.squareSize * i), this.gridStartX + (this.squareSize * this.model.getSize()), this.gridStartY + (this.squareSize * i));
}
// Draw vertical lines
for (int i = 0; i < this.model.getSize()+1; i++) {
g.drawLine(this.gridStartX + (this.squareSize * i), this.gridStartY, this.gridStartX + (this.squareSize * i), this.gridStartY + (this.squareSize * this.model.getSize()));
}
}
}
}

View File

@ -1,32 +0,0 @@
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class HGameController implements KeyListener {
private Player player;
public HGameController(Player player) {
this.player = player;
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_UP) {
player.move(-1, 0);
} else if (keyCode == KeyEvent.VK_DOWN) {
player.move(1, 0);
} else if (keyCode == KeyEvent.VK_LEFT) {
player.move(0, -1);
} else if (keyCode == KeyEvent.VK_RIGHT) {
player.move(0, 1);
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
}

45
src/HomeView.java Normal file
View File

@ -0,0 +1,45 @@
import javax.swing.*;
import java.awt.*;
public class HomeView extends JPanel {
private JFrame window;
public HomeView(JFrame window) {
this.window = window;
JLabel texte = getTitre();
//Récupération des boutons créés dans la classe Boutons
JButton choisirGrille = Boutons.ChoisirGrille(this.window);
JButton importerGrille = Boutons.ImporterGrille(this.window);
// Création du panel pour le texte
JPanel panelTexte = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
panelTexte.add(texte);
// Création du panel pour les boutons
JPanel panelBoutons = new JPanel(new FlowLayout(FlowLayout.CENTER, 100, 200));
panelBoutons.add(choisirGrille);
panelBoutons.add(importerGrille);
panelBoutons.setOpaque(false);
// Ajout des composants au JPanel principal
setLayout(new BorderLayout());
add(panelTexte, BorderLayout.NORTH);
add(panelBoutons, BorderLayout.CENTER);
}
private static JLabel getTitre() {
JLabel texte = new JLabel("Choisissez votre type de grille", SwingConstants.CENTER);
texte.setPreferredSize(new Dimension(800, 50));
texte.setFont(new Font("Arial", Font.BOLD, 30));
texte.setForeground(new Color(0, 200, 10));
return texte;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(new ImageIcon("background.jpg").getImage(), 0, 0, getWidth(), getHeight(), null);
}
}

8
src/Main.java Normal file
View File

@ -0,0 +1,8 @@
public class Main {
public static void main(String[] args) {
Window window = new Window();
HomeView home = new HomeView(window);
window.setContentPane(home);
window.setVisible(true);
}
}

View File

@ -1,32 +0,0 @@
import javax.swing.*;
import java.awt.*;
public class Play extends JFrame {
public Play() {
super("Démarrage");
this.setSize(250, 250);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panelDemarrage = new JPanel(new BorderLayout());
panelDemarrage.setBackground(Color.WHITE);
JButton play = new JButton("Jouer");
play.setPreferredSize(new Dimension(100, 50));
play.setFont(new Font("Arial", Font.BOLD, 20));
play.setBackground(new Color(0, 150, 200));
play.addActionListener(e -> {
this.dispose(); // ferme la fenêtre de démarrage
Accueil.fenetreAccueil(); // ouvre la fenêtre d'accueil
});
JPanel panelPlay = new JPanel();
panelPlay.add(play, BorderLayout.CENTER);
panelDemarrage.add(panelPlay, BorderLayout.CENTER);
panelDemarrage.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 60));
this.setContentPane(panelDemarrage);
this.setResizable(false);
this.setVisible(true);
}
}

View File

@ -1,22 +0,0 @@
public class Player {
private int x;
private int y;
public Player(int x, int y) {
this.x = x;
this.y = y;
}
public void move(int dx, int dy) {
x += dx;
y += dy;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}

View File

@ -1,13 +1,72 @@
public class Square {
private final int row;
private final int column;
private int type = 0;
public final int row;
public final int column;
public int type = 0;
private Grid gridModel;
public Square(int row, int column) {
public Square(Grid gridModel, int row, int column) {
this.gridModel = gridModel;
this.row = row;
this.column = column;
}
/**
* Checks if the current square is a wall
* @return true if the current square is a wall, false otherwise
*/
public boolean isWall() {
return this.type == 1;
}
/**
* Checks if the current square is an exit
* @return true if the current square is an exit, false otherwise
*/
public boolean isExit() {
return this.type == 2;
}
/**
* Checks if the current square is empty (not a wall and not an exit)
* @return true if the current square is empty, false otherwise
*/
public boolean isEmpty() {
return this.type == 0;
}
/**
* Sets the current square as a wall
*/
public void setWall() {
this.type = 1;
}
/**
* Removes the existing exit from the grid (if it exists) and sets the current square as an exit
*/
public void setExit() {
for (int i = 0; i < this.gridModel.getSize(); i++) {
for (int j = 0; j < this.gridModel.getSize(); j++) {
try {
if (this.gridModel.getSquare(i, j).isExit()) {
this.gridModel.getSquare(i, j).setEmpty();
}
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
this.type = 2;
}
/**
* Sets the current square as empty (removes wall or exit)
*/
public void setEmpty() {
this.type = 0;
}
public int getRow() {
return this.row;
}
@ -16,27 +75,7 @@ public class Square {
return this.column;
}
public boolean isWall() {
return this.type == 1;
}
public boolean isExit() {
return this.type == 2;
}
public boolean isEmpty() {
return this.type == 0;
}
public void setWall() {
this.type = 1;
}
public void setExit() {
this.type = 2;
}
public void setEmpty() {
this.type = 0;
public Grid getGrid() {
return this.gridModel;
}
}

18
src/Thesee.java Normal file
View File

@ -0,0 +1,18 @@
public class Thesee {
private Square square;
private Square intialSquare;
public void setSquare(Square square) {
square.setEmpty();
this.square = square;
if (this.intialSquare == null) this.intialSquare = this.square;
}
public Square getSquare() {
return this.square;
}
public void reset() {
this.square = this.intialSquare;
}
}

88
src/TheseeController.java Normal file
View File

@ -0,0 +1,88 @@
import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class TheseeController implements KeyListener {
private Thesee model;
private GridView gridView;
public TheseeController(Thesee model, GridView gridView) {
this.model = model;
this.gridView = gridView;
}
public boolean moveUp() {
try {
Square currentSquare = this.model.getSquare();
Square newSquare = currentSquare.getGrid().getSquare(currentSquare.getRow(), currentSquare.getColumn() - 1);
if (newSquare.isWall()) return false;
this.model.setSquare(newSquare);
this.gridView.repaint();
return true;
} catch (Exception e) {
return false;
}
}
public boolean moveDown() {
try {
Square currentSquare = this.model.getSquare();
Square newSquare = currentSquare.getGrid().getSquare(currentSquare.getRow(), currentSquare.getColumn() + 1);
if (newSquare.isWall()) return false;
this.model.setSquare(newSquare);
this.gridView.repaint();
return true;
} catch (Exception e) {
return false;
}
}
public boolean moveLeft() {
try {
Square currentSquare = this.model.getSquare();
Square newSquare = currentSquare.getGrid().getSquare(currentSquare.getRow() - 1, currentSquare.getColumn());
if (newSquare.isWall()) return false;
this.model.setSquare(newSquare);
this.gridView.repaint();
return true;
} catch (Exception e) {
return false;
}
}
public boolean moveRight() {
try {
Square currentSquare = this.model.getSquare();
Square newSquare = currentSquare.getGrid().getSquare(currentSquare.getRow() + 1, currentSquare.getColumn());
if (newSquare.isWall()) return false;
this.model.setSquare(newSquare);
this.gridView.repaint();
return true;
} catch (Exception e) {
return false;
}
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
boolean moved;
if (keyCode == KeyEvent.VK_UP) {
moved = moveUp();
} else if (keyCode == KeyEvent.VK_DOWN) {
moved = moveDown();
} else if (keyCode == KeyEvent.VK_LEFT) {
moved = moveLeft();
} else if (keyCode == KeyEvent.VK_RIGHT) {
moved = moveRight();
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
}

17
src/Window.java Normal file
View File

@ -0,0 +1,17 @@
import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.Toolkit;
public class Window extends JFrame {
public Window() {
super("Labyrinthe");
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension d = new Dimension(screenSize.width-150, screenSize.height-150);
this.setSize(d);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
System.out.println("Fenêtre :");
System.out.println("\tLargeur de la fenêtre : " + this.getWidth() + "px");
System.out.println("\tHauteur de la fenêtre : " + this.getHeight() + "px");
}
}