Skip to content

Commit

Permalink
Merge pull request #18 from DylanVanAssche/feature/final-release
Browse files Browse the repository at this point in the history
Final release
  • Loading branch information
DylanVanAssche authored Dec 19, 2017
2 parents 23bb6fd + 202b4f0 commit 092756d
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 29 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ A simple maze game written in Java following the MVC principles.
## Features
- MVC principles written in Java
- Find the gold in the maze
- Full GUI
- Player name
- Full GUI with basic icons
- Customize the name of the player
- Key navigation (ALT + arrow keys)
- Generate a maze with a different size (2x2, 4x4 or 8x8)
- Scrollbars if the maze is too big for the screen

## To do
- Backtracking algorithm to generate a solvable maze.
- Key bindings for the navigation of the player.
- Implement a real key listener for key navigation.

## Screenshots
![screenshot1](/docs/java-maze.png)
Expand Down
8 changes: 8 additions & 0 deletions src/be/dylanvanassche/maze/controller/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,12 @@ public void movePlayer(MovementType movement) throws UnknownMovementDirection, B
public void setPlayerName(String name) {
this.getMaze().getPlayer().setName(name);
}

public void setMazeSize(int size) {
Maze.setMazeSize(size);
}

public int getMazeSize() {
return Maze.getMazeSize();
}
}
13 changes: 10 additions & 3 deletions src/be/dylanvanassche/maze/model/Maze.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
package be.dylanvanassche.maze.model;

