【发布时间】:2018-11-04 10:23:15
【问题描述】:
我是java新手。我有一个学校项目 - 它正在创建 2048 游戏。我们有我的讲师创建的模板,它基于 Greenfoot。
编辑: 我注意到的另一件事:
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());
我将 x 位置的字段更改为 10,之后我打印 x 位置的字段值,它是 10,然后我将位置 x-1 的字段值设置为 2,然后我打印位置 x 的字段并且值是 2 而不是 10。为什么?
有设置当前字段值的功能,但是设置新值后,程序返回不同的值。
在这里我尝试获取字符串和 int 值(不知道问题可能出在哪里,所以我尝试了这个 - 没有帮助):
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;
}
}
在这个类中,我试图改变当前字段的值:
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;
}
}
打印值:
currentField.setValue(0); 向左移动 真实情况: 当前字段:1 上一个字段:1 设置值之后: 当前字段:0 当前值 1 上一个字段:0 新值 2 上一个字段:0 valueOfPrevious 1 字符串值2
游戏中显示的值: 向左第一步:值为 2 和 0 向左第二步:值为 0 和 0
currentField.setValue(10); 向左移动 真实情况: 当前字段:1 上一个字段:1 设置值之后: 当前字段:10 当前值 1 上一个字段:10 新值 2 上一个字段:10 valueOfPrevious 1 字符串值 10
游戏中显示的值: 向左第一步:值为 2 和 10 向左第二步:值为 20 和 10 第三...仍然是 20 和 10
这个类是模板的一部分:
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));
}
}
}
}
类 FieldActor 和 Game2048World 在模板中。在 FieldActor 中,我刚刚添加了 getValue 和 getIntValue 并将 number 和 intValue 更改为全局变量
感谢您的帮助!
【问题讨论】:
-
getValue返回number成员。但setValue仅在使用非零参数调用时设置number。 -
这是真的,但是例如当我将当前字段的值设置为 10 时:newValue 为 2 因为:currentField.getIntValue() + previousField.getIntValue() - 在将 currentField 设置为 10 之前 - 这是正确的,但是在 previousField.setvalue(newValue) 是控制台中 previousField 10 的值之后,我可以看到 previousField 的值是 10 - 但它应该是 2 并且在游戏中显示为 2 我只是不明白为什么以及如何
标签: java setvalue getvalue greenfoot