Skip to content

Commit

Permalink
code of the game
Browse files Browse the repository at this point in the history
  • Loading branch information
Aymane El Firdoussi committed Apr 30, 2022
1 parent d55fcd0 commit bbd6b5e
Show file tree
Hide file tree
Showing 36 changed files with 1,704 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/MenuBar/MazeMenuBar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package MenuBar;

import javax.swing.JMenuBar;

import Menus.*;
import mazeUI.*;


public final class MazeMenuBar extends JMenuBar
{
private final StartMenu fileMenu;
private final HelpMenu helpMenu;
private final ExitMenu exitMenu;
private final SaveMenu saveMenu ;



public MazeMenuBar(MazeApplication mazeApp)
{
super();
//it's Menus

add(fileMenu=new StartMenu(mazeApp));
add(helpMenu = new HelpMenu(mazeApp));
add(saveMenu = new SaveMenu(mazeApp));
add(exitMenu = new ExitMenu(mazeApp));
}


}
27 changes: 27 additions & 0 deletions src/MenuItems/ArrivalMenuItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package MenuItems;

import javax.swing.JMenuItem;

import mazeUI.MazeApplication;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public final class ArrivalMenuItem extends JMenuItem implements ActionListener {
private final MazeApplication mazeApp;

public ArrivalMenuItem(MazeApplication mazeApp)
{
super("Select Arrival");
this.mazeApp=mazeApp;
addActionListener(this);
}



@Override
public void actionPerformed(ActionEvent e) {

mazeApp.getModel().setPressedA(true);
}
}
25 changes: 25 additions & 0 deletions src/MenuItems/DepartureMenuItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package MenuItems;

import javax.swing.JMenuItem;

import mazeUI.MazeApplication;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public final class DepartureMenuItem extends JMenuItem implements ActionListener {
private final MazeApplication mazeApp;

public DepartureMenuItem(MazeApplication mazeApp)
{
super("Select Departure");
this.mazeApp = mazeApp;
addActionListener(this);
}


@Override
public void actionPerformed(ActionEvent e) {
mazeApp.getModel().setPressedD(true);
}
}
44 changes: 44 additions & 0 deletions src/MenuItems/ImportMenuItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package MenuItems;

import maze.MazeReadingException;
import mazeUI.MazeApplication;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;

public final class ImportMenuItem extends JMenuItem implements ActionListener {
private MazeApplication mazeApp;

public ImportMenuItem(MazeApplication mazeApp)
{
super("Import Maze");
this.mazeApp = mazeApp;
addActionListener(this);

}


@Override
public final void actionPerformed(ActionEvent e)
{
JFileChooser fileChooser = new JFileChooser();
//fileChooser.setCurrentDirectory(new File(System.getProperty("Desktop")));
int result = fileChooser.showOpenDialog(this.mazeApp);
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
try {
mazeApp.getModel().importMaze(selectedFile.getAbsolutePath());
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (maze.FileNotFoundException ex) {
ex.printStackTrace();
} catch (MazeReadingException ex) {
ex.printStackTrace();
}
}

}
}
36 changes: 36 additions & 0 deletions src/MenuItems/InstructionsMenuItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package MenuItems;

import javax.swing.*;

import mazeUI.MazeApplication;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class InstructionsMenuItem extends JMenuItem implements ActionListener {

private final MazeApplication mazeApp;

public InstructionsMenuItem(MazeApplication mazeApp)
{
super("Instructions");
this.mazeApp = mazeApp;
addActionListener(this);
}
@Override

public final void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(this.mazeApp,"- If you want to create a new Maze with whatever dimension you want"+
", then you should click on New Maze\n"+
"- Select a departure box by clicking on Select Departure\n"+
"- Select an arrival box by clicking on Select Arrival\n"+
"- You can make Walls and Empty boxes only by clicking on the squares\n"+
"- Then click on Solve to visualize the path from Departure to Arrival\n"+
"- If you want to import an existing Maze, click on Import Maze and select the file\n"+
"- If you want to save your Maze, click on Save\n"+
"- Finally, if you have finished playing, you can quit by clicking on Exit\n"+
" ENJOY !!");

}

}
59 changes: 59 additions & 0 deletions src/MenuItems/NewMenuItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package MenuItems;

import javax.swing.*;

