I am working on this algorithm and cant figure out why I am always getting a null return. The code adds to two arrays fx, fy it collects the x and y positions that will complete the maze with the shortest path. The issue is that nothing is being added to the array. Any suggestions?
public class Maze {
public List<Integer> fx;
public List<Integer> fy;
public int listSize;
public Point p;
public int[][] cells;
public Maze(int x, int y, int[][] cells) {
p = getPathBFS(x, y, cells);
this.cells = cells;
fx = new ArrayList<>();
fy = new ArrayList<>();
addPoint();
}
private static class Point {
int x;
int y;
Point parent;
public Point(int x, int y, Point parent) {
this.x = x;
this.y = y;
this.parent = parent;
}
public Point getParent() {
return this.parent;
}
}
public static Queue<Point> q = new LinkedList<>();
public Point getPathBFS(int x, int y, int[][] cells) {
q.add(new Point(x, y, null));
while (!q.isEmpty()) {
Point p = q.remove();
if (cells[p.x][p.y] == 9) {
return p;
}
if (isFree(p.x + 1, p.y, cells.length, cells)) {
cells[p.x][p.y] = 2;
Point nextP = new Point(p.x + 1, p.y, p);
q.add(nextP);
}
if (isFree(p.x - 1, p.y, cells.length, cells)) {
cells[p.x][p.y] = 2;
Point nextP = new Point(p.x - 1, p.y, p);
q.add(nextP);
}
if (isFree(p.x, p.y + 1, cells.length, cells)) {
cells[p.x][p.y] = 2;
Point nextP = new Point(p.x, p.y + 1, p);
q.add(nextP);
}
if (isFree(p.x, p.y - 1, cells.length, cells)) {
cells[p.x][p.y] = 2;
Point nextP = new Point(p.x, p.y - 1, p);
q.add(nextP);
}
}
return null;
}
public static boolean isFree(int x, int y, int cellLength, int[][] cells) {
if ((x >= 0 && x < cellLength) && (y >= 0 && y < cells[x].length) && (cells[x][y] == 0 || cells[x][y] == 9)) {
return true;
}
return false;
}
public synchronized void addPoint() {
System.out.println(p);
while ((p != null)) {
System.out.println("x is " + p.x + " - y is " + p.y);
fy.add(p.x);
fx.add(p.y);
p = p.getParent();
}
}
int getListSize() {
listSize = fx.size() - 1;
return listSize;
}
}
Related
This code works fine when the first creation of a new maze object. However when the BFS is complete and the game is played again( by giving the user the option to play again) the algorithm does not work as effectively. The end goal is still met however x and y coordinates are added to the array that are not part of the shortest path. I have never seen my first run ever complete the search incorrectly, however subsequent instances do. It seems as if every time the new maze object is created the maze is left affected by the previous instance. Any input?
public class Maze {
public static List<Integer> fx;
public static List<Integer> fy;
public static int listSize;
public static Point p;
public Maze(int x, int y) {
p = getPathBFS(x, y);
fx = new ArrayList<>();
fy = new ArrayList<>();
addPoint();
System.out.print("next");
}
private static class Point {
int x;
int y;
Point parent;
public Point(int x, int y, Point parent) {
this.x = x;
this.y = y;
this.parent = parent;
}
public Point getParent() {
return this.parent;
}
}
public static Queue<Point> q = new LinkedList<>();
public static Point getPathBFS(int x, int y) {
q.add(new Point(x, y, null));
while (!q.isEmpty()) {
Point p = q.remove();
if (Level.cells[p.x][p.y] == 9) {
return p;
}
if (isFree(p.x + 1, p.y)) {
Level.cells[p.x][p.y] = 2;
Point nextP = new Point(p.x + 1, p.y, p);
q.add(nextP);
}
if (isFree(p.x - 1, p.y)) {
Level.cells[p.x][p.y] = 2;
Point nextP = new Point(p.x - 1, p.y, p);
q.add(nextP);
}
if (isFree(p.x, p.y + 1)) {
Level.cells[p.x][p.y] = 2;
Point nextP = new Point(p.x, p.y + 1, p);
q.add(nextP);
}
if (isFree(p.x, p.y - 1)) {
Level.cells[p.x][p.y] = 2;
Point nextP = new Point(p.x, p.y - 1, p);
q.add(nextP);
}
}
return null;
}
public static boolean isFree(int x, int y) {
if ((x >= 0 && x < Level.cells.length) && (y >= 0 && y < Level.cells[x].length) && (Level.cells[x][y] == 0 || Level.cells[x][y] == 9)) {
return true;
}
return false;
}
public static void addPoint() {
while ((p != null)) {
System.out.println("x is " + p.x + " - y is " + p.y);
fy.add(p.x);
fx.add(p.y);
p = p.getParent();
}
}
public static int getListSize() {
listSize = fx.size();
return listSize;
}
}
It seems as if every time the new maze object is created the maze is
left affected by the previous instance
Yes, exactly. All of your fields are declared as static. Static fields are common across all instances, not just one. You can remove the static keyword in all (or almost all) instances.
public static List<Integer> fx;
public static List<Integer> fy;
public static int listSize;
public static Point p;
It looks like your Level class is suffering from the same problems:
Level.cells.length
I'd look into what 'static' actually means because it seems like you're using it without really understanding it.
I am trying to make a path finding program to play snake and I have ran into a problem. I made this class to use the A* algorithm. It accumulates more cells than are on the board and never finds the target.
import java.awt.Color;
import java.util.Collections;
import java.util.LinkedList;
public class AStar {
public static class Cell implements Comparable<Cell> {
public int F, H, G, x, y;
public Cell parent;
public Cell(int x, int y) {
this.x = x;
this.y = y;
}
#Override
public int compareTo(Cell o) {
return F < o.F ? -1 : F > o.F ? 1 : 0;
}
}
public static boolean at(int x, int y, LinkedList<Block> l) {
for (int i = 0; i < l.size(); i++) {
Block b = l.get(i);
if (b != null && b.getX() == x && b.getY() == y) {
return true;
}
}
return false;
}
public static LinkedList<Block> compute(int x, int y, Block t, LinkedList<Block> s, int w, int h) {
LinkedList<Block> path = new LinkedList<Block>();
LinkedList<Cell> open = new LinkedList<Cell>();
LinkedList<Cell> closed = new LinkedList<Cell>();
Cell current = getCell(s.getFirst().getX(), s.getFirst().getY(), t, null);
current.F = 0;
open.add(current);
while (true) {
current = open.poll();
System.out.println("X=" + current.x + " Y=" + current.y + " FOODX=" + t.getX() + " FOODY=" + t.getY());
closed.add(current);
if (current.x == t.getX() && current.y == t.getY()) {
break;
}
Cell c = getCell(current.x + 20, current.y, t, current);
if (!at(c.x, c.y, s) && !closed.contains(c) && !(c.x > w)) {
if (c.F < current.F || !open.contains(c)) {
open.add(c);
}
}
c = getCell(current.x - 20, current.y, t, current);
if (!at(c.x, c.y, s) && !closed.contains(c) && !(c.x < 0)) {
if (c.F < current.F || !open.contains(c)) {
open.add(c);
}
}
c = getCell(current.x, current.y + 20, t, current);
if (!at(c.x, c.y, s) && !closed.contains(c) && !(c.y > h)) {
if (c.F < current.F || !open.contains(c)) {
open.add(c);
}
}
c = getCell(current.x, current.y - 20, t, current);
if (!at(c.x, c.y, s) && !closed.contains(c) && !(c.y < 0)) {
if (c.F < current.F || !open.contains(c)) {
open.add(c);
}
}
Collections.sort(open);
System.out.println("" + open.size());
}
while (current.parent != null) {
path.add(new Block(current.parent.x, current.parent.y, Color.blue));
current = current.parent;
}
return path;
}
public static Cell getCell(int x, int y, Block t, Cell parent) {
Cell cell = new Cell(x, y);
cell.H = Math.abs(x - t.getX()) + Math.abs(y - t.getY());
if (parent != null) {
cell.parent = parent;
cell.G = 20 + parent.G;
}
cell.F = cell.G + cell.H;
return cell;
}
}
Help would be greatly appreciated, Thanks.
I've been trying to get this implementation of the Separating Axis Theorem to work but a collision is detected when the polygons are only close to each other... on some sides. What did I got wrong? Aside from the fact that the code is... optimization is the next step, it's not the problem here. But it should be easy enough to read.
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
public class SAT
{
public static boolean SAT(Polygon p1, Polygon p2)
{
ArrayList<Vector> normals = new ArrayList<Vector>();
//recover normal vectors for p1 and p2
for (int i = 0; i < p1.getPointCount(); i++)
{
if (i < p1.getPointCount() - 1)
{
float x = p1.getPoint(i + 1).x + p1.getPosition().x - p1.getPoint(i).x + p1.getPosition().x;
float y = p1.getPoint(i + 1).y + p1.getPosition().y - p1.getPoint(i).y + p1.getPosition().y;
normals.add(new Vector(x, y).getNormalVectorLeft());
}
else
{
float x = p1.getPoint(0).x + p1.getPosition().x - p1.getPoint(i).x + p1.getPosition().x;
float y = p1.getPoint(0).y + p1.getPosition().y - p1.getPoint(i).y + p1.getPosition().y;
normals.add(new Vector(x, y).getNormalVectorLeft());
}
}
for (int i = 0; i < p2.getPointCount(); i++)
{
if (i < p2.getPointCount() - 1)
{
float x = p2.getPoint(i + 1).x + p2.getPosition().x - p2.getPoint(i).x + p2.getPosition().x;
float y = p2.getPoint(i + 1).y + p2.getPosition().y - p2.getPoint(i).y + p2.getPosition().y;
normals.add(new Vector(x, y).getNormalVectorLeft());
}
else
{
float x = p2.getPoint(0).x + p2.getPosition().x - p2.getPoint(i).x + p2.getPosition().x;
float y = p2.getPoint(0).y + p2.getPosition().y - p2.getPoint(i).y + p2.getPosition().y;
normals.add(new Vector(x, y).getNormalVectorLeft());
}
}
//project points of p1 and p2 on each normal vector until a gap is found
for (int n = 0; n < normals.size(); n++)
{
ArrayList<Float> projectedPoints1 = new ArrayList<Float>();
ArrayList<Float> projectedPoints2 = new ArrayList<Float>();
for (int i = 0; i < p1.getPointCount(); i++)
projectedPoints1.add(new Vector(p1.getPoint(i).x + p1.getPosition().x, p1.getPoint(i).y + p1.getPosition().y).dot(normals.get(n)));
for (int i = 0; i < p2.getPointCount(); i++)
projectedPoints2.add(new Vector(p2.getPoint(i).x + p2.getPosition().x, p2.getPoint(i).y + p2.getPosition().y).dot(normals.get(n)));
float min1 = getMin(projectedPoints1);
float max1 = getMax(projectedPoints1);
float min2 = getMin(projectedPoints2);
float max2 = getMax(projectedPoints2);
if (max1 < min2 || max2 < min1)
return false;
}
return true;
}
public static float getMin(ArrayList<Float> list)
{
float min = list.get(0);
for (float f : list)
if (f < min)
min = f;
return min;
}
public static float getMax(ArrayList<Float> list)
{
float max = list.get(0);
for (float f : list)
if (f > max)
max = f;
return max;
}
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setTitle("SAT");
frame.setLocation(128, 32);
frame.setSize(800, 512);
frame.setContentPane(new Panel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private static class Panel extends JPanel implements MouseMotionListener
{
Polygon p1;
Polygon p2;
public Panel()
{
this.p1 = new Polygon();
this.p2 = new Polygon();
this.p1.setPointCount(3);
this.p1.setPoint(0, new Vector(0 * 32, 0 * 32));
this.p1.setPoint(1, new Vector(2 * 32, 3 * 32));
this.p1.setPoint(2, new Vector(0 * 32, 2 * 32));
this.p1.setPosition(128, 128);
this.p2.setPointCount(3);
this.p2.setPoint(0, new Vector(0 * 32, 0 * 32));
this.p2.setPoint(1, new Vector(1 * 32, 2 * 32));
this.p2.setPoint(2, new Vector(0 * 32, 2 * 32));
this.p2.setPosition(128, 128);
this.addMouseMotionListener(this);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if (SAT(p1, p2))
g.setColor(Color.RED);
else
g.setColor(Color.BLACK);
java.awt.Polygon p;
p = new java.awt.Polygon();
for (int i = 0; i < p1.getPointCount(); i++)
p.addPoint((int) (p1.getPoint(i).x + p1.getPosition().x), (int) (p1.getPoint(i).y + p1.getPosition().y));
g.drawPolygon(p);
p = new java.awt.Polygon();
for (int i = 0; i < p2.getPointCount(); i++)
p.addPoint((int) (p2.getPoint(i).x + p2.getPosition().x), (int) (p2.getPoint(i).y + p2.getPosition().y));
g.drawPolygon(p);
}
public void mouseDragged(MouseEvent e)
{
return;
}
public void mouseMoved(MouseEvent e)
{
p2.setPosition(e.getX(), e.getY());
repaint();
}
}
private static class Polygon
{
private Vector[] points;
private Vector position;
public Polygon()
{
this.points = new Vector[0];
}
public void setPointCount(int n)
{
points = new Vector[n];
}
public void setPoint(int i, Vector v)
{
points[i] = v;
}
public void setPosition(float x, float y)
{
position = new Vector(x, y);
}
public Vector getPoint(int i)
{
return points[i];
}
public Vector getPosition()
{
return position;
}
public int getPointCount()
{
return points.length;
}
}
private static class Vector
{
public final float x;
public final float y;
public Vector(float x, float y)
{
this.x = x;
this.y = y;
}
public float dot(Vector v)
{
return x * v.x + y * v.y;
}
public float length()
{
return (float) Math.sqrt(x * x + y * y);
}
public Vector normalize()
{
float l = length();
return new Vector(x / l, y / l);
}
public Vector getNormalVectorLeft()
{
return new Vector(-y, x);
}
public Vector getNormalVectorRight()
{
return new Vector(y, -x);
}
}
}
Okay, I found what was the problem... I'll just post the fixed code for the problematic part so this question will contain a valid, completely not optimized implementation of SAT (it's used to detect collisions between convex polygons if you're just passing by):
//recover normal vectors for p1 and p2
for (int i = 0; i < p1.getPointCount(); i++)
{
if (i < p1.getPointCount() - 1)
{
float x = p1.getPoint(i + 1).x - p1.getPoint(i).x;
float y = p1.getPoint(i + 1).y - p1.getPoint(i).y;
normals.add(new Vector(x, y).getNormalVectorLeft());
}
else
{
float x = p1.getPoint(0).x - p1.getPoint(i).x;
float y = p1.getPoint(0).y - p1.getPoint(i).y;
normals.add(new Vector(x, y).getNormalVectorLeft());
}
}
for (int i = 0; i < p2.getPointCount(); i++)
{
if (i < p2.getPointCount() - 1)
{
float x = p2.getPoint(i + 1).x - p2.getPoint(i).x;
float y = p2.getPoint(i + 1).y - p2.getPoint(i).y;
normals.add(new Vector(x, y).getNormalVectorLeft());
}
else
{
float x = p2.getPoint(0).x - p2.getPoint(i).x;
float y = p2.getPoint(0).y - p2.getPoint(i).y;
normals.add(new Vector(x, y).getNormalVectorLeft());
}
I am a self taught programmer and I am coding Screen Snake for fun. I am using not using integers to store the position of the snake or apples, I am using doubles. I am having an issue when the snake goes through the apple. When the collide, the code does not register that it collided. I am assuming that this is because their X and Y values might be like .1 off. I have been trying to fix this for 2 weeks but have not been able to. Sorry if my code is a bit messy. I don't know exactly what you guys need from the code so I posted all of it. Also I really appreciate the help! Thanks!!
Main class:
Random random = new Random();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double ScreenW = screenSize.getWidth();
double ScreenH = screenSize.getHeight();
int ScreenX = (int)Math.round(ScreenW);
int ScreenY = (int)Math.round(ScreenH);
JFrame frame = new JFrame();
double x = 1, y = 1;
int size = 5;
int ticks;
private int columnCount = 25;
private int rowCount = 15;
double a = (ScreenW / columnCount) - 1;
double b = (ScreenH / rowCount) - 1;
private Key key;
private List<Rectangle2D> cells;
private Point selectedCell;
boolean up = false;
boolean down = false;
boolean right = true;
boolean left = false;
boolean running = true;
private Thread thread;
private BodyP p;
private ArrayList<BodyP> snake;
private Apple apple;
private ArrayList<Apple> apples;
double width = screenSize.width;
double height = screenSize.height;
double cellWidth = width / columnCount;
double cellHeight = height / rowCount;
double xOffset = (width - (columnCount * cellWidth)) / 2;
double yOffset = (height - (rowCount * cellHeight)) / 2;
public Max_SnakeGame() throws IOException {
System.out.println(screenSize);
System.out.println(a + "," + b);
System.out.println(ScreenH + b);
System.out.println(ScreenW + a);
frame.getContentPane().add(new Screen());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
frame.setBackground(new Color(0, 0, 0, 0));
frame.setLocationRelativeTo(null);
frame.setMaximumSize(screenSize);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
Image img = Toolkit
.getDefaultToolkit()
.getImage(
"C:/Users/Max/My Documents/High School/Sophomore year/Graphic Disign/People art/The Mods Who Tell Pointless Stories.jpg");
frame.setIconImage(img);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
new Max_SnakeGame();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public class Screen extends JPanel implements Runnable {
private static final long serialVersionUID = 1L;
public Screen() {
key = new Key();
addKeyListener(key);
setMaximumSize(screenSize);
setOpaque(false);
setBackground(new Color(0, 0, 0, 0));
setFocusable(true);
snake = new ArrayList<BodyP>();
apples = new ArrayList<>();
start();
}
public void start() {
running = true;
thread = new Thread(this);
thread.start();
}
public void run() {
while (running) {
MoveUpdate();
repaint();
}
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
repaint();
Graphics2D g2d = (Graphics2D) g.create();
cells = new ArrayList<>(columnCount * rowCount);
if (cells.isEmpty()) {
for (int row = 0; row < rowCount; row++) {
for (int col = 0; col < columnCount; col++) {
Rectangle2D cell = new Rectangle2D.Double(xOffset
+ (col * cellWidth), yOffset
+ (row * cellHeight), cellWidth, cellHeight);
cells.add(cell);
}
}
}
g2d.setColor(Color.GRAY);
for (Rectangle2D cell : cells) {
g2d.draw(cell);
}
for (int i = 0; i < snake.size(); i++) {
snake.get(i).draw(g);
}
for (int i = 0; i < apples.size(); i++) {
apples.get(i).draw(g);
}
}
}
private class Key implements KeyListener {
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_RIGHT && !left) {
up = false;
down = false;
right = true;
}
if (keyCode == KeyEvent.VK_LEFT && !right) {
up = false;
down = false;
left = true;
}
if (keyCode == KeyEvent.VK_UP && !down) {
left = false;
right = false;
up = true;
}
if (keyCode == KeyEvent.VK_DOWN && !up) {
left = false;
right = false;
down = true;
}
}
#Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
#Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
public void MoveUpdate() {
if (snake.size() == 0) {
p = new BodyP(x, y, a, b);
snake.add(p);
}
if (apples.size() == 0){
double x1 = random.nextInt(25);
double Ax = ((x1*a+x1+1)*10)/10;
double y1 = random.nextInt(15);
double Ay = ((y1*b+y1+1)*10)/10;
double Afx = Math.round(Ax);
double Afy = Math.round(Ay);
System.out.println("Ax:"+Afx);
System.out.println("Ay:"+Afy);
apple = new Apple(Ax, Ay, a, b);
apples.add(apple);
}
for(int i = 0; i < apples.size(); i++) {
if(Math.round(x)-1 == apples.get(i).getx() || Math.round(x) == apples.get(i).getx() && Math.round(y)== apples.get(i).gety() || Math.round(y)-1 == apples.get(i).gety()) {
size++;
apples.remove(i);
i--;
}
}
ticks++;
if (ticks > 2500000) {
if (up == true) {
if (y <= 2) {
y = ScreenH - b;
System.out.println("Y:" + y);
} else {
y -= b + 1;
System.out.println("Y:" + y);
}
}
// down loop
else if (down == true) {
if (y >= ScreenH - b) {
y = 1;
System.out.println("Y:" + y);
}
else {
y += b + 1;
System.out.println("Y:" + y);
}
}
// left loop
else if (left == true) {
if (x <= 1) {
x = ScreenW - a;
System.out.println("X:" + x);
}
else {
x -= a + 1;
System.out.println("X:" + x);
}
}
// right loop
else if (right == true) {
if (x >= ScreenW - a) {
x = 1;
System.out.println("X:" + x);
}
else {
x += a + 1;
System.out.println("X:" + x);
}
}
ticks = 0;
p = new BodyP(x, y, a, b);
snake.add(p);
// rect.setFrame(x, y, a, b);
if (snake.size() > size) {
snake.remove(0);
}
}
}
}
Snake class:
public class BodyP {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double ScreenW = screenSize.getWidth();
double ScreenH = screen`enter code here`Size.getHeight();
double x = 1, y = 1;
private int columnCount = 25;
private int rowCount = 15;
double a = (ScreenW / columnCount) - 1;
double b = (ScreenH / rowCount) - 1;
public BodyP(double x, double y, double a, double b) {
this.x = x;
this.y = y;
this.a = a;
this.b = b;
}
public void MoveUpdate(){
}
public void draw(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Rectangle2D rect = new Rectangle2D.Double(x, y, a, b);
g.setColor(Color.BLACK);
g2.fill(rect);
}
public double getx() {
return x;
}
public void setx(double x) {
this.x = x;
}
public double gety() {
return y;
}
public void sety(double y) {
this.y = y;
}
}
Apple class:
public class Apple {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double ScreenW = screenSize.getWidth();
double ScreenH = screenSize.getHeight();
double x = 1, y = 1;
private int columnCount = 25;
private int rowCount = 15;
double a = (ScreenW / columnCount) - 1;
double b = (ScreenH / rowCount) - 1;
public Apple(double x, double y, double a, double b) {
this.x = x;
this.y = y;
this.a = a;
this.b = b;
}
public void MoveUpdate(){
}
public void draw(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Rectangle2D rect = new Rectangle2D.Double(x, y, a, b);
g.setColor(Color.RED);
g2.fill(rect);
}
public double getx() {
return x;
}
public void setx(double x) {
this.x = x;
}
public double gety() {
return y;
}
public void sety(double y) {
this.y = y;
}
}
If you think this is due rounding errors, use Euclidean distance and compare with the desired tolerance:
final double tolerance = 1.0; // or whatsoever
double dx = snake.x - apple.x;
double dy = snake.y - apple.y;
if ( dx*dx + dy*dy < tolearance * tolerance ) ...
I suggest to implement something like Point.distanceTo(Point) method to make this convenient.
I,ve got a problem with my project. I'm trying to add 16 objects (buildings) to ArrayList when I'm reading map for my game from file. After all, I get 16 same objects (last found building on map) and I dont know why...
Creating ArrayList in class Game:
public static ArrayList<Building> buildingList = new ArrayList<>();
Reading map (class Map) [Terrain goes to Array, and building if found, goes to ArrayList]:
private void readBuilding(Terrain objct, int x, int y) {
System.out.format("DodajÄ™: ");
//Czerwone centrum dowodzenia
if(objct.getSSX() == 1 && objct.getSSY() == 3) {
Building b = new Building(x, y, 1, 1);
b.writeBuilding();
Game.buildingList.add(b);
}
//Czerwona miasto
if(objct.getSSX() == 2 && objct.getSSY() == 3) {
Building b = new Building(x, y, 2, 1);
b.writeBuilding();
Game.buildingList.add(b);
}
//Czerwona fabryka
if(objct.getSSX() == 3 && objct.getSSY() == 3) {
Building b = new Building(x, y, 3, 1);
b.writeBuilding();
Game.buildingList.add(b);
}
//Niebieskie centrum dowodzenia
if(objct.getSSX() == 1 && objct.getSSY() == 4) {
Building b = new Building(x, y, 1, 2);
b.writeBuilding();
Game.buildingList.add(b);
}
//Niebieska fabryka
if(objct.getSSX() == 2 && objct.getSSY() == 4) {
Building b = new Building(x, y, 2, 2);
b.writeBuilding();
Game.buildingList.add(b);
}
//Niebieskie miasto
if(objct.getSSX() == 3 && objct.getSSY() == 4) {
Building b = new Building(x, y, 3, 2);
b.writeBuilding();
Game.buildingList.add(b);
}
//Niczyje miasto
if(objct.getSSX() == 4 && objct.getSSY() == 3) {
Building b = new Building(x, y, 3, 0);
b.writeBuilding();
Game.buildingList.add(b);
}
//Niczyja fabryka
if(objct.getSSX() == 4 && objct.getSSY() == 4) {
Building b = new Building(x, y, 2, 0);
b.writeBuilding();
Game.buildingList.add(b);
}
}
private void createMap() {
map = new Terrain[40][22];
String[] fields = csvFileContent.split(";");
int x, y;
int fieldNo = 0;
for(y = 0; y < 22; y++) {
for(x = 0; x < 40; x++) {
Terrain objct = new Terrain();
objct.setSSX(Integer.parseInt(fields[fieldNo].substring(0,2)));
objct.setSSY(Integer.parseInt(fields[fieldNo].substring(2,4)));
if(objct.getSSY() > 2) {
readBuilding(objct, x, y);
objct.setSSX(3);
objct.setSSY(1);
}
fieldNo++;
map[x][y] = objct;
}
}
System.out.format("Elementow: %d\n",Game.buildingList.size());
for(Building b : Game.buildingList) {
b.writeBuilding();
}
}
Rendering map on screen (clas Map):
public void render(Graphics graphics, Game game) {
int x, y;
//System.out.println("Budynkow: " + game.getBuildingList().size());
for(y = 0; y < 22; y++) {
for(x = 0; x < 40; x++) {
field = ss.grabImage(map[x][y].getSSX(), map[x][y].getSSY(), 32, 32);
graphics.drawImage(field, x*32, y*32, null);
}
}
Iterator<Building> it = Game.buildingList.iterator();
while(it.hasNext()) {
Building b = it.next();
field = ss.grabImage(b.getSSX(), b.getSSY(), 32, 32);
graphics.drawImage(field, b.getX()*32, b.getY()*32, null);
}
}
I've also trying to iterate on List like this:
for(Building b : Game.buildingList) {
field = ss.grabImage(b.getSSX(), b.getSSY(), 32, 32);
graphics.drawImage(field, b.getX()*32, b.getY()*32, null);
}
And its also not work.
Method in class Map:
b.writeBuilding();
gives me right fields value when im writing it to NetBeans console, but it adding all the time the same.
Class terrain:
package game;
class Terrain {
private int ssX;
private int ssY;
private int x;
private int y;
private boolean occupied;
private boolean crossable;
Terrain() {
this.occupied = false;
}
public void setSSX(int ssX) {
this.ssX = ssX;
}
public void setSSY(int ssY) {
this.ssY = ssY;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public int getSSX() {
return ssX;
}
public int getSSY() {
return ssY;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setCrossable() {
if(ssX == 2 && ssY == 2) {
this.crossable = false;
} else {
if(ssX >= 6 && ssX <= 11) {
if(ssY >= 1 && ssY <= 2) {
this.crossable = false;
} else {
this.crossable = true;
}
}
}
}
}
Class Building:
package game;
public class Building {
private static int owner;
private static int type;
private static int x;
private static int y;
private static int ssX;
private static int ssY;
public Building(int x, int y, int type, int owner) {
this.owner = owner;
this.type = type;
this.x = x;
this.y = y;
if(this.owner == 0) {
if(this.type == 2) {
this.ssX = 4;
this.ssY = 4;
} else if(this.type == 3) {
this.ssX = 4;
this.ssY = 3;
}
} else if(this.owner == 1) {
if(this.type == 1) {
this.ssX = 1;
this.ssY = 3;
} else if(this.type == 2) {
this.ssX = 2;
this.ssY = 3;
} else if(this.type == 3) {
this.ssX = 3;
this.ssY = 3;
}
} else if(this.owner == 2) {
if(this.type == 1) {
this.ssX = 1;
this.ssY = 4;
} else if(this.type == 2) {
this.ssX = 2;
this.ssY = 4;
} else if(this.type == 3) {
this.ssX = 3;
this.ssY = 4;
}
}
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getSSX() {
return ssX;
}
public int getSSY() {
return ssY;
}
public void writeBuilding() {
System.out.format("Owner: %d Type: %d X: %d Y: %d SSX: %d SSY %d\n"
,this.owner,this.type,this.x,this.y,this.ssX,this.ssY);
}
}
Any ideas?
Thanks in advance for help. ;)
Building uses static fields, which get overwritten each time you initalize an object, so all will be equal in the end.