Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Describtion ****MAZE GAME IN JAVA (Eclipse)**** (Please follow all directions) Y

ID: 3861484 • Letter: D

Question

Describtion

****MAZE GAME IN JAVA (Eclipse)****

(Please follow all directions)

You should create a maze game. The aim of the game is to get out of the maze. The game will read inn maze from a text file with the format specified below. I'm going to lay out some such as mazes, but you can feel free to make your own as well. Here is an example of a file that specifies a maze:

7

6

##### - #

# * # # #

# # #

### # #

# #

Files that specifies a maze should start with two numbers on each line. The numbers indicate the width and height of the maze. Example file indicates that the maze will be 7 squares wide and 6 squares high. Then comes the maze. Each line of the text file is a row in the maze and each character in the text file marks what should be included in a particular cell in the maze. "#" Character marks a wall, a blank character marks an opening (hallway), "*" indicates where the player starts and "-" marks the end. An example of how the game might look like based on sample file stated above are given in Figure 1. The left is so game starts and the right is such a game is right before the player wins.

Tasks:

Create an abstract class ”Maze Route” representing a route in the maze. All maze routes

To save his position on the board (x and y coordinates). The class will also declare two

abstract methods: A method of moving the player to the route, and a

method that returns a reference to the appearance of the route.

Then Make concrete subclasses of the three types of routes Wall, Hallway and output(end).

If the player moves to the end the player has won and the game will end. You choose if the look of the routes are easy

forms like javafx.scene.shape.Rectangle, or more advanced selfdrawn forms with Canvas,

or pictures that you show off with ImageView objects.

After that, create a class player that stores the position of the player in the board

and create a method that reads a text file and create a two-dimensional array of maze routes

based on the scanned file.

Create a Java FX main class that displays the maze.

Then make a Event Handler <KeyEvent> responsive to arrow keys by trying to move the player in

direction of the arrow. Then finish by

register keylistener in Scene object to let it be applicable for the entire game.

M

Explanation / Answer

I am pasting the code of the game below. I has for files.

=======Maze.java=======

package chegg.maze;
import javax.swing.*;

public class Maze {
   public Maze()
   {
       JFrame jf=new JFrame();
       jf.setTitle("The Game Of Maze");
       jf.add(new Board());
       jf.setSize(500,500);
       jf.setLocationRelativeTo(null);
       jf.setVisible(true);
       jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       new Maze();
   }

}

==========

======Board.java========

package chegg.maze;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JPanel;
import javax.swing.Timer;

public class Board extends JPanel implements ActionListener {
  
   private Timer timer;
   private Map m;
   private Player p;
   private String Message="";
   public Board()
   {
       m=new Map();
       p=new Player();
       addKeyListener(new Al());
       setFocusable(true);
   timer=new Timer(25,this);
   timer.start();
   }
public void actionPerformed(ActionEvent e){
   if(m.getMap(p.gettX(),p.gettY()).equals("f"))
   {
       Message="Winner";
   }
   repaint();
}
public void paint(Graphics g)
{
   super.paint(g);
   for(int y=0;y<14;y++)
   {
       for(int x=0;x<14;x++)
       {
           if(m.getMap(x, y).equals("f"))
           {
               g.drawImage(m.getFinish(), x*32, y*32, null);
           }
           if(m.getMap(x, y).equals("g"))
           {
               g.drawImage(m.getGrass(), x*32, y*32, null);
           }
           if(m.getMap(x, y).equals("g"))
           {
               g.drawImage(m.getWall(), x*32, y*32, null);
           }
       }
   }
   g.drawString(Message,50,50);
   g.drawImage(p.getPlayer(),p.gettX()*32,p.gettY()*32,null);
}
public class Al extends KeyAdapter{
   public void keyPressed(KeyEvent e){
       int kc=e.getKeyCode();
       if(kc==KeyEvent.VK_W)
       {
           if(!m.getMap(p.gettX(), p.gettY()-1).equals("w"))
           p.move( 0, -1);
       }
       if(kc==KeyEvent.VK_S)
       {
           if(!m.getMap(p.gettX(), p.gettY()+1).equals("w"))
           p.move( 0, 1);
       }
       if(kc==KeyEvent.VK_A)
       {
           if(!m.getMap(p.gettX()-1, p.gettY()).equals("w"))
           p.move( -1, 0);
       }
       if(kc==KeyEvent.VK_D)
       {
           if(!m.getMap(p.gettX()+1, p.gettY()).equals("w"))
           p.move( 32, 0);
       }
   }
public void keyReleased(KeyEvent e){
      
   }
public void keyTyped(KeyEvent e){
  
}
}
}

===============

=======================Map.java===========

package chegg.maze;

import java.awt.Image;
import java.io.File;
import java.util.Scanner;

import javax.swing.ImageIcon;

public class Map {
  
   private Scanner sc;
   private String Map[]=new String[16];
   private Image grass,wall,finish;
   public Map()
   {
       ImageIcon img=new ImageIcon("C:\Users\aaa\Desktop\Maze\grass.png");
       grass=img.getImage();
       img=new ImageIcon("C:\Users\aaa\Desktop\Maze\wall.jpg");
           wall=img.getImage();
           img=new ImageIcon("C:\Users\aaa\Desktop\Maze\finish.jpg");
           finish=img.getImage();
       openFile();
       readFile();
       closeFile();
   }
   public Image getGrass()
   {
       return grass;
   }
   public Image getWall()
   {
       return wall;
   }
   public Image getFinish()
   {
       return finish;
   }
   public String getMap(int x,int y)
   {
       String index=Map[y].substring(x,x+1);
       return index;
   }
public void openFile()
{
   try{
   sc=new Scanner(new File("C:\Users\aaa\Desktop\Maze\Map.txt"));
   }catch(Exception e)
   {
       e.printStackTrace();
   }
}
public void readFile()
{
   while(sc.hasNext()){
       for(int i=0;i<14;i++)
       {
           Map[i]=sc.next();
       }
   }
}
public void closeFile()
{
   sc.close();
}
}

===================================

====================Player.java============

package chegg.maze;

import java.awt.Image;

import javax.swing.ImageIcon;

public class Player {
   private int x,y,tx,ty;
   private Image player;
   public Player()
   {
       ImageIcon ply=new ImageIcon("C:\Users\aaa\Desktop\Maze\player.png");
       player=ply.getImage();
  
       tx=1;ty=1;
   }
   public Image getPlayer()
   {
       return player;
   }

   public int gettX(){
       return tx;
   }
   public int gettY(){
       return ty;
   }
   public void move(int ttx,int tty)
   {
      
       tx+=ttx;
       ty+=tty;
      
   }

}

===============================

create a java project and include these files in chegg.maze package and run it. It has links to the source of the files files wall.png,grass.png,player.png, Map.txt.

The Map.txt will contain-

wwwwwwwwwwwwww
wggggggggggggw
wggwwwgggggggw
wggggwgggggggw
wggggwgggggggw
wggggwggwggggw
wggggwgggwgggw
wggggwgggggggw
wggggwwggggggw
wggggggggggggw
wggggggggggggw
wggggggggggggw
wggggggggggggw
wggggggggggggw
wwwwwwwwwwwwww