Problem bei Übergeben eines Objekts

Hallo,

es geht hier um einen Irrgarten, welcher auch vom Compiler durchschritten werden soll. Und der zufallsbedingte Weg, den er nimmt (natürlich mit gewissen logischen Einschränkungen), soll auch auf ein JPanel gezeichnet werden. Seine "current Location" soll als "X" eingezeichnet werden.

Im Konstruktor von Visual übergebe ich dem VisualPanel-Objekt das Maze-Objekt, in dem nur das Layrinth und das Ziel sichtbar ist, eben wie in der Textdatei. Dies geschieht einmalig. Das VisualPanel-Objekt weiß also jetzt, wie das Labyrinth am Anfang aussah.

Jetzt übergebe ich in Visual.draw() immer wieder eine neue Location (currLoc) dem VisualPanel-Objekt, die ich vom Tracking-Objekt erhalte. Das aktuelle Labyrinth (currMaze) besteht also aus der anfänglichen Labyrinth und der immer wieder neuen Location. Also muss ich immer nur in das anfängliche Labyrinth die neue Location reinschreiben.

Das mache ich doch auch in VisualPanel.setCurrMaze() oder nicht ?

Aber irgendwie verändert sich mein anfängliches Labyrinth auch durch die Zuweisung "currMaze = maze;" sodass anstatt nur einem "X", nämlich der momentanen Position, auch alle vorherigen Positionen eingezeichnet bleiben. Und hier bin ich ratlos.

Irrgartenspiel.java
Java:
import java.io.*;
public class Irrgartenspiel
{
  public static void main(String[] args) throws FileNotFoundException, IOException, InterruptedException
  {
    Player player1 = new Player();
    player1.play();
  }
}

Player.java
Java:
import java.io.*;
public class Player
{
  char[][] maze;
  int[] startLoc;
  int[] destLoc;
  
  public Player() throws FileNotFoundException, IOException
  {
    Maze m = new Maze();
    maze = m.getMaze();
    startLoc = m.getStartLoc();
    destLoc = m.getDestLoc();
  }
  public void play() throws FileNotFoundException, IOException, InterruptedException
  {
    Tracking t = new Tracking(maze,startLoc,destLoc);
    Visual v = new Visual();
    int count = 0;    
    while (!t.hitDest()) 
    { 
      t.checkPossDirect();
      v.draw(t.getCurrLoc());
      t.move();
      count++;      
    } // end of while
    System.out.println("Schritte: " + count);
  }
}

Maze.java
Java:
import java.io.*;
public class Maze
{
  char[][] maze;
  
  //Einlesen des Labyrinths aus Textdatei
  public Maze() throws FileNotFoundException, IOException
  {
    String[] line;
    int countLine = 0;
    int i,j,k;
    FileReader fr = new FileReader("maze.txt");
    BufferedReader br = new BufferedReader(fr);
    
    br.mark(100000);
    while (br.readLine()!=null) 
    { 
      countLine++; 
    } // end of while
    br.reset();
    
    line = new String[countLine];
    for (i=0;i<countLine;i++) 
    {
      line[i] = br.readLine();
    } // end of for
    br.close();
    
    k = 0;
    maze = new char[line[0].length()][countLine];
    for (i=0;i<countLine;i++) 
    {
      for (j=0;j<line[0].length();j++) 
      {
        maze[j][i] = line[k].charAt(j);
        //System.out.print(maze[j][i]); 
      } // end of for
      //System.out.println();
      k++;
    } // end of for
  }
  
  public char[][] getMaze()
  {
    return maze;
  }
  public int[] getStartLoc()
  {
    int i,j;
    int[] startLoc = new int[2];
    for (i=0;i<maze.length;i++) 
    {
      for (j=0;j<maze[0].length;j++) 
      {
        if (maze[i][j]=='S') 
        {
          startLoc[0] = j; //x-Koordinate (rechts,links)
          startLoc[1] = i; //y-Koordinate (oben,unten)
        } // end of if
      } // end of for
    } // end of for
    return startLoc;
  }
  public int[] getDestLoc()
  {
    int i,j;
    int[] destLoc = new int[2];
    for (i=0;i<maze.length;i++) 
    {
      for (j=0;j<maze[0].length;j++) 
      {
        if (maze[i][j]=='Z') 
        {
          destLoc[0] = j; //x-Koordinate (rechts,links)
          destLoc[1] = i; //y-Koordinate (oben,unten)
        } // end of if
      } // end of for
    } // end of for
    return destLoc; 
  }
}

Tracking.java
Java:
import java.util.ArrayList;

public class Tracking
{
  char[][] maze;
  int[] currLoc;
  int[] destLoc;
  boolean[] possDirect = new boolean[4]; //0 = links, 1 = oben, 2 = rechts, 3 = unten
  ArrayList<Location> moveList = new ArrayList<Location>();
  int countBackTrack = 0;
  
