我已经检查了所有内容,我无法找出为什么我的得分变量没有存储在我的Move类文件中。在搞砸了一段时间之后,我想我把它缩小到了类文件,但我真的不太确定。我只是希望有人查看它,看看是否有任何明显的解决方案。
正如你所看到的,我想做的就是为自己的练习做一个简单的2048游戏。它应该很直接。感谢您帮助我这一直困扰我几周,我们非常感谢您的建议。
这是主文件:它打印电路板并生成一个随机的新数字。
import java.util.*;
import java.util.Random;
public class Game2048 {
public static void main(String[] args){
Scanner scn = new Scanner(System.in);
int inp = 0;
int end = 0;
int zc = 0;
System.out.println("+------------------------------------------------------------------+");
//welcome message
while(inp != 1){
System.out.println("2408 Game");
System.out.println("---------");
System.out.println("1 To start!");
System.out.println("2. Help.");
inp = scn.nextInt();
if(inp == 2){
System.out.println("wsad are their respective controls.");
System.out.println("Try to make 2048!");
System.out.println("Press and number to continue.");
inp = scn.nextInt();
}
}
//initializing game board
Move move = new Move();
drawBoard(move);
while(end == 0){
move(move, scn.next());
spawn(move.getBoard());
drawBoard(move);
}
System.out.println("You lost sucka");
}
public static Move move(Move mM, String m){ //Sends move keys to move class
if(m.equals("w")){
mM.moveUp(mM.getBoard(), mM.getScore());
}
if(m.equals("s")){
mM.moveDown(mM.getBoard(), mM.getScore());
}
if(m.equals("a")){
mM.moveLeft(mM.getBoard(), mM.getScore());
}
if(m.equals("d")){
mM.moveRight(mM.getBoard(), mM.getScore());
}
return mM;
}
public static int[][] spawn(int[][] b){ //spawns 2 or 4 in a random block
Random rand = new Random();
int x = rand.nextInt(4);
int y = rand.nextInt(4);
int c = rand.nextInt(9);
while(b[x][y]!=0){
x = rand.nextInt(4);
y = rand.nextInt(4);
}
if(c==9){
b[x][y]=4;
}else b[x][y]=2;
return b;
}
public static void drawBoard(Move m){ //draws board
int[][] b = m.getBoard();
System.out.println("+-------------------------------------+");
System.out.println("| | | | |");
System.out.println("| "+b[0][0]+" | "+b[1][0]+" | "+b[2][0]+" | "+b[3][0]+" |");
System.out.println("| | | | |");
System.out.println("---------------------------------------");
System.out.println("| | | | |");
System.out.println("| "+b[0][1]+" | "+b[1][1]+" | "+b[2][1]+" | "+b[3][1]+" |");
System.out.println("| | | | |");
System.out.println("---------------------------------------");
System.out.println("| | | | |");
System.out.println("| "+b[0][2]+" | "+b[1][2]+" | "+b[2][2]+" | "+b[3][2]+" |");
System.out.println("| | | | |");
System.out.println("---------------------------------------");
System.out.println("| | | | |");
System.out.println("| "+b[0][3]+" | "+b[1][3]+" | "+b[2][3]+" | "+b[3][3]+" |");
System.out.println("| | | | |");
System.out.println("+-------------------------------------+");
System.out.println(m.getScore());
}
}
这是Move类,它处理所有棋盘移动和(据称)得分。
import java.util.Random;
import java.util.*;
public class Move {
private int[][] b = new int[4][4];
private int s = 0;
public Move(){ //initialized a new board with two 2's randomly placed on board
Random rand = new Random();
int x = rand.nextInt(4);
int y = rand.nextInt(4);
int x1 = rand.nextInt(4);
int y1 = rand.nextInt(4);
while(x == x1 && y == y1){ //makes sure the numbers are not placed in the same block
x = rand.nextInt(4);
}
b[x][y]=2;
b[x1][y1]=2;
}
/*public Move(){
b[0][0]=8;
b[0][1]=4;
b[0][2]=2;
b[0][3]=2;
b[2][0]=2;
b[2][1]=2;
b[2][3]=2;
b[3][0]=2;
b[3][1]=2;
b[3][2]=2;
}*/
public int[][] getBoard(){
return b;
}
public int getScore(){
return s;
}
public void moveUp(int[][] b, int s){
int c;
for(int y = 0; y < 3; y++){
for(int x = 0; x < 4; x++){
c = 1;
while(b[x][y]==0 && (c+y)<4){
if(b[x][y+c]!=0){
b[x][y]=b[x][y+c];
b[x][y+c]=0;
}else c++;
}c = 2;
while(b[x][y+1]==0 && (c+y)<4){
if(b[x][y+c]!=0){
b[x][y+1]=b[x][y+c];
b[x][y+c]=0;
}else c++;
}if(b[x][y]==b[x][y+1]){
b[x][y]+=b[x][y+1];
b[x][y+1]=0;
s+=b[x][y];
}
}
}
System.out.println(s);
}
public void moveDown(int[][] b, int s){
int c;
for(int y = 3; y > 0; y--){
for(int x = 0; x < 4; x++){
c = 1;
while(b[x][y]==0 && c <= y){
if(b[x][y-c]!=0){
b[x][y]=b[x][y-c];
b[x][y-c]=0;
}else c++;
}c = 2;
while(b[x][y-1]==0 && c <= y){
if(b[x][y-c]!=0){
b[x][y-1]=b[x][y-c];
b[x][y-c]=0;
}else c++;
}if(b[x][y]==b[x][y-1]){
b[x][y]+=b[x][y-1];
b[x][y-1]=0;
s+=b[x][y];
}
}
}
}
public void moveLeft(int[][] b, int s){
int c;
for(int y = 0; y < 4;y++){
for(int x = 0; x < 3;x++){
c=2;
while(b[x+1][y]==0 && (c+x)<4){
if(b[x+c][y]!=0){
b[x+1][y]=b[x+c][y];
b[x+c][y]=0;
}else c++;
}if(b[x][y]==b[x+1][y]){
b[x][y]+=b[x+1][y];
b[x+1][y]=0;
s+=b[x][y];
}
}
}
}
public void moveRight(int[][] b, int s){
int c;
for(int y = 0; y < 4;y++){
for(int x = 3; x > 0;x--){
c = 1;
while(b[x][y]==0 && c <= x){
if(b[x-c][y]!=0){
b[x][y]=b[x-c][y];
b[x-c][y]=0;
}else c++;
}c=2;
while(b[x-1][y]==0 && c <= x){
if(b[x-c][y]!=0){
b[x-1][y]=b[x-c][y];
b[x-c][y]=0;
}else c++;
}if(b[x][y]==b[x-1][y]){
b[x][y]+=b[x-1][y];
b[x-1][y]=0;
s+=b[x][y];
}
}
}
}
}
答案 0 :(得分:0)
董事会和分数是Move
类的成员变量。当您实例化Move
类Move Object
时,this
现在有一个特定的板和与之关联的分数。您不需要将这些成员变量传递到类中,因为它们现在本身就是您可以使用Move
类的成员函数中的myMoveObj.moveUp()
关键字访问的对象的一部分。您只需要操作对象成员。所以它就像public void moveDown(){
//other code here
this.s+=this.b[x][y];
}
在你的功能中
{{1}}
答案 1 :(得分:0)
希望我能为您的问题找到正确的解释:
mM.getScore()
将当前得分传递给moveXXX方法进行更新。 s
(如public void moveRight(int[][] b, int s)
中所示)。这个掩盖了你在课堂上定义的s
。 s
进行更新时(例如添加s+=b[x][y];
),s
引用方法local-field。