i am new in java. I have one project for school - it's creating 2048 game. We have template which created by my lector and it's based on Greenfoot.
EDIT: another thing I noticed:
fields[x][y].setValue(10);
System.out.println("x1"+fields[x][y].getIntValue());
fields[x-1][y].setValue(newValue);
System.out.println("x1"+fields[x][y].getIntValue());
I am changing field in x position to 10, after this I print the value of field in x position and it's 10, then I set value of field in position x-1 which is 2, then I print field in position x and the value is 2 and not 10. Why?
There is function for set value of current field, but after setting new value, program is returning different value.
Here I tried to get string and int value (don't know where the problem could be, so I tried this - didn't help):
package cz.mendelu.pjj.game2048.greenfoot;
import greenfoot.Actor;
import greenfoot.GreenfootImage;
import greenfoot.World;
import java.awt.*;
public class FieldActor extends Actor {
private static final float ONE = Game2048World.SIZE * Game2048World.SIZE;
private static final Font FONT = new Font(Font.MONOSPACED, Font.BOLD, 64);
private static final int MARGIN = 5;
private static String number = "";
private static int sentValue = 0;
public void setValue(int value) {
GreenfootImage image = new GreenfootImage(Game2048World.CELL, Game2048World.CELL);
sentValue = value;
// Calculate nice color for background
Color color = Color.LIGHT_GRAY;
if (value > 1) {
float base = Integer.SIZE - Integer.numberOfLeadingZeros(value);
float c = base / ONE;
color = new Color(1f - c, 1f, c);
}
// Draw background rectangle
image.setColor(color);
image.fillRect(MARGIN, MARGIN, Game2048World.CELL - (MARGIN * 2), Game2048World.CELL - (MARGIN * 2));
// If not 0 draw Number
if (value != 0) {
image.setColor(Color.BLACK);
number = Integer.toString(value);
image.setFont(FONT);
int x = Game2048World.CELL / 2 - (number.length() * 19);
int y = Game2048World.CELL / 2 + 26;
image.drawString(number, x, y);
}
setImage(image);
}
public String getValue() {
return number;
}
public int getIntValue() {
return sentValue;
}
}
In this class I am trying to change the value of the current field:
package cz.mendelu.pjj.game2048;
import cz.mendelu.pjj.game2048.greenfoot.FieldActor;
public class Game2048 {
public Game2048(int size) {
}
public int get(int x, int y, FieldActor[][] fields) {
return 1;
}
public void addNewNumber() {
System.out.println("addNewNumber");
// Pokud číslo nejde pridat čísla (všechna pole jsou plná), pak vyvolejte kontrolovanou výjimku AddNewNumberException.
}
public boolean moveLeft(FieldActor[][] fields) {
System.out.println("moveLeft");
int x = 1;
int y = 1;
FieldActor currentField = fields[x][y];
FieldActor previousField = fields[x-1][y];
if (currentField.getValue() == previousField.getValue()) {
System.out.println("true condition: ");
System.out.println("current field: "+currentField.getIntValue());
System.out.println("previous field: "+previousField.getIntValue());
int valueOfCurrent = currentField.getIntValue();
int valueOfPrevious = previousField.getIntValue();
int newValue = valueOfCurrent+valueOfPrevious;
previousField.setValue(newValue);
currentField.setValue(0);
System.out.println("after setValue: ");
System.out.println("current field: "+currentField.getIntValue());
System.out.println("ValueOfCurrent "+valueOfCurrent);
System.out.println("previous field: "+previousField.getIntValue());
System.out.println("newValue "+newValue);
System.out.println("previous field: "+previousField.getIntValue());
System.out.println("valueOfPrevious "+valueOfPrevious);
System.out.println("string value "+previousField.getValue());
} else {
System.out.println("false condition: ");
}
return false;
}
public boolean moveRight() {
System.out.println("moveRight");
return false;
}
public boolean moveUp() {
System.out.println("moveUp");
return false;
}
public boolean moveDown() {
System.out.println("moveDown");
return false;
}
}
Printed values:
currentField.setValue(0); moveLeft true condition: current field: 1 previous field: 1 after setValue: current field: 0 ValueOfCurrent 1 previous field: 0 newValue 2 previous field: 0 valueOfPrevious 1 string value 2
Displayed values in game: 1st move left: values are 2 and 0 2nd move left: values are 0 and 0
currentField.setValue(10); moveLeft true condition: current field: 1 previous field: 1 after setValue: current field: 10 ValueOfCurrent 1 previous field: 10 newValue 2 previous field: 10 valueOfPrevious 1 string value 10
Displayed values in game: 1st move left: values are 2 and 10 2nd move left: values are 20 and 10 3rd ... still 20 and 10
this class is part of the template:
package cz.mendelu.pjj.game2048.greenfoot;
import cz.mendelu.pjj.game2048.Game2048;
import greenfoot.Greenfoot;
import greenfoot.World;
public class Game2048World extends World {
static final int SIZE = 4;
static final int CELL = 200;
private Game2048 game2048 = new Game2048(SIZE);
private FieldActor[][] fields = new FieldActor[SIZE][SIZE];
public Game2048World() {
super(SIZE, SIZE, CELL);
for (int x = 0; x < SIZE ; x++) {
for (int y = 0; y < SIZE; y++) {
fields[x][y] = new FieldActor();
addObject(fields[x][y], x, y);
}
}
update();
}
@Override
public void act() {
String key = Greenfoot.getKey();
if (key != null) {
boolean valid = false;
if (key == "left") {
valid = game2048.moveLeft(fields);
} else if (key == "right") {
valid = game2048.moveRight();
} else if (key == "up") {
valid = game2048.moveUp();
} else if (key == "down") {
valid = game2048.moveDown();
}
if (valid == true) {
try {
game2048.addNewNumber();
// update();
} catch (RuntimeException e) {
//Gr
}
}
}
}
private void update() {
for (int x = 0; x < SIZE ; x++) {
for (int y = 0; y < SIZE; y++) {
fields[x][y].setValue(game2048.get(x, y, fields));
}
}
}
}
classes FieldActor and Game2048World were in the template. In FieldActor I just added getValue and getIntValue and changed number and intValue as global variables
Thank you for your help!
I think the confusion probably lies in sentValue being static. The word static means that a single sentValue field is shared among all FieldActor instances. Thus when you set the value for one FieldActor (really: setting the shared value for all of them), it appears to change for another FieldActor. I think if you make the relevant fields non-static, you will start to get the behaviour you expect.