  public Tracking(char[][] maze, int[] startLoc, int[] destLoc)
  {
    this.maze = maze;
    this.currLoc = startLoc;
    this.destLoc = destLoc;
  }
  
  public boolean hitDest()
  {
    if (maze[currLoc[0]][currLoc[1]]=='Z') 
    {
      return true; 
    } // end of if
    else 
    {
      return false;
    } // end of if-else
  }
  
  public void checkPossDirect()
  {
    int i;
    for (i=0;i<possDirect.length;i++) 
    {
      possDirect[i] = true; 
    } // end of for
    
    if (hitWall(currLoc[0]-1,currLoc[1])) 
    {
      possDirect[0] = false;
    } // end of if
    if (hitWall(currLoc[0],currLoc[1]-1)) 
    {
      possDirect[1] = false;  
    } // end of if
    if (hitWall(currLoc[0]+1,currLoc[1])) 
    {
      possDirect[2] = false;
    } // end of if
    if (hitWall(currLoc[0],currLoc[1]+1)) 
    {
      possDirect[3] = false;
    } // end of if
    
    moveList.add(new Location(currLoc[0],currLoc[1]));
  }
  
  public boolean hitWall(int x, int y)
  {
    if (maze[x][y]=='-' || maze[x][y] == '|' || maze[x][y] == 'X') 
    {
      return true;
    } // end of if
    else 
    {
      return false;
    } // end of if-else
  }
  
  public void move()
  {
    int i;
    System.out.print("currLoc: ");
    for (i=0;i<currLoc.length;i++) 
    {
      System.out.print(currLoc[i]+" "); 
    } // end of for
    System.out.println();
    
    maze[currLoc[0]][currLoc[1]] = 'X';
    
    int countPossDirects=0;
    for (i=0;i<possDirect.length;i++) 
    {
      if (possDirect[i]) 
      {
        countPossDirects++;
      } // end of if
    } // end of for
    
    if (countPossDirects>0) 
    {
      countBackTrack = 0;
      int direct = (int)(Math.random()*4);
      while (!possDirect[direct]) 
      { 
        direct = (int)(Math.random()*4);  
      } // end of while
      switch (direct) 
      {
        case 0 : currLoc[0] = currLoc[0]-1;
        break;
        case 1 : currLoc[1] = currLoc[1]-1;
        break;
        case 2 : currLoc[0] = currLoc[0]+1;
        break;
        case 3 : currLoc[1] = currLoc[1]+1;
        break;
        default :  System.out.println("dafuq?");
        break;
      } // end of switch
    } // end of if
    else 
    {
      countBackTrack++;
      if (countBackTrack<2) 
      {
        moveList.remove(moveList.size()-1);
        currLoc[0] = moveList.get(moveList.size()-1).getX();
        currLoc[1] = moveList.get(moveList.size()-1).getY();
      } // end of if
      else 
      {
        moveList.remove(moveList.size()-1);
        moveList.remove(moveList.size()-1);
        currLoc[0] = moveList.get(moveList.size()-1).getX();
        currLoc[1] = moveList.get(moveList.size()-1).getY();  
      } // end of if-else
    } // end of if-else
  }
  
  public Location getCurrLoc()
  {
    return moveList.get(moveList.size()-1);  
  }
}

Visual.java
Java:
import java.awt.*;
import javax.swing.*;
import java.io.*;

public class Visual extends JFrame
{
  Maze m;
  char[][] maze;
  String[] mazeLines;
  VisualPanel panel = new VisualPanel();
  
  public Visual() throws FileNotFoundException, IOException
  {
    super("Irrgartenspiel");
    m = new Maze();
    maze = m.getMaze();
    mazeLines = getMazeLines();
    panel.setMaze(maze);
    int i;
    for (i=0;i<mazeLines.length;i++) 
    {
      System.out.println("mazeLines["+i+"] " + mazeLines[i]);
    } // end of for
    setBounds(10,10,300,300);
    setLayout(null);
    panel.setBounds(0,0,300,300);
    add(panel);
    this.setVisible(true);
  }

  public String[] getMazeLines()
  {
    int i,j,k=0;
    String[] mL = new String[maze[0].length];
    for (i=0;i<maze[0].length;i++) 
    {
      mL[i] = "" + maze[0][i];
    } // end of for
    for (i=0;i<maze[0].length;i++) 
    {
      for (j=0;j<maze.length-1;j++) 
      {
        mL[k] = mL[k] + "" + maze[j+1][i];  
      } // end of for
      k++;
    } // end of for
    
    return mL; 
  }
  
