I have created a game where the player collects pods on the game board. I need to make an evasive maneuver so that after five turns when the player is one space away from the pod, the pod will transport 10 spaces away from the player. I believe my counter is not working and that is why the evasive maneuver is not working.
public class Pod {
int podPositionX;
int podPositionY;
String podDirection;
int layoutWidth;
int layoutHeight;
boolean podVisable = true;
int playerX;
int playerY;
int count = 0;
public Pod(int x, int y, String direction, int width, int height){
podPositionX = x;
podPositionY = y;
podDirection = direction;
layoutWidth = width;
layoutHeight = height;
count=count+1;
}
//Inspector Methods?
// Will get the the pods positon and will return it
public int getX()
{
return podPositionX;
}
//This method returns the current Y coordinate of the pod.
public int getY()
{
return podPositionY;
}
//This method returns true if the pod is visible, false otherwise. Once the
//pod has been caught, it should always return false.
public boolean isVisible(){
if (playerX == podPositionX && playerY == podPositionY && podVisable == true){
podVisable = false;}
return podVisable;
}
// to move pod diagonally across screen//
public void move(){
//Calling invasive methods!!
while (count>5 && count < 100){
podPositionX=transportX();
podPositionY=transportY();
}
/****************** To make pod move **************/
if (podDirection.equals("NW")|| podDirection.equals("NE")){
podPositionY = podPositionY + 1;}
if (podDirection.equals("SW")|| podDirection.equals("SE")){
podPositionY = podPositionY-1;}
if (podDirection.equals("NE")|| podDirection.equals("SE")){
podPositionX = podPositionX+1;}
if(podDirection.equals("NW")|| podDirection.equals("SW")){
podPositionX = podPositionX-1;}
/****************To make Pod bounce off wall******************/
//make pod bounce off left wall
if (podPositionX <= 1){
if (podDirection.equals("SW")){
podDirection = "SE";}
if (podDirection.equals("NW")){
podDirection = "NE";}
}
//make pod bounce off top
if (podPositionY >= layoutHeight-1){
if (podDirection.equals("NW")){
podDirection = "SW";}
if (podDirection.equals("NE")){
podDirection = "SE";}
}
//make pod bounce off right wall
if (podPositionX >= layoutWidth-1){
if (podDirection.equals("NE")){
podDirection = "NW";}
if (podDirection.equals("SE" )){
podDirection = "SW";}
}
//make pod bounce off bottom wall
if (podPositionY <= 1){
if (podDirection.equals("SW")){
podDirection = "NW";}
if (podDirection.equals("SE")){
podDirection = "NE";}
}
}
// to get player x and y positions
public void playerAt(int x, int y){
playerX = x;
playerY = y;
}
//envasive maneuver so that after 5 turns the pod can be transported away from the player if it is 1 spot away from the player.
//then the count is set to 100 so it will exit the loop and not be able to transport the pod again.
public int transportX(){
if (podPositionX == playerX -1 || podPositionX == playerX +1){
podPositionX= playerX +10;
count = 100;}
return podPositionX;
}
public int transportY(){
if (podPositionY == playerY -1 || podPositionY == playerY +1){
podPositionY= playerY +10;
count=100;}
return podPositionY;
}
}
the code my teacher provided us. Can no be touched so i can not put a counter in this file.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Project1 extends JFrame implements ActionListener {
// Member variables for visual objects.
private JLabel[][] board; // 2D array of labels. Displays either # for player,
// * for pod, or empty space
private JButton northButton, // player presses to move up
southButton, // player presses to move down
eastButton, // player presses to move right
westButton; // player presses to move left
// Current width and height of board (will make static later).
private int width = 15;
private int height = 9;
// Current location of player
private int playerX = 7;
private int playerY = 4;
// Pod object stored in array for efficiency
private Pod[] pods;
int podCount = 4;
public Project1() {
// Construct a panel to put the board on and another for the buttons
JPanel boardPanel = new JPanel(new GridLayout(height, width));
JPanel buttonPanel = new JPanel(new GridLayout(1, 4));
// Use a loop to construct the array of labels, adding each to the
// board panel as it is constructed. Note that we create this in
// "row major" fashion by making the y-coordinate the major
// coordinate. We also make sure that increasing y means going "up"
// by building the rows in revers order.
board = new JLabel[height][width];
for (int y = height-1; y >= 0; y--) {
for (int x = 0; x < width; x++) {
// Construct a label to represent the tile at (x, y)
board[y][x] = new JLabel(" ", JLabel.CENTER);
// Add it to the 2D array of labels representing the visible board
boardPanel.add(board[y][x]);
}
}
// Construct the buttons, register to listen for their events,
// and add them to the button panel
northButton = new JButton("N");
southButton = new JButton("S");
eastButton = new JButton("E");
westButton = new JButton("W");
// Listen for events on each button
northButton.addActionListener(this);
southButton.addActionListener(this);
eastButton.addActionListener(this);
westButton.addActionListener(this);
// Add each to the panel of buttons
buttonPanel.add(northButton);
buttonPanel.add(southButton);
buttonPanel.add(eastButton);
buttonPanel.add(westButton);
// Add everything to a main panel attached to the content pane
JPanel mainPanel = new JPanel(new BorderLayout());
getContentPane().add(mainPanel);
mainPanel.add(boardPanel, BorderLayout.CENTER);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
// Size the app and make it visible
setSize(300, 200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Auxiliary method to create game setup
createGame();
}
// Auxiliary method used to create board. Sets player, treasure, and walls.
public void createGame() {
// Construct array of Pod objects
pods = new Pod[podCount];
// Construct each Pod in the array, passing it its initial location,
// direction of movement, and the width and heigh of board. This will
// later be modified to be done at random.
pods[0] = new Pod(1, 5, "NE", width, height);
pods[1] = new Pod(2, 1, "SW", width, height);
pods[2] = new Pod(12, 2, "NW", width, height);
pods[3] = new Pod(13, 6, "SE", width, height);
// Call method to draw board
drawBoard();
}
// Auxiliary method to display player and pods in labels.
public void drawBoard() {
// "Erase" previous board by writing " " in each label
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
board[y][x].setText(" ");
}
}
// Get location of each pod and write * into that label. We only
// do this for pods not yet caught.
for (int p = 0; p < podCount; p++) {
if (pods[p].isVisible()) {
board[pods[p].getY()][pods[p].getX()].setText("*");
}
}
// Write the player onto the board.
board[playerY][playerX].setText("#");
}
public void actionPerformed(ActionEvent e) {
// Determine which button was pressed, and move player in that
// direction (making sure they don't leave the board).
if (e.getSource() == southButton && playerY > 0) {
playerY--;
}
if (e.getSource() == northButton && playerY < height-1) {
playerY++;
}
if (e.getSource() == eastButton && playerX < width-1) {
playerX++;
}
if (e.getSource() == westButton && playerX > 0) {
playerX--;
}
// Move the pods and notify the pods about player location.
for (int p = 0; p < podCount; p++) {
pods[p].move();
pods[p].playerAt(playerX, playerY);
}
// Redraw the board
drawBoard();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Project1 a = new Project1();
}
}
I am looking at this quickly now. You said the problem is with the count variable. I see that you are incrementing it in the constructor i.e
public Pod(int, int, String, int, int){
count = count + 1;
}
why not put it in the move method. That way every time the move method is used the count variable will be incremented by one.
public void move(){
this.count++;
}
Also it is very hard to see exactly what you think your code is doing. I tried to run it and nothing happens. Sometimes it is good to print something to the console.
Related
I wrote a program that allows a user to add nodes and connect them with edges with a GUI interface. The first node is always labeled "0". When I call the findPath method however it shows the entire path appropriately just lacking the 0. For example if I want to find the path from 0-4, and they're all connected numerically it will show [1,2,3,4]. To get a proper reading of the path after connecting the nodes you need to hit the print adjacency button. Everything else is running as expected but I can't seem to figure out why this won't add the first node.
Thanks Guys!
import javax.swing.JFrame;//imports JFrame package
public class Graph//class header
{
public static void main (String[] args)//main method
{
JFrame frame = new JFrame ("Directed Graph");//sets variable frame to a JFrame object and titles it
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);//exits application when it is closed
frame.getContentPane().add (new GraphPanel());//adding GraphPanel() to the frame
frame.setLocationRelativeTo(null);//sets location to the center
frame.pack();//packs the frame, fits everything inside
frame.setVisible(true);//makes frame visible
}
}
import java.util.ArrayList;//all of these are the required to get classes from other packages
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JPanel;
public class GraphPanel extends JPanel
{
private final int SIZE = 10; // radius of each node
private double alpha; // double variable to define later
private JButton print, create, delete, path1; //allpath;//button variables
private JTextField label, label1;
boolean mode = true;//sets boolean for entire class to true, until later when I define it as false for certain methods to be complete
String x1, y1;
String str1 = new String("Mode Chosen Is : create" );//when you choose create it will print this instead
String str2 = new String("Mode Chosen Is : delete" );//when you choose delete it will print this
int x, y;
int mode1 = 1; // int mode1 variable, will comment why i need this when it is later defined/changed
private Point point1 = null, point2 = null, point3 = null;//makes pointers null, i added a third pointer to put the distance from the first drawn string at the top right, but no longer needed it
//keeping it incase I wanted to add it again
private ArrayList<Point> nodeList; // Graph nodes
private ArrayList<Edge> edgeList; // Graph edges
static private int[][] a = new int[100][100]; // Graph adjacency matrix
static ArrayList<Integer> path = new ArrayList<Integer>();
public GraphPanel()
{
nodeList = new ArrayList<Point>();//declares ArrayList<Point>
edgeList = new ArrayList<Edge>();//declares ArrayList<Edge>
GraphListener listener = new GraphListener();//assigns 'listener' to a Graphlistener object
addMouseListener (listener);//accepts and reads mouseListener methods to the GraphListener object
addMouseMotionListener (listener);//accepts and reads MouseMotionListener methods to the GraphListener object
label = new JTextField (1);
label1 = new JTextField (1);
//allpath = new JButton ("Find All Paths");
path1 = new JButton ("Find Path");//path button
create = new JButton("Create");//declares 'create' as a JButton object with text on it that says 'create'
delete = new JButton("Delete");//declares 'delete' as a JButton object with text that says 'delete'
JButton print = new JButton("Print adjacency matrix");//declares 'print' as a JButton object with text that reads 'print adjacency matrix'
print.addActionListener (new ButtonListener());//adds an action listener to the print button from the buttonlistener object
create.addActionListener (new ButtonListener1());//adds an actionListener to the create button from the buttonlistener1 object
delete.addActionListener (new ButtonListener2());//adds an actionliseneer to the delete button from the buttonlistener2 object
path1.addActionListener (new ButtonListener3());//adds button that will find path of nodes
//allpath.addActionListener (new ButtonListener4());
setBackground (Color.black);//sets backround as black
setPreferredSize (new Dimension(400, 300));//this is preferred size of graphpanel
add(print);//these next six add()'s add each button to GraphPanel
add(delete);//
add(create);//
//add(allpath);
add(path1);
add(label);
add(label1);
}
public static boolean findPath(int x, int y) //boolean method findpath that takes two ints as parameters
{
boolean found = false;
if (a[x][y]==1) //base case
{
//System.out.println(x + " -> " + y);//prints node -> node
found = true;//path is found
path.add(0,y);
}
else
{
int z;
for (z=0; !found& z<a.length; z++)
{
if (a[x][z]==1)
{
a[x][z]=2;
found = findPath(z,y);
}
if (found)
{
//System.out.println(x + " -> " + z);
path.add(0,z);
}
}
}
if (!found){
System.out.println("Path Does Not Exist");
}
return found;
}
// public static ArrayList findPath1(int x, int y, ArrayList path)
// {
// System.out.println("CALL FIND: " + path);
// ArrayList path2 = null;
// if (a[x][y]==1)
// {
// ArrayList path3 = (ArrayList)path.clone();
// path3.add(y);
// System.out.println("EXIT BASE: " + path3);
// path2 = path3;
// }
// else
// {
// int z;
// for (z=0; z<a.length; z++)
// if (a[x][z]==1)
// {
// ArrayList path3 = (ArrayList)path.clone();
// path3.add(z);
// path3 = findPath1(z,y,path3);
// System.out.println("EXIT RECU: " + path);
// path2 = path3;
// }
// }
// return path2;
// }
// Draws the graph
public void paintComponent (Graphics page)
{
super.paintComponent(page);//using parent methods
// Draws the edge that is being dragged
page.setColor (Color.green);//setting color of what will be drawn to green
if (mode1 == 1)//if mode1 is set to 1, which is when the create button is selected
{
page.drawString(str1, 125, 75);//then this is printed
}
if (mode1 == 2)//if mode1 is set to 2, which is when the delete button is selected
{
page.drawString(str2, 125, 75);//then this is printed
}
if (point1 != null && point2 != null)//if and only if both points are not null
{
page.drawLine (point1.x, point1.y, point2.x, point2.y);//then it draws the line from between point1(x, y cords), and point2(x, y cords)
page.fillOval (point2.x-3, point2.y-3, 6, 6);//fills this oval by the rectangle it is specified from
}
// Draws the nodes
for (int i=0; i<nodeList.size(); i++) //for loop going through the nodeList ArrayList
{
page.setColor (Color.green);//color is set to green
page.fillOval (nodeList.get(i).x-SIZE, nodeList.get(i).y-SIZE, SIZE*2, SIZE*2);//fills oval by subtracting the SIZE from the x, y and setting the height and width as two times the SIZE
page.setColor (Color.black);//sets next line to black
page.drawString (String.valueOf(i), nodeList.get(i).x-SIZE/2, nodeList.get(i).y+SIZE/2);//writes inside the node what number index it is from the ArrayList with black text
}
// Draws the edges
for (int i=0; i<edgeList.size(); i++) // for loop going through the edgeList ArrayList
{
page.setColor (Color.green);//sets the next line to green
page.drawLine (edgeList.get(i).a.x, edgeList.get(i).a.y,edgeList.get(i).b.x, edgeList.get(i).b.y);//draws the line from x and y cords of where it starts to end
//page.fillOval (edgeList.get(i).b.x-3, edgeList.get(i).b.y-3, 6, 6);
//page.drawString (String.valueOf(point1.x*(point2.y-point3.y)+point2.x*(point3.y-point1.y)+point3.x*(point1.y-point2.y)), 5, 15);
alpha = Math.atan((double)(edgeList.get(i).b.y-edgeList.get(i).a.y)/(edgeList.get(i).b.x-edgeList.get(i).a.x));//alpha = b.y-a.y cords/b.x-a.x chords as a double variable
if (edgeList.get(i).a.x > edgeList.get(i).b.x)//if the x chord of a is greater than the x cordinate of b
{
alpha = alpha + Math.PI;//then alpha = previously defined alpha multiplied by PI
}
if (edgeList.get(i).a.x < edgeList.get(i).b.x && edgeList.get(i).a.y > edgeList.get(i).b.y)//if a.x is less than b.x and a.y is less than b.y
{
alpha = alpha + 2*Math.PI;//then alpha becomes the previously defined alpha multiplied by 2 PI
}
arrow(page,edgeList.get(i).b.x,edgeList.get(i).b.y,0,1.57-alpha);//arrow method, taking a x(edgeList.get(i).b.x, y(edgeList.get(i).b.y, length = 0 so arrow is at the
// very end of the drawn line subtracted by a double aplha
}
}
// arrow method to call when making an arrow
private void arrow(Graphics page, int x, int y, int len, double alpha)//arrow method, taking a x, y, length and double parameter
{
page.setColor (Color.green);//sets arrow to green
int x1 = x+(int)(len*Math.sin(alpha));//x1 is set to x plus length *sin of alpha
int y1 = y+(int)(len*Math.cos(alpha));//y1 is set to y plus the length * cosin of alpha
page.drawLine (x, y, x1, y1);//drawa the x and y, and then previously defined x1, and y1
page.drawLine (x1, y1, x1+(int)(20*Math.sin(alpha+2.5)), y1+(int)(20*Math.cos(alpha+2.5)));//arithmatic to draw one side of the line for the arrow
page.drawLine (x1, y1, x1+(int)(20*Math.sin(alpha+3.7)), y1+(int)(20*Math.cos(alpha+3.7)));//arithmatic to draw the corresponding line for the arrow to complete the arrow
}
// The listener for mouse events.
private class GraphListener implements MouseListener, MouseMotionListener//GraphListener which implements MouseListener and MouseMotionListener, meaning it takes methods from both of those classes
{
public void mouseClicked (MouseEvent event)//when mouse is clicked and released
{
if (mode == true)//if the mode is true
{
nodeList.add(event.getPoint());//the current point to the nodeList
repaint();//whenever you change the look of a componenet you call this method
}
if (mode == false)//if boolean mode is false
{
for (int i=0; i<nodeList.size(); i++)//for loop to go through the nodeList ArrayList
{
if (distance(nodeList.get(i), event.getPoint()) < SIZE)//if the distance between a specific node and the pointer is less than the radius of the node (SIZE)
nodeList.remove(i);//then remove that node
}
for (int i=0; i<edgeList.size(); i++)
{
if (distance(edgeList.get(i).a, event.getPoint())+distance(event.getPoint(), edgeList.get(i).b)-distance(edgeList.get(i).a, edgeList.get(i).b) < SIZE)
//if the (distance between starting point of that edgelist index and the current point) plus (the distance between the current point and ending point of that edgeList index)
//subtracted by the (distance of the starting and ending point of that edgeList index)(AB+BC-AC) is all less than the size
edgeList.remove(i);//then remove that index
}
}
}
public void mousePressed (MouseEvent event)//when the mouse is pressed down
{
if (mode == true)//if the mode is true
{
point1 = event.getPoint();//you start that point
}
}
public void mouseDragged (MouseEvent event)//when the mouse is dragged while pressed down
{
if (mode == true)//if the mode is true
{
point2 = event.getPoint();//then you get the second point
repaint();//must use this method because you are making a change to the component
}
}
public void mouseReleased (MouseEvent event)//when the mouse is released after being pressed (not to be confused with a click)
{
if (mode == true)//if the mode is true
{
point2 = event.getPoint();//then you set point2 to where that point landed
if (point1.x != point2.x && point1.y != point2.y)//if the points are not in the same spots
{
edgeList.add(new Edge(point1,point2));//then you add a new edgeList element to the ArrayList, by definition of Edge() object
repaint();//must use this method because changes to component have been made
}
}
}
public void mouseMoved (MouseEvent event)// if the mouse is moved, without it being clicked (not to be confused by mouseDragged)
{
point3 = event.getPoint();//point three is set to be where the mouse is at the current time(used this to find the distance of original edge drawn, keeping here incase I want to add)
repaint();//must use this method because changes were made to component
}
// Empty definitions for unused event methods.
public void mouseEntered (MouseEvent event){}//when the mouse enters a component
public void mouseExited (MouseEvent event) {}//when the mouse exits a component
private int distance(Point p1, Point p2) //private distance formula but inside of this class so i was able to use it for determining how to erase nodes and edges
{
return (int)Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));//square root of x cordinants of first point minus second point times its self plus the y coordinates
//of first point minus second point times itself
}
}
// Represents the graph edges
private class Edge //defines how edges are made
{
Point a, b;
public Edge(Point a, Point b)
{
this.a = a;
this.b = b;
}
}
private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
if (event.getSource()== create)
{
mode = true;
mode1 = 1;
}
if (event.getSource()==delete)
{
mode = !true;
mode1 = 2;
}
// Initializes graph adjacency matrix
for (int i=0; i<nodeList.size(); i++)
for (int j=0; j<nodeList.size(); j++) a[i][j]=0;
// Includes the edges in the graph adjacency matrix
for (int i=0; i<edgeList.size(); i++)
{
for (int j=0; j<nodeList.size(); j++)
if (distance(nodeList.get(j),edgeList.get(i).a)<=SIZE+3)
for (int k=0; k<nodeList.size(); k++)
if (distance(nodeList.get(k),edgeList.get(i).b)<=SIZE+3)
{
System.out.println(j+"->"+k);
a[j][k]=1;
}
}
if (event.getSource()==print)
{
// Prints the graph adjacency matrix
for (int i=0; i<nodeList.size(); i++)
{
for (int j=0; j<nodeList.size(); j++)
System.out.print(a[i][j]+"\t");
System.out.println();
}
}
}
// Euclidean distance function
private int distance(Point p1, Point p2)
{
return (int)Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
}
private class ButtonListener1 implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
if (event.getSource()== create)//if create button is selected
{
mode = true;//then the mode is set to true
mode1 = 1;//and mode1 is set to 1
}
}
}
private class ButtonListener2 implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
if (event.getSource()== delete)//if create button is selected
{
mode = !true;//then the mode is set to true
mode1 = 2;//and mode1 is set to 2, which would change the string that is output
}
}
}
private class ButtonListener3 implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
x1 = label.getText();
y1 = label1.getText();
x = Integer.parseInt(x1);
y = Integer.parseInt(y1);
System.out.println("Path from " +x+" to " +y);
System.out.println(findPath(x,y));
System.out.println(path);
}
}
You just never add the first node to path. You can do it when you check if path exists:
if (!found){
System.out.println("Path Does Not Exist");
} else {
path.add(0, x);
}
and then you also need to remove this block:
if (found) {
//System.out.println(x + " -> " + z);
path.add(0,z);
}
So complete method will look like
public static boolean findPath(int x, int y) //boolean method findpath that takes two ints as parameters
{
boolean found = false;
if (a[x][y]==1) //base case
{
//System.out.println(x + " -> " + y);//prints node -> node
found = true;//path is found
path.add(0,y);
}
else
{
int z;
for (z=0; !found& z<a.length; z++)
{
if (a[x][z]==1)
{
a[x][z]=2;
found = findPath(z,y);
}
/*if (found)
{
//System.out.println(x + " -> " + z);
path.add(0,z);
}*/
}
}
if (!found){
System.out.println("Path Does Not Exist");
} else {
path.add(0,x);
}
return found;
}
I also suggest you to look at Dijkstra's algorithm of finding the shortest path between nodes
I was able to find it, and of course it was the smallest mistake I missed, adding this because in case anyone else has the same problem I hope this helps. The main differences are in my buttonlistener3 class but I'll add the whole thing again just incase this helps anyone out.
import java.util.ArrayList;//all of these are the required to get classes from other packages
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JPanel;
public class GraphPanel extends JPanel
{
private final int SIZE = 10; // radius of each node
private double alpha; // double variable to define later
private JButton print, create, delete, path1; //allpath;//button variables
private JTextField label, label1;
boolean mode = true;//sets boolean for entire class to true, until later when I define it as false for certain methods to be complete
String x1, y1;
String str1 = new String("Mode Chosen Is : create" );//when you choose create it will print this instead
String str2 = new String("Mode Chosen Is : delete" );//when you choose delete it will print this
int x, y;
int mode1 = 1; // int mode1 variable, will comment why i need this when it is later defined/changed
private Point point1 = null, point2 = null, point3 = null;//makes pointers null, i added a third pointer to put the distance from the first drawn string at the top right, but no longer needed it
//keeping it incase I wanted to add it again
private ArrayList<Point> nodeList; // Graph nodes
private ArrayList<Edge> edgeList; // Graph edges
static private int[][] a = new int[100][100]; // Graph adjacency matrix
static ArrayList<Integer> path = new ArrayList<Integer>();
public GraphPanel()
{
nodeList = new ArrayList<Point>();//declares ArrayList<Point>
edgeList = new ArrayList<Edge>();//declares ArrayList<Edge>
GraphListener listener = new GraphListener();//assigns 'listener' to a Graphlistener object
addMouseListener (listener);//accepts and reads mouseListener methods to the GraphListener object
addMouseMotionListener (listener);//accepts and reads MouseMotionListener methods to the GraphListener object
label = new JTextField (1);
label1 = new JTextField (1);
//allpath = new JButton ("Find All Paths");
path1 = new JButton ("Find Path");//path button
create = new JButton("Create");//declares 'create' as a JButton object with text on it that says 'create'
delete = new JButton("Delete");//declares 'delete' as a JButton object with text that says 'delete'
JButton print = new JButton("Print adjacency matrix");//declares 'print' as a JButton object with text that reads 'print adjacency matrix'
print.addActionListener (new ButtonListener());//adds an action listener to the print button from the buttonlistener object
create.addActionListener (new ButtonListener1());//adds an actionListener to the create button from the buttonlistener1 object
delete.addActionListener (new ButtonListener2());//adds an actionliseneer to the delete button from the buttonlistener2 object
path1.addActionListener (new ButtonListener3());//adds button that will find path of nodes
//allpath.addActionListener (new ButtonListener4());
setBackground (Color.black);//sets backround as black
setPreferredSize (new Dimension(400, 300));//this is preferred size of graphpanel
add(print);//these next six add()'s add each button to GraphPanel
add(delete);//
add(create);//
//add(allpath);
add(path1);
add(label);
add(label1);
}
public static boolean findPath(int x, int y) //boolean method findpath that takes two ints as parameters
{
boolean found = false;
if (a[x][y]==1) //base case
{
//System.out.println(x + " -> " + y);//prints node -> node
found = true;//path is found
path.add(0,y);
}
else
{
int z;
for (z=0; !found& z<a.length; z++)
{
if (a[x][z]==1)
{
a[x][z]=2;
found = findPath(z,y);
}
if (found)
{
//System.out.println(x + " -> " + z);
path.add(0,z);
}
}
}
return found;
}
// public static ArrayList findPath1(int x, int y, ArrayList path)
// {
// System.out.println("CALL FIND: " + path);
// ArrayList path2 = null;
// if (a[x][y]==1)
// {
// ArrayList path3 = (ArrayList)path.clone();
// path3.add(y);
// System.out.println("EXIT BASE: " + path3);
// path2 = path3;
// }
// else
// {
// int z;
// for (z=0; z<a.length; z++)
// if (a[x][z]==1)
// {
// ArrayList path3 = (ArrayList)path.clone();
// path3.add(z);
// path3 = findPath1(z,y,path3);
// System.out.println("EXIT RECU: " + path);
// path2 = path3;
// }
// }
// return path2;
// }
// Draws the graph
public void paintComponent (Graphics page)
{
super.paintComponent(page);//using parent methods
// Draws the edge that is being dragged
page.setColor (Color.green);//setting color of what will be drawn to green
if (mode1 == 1)//if mode1 is set to 1, which is when the create button is selected
{
page.drawString(str1, 125, 75);//then this is printed
}
if (mode1 == 2)//if mode1 is set to 2, which is when the delete button is selected
{
page.drawString(str2, 125, 75);//then this is printed
}
if (point1 != null && point2 != null)//if and only if both points are not null
{
page.drawLine (point1.x, point1.y, point2.x, point2.y);//then it draws the line from between point1(x, y cords), and point2(x, y cords)
page.fillOval (point2.x-3, point2.y-3, 6, 6);//fills this oval by the rectangle it is specified from
}
// Draws the nodes
for (int i=0; i<nodeList.size(); i++) //for loop going through the nodeList ArrayList
{
page.setColor (Color.green);//color is set to green
page.fillOval (nodeList.get(i).x-SIZE, nodeList.get(i).y-SIZE, SIZE*2, SIZE*2);//fills oval by subtracting the SIZE from the x, y and setting the height and width as two times the SIZE
page.setColor (Color.black);//sets next line to black
page.drawString (String.valueOf(i), nodeList.get(i).x-SIZE/2, nodeList.get(i).y+SIZE/2);//writes inside the node what number index it is from the ArrayList with black text
}
// Draws the edges
for (int i=0; i<edgeList.size(); i++) // for loop going through the edgeList ArrayList
{
page.setColor (Color.green);//sets the next line to green
page.drawLine (edgeList.get(i).a.x, edgeList.get(i).a.y,edgeList.get(i).b.x, edgeList.get(i).b.y);//draws the line from x and y cords of where it starts to end
//page.fillOval (edgeList.get(i).b.x-3, edgeList.get(i).b.y-3, 6, 6);
//page.drawString (String.valueOf(point1.x*(point2.y-point3.y)+point2.x*(point3.y-point1.y)+point3.x*(point1.y-point2.y)), 5, 15);
alpha = Math.atan((double)(edgeList.get(i).b.y-edgeList.get(i).a.y)/(edgeList.get(i).b.x-edgeList.get(i).a.x));//alpha = b.y-a.y cords/b.x-a.x chords as a double variable
if (edgeList.get(i).a.x > edgeList.get(i).b.x)//if the x chord of a is greater than the x cordinate of b
{
alpha = alpha + Math.PI;//then alpha = previously defined alpha multiplied by PI
}
if (edgeList.get(i).a.x < edgeList.get(i).b.x && edgeList.get(i).a.y > edgeList.get(i).b.y)//if a.x is less than b.x and a.y is less than b.y
{
alpha = alpha + 2*Math.PI;//then alpha becomes the previously defined alpha multiplied by 2 PI
}
arrow(page,edgeList.get(i).b.x,edgeList.get(i).b.y,0,1.57-alpha);//arrow method, taking a x(edgeList.get(i).b.x, y(edgeList.get(i).b.y, length = 0 so arrow is at the
// very end of the drawn line subtracted by a double aplha
}
}
// arrow method to call when making an arrow
private void arrow(Graphics page, int x, int y, int len, double alpha)//arrow method, taking a x, y, length and double parameter
{
page.setColor (Color.green);//sets arrow to green
int x1 = x+(int)(len*Math.sin(alpha));//x1 is set to x plus length *sin of alpha
int y1 = y+(int)(len*Math.cos(alpha));//y1 is set to y plus the length * cosin of alpha
page.drawLine (x, y, x1, y1);//drawa the x and y, and then previously defined x1, and y1
page.drawLine (x1, y1, x1+(int)(20*Math.sin(alpha+2.5)), y1+(int)(20*Math.cos(alpha+2.5)));//arithmatic to draw one side of the line for the arrow
page.drawLine (x1, y1, x1+(int)(20*Math.sin(alpha+3.7)), y1+(int)(20*Math.cos(alpha+3.7)));//arithmatic to draw the corresponding line for the arrow to complete the arrow
}
// The listener for mouse events.
private class GraphListener implements MouseListener, MouseMotionListener//GraphListener which implements MouseListener and MouseMotionListener, meaning it takes methods from both of those classes
{
public void mouseClicked (MouseEvent event)//when mouse is clicked and released
{
if (mode == true)//if the mode is true
{
nodeList.add(event.getPoint());//the current point to the nodeList
repaint();//whenever you change the look of a componenet you call this method
}
if (mode == false)//if boolean mode is false
{
for (int i=0; i<nodeList.size(); i++)//for loop to go through the nodeList ArrayList
{
if (distance(nodeList.get(i), event.getPoint()) < SIZE)//if the distance between a specific node and the pointer is less than the radius of the node (SIZE)
nodeList.remove(i);//then remove that node
}
for (int i=0; i<edgeList.size(); i++)
{
if (distance(edgeList.get(i).a, event.getPoint())+distance(event.getPoint(), edgeList.get(i).b)-distance(edgeList.get(i).a, edgeList.get(i).b) < SIZE)
//if the (distance between starting point of that edgelist index and the current point) plus (the distance between the current point and ending point of that edgeList index)
//subtracted by the (distance of the starting and ending point of that edgeList index)(AB+BC-AC) is all less than the size
edgeList.remove(i);//then remove that index
}
}
}
public void mousePressed (MouseEvent event)//when the mouse is pressed down
{
if (mode == true)//if the mode is true
{
point1 = event.getPoint();//you start that point
}
}
public void mouseDragged (MouseEvent event)//when the mouse is dragged while pressed down
{
if (mode == true)//if the mode is true
{
point2 = event.getPoint();//then you get the second point
repaint();//must use this method because you are making a change to the component
}
}
public void mouseReleased (MouseEvent event)//when the mouse is released after being pressed (not to be confused with a click)
{
if (mode == true)//if the mode is true
{
point2 = event.getPoint();//then you set point2 to where that point landed
if (point1.x != point2.x && point1.y != point2.y)//if the points are not in the same spots
{
edgeList.add(new Edge(point1,point2));//then you add a new edgeList element to the ArrayList, by definition of Edge() object
repaint();//must use this method because changes to component have been made
}
}
}
public void mouseMoved (MouseEvent event)// if the mouse is moved, without it being clicked (not to be confused by mouseDragged)
{
point3 = event.getPoint();//point three is set to be where the mouse is at the current time(used this to find the distance of original edge drawn, keeping here incase I want to add)
repaint();//must use this method because changes were made to component
}
// Empty definitions for unused event methods.
public void mouseEntered (MouseEvent event){}//when the mouse enters a component
public void mouseExited (MouseEvent event) {}//when the mouse exits a component
private int distance(Point p1, Point p2) //private distance formula but inside of this class so i was able to use it for determining how to erase nodes and edges
{
return (int)Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));//square root of x cordinants of first point minus second point times its self plus the y coordinates
//of first point minus second point times itself
}
}
// Represents the graph edges
private class Edge //defines how edges are made
{
Point a, b;
public Edge(Point a, Point b)
{
this.a = a;
this.b = b;
}
}
private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
if (event.getSource()== create)
{
mode = true;
mode1 = 1;
}
if (event.getSource()==delete)
{
mode = !true;
mode1 = 2;
}
// Initializes graph adjacency matrix
for (int i=0; i<nodeList.size(); i++)
for (int j=0; j<nodeList.size(); j++) a[i][j]=0;
// Includes the edges in the graph adjacency matrix
for (int i=0; i<edgeList.size(); i++)
{
for (int j=0; j<nodeList.size(); j++)
if (distance(nodeList.get(j),edgeList.get(i).a)<=SIZE+3)
for (int k=0; k<nodeList.size(); k++)
if (distance(nodeList.get(k),edgeList.get(i).b)<=SIZE+3)
{
System.out.println(j+"->"+k);
a[j][k]=1;
}
}
if (event.getSource()==print)
{
// Prints the graph adjacency matrix
for (int i=0; i<nodeList.size(); i++)
{
for (int j=0; j<nodeList.size(); j++)
System.out.print(a[i][j]+"\t");
System.out.println();
}
}
}
// Euclidean distance function
private int distance(Point p1, Point p2)
{
return (int)Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
}
private class ButtonListener1 implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
if (event.getSource()== create)//if create button is selected
{
mode = true;//then the mode is set to true
mode1 = 1;//and mode1 is set to 1
}
}
}
private class ButtonListener2 implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
if (event.getSource()== delete)//if create button is selected
{
mode = !true;//then the mode is set to true
mode1 = 2;//and mode1 is set to 2, which would change the string that is output
}
}
}
private class ButtonListener3 implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
x1 = label.getText();
y1 = label1.getText();
x = Integer.parseInt(x1);
y = Integer.parseInt(y1);
System.out.println("Path from " +x+" to " +y);
System.out.println(findPath(x,y));
path.add(0,x);
System.out.println(path);
path.clear();
}
}
// private class ButtonListener4 implements ActionListener
// {
// public void actionPerformed (ActionEvent event)
// {
// x1 = label.getText();
// y1 = label1.getText();
// x = Integer.parseInt(x1);
// y = Integer.parseInt(y1);
// System.out.println("Path from " +x+" to " +y);
// System.out.println(findPath1(x,y,path));
// System.out.println(path);
// }
// }
}
Back again with another problem that has stumped me completely and cannot for the life of me fix.
So I had previously posted a question about my Java game I am making for a class that with the help of #MadProgrammer was fixed...mostly. Now there is a new problem that needs a post all to it's own
Previous Post:
Rows and columns with multidimensional array java
Problem:
In the code below I have it set up to loop through the variables x and y to make rows and columns on a jPanel. Each time it loops through it should randomly mark the "x,y" location with one of the "terrains" so that later it can "paint" that location with the appropriate colored 20x20 square.
The code runs great except for one thing, it looks like it skips the very first "if statement" that marks the "terran[0]" which is "floor". When the code is ran it "paints" the other three "terrains" and not a single "floor" "terrain".
I have looked for a solution on these posts but no success:
Java if statement is skipped
If statement being skipped during execution
Java - for loops being skipped
Java if-statement being skipped
Here is a working piece of code that results in the problem at hand:
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class gamePanel extends JPanel
{
public gamePanel()
{
setBounds(115,93,480,480);
}
private Random generator = new Random();
int floor = 0; //initializes the variable floor to zero for later use
int dirt = 1;
int stone = 2;
int water = 3;
int width = 24;
int height = 24;
int x, y; // my x & y variables for coordinates
int[][] coords = new int[width][height]; //my array that I want to store the coordinates for later use in painting
int[] terrain = {floor, dirt, stone, water}; //my terrain that will determine the color of the paint
public void mapGen() //what should mark/generate the JPanel
{
for(x = 0; x < width; x++)
{
for(y = 0; y < height; y++)
{
int z = generator.nextInt(20);// part of the randomization
if(z <= 10)
{
coords[x][y] = terrain[0]; //should mark the coordinates as floor
}
if(z == 11)
{
coords[x][y] = terrain[3];//should mark the coordinates as water
}
if(z >= 12 && z <= 16)
{
coords[x][y] = terrain[2];//should mark the coordinates as stone
}
if(z >= 17 && z <= 19)
{
coords[x][y] = terrain[1];//should mark the coordinates as dirt
}
coords[0][0] = terrain[0]; // sets coordinate 0,0 to floor //need to have these always be floor
coords[23][23] = terrain[0]; // sets coordinate 24,24 to floor //^^^^^^^^^^
}
}
}
#Override
public void paintComponent(Graphics g)//what will paint each 20x20 square on the grid what it is assigned
{
super.paintComponent(g);
for(int x = 0; x < width; x++)
{
for(int y = 0; y < height; y++)
{
mapGen();
if(coords[x][y] == terrain[floor])//should paint the floor color at marked coordinates
{
g.setColor(Color.white);
g.fillRect((x*20), (y*20), 20, 20);
}
if(coords[x][y] == terrain[dirt]);//should paint the dirt color at marked coordinates
{
g.setColor(new Color(135,102,31));
g.fillRect((x*20), (y*20), 20, 20);
}
if(coords[x][y] == terrain[stone])//should paint the stone color at marked coordinates
{
g.setColor(new Color(196,196,196));
g.fillRect((x*20),(y*20),20,20);
}
if(coords[x][y] == terrain[water])//should paint the water color at marked coordinates
{
g.setColor(new Color(85,199,237));
g.fillRect((x*20),(y*20),20,20);
}
}
}
}//end paintComponent
public static void main(String[] args)
{
gamePanel panel = new gamePanel();
JFrame frame = new JFrame();
frame.setSize(500,550);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.setVisible(true);
}//end main
}// end gamePanel
Please keep in mind that I am a novice programmer and I am still learning. So anything that is not considered "basic" code please explain in detail.
Im having difficulty drawing a Sub Image of a Buffered Image everytime the Mouse Pointer Location equals that of the each border of the JPanel. The problem is that the BufferedImage that is equals the SubImage wont display
Here is the JPanel the initialization might not be correct Im still learning the components of Java and 2D graphics.
public class Map extends JPanel implements MouseListener, MouseMotionListener {
private final int SCR_W = 800;
private final int SCR_H = 600;
private int x;
private int y;
private int dx;
private int dy;
String dir = "C:\\imgs\\war\\";
private BufferedImage map_buffer;
public BufferedImage scr_buffer;
public void initScreen(int x, int y, int stage){
if(stage == 0){
try{ map_buffer = ImageIO.read(new File(dir + "map" + stage + ".jpg" ));
}catch(Exception error) { System.out.println("Error: cannot read tileset image.");
}
}
scr_buffer = map_buffer.getSubimage(x, y, SCR_W, SCR_H);
}
#Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
if(scr_buffer == null)
initScreen(x, y, 0);
g.drawImage(scr_buffer, 0, 0, this);
}
boolean isLeftBorder = false;
boolean isRightBorder = false;
boolean isTopBorder = false;
boolean isBottomBorder = false;
public Map(){
addMouseListener(new MouseAdapter() {
public void mouseMoved(MouseEvent e) {
/**
* Check location of mouse pointer if(specified_edge)move(scr_buffer)
*
*/
System.out.println("MouseMove: " + e.getPoint().getX() + " , " + e.getPoint().getY());
if(e.getPoint().getX() == SCR_W)isRightBorder = true;
if(e.getPoint().getY() == SCR_H)isBottomBorder = true;
if(e.getPoint().getX() == 0 && e.getPoint().getY() == SCR_H)isLeftBorder = true;
if(e.getPoint().getY() == 0 && e.getPoint().getX() == SCR_W)isTopBorder = true;
if(e.getPoint().getX() != 0 && e.getPoint().getX() != SCR_W
&& e.getPoint().getY() != 0 && e.getPoint().getY() != SCR_H){
isLeftBorder = false;
isRightBorder = false;
isTopBorder = false;
isBottomBorder = false;
}
if(isRightBorder){ x += 2; repaint(); }
if(isBottomBorder){ y -= 2; repaint(); }
if(isLeftBorder){ x -= 2; repaint();}
if(isTopBorder){ y += 2; repaint(); }
}
});
}
}
In the main I init a JFrame to contain the Panel all im getting is a error
public static void main(String[] args) {
JFrame f = new JFrame("War");
f.setSize(800, 600);
f.setLayout(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Map m = new Map();
f.getContentPane().add(f);
f.setVisible(true);
}
In order to detect mouse movement you should use a MosuseMotionListener, while technically MouseAdapter implements this, you need to register it with the JPanel correctly
Instead of using addMouseListener, you'll want to use addMouseMotionListener instead
I'd also be worried about the use of SRC_W and SRC_H, as you can't guarantee the size of the panel. Instead, you should be using getWidth and getHeight, which will tell you the actual size of the component
You can improve the chances of obtaining the size you want by overriding the getPreferredSize and return the size you would like. You'd then use pack on the frame to wrap the frame about it
f.getContentPane().add(f); is adding the frame to itself, it should probably be more like f.getContentPane().add(m);
f.setLayout(null); will prevent any of the child components from been sized and positioned and is best avoid, just get rid of it.
Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify
This scr_buffer = map_buffer.getSubimage(x, y, SCR_W, SCR_H); is also a little dangerous, as it could be asking for more of the image then is available, you should be testing to see if x + SCR_W < image width (and same goes for the height)
I don't know if this deliberate or not, but you never reset the "border" flags, so once set, they will always be true...
addMouseMotionListener(new MouseAdapter() {
public void mouseMoved(MouseEvent e) {
/**
* Check location of mouse pointer if(specified_edge)move(scr_buffer)
*
*/
isRightBorder = false;
isBottomBorder = false;
isTopBorder = false;
isLeftBorder = false;
You may also want to have a "space" around the edge, which when the mouse enters it, it will set the border flags, for example...
if (e.getPoint().getX() >= getWidth() - 4) {
isRightBorder = true;
}
if (e.getPoint().getY() >= getHeight() - 4) {
isBottomBorder = true;
}
if (e.getPoint().getX() <= 4) {
isLeftBorder = true;
}
if (e.getPoint().getY() <= 4) {
isTopBorder = true;
}
Your logic for the vertical movement is wrong, when the mouse is within the bottom border, it should add to the y position and subtract when it's within the top border...
if (isBottomBorder) {
y += 2;
}
if (isTopBorder) {
y -= 2;
}
You need to perform some range checking after you've modified the x/y positions to make sure you're not request for a portion of the image which is not available...
if (x < 0) {
x = 0;
} else if (x + getWidth() > map_buffer.getWidth()) {
x = map_buffer.getWidth() - getWidth();
}
if (y < 0) {
y = 0;
} else if (y + getHeight() > map_buffer.getHeight()) {
y = map_buffer.getHeight() - getHeight();
}
There is a logic error within the initScreen method, src_buffer is never set to null, meaning that once it has a "sub image", it never tries to obtain a new one (also, you shouldn't be loading the map_buffer in there either).
scr_buffer = null;
repaint();
Thank you for you time and understanding.
Inside mouse moved
if (e.getPoint().getX() >= getWidth() - 4) {
isRightBorder = true; // unnecessary
scr_buffer = null;
x = x + 2;
repaint();
}
I have a project to have a red dot inside the first box of a maze i randomly generated and the dot is supposed to follow its way through the boxes and find the end of the maze. Now if it hits a dead end, its supposed to go back to where its path started and not go back down that path, that leads to a dead end. i made it so each box represents the #1, this way when the red dot travels over the box, it increments by 1, so it can realize where its been. its always supposed to go to the lowest number possible so it can never go back to the dead ends its already been to. i am able to reach the end of the maze but i come into 2 problems.
the method i wrote that does all this work is the solve() function. I cant understand why 2 things happen...
1st thing is that when the red dot comes to a branch of dead ends, sometimes itll just go to one dead end, to a different dead end , back to the same dead end.. traveling to the same 'numbers' when im trying to have it only go towards the boxes that have 1's or just the lower numbers.
2nd thing is that once it inevitably reaches the end of the maze.. the red dot goes into the green area, where i specifically say in the while loop, it can not be in a green box.
if M[y][x] = 0, its a green box and if its = 1 its a black box. anything higher than 1 will also be inside the box.
your help is highly appreciated as ive been stuck on this problem for hours and cant seem to find out the problem.
the problem persists in the solve() method
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
import javax.swing.*;
public class mazedfs extends JFrame implements KeyListener
{
/* default values: */
private static int bh = 16; // height of a graphical block
private static int bw = 16; // width of a graphical block
private int mh = 41; // height and width of maze
private int mw = 51;
private int ah, aw; // height and width of graphical maze
private int yoff = 40; // init y-cord of maze
private Graphics g;
private int dtime = 40; // 40 ms delay time
byte[][] M; // the array for the maze
public static final int SOUTH = 0;
public static final int EAST = 1;
public static final int NORTH = 2;
public static final int WEST = 3;
public static boolean showvalue = true; // affects drawblock
// args determine block size, maze height, and maze width
public mazedfs(int bh0, int mh0, int mw0)
{
bh = bw = bh0; mh = mh0; mw = mw0;
ah = bh*mh;
aw = bw*mw;
M = new byte[mh][mw]; // initialize maze (all 0's - walls).
this.setBounds(0,0,aw+10,10+ah+yoff);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try{Thread.sleep(500);} catch(Exception e) {} // Synch with system
this.addKeyListener(this);
g = getGraphics(); //g.setColor(Color.red);
setup();
}
public void paint(Graphics g) {} // override automatic repaint
public void setup()
{
g.setColor(Color.green);
g.fill3DRect(0,yoff,aw,ah,true); // fill raised rectangle
g.setColor(Color.black);
// showStatus("Generating maze...");
digout(mh-2,mw-2); // start digging!
// digout exit
M[mh-1][mw-2] = M[mh-2][mw-1] = 1;
drawblock(mh-2,mw-1);
solve(); // this is the function you will write for parts 1 and 2
play(); // for part 3
}
public static void main(String[] args)
{
int blocksize = bh, mheight = 41, mwidth = 41; // need to be odd
if (args.length==3)
{
mheight=Integer.parseInt(args[0]);
mwidth=Integer.parseInt(args[1]);
blocksize=Integer.parseInt(args[2]);
}
mazedfs W = new mazedfs(blocksize,mheight,mwidth);
}
public void drawblock(int y, int x)
{
g.setColor(Color.black);
g.fillRect(x*bw,yoff+(y*bh),bw,bh);
g.setColor(Color.yellow);
// following line displays value of M[y][x] in the graphical maze:
if (showvalue)
g.drawString(""+M[y][x],(x*bw)+(bw/2-4),yoff+(y*bh)+(bh/2+6));
}
void drawdot(int y, int x)
{
g.setColor(Color.red);
g.fillOval(x*bw,yoff+(y*bh),bw,bh);
try{Thread.sleep(dtime);} catch(Exception e) {}
}
/////////////////////////////////////////////////////////////////////
/* function to generate random maze */
public void digout(int y, int x)
{
M[y][x] = 1; // digout maze at coordinate y,x
drawblock(y,x); // change graphical display to reflect space dug out
int dir = (int)(Math.random()*4);
for (int i=0;i<4;i++){
int [] DX = {0,0,2,-2};
int [] DY = {-2,2,0,0};
int newx = x + DX[dir];
int newy = y + DY[dir];
if(newx>=0 && newx<mw && newy>=0 && newy<mh && M[newy][newx]==0)
{
M[y+DY[dir]/2][x+DX[dir]/2] = 1;
drawblock(y+DY[dir]/2,x+DX[dir]/2);
digout(newy,newx);
}
dir = (dir + 1)%4;}
} // digout
public void solve() // This is the method i need help with.
{
int x=1, y=1;
drawdot(y,x);
while(y!=mh-1 || x!=mw-1 && M[y][x]!=0){
int min = 0x7fffffff;
int DX = 0;
int DY = 0;
if (y-1>0 && min>M[y-1][x] && M[y-1][x]!=0){
min = M[y-1][x];
DX = 0;
DY = -1;
}//ifNORTH
if (y+1>0 && min>M[y+1][x] && M[y+1][x]!=0){
min = M[y+1][x];
DY = 1;
DX = 0;
}//ifSOUTH
if (x-1>0 && min>M[y][x-1] && M[y][x-1]!=0){
min = M[y][x-1];
DX = -1;
DY = 0;
}//ifWEST
if (x+1>0 && min>M[y][x+1] && M[y][x+1]!=0){
min = M[y][x+1];
DX = 1;
DY = 0;
}//ifEAST
M[y][x]++;
drawblock(y,x);
x = x+DX;
y = y+DY;
drawdot(y,x);
}//while
// modify this function to move the dot to the end of the maze. That
// is, when the dot reaches y==mh-2, x==mw-2
} // solve
///////////////////////////////////////////////////////////////
/// For part three (save a copy of part 2 version first!), you
// need to implement the KeyListener interface.
public void play() // for part 3
{
// code to setup game
}
// for part 3 you may also define some other instance vars outside of
// the play function.
// for KeyListener interface
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e) // change this one
{
int key = e.getKeyCode(); // code for key pressed
System.out.println("YOU JUST PRESSED KEY "+key);
}
} // mazedfs
////////////
// define additional classes (stack) you may need here.
The issue causing the second problem you are facing (dot moving to green box) lies in the while loop conditiony!=mh-1 || x!=mw-1 && M[y][x]!=0 . The condition evaluates to y!=mh-1 ||(x!=mw-1 && M[y][x]!=0) since && has higher precedence over the || and || just needs one of its operand to be true. In your case, y!=mh-1 is still ture at the end of maze. Hence the loop continues and the dot moves into green area. To fix the issue modify the condition as (y!=mh-1 || x!=mw-1) && M[y][x]!=0. Hope this helps.
i have a program that generates a random maze. In the maze a red dot is displayed and the red dot flashes on by each block in the maze. all the blocks in the maze are == 1 and if the red dot goes through that block, it increments ++. the red dot goes in the direction towards the lowest number, that way it wont stay in an infinite loop by a dead end. once it reaches the end, ive solved the maze.
This is where im stumped, im trying to print the red dot to find the optimal route all the way back to the beginning where it started. I used a stack class that i made to record all the y and x components of where the red dot traveled. im able to traceback every where the red dot has gone but that isnt the optimal solution.
My question is how could i print the red dot tracing back in only the optimal path. My idea of solving this would be to check and see if the coordinates of a stack where visited before, if so..find the last case where it was visited and print the red dot up until that point. that way itll never deal with the dead ends it traveled.
the method solve() is what contains the traceback and solving technique for the red dot to travel through the maze and back.
Im not the greatest programmer and im still learning how to use stacks, ive been stumped for hours and dont know how to approach this. Please be kind and explain how you would do it using the stack i made. Thank you
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
import javax.swing.*;
public class mazedfs extends JFrame implements KeyListener
{
/* default values: */
private static int bh = 16; // height of a graphical block
private static int bw = 16; // width of a graphical block
private int mh = 41; // height and width of maze
private int mw = 51;
private int ah, aw; // height and width of graphical maze
private int yoff = 40; // init y-cord of maze
private Graphics g;
private int dtime = 40; // 40 ms delay time
byte[][] M; // the array for the maze
public static final int SOUTH = 0;
public static final int EAST = 1;
public static final int NORTH = 2;
public static final int WEST = 3;
public static boolean showvalue = true; // affects drawblock
// args determine block size, maze height, and maze width
public mazedfs(int bh0, int mh0, int mw0)
{
bh = bw = bh0; mh = mh0; mw = mw0;
ah = bh*mh;
aw = bw*mw;
M = new byte[mh][mw]; // initialize maze (all 0's - walls).
this.setBounds(0,0,aw+10,10+ah+yoff);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try{Thread.sleep(500);} catch(Exception e) {} // Synch with system
this.addKeyListener(this);
g = getGraphics(); //g.setColor(Color.red);
setup();
}
public void paint(Graphics g) {} // override automatic repaint
public void setup()
{
g.setColor(Color.green);
g.fill3DRect(0,yoff,aw,ah,true); // fill raised rectangle
g.setColor(Color.black);
// showStatus("Generating maze...");
digout(mh-2,mw-2); // start digging!
// digout exit
M[mh-1][mw-2] = M[mh-2][mw-1] = 1;
drawblock(mh-2,mw-1);
solve(); // this is the function you will write for parts 1 and 2
play(); // for part 3
}
public static void main(String[] args)
{
int blocksize = bh, mheight = 41, mwidth = 41; // need to be odd
if (args.length==3)
{
mheight=Integer.parseInt(args[0]);
mwidth=Integer.parseInt(args[1]);
blocksize=Integer.parseInt(args[2]);
}
mazedfs W = new mazedfs(blocksize,mheight,mwidth);
}
public void drawblock(int y, int x)
{
g.setColor(Color.black);
g.fillRect(x*bw,yoff+(y*bh),bw,bh);
g.setColor(Color.yellow);
// following line displays value of M[y][x] in the graphical maze:
if (showvalue)
g.drawString(""+M[y][x],(x*bw)+(bw/2-4),yoff+(y*bh)+(bh/2+6));
}
void drawdot(int y, int x)
{
g.setColor(Color.red);
g.fillOval(x*bw,yoff+(y*bh),bw,bh);
try{Thread.sleep(dtime);} catch(Exception e) {}
}
/////////////////////////////////////////////////////////////////////
/* function to generate random maze */
public void digout(int y, int x)
{
M[y][x] = 1; // digout maze at coordinate y,x
drawblock(y,x); // change graphical display to reflect space dug out
int dir = (int)(Math.random()*4);
for (int i=0;i<4;i++){
int [] DX = {0,0,2,-2};
int [] DY = {-2,2,0,0};
int newx = x + DX[dir];
int newy = y + DY[dir];
if(newx>=0 && newx<mw && newy>=0 && newy<mh && M[newy][newx]==0)
{
M[y+DY[dir]/2][x+DX[dir]/2] = 1;
drawblock(y+DY[dir]/2,x+DX[dir]/2);
digout(newy,newx);
}
dir = (dir + 1)%4;}
} // digout
/* Write a routine to solve the maze.
Start at coordinates x=1, y=1, and stop at coordinates
x=mw-1, y=mh-2. This coordinate was especially dug out
after the program called your digout function (in the "actionPerformed"
method).
*/
public void solve()
{
int x=1, y=1;
Stack yourstack = null;
drawdot(y,x);
while(y!=mh-2 || x!=mw-2 && M[y][x]!=0){
int min = 0x7fffffff;
int DX = 0;
int DY = 0;
if (y-1>0 && min>M[y-1][x] && M[y-1][x]!=0){
min = M[y-1][x];
DX = 0;
DY = -1;
}//ifNORTH
if (y+1>0 && min>M[y+1][x] && M[y+1][x]!=0){
min = M[y+1][x];
DY = 1;
DX = 0;
}//ifSOUTH
if (x-1>0 && min>M[y][x-1] && M[y][x-1]!=0){
min = M[y][x-1];
DX = -1;
DY = 0;
}//ifWEST
if (x+1>0 && min>M[y][x+1] && M[y][x+1]!=0){
min = M[y][x+1];
DX = 1;
DY = 0;
}//ifEAST
M[y][x]++;
drawblock(y,x);
x = x+DX;
y = y+DY;
drawdot(y,x);
yourstack = new Stack(y,x,yourstack); // creates new stack for each coordinate travelled
}//while
while(yourstack != null){
yourstack = yourstack.tail;
drawdot(yourstack.y,yourstack.x); // this will traceback every box ive been through
}//while
} // solve
class Stack{
int x;
int y;
Stack tail;
public Stack(int a, int b, Stack t){
y = a;
x=b;
tail=t;
}
}//stackclass
///////////////////////////////////////////////////////////////
/// For part three (save a copy of part 2 version first!), you
// need to implement the KeyListener interface.
public void play() // for part 3
{
// code to setup game
}
// for part 3 you may also define some other instance vars outside of
// the play function.
// for KeyListener interface
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e) // change this one
{
int key = e.getKeyCode(); // code for key pressed
System.out.println("YOU JUST PRESSED KEY "+key);
}
} // mazedfs
////////////
// define additional classes (stack) you may need here.
when you trace your path back you currently just go back to your stack - but thats not the shortest path...
...whenever you can go back check the values of M around you:
byte valueOfFieldNorthOfXY = M[x][y-1]; //x/y is your current position
byte valueOfFieldWesthOfXY = M[x-1][y];
byte ...;
byte ...; //and so on...
while the first while-loop in your solve-methode simplay solves the maze by flooding it the second while-method is for going back...
and when i say flooding i mean: each time a field has been passed by the 'walker' the value of M[x][y] has been increased by 1 (when the 'walker' has walked 3x over field 5/6 then the value from M[5][6] = 3)
so when you go back from the end (#40/50) to the start (#1/1), you do this algorith:
1) i stand on x/y
2) i check the values north/east/south/west of me
2a) if i come from north, then i ignore the north field
2 ) ... and so on...
2d) if i come from west , then i ignore the west field
3) i pick that direction, where the value is the least
4) put the current field int your packPathStack and walk to
the 'best' direction
5) repeat (go back to Nr.1) until you are #1/1
example
? 4 ? //i'm standing at X (x/y)
2 x f //? are values from walls or not yet considerd
? ? ? //f is where i come from
--> i pick direction WEST(2) because thats less than NORTH(4)
implement this algorithm and you a NEW stack i call it yourPathBackStack
Stack yourPathBackStack = new Stack();
while(x != 1 && y != 1 ){ //x/y = 1/1 = start - do it until you are there (Nr. 5)
// 1) i stand on x/y
int x = yourPathBackStack.x;
int y = yourPathBackStack.y;
// 2) i check the values north/east/south/west of me
byte valueOfFieldNorthOfXY = ... ; //as seen above
// 2a) if i come from north, then i ignore the north field
if (yourstack.tail.x == x && yourstack.tail.y == y-1){
//check - i DO come from north
//make then valueOfFieldNorthOfXY very high
valueOfFieldNorthOfXY = 100; //it's the same as ignoring
}
// 2 ) ... and so on...
// 2d) if i come from west , then i ignore the west field
if (yourstack.tail.x == x-1 && ...// as seen above
// 3) i pick that direction, where the value is the least
int direction = NORTH; //lets simply start with north;
byte maxValue = 100;
if ( valueOfFieldNorthOfXY < maxValue ){ //First north
maxValue = valueOfFieldNorthOfXY;
direction = NORTH;
}
if ( valueOfFieldWestOfXY < maxValue ){ //Then east
maxValue = valueOfFieldWestOfXY ;
direction = WEST;
}
//then the also other directions
if ( value ... //as seen above
// 4) put the current field int your yourPathBackStack and walk to
// the 'best' direction
int newx = x;
int newy = y;
if (direction == NORTH){ //direction has been caclulated above
newy = newy - 1;
}
if (direc ... //with all other direction)
yourPathBackStack = new Stack(newx, newy, yourPathBackStack );
drawdot(yourPathBackStack.y,yourPathBackStack.x);
}