public class Maze {
public static final int mazeSize = 2; // define n
public static int mazeSize = 2; // define n
public static final int tileSize = 3;
private int currentTileIndex = 0;
private int currentTileIndexRow = 0;
Expand Down Expand Up @@ -49,6 +49,14 @@ public Tile[][] getTiles() {
public void setTiles(Tile[][] tiles) {
this.tiles = tiles;
}

public static int getMazeSize() {
return mazeSize;
}

public static void setMazeSize(int mazeSize) {
Maze.mazeSize = mazeSize;
}

/*
* @brief: constructs a new random Maze
Expand All @@ -68,7 +76,7 @@ public Maze() {
while(target.getMiddleSquare().getContent() == SquareType.GOLD) { // It's never possible that the content is WALL
target = this.getTiles()[(int)(Math.random()*2*mazeSize)][(int)(Math.random()*2*mazeSize)];
}
Position newPlayerPosition = this.getTiles()[(int)(Math.random()*2*mazeSize)][(int)(Math.random()*2*mazeSize)].enablePlayer();
Position newPlayerPosition = target.enablePlayer();
this.getPlayer().setPosition(newPlayerPosition);
}

Expand Down Expand Up @@ -139,7 +147,6 @@ private Tile nextTileFromMovement(MovementType movement) throws UnknownMovementD
// Retrieve the tileIndex by searching it in the ArrayList of Tiles
int oldTileIndexRow = -1;
int oldTileIndexColumn = -1;
int newTileIndex = -1;
for(int i=0; i<mazeSize*2; i++) {
for(int j=0; j<mazeSize*2; j++) {
if(this.getPlayer().getPosition().getTile() == this.getTiles()[i][j]) {
Expand Down
13 changes: 12 additions & 1 deletion src/be/dylanvanassche/maze/view/MainFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class MainFrame extends JFrame {
private Controller controller;
private MazeView mazeView;
private NavigationView navigationView;
private JScrollPane scrollView;

public Controller getController() {
return controller;
Expand All @@ -35,6 +36,14 @@ public void setNavigationView(NavigationView navigationView) {
this.navigationView = navigationView;
}

public JScrollPane getScrollView() {
return scrollView;
}

public void setScrollView(JScrollPane scrollView) {
this.scrollView = scrollView;
}

public MainFrame(final Controller c) {
this.setController(c);
this.getContentPane().setLayout(new BorderLayout());
Expand All @@ -56,7 +65,9 @@ public void newGame() {
this.getController().setPlayerName(name);
this.getContentPane().removeAll(); // clean it up
this.setMazeView(new MazeView(this.getController()));
this.getContentPane().add(this.getMazeView(), BorderLayout.CENTER);
this.setScrollView(new JScrollPane(this.getMazeView()));
this.getMazeView().setAutoscrolls(true);
this.getContentPane().add(this.getScrollView(), BorderLayout.CENTER);
this.getContentPane().add(this.getNavigationView(), BorderLayout.SOUTH);
// Revalidates the component hierarchy, when adding/removing stuff at runtime you need to reload the UI,
// this is NOT repaint since we add/remove the components completely without modifying their properties!
Expand Down
8 changes: 2 additions & 6 deletions src/be/dylanvanassche/maze/view/MazeView.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList;
import be.dylanvanassche.maze.controller.*;
import be.dylanvanassche.maze.model.*;

public class MazeView extends JPanel {
public static final int mazeSize = 2; // define n
private Controller controller;

public Controller getController() {
Expand All @@ -22,14 +18,14 @@ public void setController(Controller controller) {

public MazeView(final Controller c) {
this.setController(c);
this.setLayout(new GridLayout(0,4,0,0)); // gap = 0
this.setLayout(new GridLayout(0, 2*this.getController().getMazeSize(), 0, 0)); // gap = 0
this.newGame();
this.setVisible(true);
}

public void newGame() {
this.removeAll();
for(int i = 0; i < Math.pow(2*mazeSize, 2); i++) {
for(int i = 0; i < Math.pow(2*this.getController().getMazeSize(), 2); i++) {
this.add(new TileView(this.getController(), this.getController().nextMazeTile()));
}
}
Expand Down
53 changes: 47 additions & 6 deletions src/be/dylanvanassche/maze/view/MenuBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ public class MenuBar extends JMenuBar {
private Controller controller;
private JMenu gameMenu = new JMenu("Game");
private JMenu aboutMenu = new JMenu("About");
private JMenuItem newGameMenu = new JMenuItem("New");
private JMenu newGameMenu = new JMenu("New");
private JMenuItem newGame2Menu = new JMenuItem("2x2");
private JMenuItem newGame4Menu = new JMenuItem("4x4");
private JMenuItem newGame8Menu = new JMenuItem("8x8");
private JMenuItem exitGameMenu = new JMenuItem("Exit");
private JMenuItem ApplicationAboutMenu = new JMenuItem("Application"); // Needs icon
private JMenuItem GithubAboutMenu = new JMenuItem("GitHub"); // Needs icon
Expand Down Expand Up @@ -41,13 +44,37 @@ public void setAboutMenu(JMenu aboutMenu) {
this.aboutMenu = aboutMenu;
}

public JMenuItem getNewGameMenu() {
public JMenu getNewGameMenu() {
return newGameMenu;
}

public void setNewGameMenu(JMenuItem newGameMenu) {
public void setNewGameMenu(JMenu newGameMenu) {
this.newGameMenu = newGameMenu;
}

public JMenuItem getNewGame2Menu() {
return newGame2Menu;
}

public void setNewGame2Menu(JMenuItem newGame2Menu) {
this.newGame2Menu = newGame2Menu;
}

public JMenuItem getNewGame4Menu() {
return newGame4Menu;
}

public void setNewGame4Menu(JMenuItem newGame4Menu) {
this.newGame4Menu = newGame4Menu;
}

public JMenuItem getNewGame8Menu() {
return newGame8Menu;
}

public void setNewGame8Menu(JMenuItem newGame8Menu) {
this.newGame8Menu = newGame8Menu;
}

public JMenuItem getExitGameMenu() {
return exitGameMenu;
Expand Down Expand Up @@ -79,14 +106,28 @@ public MenuBar(final Controller controller) {
this.add(Box.createHorizontalGlue());
this.add(getAboutMenu());
this.getGameMenu().add(this.getNewGameMenu());
this.getNewGameMenu().add(this.getNewGame2Menu());
this.getNewGameMenu().add(this.getNewGame4Menu());
this.getNewGameMenu().add(this.getNewGame8Menu());
this.getGameMenu().add(this.getExitGameMenu());
this.getAboutMenu().add(this.getApplicationAboutMenu());
this.getAboutMenu().add(this.getGithubAboutMenu());
this.getNewGameMenu().setToolTipText("Start a new game");
this.getNewGameMenu().addActionListener(new ActionListener() {
this.getNewGame2Menu().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
getController().setMazeSize(2);
getController().newGame();
}
});
this.getNewGame4Menu().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
getController().setMazeSize(4);
getController().newGame();
}
});
this.getNewGame8Menu().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
//getController().newGame();
//getView().newGame();
getController().setMazeSize(8);
getController().newGame();
}
});
Expand Down
5 changes: 0 additions & 5 deletions src/be/dylanvanassche/maze/view/SquareView.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@
package be.dylanvanassche.maze.view;

import java.awt.*;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import be.dylanvanassche.maze.controller.Controller;
import be.dylanvanassche.maze.model.*;
import be.dylanvanassche.maze.view.*;

public class SquareView extends JPanel {
private Controller controller;
Expand Down
9 changes: 4 additions & 5 deletions src/be/dylanvanassche/maze/view/SquareViewElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public SquareViewElement(SquareType content) {
this.setFreeImg(ImageIO.read(new File(FREE_PATH)));
}
catch (IOException e) {

e.printStackTrace();
}
}
Expand All @@ -84,21 +83,21 @@ public Dimension getPreferredSize() {

@Override
protected void paintComponent(Graphics graphics) {
//super.paintComponent(graphics);
super.paintComponent(graphics);
switch(this.getContent())
{
case WALL:
graphics.drawImage(this.getWallImg(), 0, 0, IMG_SIZE, IMG_SIZE, null);
break;
case PLAYER:
graphics.drawImage(this.getPlayerImg(), 0, 0, IMG_SIZE, IMG_SIZE, this);
graphics.drawImage(this.getPlayerImg(), 0, 0, IMG_SIZE, IMG_SIZE, null);
break;
case GOLD:
graphics.drawImage(this.getGoldImg(), 0, 0, IMG_SIZE, IMG_SIZE, this);
graphics.drawImage(this.getGoldImg(), 0, 0, IMG_SIZE, IMG_SIZE, null);
break;
default:
case FREE:
graphics.drawImage(this.getFreeImg(), 0, 0, IMG_SIZE, IMG_SIZE, this);
graphics.drawImage(this.getFreeImg(), 0, 0, IMG_SIZE, IMG_SIZE, null);
break;
}

Expand Down

0 comments on commit 092756d

Please sign in to comment.