  public void draw(Location loc) throws InterruptedException
  {
    int x = loc.getX();
    int y = loc.getY();
    int i;
    char[] c = mazeLines[y].toCharArray();
    c[x] = 'X';
    String newLine = "" + c[0];
    for (i=1;i<c.length;i++) 
    {
      newLine = newLine + "" + c[i];
    } // end of for
    mazeLines[y] = newLine;
    /*for (i=0;i<mazeLines.length;i++) 
    {
    System.out.println(mazeLines[i]);
    } // end of for*/   
    mazeLines = getMazeLines();
    panel.setCurrLoc(loc);
    panel.setCurrMaze();
    repaint();
    Thread.sleep(1000);
  }
}

VisualPanel.java
Java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*; 
import javax.swing.event.*;
 
 public class VisualPanel extends JPanel
{
  char[][] maze;
  char[][] currMaze;
  public String[] currMazeString;
  Location loc;
  int x = 0;
  
  @Override
  public void paintComponent(Graphics g)
  {
    super.paintComponent(g);
    if (x!=0) 
    {
      g.setFont(new Font("MS Gothic", Font.PLAIN, 18));
      int i;
      for (i=0;i<currMazeString.length;i++)
      {
        g.drawString(currMazeString[i],10,20*i+20);  
      } // end of for
    } // end of if
    System.out.println("x");
    System.out.println(x);
    
  }
  
  public void setMaze(char[][] maze)
  {
    this.maze = maze;
    x++;
  }
  
  public void setCurrMaze()
  {
    currMaze = maze;
    int x = loc.getX();
    int y = loc.getY();
    currMaze[x][y] = 'X';
    currMazeString = new String[currMaze[x].length];
    int i,j;
    String s = "" + currMaze[0][0];
    System.out.println("+++");
    for (i=0;i<maze[0].length;i++) 
    {
      for (j=0;j<maze.length;j++) 
      {
        System.out.print(maze[j][i]);  
      } // end of for
      System.out.println();
    } // end of for
    System.out.println("+++");
    for (i=0;i<currMaze[0].length;i++) 
    {
      for (j=1;j<currMaze.length;j++) 
      {
        currMazeString[i] = s + "" + currMaze[j][i];
        s = currMazeString[i];
      } // end of for
      s = "" + currMaze[0][i];
    } // end of for
    
    
  }
  
  public void setCurrLoc(Location loc)
  {
    this.loc = loc; 
  }
}

Location.java
Java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*; 
import javax.swing.event.*;
 
 public class VisualPanel extends JPanel
{
  char[][] maze;
  char[][] currMaze;
  public String[] currMazeString;
  Location loc;
  int x = 0;
  
  @Override
  public void paintComponent(Graphics g)
  {
    super.paintComponent(g);
    if (x!=0) 
    {
      g.setFont(new Font("MS Gothic", Font.PLAIN, 18));
      int i;
      for (i=0;i<currMazeString.length;i++)
      {
        g.drawString(currMazeString[i],10,20*i+20);  
      } // end of for
    } // end of if
    System.out.println("x");
    System.out.println(x);
    
  }
  
  public void setMaze(char[][] maze)
  {
    this.maze = maze;
    x++;
  }
  
  public void setCurrMaze()
  {
    currMaze = maze;
    int x = loc.getX();
    int y = loc.getY();
    currMaze[x][y] = 'X';
    currMazeString = new String[currMaze[x].length];
    int i,j;
    String s = "" + currMaze[0][0];
    System.out.println("+++");
    for (i=0;i<maze[0].length;i++) 
    {
      for (j=0;j<maze.length;j++) 
      {
        System.out.print(maze[j][i]);  
      } // end of for
      System.out.println();
    } // end of for
    System.out.println("+++");
    for (i=0;i<currMaze[0].length;i++) 
    {
      for (j=1;j<currMaze.length;j++) 
      {
        currMazeString[i] = s + "" + currMaze[j][i];
        s = currMazeString[i];
      } // end of for
      s = "" + currMaze[0][i];
    } // end of for
    
    
  }
  
  public void setCurrLoc(Location loc)
  {
    this.loc = loc; 
  }
}
 
Zuletzt bearbeitet:
es geht hier um einen Irrgarten, welcher auch vom Compiler durchschritten werden soll

öhm.... du weißt aber schon das der Compiler lediglich deinen (Java) Code in Maschinen-lesbaren Bytecode übersetzt oder ?

Wer soll da was durchschreiten ^^ ?

Aber irgendwie verändert sich mein anfängliches Labyrinth auch durch die Zuweisung "currMaze = maze;" sodass anstatt nur einem "X", nämlich der momentanen Position, auch alle vorherigen Positionen eingezeichnet bleiben. Und hier bin ich ratlos.

Das wird wohl daran liegen, wie du das ganze zeichnest, da du die Bereich der bereits bemalt wurde nicht "säuberst" werden halt alle "x" gemalt , und nicht nur das aktuellste.

also musst du hergehen und ähnlich wie bei einer Animation , die vorherigen bemalten bereiche, einfach z.B per fillRect(x,y, width, height); clearen.
 
Zuletzt bearbeitet:

Neue Themen


Zurück
Oben