import maze.FileNotFoundException;
import maze.MazeReadingException;
import mazeUI.MazeApplication;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class NewMenuItem extends JMenuItem implements ActionListener {

private final MazeApplication mazeApp; // pincipal window

public NewMenuItem(MazeApplication mazeApp)
{
super("New Maze");
this.mazeApp=mazeApp;
addActionListener(this);
}

@Override
public void actionPerformed(ActionEvent e) {
String widthEntered = JOptionPane.showInputDialog(mazeApp,"Width of the Maze");
mazeApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

String heightEntered = JOptionPane.showInputDialog(mazeApp,"Height of the Maze");
mazeApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
int width = Integer.valueOf(widthEntered);
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(
this.mazeApp,
"Invalid value for width ! Must be integer",
"Incorrect",
JOptionPane.WARNING_MESSAGE
);
return;
}

try{
int height = Integer.valueOf(heightEntered);
}catch (Exception ex){
ex.printStackTrace();
JOptionPane.showMessageDialog(
this.mazeApp,
"Invalid value for height ! Must be integer",
"Incorrect",
JOptionPane.WARNING_MESSAGE
);
return;
}
int width = Integer.valueOf(widthEntered);
int height = Integer.valueOf(heightEntered);
mazeApp.getModel().newMaze(width,height);
}
}
60 changes: 60 additions & 0 deletions src/MenuItems/QuitMenuItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package MenuItems;

import javax.swing.*;

import mazeUI.MazeApplication;
import model.MazeAppModel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public final class QuitMenuItem extends JMenuItem implements ActionListener
{
private final MazeApplication mazeApp;

public QuitMenuItem(MazeApplication mazeApp)
{
super("Quit Game");
this.mazeApp = mazeApp;
addActionListener(this);
}
public final void actionPerformed(ActionEvent evt)
{
MazeAppModel model = mazeApp.getModel() ;

if (model.getModified()==true) {
int response = JOptionPane.showInternalOptionDialog(this,
"Drawing not saved. Save it ?",
"Quit application",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE,
null,null,null) ;
switch (response) {
case JOptionPane.CANCEL_OPTION:
return ;
case JOptionPane.OK_OPTION:
String fileName = JOptionPane.showInputDialog(mazeApp, "Name of the File");
String notAccepted = MazeAppModel.notAccepted;
int n = fileName.length();
for (int i = 0; i < n; i++) {
String c = String.valueOf(fileName.charAt(i));
if (notAccepted.contains(c)) {
JOptionPane.showMessageDialog(
this.mazeApp,
"Name should not contain " + notAccepted,
"Invalid Name",
JOptionPane.WARNING_MESSAGE
);
return;
}
}
model.save(fileName);
break ;
case JOptionPane.NO_OPTION:
break ;
}
}
System.exit(0) ;
}

}
41 changes: 41 additions & 0 deletions src/MenuItems/SaveMenuItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package MenuItems;

import javax.swing.*;

import mazeUI.*;
import model.MazeAppModel;
import java.lang.String;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public final class SaveMenuItem extends JMenuItem implements ActionListener {

private final MazeApplication mazeApp;

public SaveMenuItem(MazeApplication mazeApp) {
super("Save Maze");
this.mazeApp = mazeApp;
addActionListener(this);
}

@Override
// Enregistre le labyrinthe dans le fichier SavedMaze
public void actionPerformed(ActionEvent e) {
String fileName = JOptionPane.showInputDialog(mazeApp, "Name of the File");
String notAccepted = MazeAppModel.notAccepted;
int n = fileName.length();
for (int i = 0; i < n; i++) {
String c = String.valueOf(fileName.charAt(i));
if (notAccepted.contains(c)) {
JOptionPane.showMessageDialog(
this.mazeApp,
"Name should not contain " + notAccepted,
"Invalid Name",
JOptionPane.WARNING_MESSAGE
);
return;
}
}
mazeApp.getModel().save(fileName);
}
}
48 changes: 48 additions & 0 deletions src/MenuItems/SolveMenuItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package MenuItems;

import maze.ABox;
import maze.DBox;
import maze.Maze;
import mazeUI.MazeApplication;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SolveMenuItem extends JMenuItem implements ActionListener {
private MazeApplication mazeApp;

public SolveMenuItem(MazeApplication mazeApp){
super("Solve");
this.mazeApp = mazeApp;
addActionListener(this);
}

@Override
public void actionPerformed(ActionEvent e) {
Maze currentMaze = mazeApp.getModel().getCurrentMaze();
int numA = currentMaze.numberofA();
int numD = currentMaze.numberofD();
if(numA !=1) {
JOptionPane.showMessageDialog(
this.mazeApp,
"You should have only ONE Arrival !",
"Solving Error",
JOptionPane.WARNING_MESSAGE
);
return;
}else if(numD!=1) {
JOptionPane.showMessageDialog(
this.mazeApp,
"You should have only ONE Departure !",
"Solving Error",
JOptionPane.WARNING_MESSAGE
);
return;
} else{
mazeApp.getModel().solve();
}


}
}
Loading

0 comments on commit bbd6b5e

Please sign in to comment.