In my program I have a class called Cell, defined like so:
public class Cell {
private int x;
private int y;
public Cell (int x, int y) {
this.x = x;
this.y = y;
}
#Override
public boolean equals (Object o) {
boolean result = false;
if (o instanceof Cell) {
Cell other = (Cell) o;
result = (this.x == other.x && this.y == other.y)
}
return result;
}
#Override
public int hashCode() {
int result = x;
result = 31 * result + y;
return result;
}
}
I have a Grid class, like so (many methods cut out and variable names simplified):
public class Grid {
private Set<Cell> cellArray;
public Grid() {
cellArray = new HashSet<Cell>();
}
public Set<Cell> getCellArray() {
return cellArray;
}
public void addCellArray(Cell cell) {
cellArray.add(cell)
}
}
In my main body of code, I take in a grid object, like so:
public class Controller {
private Grid grid;
public Controller (Grid grid) (
this.grid = grid;
Then, I have a series of loops that look like this:
private set<Cell> cellArray = grid.getCellArray();
boolean endLoop = false;
do {
x = randomGenerator.nextInt(10);
y = randomGenerator.nextInt(10);
for (int i = 0; i < length; i++) {
if (cellArray.contains(new Cell(x, y+i))) {
continue;
}
}
for (int j = 0; j < length; j++) {
cellArray.add(new Cell(x, y+i));
}
endLoop = true;
} while(!endLoop);
I'm aware it's a very messy, with too much instantiation going on (and if anyone has pointers to make it cleaner, feel free to point them out) - however, the main issue is the fact that the first for loop is meant to check if the cellArray contains the items - it doesn't seem to be doing this.
There's no error message, no null pointer or anything like that. I've tried debugging it and have seen it compare two cells with identical x and y values, without proceeding to the continue statement to start the do while loop again.
I am assuming this is because even though they have identical values, they are different 'objects' and so aren't coming back as equal.
How could I fix this and get them to equate to one another if their values are the same?
Your continue statement continues the inner for-loop (which is quite useless here). You probably want to continue the outer loop: continue outerLoop;, with the label outerLoop: put in front of do {.
As the Java API states, the contains method should rely on your equals method, so object equality should work as you expect it.
Related
I have a matrix that represents a grid and would like to find out all possible places an object can move to.
An object can only move horizontally or vertically.
Let's assume that the example below is the grid I'm looking at, which is represented as a 2d matrix. The object is the *, the 0s are empty spaces that an object can move to, and the 1s are walls which the object cannot jump over or go on to.
What is the best way to find all possible movements of this object provided that it can only move horizontally or vertically?
I'd like to print a message saying: "There are 9 places the object can go to." The 9 is for the example below, but I would like it to work for any configuration of the below grid. So all I have to do is give the current coordinates of the * and it will give me the number of possible positions it can move to.
A thing to note is that the *'s original position is not considered in the calculations, which is why for the example below the message would print 9 and not 10.
I have a isaWall method that tells me if the cell is a wall or not. The isaWall method is in a Cell class. Each cell is represented by its coordinates. I looked into using Algorithms like BFS or DFS, but I didn't quite understand how to implement them in this case, as I am not too familiar with the algorithms. I thought of using the Cells as nodes of the graph, but wasn't too sure how to traverse the graph because from the examples I saw online of BFS and DFS, you would usually have a destination node and source node (the source being the position of the *), but I don't really have a destination node in this case. I would really appreciate some help.
00111110
01000010
100*1100
10001000
11111000
EDIT: I checked the website that was recommend in the comments and tried to implement my own version. It unfortunately didn't work. I understand that I have to expand the "frontier" and I basically just translated the expansion code to Java, but it still doesn't work. The website continues explaining the process, but in my case, there is no destination cell to go to. I'd really appreciate an example or a clearer explanation pertaining to my case.
EDIT2: I'm still quite confused by it, can someone please help?
While BFS/DFS are commonly used to find connections between a start and end point, that isn't really what they are. BFS/DFS are "graph traversal algorithms," which is a fancy way of saying that they find every point reachable from a start point. DFS (Depth First Search) is easier to implement, so we'll use that for your needs (note: BFS is used when you need to find how far away any point is from the start point, and DFS is used when you only need to go to every point).
I don't know exactly how your data is structured, but I'll assume your map is an array of integers and define some basic functionality (for simplicity's sake I made the start cell 2):
Map.java
import java.awt.*;
public class Map {
public final int width;
public final int height;
private final Cell[][] cells;
private final Move[] moves;
private Point startPoint;
public Map(int[][] mapData) {
this.width = mapData[0].length;
this.height = mapData.length;
cells = new Cell[height][width];
// define valid movements
moves = new Move[]{
new Move(1, 0),
new Move(-1, 0),
new Move(0, 1),
new Move(0, -1)
};
generateCells(mapData);
}
public Point getStartPoint() {
return startPoint;
}
public void setStartPoint(Point p) {
if (!isValidLocation(p)) throw new IllegalArgumentException("Invalid point");
startPoint.setLocation(p);
}
public Cell getStartCell() {
return getCellAtPoint(getStartPoint());
}
public Cell getCellAtPoint(Point p) {
if (!isValidLocation(p)) throw new IllegalArgumentException("Invalid point");
return cells[p.y][p.x];
}
private void generateCells(int[][] mapData) {
boolean foundStart = false;
for (int i = 0; i < mapData.length; i++) {
for (int j = 0; j < mapData[i].length; j++) {
/*
0 = empty space
1 = wall
2 = starting point
*/
if (mapData[i][j] == 2) {
if (foundStart) throw new IllegalArgumentException("Cannot have more than one start position");
foundStart = true;
startPoint = new Point(j, i);
} else if (mapData[i][j] != 0 && mapData[i][j] != 1) {
throw new IllegalArgumentException("Map input data must contain only 0, 1, 2");
}
cells[i][j] = new Cell(j, i, mapData[i][j] == 1);
}
}
if (!foundStart) throw new IllegalArgumentException("No start point in map data");
// Add all cells adjacencies based on up, down, left, right movement
generateAdj();
}
private void generateAdj() {
for (int i = 0; i < cells.length; i++) {
for (int j = 0; j < cells[i].length; j++) {
for (Move move : moves) {
Point p2 = new Point(j + move.getX(), i + move.getY());
if (isValidLocation(p2)) {
cells[i][j].addAdjCell(cells[p2.y][p2.x]);
}
}
}
}
}
private boolean isValidLocation(Point p) {
if (p == null) throw new IllegalArgumentException("Point cannot be null");
return (p.x >= 0 && p.y >= 0) && (p.y < cells.length && p.x < cells[p.y].length);
}
private class Move {
private int x;
private int y;
public Move(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
}
Cell.java
import java.util.LinkedList;
public class Cell {
public final int x;
public final int y;
public final boolean isWall;
private final LinkedList<Cell> adjCells;
public Cell(int x, int y, boolean isWall) {
if (x < 0 || y < 0) throw new IllegalArgumentException("x, y must be greater than 0");
this.x = x;
this.y = y;
this.isWall = isWall;
adjCells = new LinkedList<>();
}
public void addAdjCell(Cell c) {
if (c == null) throw new IllegalArgumentException("Cell cannot be null");
adjCells.add(c);
}
public LinkedList<Cell> getAdjCells() {
return adjCells;
}
}
Now to write our DFS function. A DFS recursively touches every reachable cell once with the following steps:
Mark current cell as visited
Loop through each adjacent cell
If the cell has not already been visited, DFS that cell, and add the number of cells adjacent to that cell to the current tally
Return the number of cells adjacent to the current cell + 1
You can see a visualization of this here. With all the helper functionality we wrote already, this is pretty simple:
MapHelper.java
class MapHelper {
public static int countReachableCells(Map map) {
if (map == null) throw new IllegalArgumentException("Arguments cannot be null");
boolean[][] visited = new boolean[map.height][map.width];
// subtract one to exclude starting point
return dfs(map.getStartCell(), visited) - 1;
}
private static int dfs(Cell currentCell, boolean[][] visited) {
visited[currentCell.y][currentCell.x] = true;
int touchedCells = 0;
for (Cell adjCell : currentCell.getAdjCells()) {
if (!adjCell.isWall && !visited[adjCell.y][adjCell.x]) {
touchedCells += dfs(adjCell, visited);
}
}
return ++touchedCells;
}
}
And that's it! Let me know if you need any explanations about the code.
I want to get some values from one function inside of different class and use it on Main class. But it seems like I am doing somethings wrong.
public class test {
public static int enkucukbul ( double[] x){ // this method finds the smallest index
return IntStream.range(0, x.length)
.mapToObj(i -> i)
.min(Comparator.comparing(i -> x[i]))
.orElse(Integer.MIN_VALUE);
}
public static double tabu(double x, int isayi) {
Random rrandom = new Random();
float r;
double[] fxdizi = new double[4];
double[] xdizi = new double[4];
double[] hareket = new double[4];
for (int j = 0; j < isayi; j++) {
r = rrandom.nextFloat();
hareket[0] = x + 2 * r;
hareket[1] = x + 4 * r;
hareket[2] = x - 2 * r;
hareket[3] = x - 4 * r;
xdizi[0] = hareket[0];
xdizi[1] = hareket[1];
xdizi[2] = hareket[2];
xdizi[3] = hareket[3];
for (int i = 0; i < 4; i++) {
if (xdizi[i] <= 1) {
fxdizi[i] = xdizi[i] * xdizi[i];
} else {
fxdizi[i] = Math.pow((xdizi[i] - 3), 2) - 3;
}
} // for dongusu
int minIndex = enkucukbul(fxdizi); // found the smallest index
return x;
return minIndex;
return j;
return xdizi[minIndex];
return fxdizi[minIndex];
x = xdizi[minIndex]; // we found the smallest x
} // all things
}
}
Also my Main class:
public class Main {
public static void main(String[] args) {
test ts = new test();
System.out.println(ts.tabu(7.26,2));
}
}
I just want to get the values in return statements like x, j ...etc. But I get error "java:unreachable statement" on every return statement and also "java:missing return statement" in the end. Where do i do wrong?
Extra Note: I'm sorry for the localized variable names since this is my optimization class assignment
You've wrote multiple return statements in following lines :
return x;
return minIndex;
return j;
return xdizi[minIndex];
return fxdizi[minIndex];
It is not acceptable in java.
Also you are missing return value at the end of method tabuoutside of for loop. I see several logical mistakes in your code.
You need to refactor your code, also if you can explain what you are trying to achieve it will be much more clear.
In java a method can have only one return statement. You can't have more than one return statement per method unless used in an if-else block.
As the compiler tells you the remaining statements are unreachable after the first return statement.
As per your latest comment, you can create a wrapper class to hold multiple values that you want to return. For eg :
class Calculation {
public double a;
public double b;
// getters and setters
}
Now from your method you could capture the values of the variables and then store them in an object of the class created above :
public static Calculation tabu(double x, int y) {
Calculation cal = new Calculation();
// do something with x and y and other things
cal.setA(x);
cal.setB(y);
return cal;
}
I am currently trying to create an array of objects out of a char Array.
My problem is that somehow the for-loop seems to insert wrong values into the objects. And I just can't figure out what's going wrong. I was trying to fix it for hours but nothing worked.
For example: The if inside of the for loop detects it is at the right char '#' and j=6, k=5. (checked it with System.out.println().)
I tell it to create an object inside for the GameObject array and give it the appropriate coordinates (x=k, y=j). But for some reason the coordinates turn out to be (k=)x=5 and (j=)y=5 instead?!
Both values always seem to be identical for some reason. No matter what, it's always 1,1; 2,2; 3,3 etc... The number is basically k (=the intended y-coordinate) taken twice. It seems to not access the "j" correctly?
The object array itself is all right, just the values inside of the objects turn out wrong.
In code, the method looks like this:
public static GameObject[][] initGO(int x, int y) throws IOException {
GameObject[][] goMap = new GameObject[x][y];
LoadFile loader = new LoadFile();
char[] ch = loader.readChar("src\\control\\bla.txt");
int i = 0;
while (i < ch.length) {
for (int j = 0; j < y; j++) {
for (int k = 0; k < x; k++) {
if (ch[i] == '#') {
goMap[k][j] = new Player(k, j, "Player");
}
i++;
}
}
}
return goMap;
}
Player just looks like this:
public class Player extends GameObject {
public Player(int x, int y, String name) {
super(x, y, name);
this.x=y;
this.y=y;
this.name = name;
}
}
Thanks a lot for anyone trying to help!
You made a simple copy-paste-edit error when setting variables. Look at this part of the code:
public Player(int x, int y, String name) {
super(x, y, name);
this.x=y; // <-- This should be "this.x=x;"
this.y=y;
this.name = name;
}
i am currently working on this for personal gratification and would like some advice on how i can make this code faster :
I have one ArrayList composed of an object note, which have coordinates and color value stored in it.
Each "note" is created in real time during the rendering call.
I have made this function :
void keyPressed() {
if (key == 's' || key == 'S') {
PImage img = createImage(posX, specSize, RGB);
for(int x = 0; x < posX; x++){
for(int y = 0; y < specSize; y++){
for(int i = 0; i < notes.size(); i++){
if( (notes.get(i).getX() == x)
&& (notes.get(i).getY() == y) ){
int loc = x + y*posX;
img.pixels[loc] = color(notes.get(i).getR(),
notes.get(i).getG(), notes.get(i).getB());
}
}
}
}
img.updatePixels();
img.save("outputImage.png");
}
}
So when i press the "S" key, i run a loop on the width and height because they can be different in each run, and then on my arrayList and get the corresponding "note" with it's x and y position.
then i write my picture file.
As you can imagine, this is really, really, slow...
Around 5 to 6 minutes for a 1976x256px file.
For me it's okay but it would be great to shorten this a little.
Is there a way to optimize this three loops?
If you need more code, please let me know it's a small code and i don't mind.
How about this?
void keyPressed() {
if (key == 's' || key == 'S') {
PImage img = createImage(posX, specSize, RGB);
for(int i = 0; i < notes.size(); i++){
int x = notes.get(i).getX();
int y = notes.get(i).getY();
int loc = x + y*posX;
img.pixels[loc] = color(notes.get(i).getR(),
notes.get(i).getG(), notes.get(i).getB());
}
img.updatePixels();
img.save("outputImage.png");
}
}
Update:
Not sure what the type of notes is, but something like this might work too. Insert the correct type for one element of Notes into the for loop where I wrote ???.
void keyPressed() {
if (key == 's' || key == 'S') {
PImage img = createImage(posX, specSize, RGB);
for(??? note : notes ){
int x = note.getX();
int y = note.getY();
int loc = x + y * posX;
img.pixels[loc] = color(note.getR(), note.getG(), note.getB());
}
img.updatePixels();
img.save("outputImage.png");
}
}
Can clone notes (and any other object that is used to save) and do this in a different thread so its async to UI. the code will take same or more time but the user can use the rest of the app. Clone is neccesary as you want a snap shot of state when save was clicked.
Dont make a thread put use a ThreadPoolExecutor with one thread max. In the run method could apply what David suggested - one loop instead of two.
Convert your list of notes into a structure mapped like
Map<Integer, Map<Integer, Note> noteMap
Then replace your inner-most loop with a single call like
yNoteMap = note.get(x);
if (yNoteMap != null) {
note = yNoteMap.get(y);
if (note != null) {
// do stuff with note
}
}
Your computational complexity will go from about O(n^3) to O(n^2).
Create a class such as Point with two properties of x and y and implement proper equals and hashcode methods as:
public class Point {
private final int x;
private final int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
#Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Point point = (Point) o;
if (x != point.x)
return false;
if (y != point.y)
return false;
return true;
}
#Override
public int hashCode() {
int result = x;
result = 31 * result + y;
return result;
}
}
now put the Point as key of a map, and find your points using this, so you don't have to iterate over the whole lists.
I have a method with a flag argument. I think that passing a boolean to a method is a bad practice (complicates the signature, violates the "each method does one thing" principle). I think splitting the method into two different methods is better. But if I do that, the two methods would be very similar (code duplication).
I wonder if there are some general techniques for splitting methods with a flag argument into two separate methods.
Here's the code of my method (Java):
int calculateNumOfLiveOrDeadNeighbors(Cell c, int gen, boolean countLiveOnes) {
int x = c.getX();
int y = c.getY();
CellState state;
int aliveCounter = 0;
int deadCounter = 0;
for (int i = x - 1; i <= x + 1; i++) {
for (int j = y - 1; j <= y + 1; j++) {
if (i == x && j == y)
continue;
state = getCell(i, j).getCellState(gen);
if (state == CellState.LIVE || state == CellState.SICK){
aliveCounter++;
}
if(state == CellState.DEAD || state == CellState.DEAD4GOOD){
deadCounter++;
}
}
}
if(countLiveOnes){
return aliveCounter;
}
return deadCounter;
}
If you don't like the boolean on your signature, you could add two different methods without it, refactoring to private the main one:
int calculateNumOfLiveNeighbors(Cell c, int gen) {
return calculateNumOfLiveOrDeadNeighbors(c, gen, true);
}
int calculateNumOfDeadNeighbors(Cell c, int gen) {
return calculateNumOfLiveOrDeadNeighbors(c, gen, false);
}
OR
you could code a Result Class or int array as output parameter for storing both the results; this would let you get rid of the annoying boolean parameter.
I guess it depends on every single case.
In this example you have two choices, in my opinion.
Say you want to split the call calculateNumOfLiveOrDeadNeighbors()
in two:
calculateNumOfLiveNeighbors()
and
calculateNumOfDeadNeighbors()
You can use Template Method to move the loop to another method.
You can use it to count dead / alive cells in the two methods.
private int countCells(Cell c, int gen, Filter filter)
{
int x = c.getX();
int y = c.getY();
CellState state;
int counter = 0;
for (int i = x - 1; i <= x + 1; i++)
{
for (int j = y - 1; j <= y + 1; j++)
{
if (i == x && j == y)
continue;
state = getCell(i, j).getCellState(gen);
if (filter.countMeIn(state))
{
counter++;
}
}
}
return counter;
}
private interface Filter
{
boolean countMeIn(State state);
}
public int calculateNumOfDeadNeighbors(Cell c, int gen)
{
return countCells(c, gen, new Filter()
{
public boolean countMeIn(CellState state)
{
return (state == CellState.DEAD || state == CellState.DEAD4GOOD);
}
});
}
public int calculateNumOfLiveNeighbors(Cell c, int gen)
{
return countCells(c, gen, new Filter()
{
public boolean countMeIn(CellState state)
{
return (state == CellState.LIVE || state == CellState.SICK);
}
});
}
It's cumbersome, maybe not even worth the pain. You can, alternatively, use a monad to store the results of your statistics calculation and then use getDeadCounter() or getLiveCounter() on the monad, as many suggested already.
you can try to extract the common functionality in a single method and only use the specific functionality
you can create a private method with that flag, and invoke it from the two public methods. Thus your public API will not have the 'complicated' method signature, and you won't have duplicated code
make a method that returns both values, and choose one in each caller (public method).
In the example above I think the 2nd and 3rd options are more applicable.
Seems like the most semantically clean approach would be to return a result object that contains both values, and let the calling code extract what it cares about from the result object.
Like Bozho said: But but combine point 2 and 3 in the other way arround:
Create a (possible private method) that returns both (living and dead) and (only if you need dead or alive seperate in the most cases) then add two methods that pick dead or both out of the result:
DeadLiveCounter calcLiveAndDead(..) {}
int calcLive(..) { return calcLiveAndDead(..).getLive; }
int calcDead(..) { return calcLiveAndDead(..).getDead; }
IMO, this so-called "each method does one thing" principle needs to be applied selectively. Your example is one where, it is probably better NOT to apply it. Rather, I'd just simplify the method implementation a bit:
int countNeighbors(Cell c, int gen, boolean countLive) {
int x = c.getX();
int y = c.getY();
int counter = 0;
for (int i = x - 1; i <= x + 1; i++) {
for (int j = y - 1; j <= y + 1; j++) {
if (i == x && j == y)
continue;
CellState s = getCell(i, j).getCellState(gen);
if ((countLive && (s == CellState.LIVE || s == CellState.SICK)) ||
(!countLive && (s == CellState.DEAD || s == CellState.DEAD4GOOD))) {
counter++;
}
}
}
return counter;
}
In terms of using refactoring, some things you can do are;
copy the method and create two version, one with true hard coded and the other false hard coded. Your refactoring tools should help you inline this constant and remove code as required.
recreate the method which calls the right true/false method as above for backward compatibility. You can then inline this method.
I would be inclined here to keep a map from the CellState enum to count, then add the LIVE and the SICK or the DEAD and the DEAD4GOOD as needed.
int calculateNumOfLiveOrDeadNeighbors(Cell c, int gen, boolean countLiveOnes) {
final int x = c.getX();
final int y = c.getY();
final HashMap<CellState, Integer> counts = new HashMap<CellState, Integer>();
for (CellState state : CellState.values())
counts.put(state, 0);
for (int i = x - 1; i < x + 2; i++) {
for (int j = y - 1; j < y + 2; j++) {
if (i == x && j == y)
continue;
CellState state = getCell(i, j).getCellState(gen);
counts.put(state, counts.get(state) + 1);
}
}
if (countLiveOnes)
return counts.get(CellState.LIVE) + counts.get(CellState.SICK);
else
return counts.get(CellState.DEAD) + counts.get(CellState.DEAD4GOOD);
}
have a private method which is an exact copy and paste of what you currently have.
Then create two new methods, each with a more descriptive name that simply call your private method with appropriate boolean