不知道为什么一直不能出while循环

Ruuku 2018-11-09 02:14:02
做了一个在控制台运行的俄罗斯方块的游戏

但现在卡住了

运行这个代码的时候,方块的循环一直出不去

我的想法大致是方块触底或接触方块时,将mainScreen有方块的地方改了,然后直接出循环,出下一个方块

但现在不知道为什么,主界面是改了,但新房快一直不出来

输入命令动的还是原来的方块

public class TetrisBlock {
public static void main( String[] args ) {
java.util.Scanner sc = new java.util.Scanner(System.in);
int[][] mainScreen = new int[25][15];
for (int i = 0 ; i <= 14 ; i++) {
for (int j = 0; j <= 24 ; j++ ) {
if (j==0||j==24) {
mainScreen[j][i]=1;
} else {
mainScreen[j][i]=0;
}
}
}
int[][] mainScreen1 = mainScreen;
while ( true ) {
int a = 11, b = -1;
int random = (int)(Math.random()*7);
int[][] block1 = block(random);
reprint(a,b,block1,mainScreen1);
while ( end(mainScreen, mainScreen1) ) {
String input = sc.next();
int[][] block2 = {{0,1,0,0},{0,1,0,0},{0,1,0,0},{0,1,0,0}};
switch(input) {
case "w":
if ( random == 0 || random == 1 || random == 2 || random == 3 || random ==4) {
int[][] temp = new int[3][3];
for (int i = 0; i <= 2 ; i++) {
for (int j = 0; j <= 2 ; j++ ) {
temp[j][i] = block1[i][2-j];
}
}
block1 = temp;
};
if ( random == 5) {
int[][] temp = block1;
block1 = block2;
block2 = temp;
}
if (random == 6) {
}
break;
case "a":
a-=1;
b+=1;
break;
case "s":
b+=1;
break;
case "d":
a+=1;
b+=1;
break;
default:
System.out.println("NOT AVAILABLE");
break;
}
mainScreen = mainScreen1;
mainScreen1 = border1(random, a, b, block1, mainScreen);
reprint(a,b,block1,mainScreen);
}
}
}

public static boolean end( int[][] mainScreen, int[][] mainScreen1 ) {
if (mainScreen == mainScreen1 ) {
return true;
}
return false;
}



public static int[][] block(int random) {
if (random == 0) {
return new int[][] {{1,0,0},{1,0,0},{1,1,0}};
}
if (random == 1) {
return new int[][] {{1,1,0},{1,0,0},{1,0,0}};
}
if (random == 2) {
return new int[][] {{1,1,0},{0,1,1},{0,0,0}};
}
if (random == 3) {
return new int[][] {{0,1,1},{1,1,0},{0,0,0}};
}
if (random == 4) {
return new int[][] {{1,0,0},{1,1,0},{1,0,0}};
}
if (random == 5) {
return new int[][] {{0,0,0,0},{1,1,1,1},{0,0,0,0},{0,0,0,0}};
}
if (random == 6) {
return new int[][] {{1,1},{1,1}};
} else {
return new int[][] {{0},{0}};
}
}


public static void reprint(int a, int b,int[][] block, int[][] mainScreen/*, int[][] block*/) {
System.out.println("Next Block");
/*for (int i = 0 ; i< length.block ; i++ )
{
for (int j = 0; j< length.block[i] ; j++ )
{
if (block[j][i] = 1)
{
System.out.print("*");
}
if (j=2)
{
System.out.print("\n");
}
else
{
System.out.print(" ");
}
}
}*/

for (int i = 0 ; i <= 14 ; i++) {
for (int j = 0; j <= 24 ; j++ ) {
if (mainScreen[j][i]==1) {
System.out.print("|");
if (j == 24) {
System.out.println();
}
continue;
}
if (mainScreen[j][i] == 2) {
System.out.print("*");
continue;
}
if (j>=a&&i>=b&&j<=(a+2)&&i<=(b+2)&&block[j-a][i-b]==1&&mainScreen[j][i] == 0) {
System.out.print("*");
continue;
} else {
System.out.print(" ");
continue;
}
}
}
System.out.println("-------------------------");
System.out.println("INSTRUCTOR: a=left b=right w=rotate s=down");
System.out.print("Command:");

}

public static int[][] border1(int random, int a, int b, int[][] block, int[][] mainScreen ) {
int temp = a;
if ( random == 0 || random == 1 || random == 2 || random == 3 || random ==4) {
for (int i = 0 ; i <= 14 ; i++) {
for (int j = 0; j <= 24 ; j++ ) {
if (j>=a && i>=b && j<=(a+2) &&i <=(b+2) && block[j-a][i-b]==1 && (mainScreen[j][i] == 2 || i == 14)) {
temp -= 1;
break;
} else {
}
}
}
}
if (random == 5) {
for (int i = 0 ; i <= 14 ; i++) {
for (int j = 0; j <= 24 ; j++ ) {
if (j>=a && i>=b && j<=(a+3) &&i <=(b+3) && block[j-a][i-b]==1 && (mainScreen[j][i] == 2 || i == 14)) {
temp -= 1;
break;
} else {
}
}
}
}
if (random == 6) {
for (int i = 0 ; i <= 14 ; i++) {
for (int j = 0; j <= 24 ; j++ ) {
if (j>=a && i>=b && j<=(a+1) && i <=(b+1) && block[j-a][i-b]==1 && (mainScreen[j][i] == 2 || i == 14)) {
temp -= 1;
break;
} else {
}
}
}
}
if (temp < a) {
a = temp;
for (int i = 0 ; i <= 14 ; i++) {
for (int j = 0; j <= 24 ; j++ ) {
if (j>=a&&i>=b&&j<=(a+2)&&i<=(b+2)&&block[j-a][i-b]==1) {
mainScreen[j+1][i]=2;
}
}
}
}
return mainScreen;
}

public static int border2(int random, int a, int b, int[][] block, int[][] mainScreen ) {
int j1 = 1;
int j2 = 24;
for (int i = 0 ; i <= 14 ; i++ ) {
if (i>=b && i <=(b+1) && block[j1-a][i-b]==1) {
return 1;
}
if (i>=b && i <=(b+1) && block[j1-a][i-b]==1) {
return 2;
}
}
return 0;
}
}
...全文
11 点赞 收藏 4
写回复
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
xin若止水 2018-11-11
java输入一个自然数n 1到n有多少个2怎么用循环写啊
回复
C_L101_10_0 2018-11-09
引用 2 楼 qq_40224672 的回复:
int[][] mainScreen1 = mainScreen; 两个数组指向同一个地址

大致改了一下

public class TetrisBlock {

public static void main( String[] args ) {
java.util.Scanner sc = new java.util.Scanner(System.in);
int[][] mainScreen = new int[25][15];
int[][] mainScreen1 = new int[25][15];
for (int i = 0 ; i <= 14 ; i++) {
for (int j = 0; j <= 24 ; j++ ) {
if (j==0||j==24) {
mainScreen[j][i]=1;
mainScreen1[j][i]=1;
} else {
mainScreen[j][i]=0;
mainScreen1[j][i]=0;
}
}
}
//int[][] mainScreen1 =mainScreen;
while ( true ) {
int a = 11, b = -1;
int random = (int)(Math.random()*7);
int[][] block1 = block(random);
reprint(a,b,block1,mainScreen1);
while ( end(mainScreen, mainScreen1) ) {
String input = sc.next();
int[][] block2 = {{0,1,0,0},{0,1,0,0},{0,1,0,0},{0,1,0,0}};
switch(input) {
case "w":
if ( random == 0 || random == 1 || random == 2 || random == 3 || random ==4) {
int[][] temp = new int[3][3];
for (int i = 0; i <= 2 ; i++) {
for (int j = 0; j <= 2 ; j++ ) {
temp[j][i] = block1[i][2-j];
}
}
block1 = temp;
};
if ( random == 5) {
int[][] temp = block1;
block1 = block2;
block2 = temp;
}
if (random == 6) {
}
break;
case "a":
a-=1;
b+=1;
break;
case "s":
b+=1;
break;
case "d":
a+=1;
b+=1;
break;
default:
System.out.println("NOT AVAILABLE");
break;
}
for (int i = 0; i < mainScreen1.length; i++) {
for (int j = 0; j < mainScreen1[i].length; j++) {
mainScreen[i][j]=mainScreen1[i][j];
}
}
//mainScreen = mainScreen1;
mainScreen= border1(random, a, b, block1, mainScreen);
reprint(a,b,block1,mainScreen);
}
}
}

public static boolean end( int[][] mainScreen, int[][] mainScreen1 ) {
for (int i = 0; i < mainScreen.length; i++) {
for (int j = 0; j < mainScreen[i].length; j++) {
if (mainScreen[i][j] !=mainScreen1[i][j]) {
for (int j2 = 0; j2 < mainScreen.length; j2++) {
for (int k = 0; k < mainScreen[j2].length; k++) {
mainScreen1[j2][k] =mainScreen[j2][k];
}
}
return false;
}
}
}
/*if (mainScreen == mainScreen1 ) {
return true;
}*/
return true;
}



public static int[][] block(int random) {
if (random == 0) {
return new int[][] {{1,0,0},{1,0,0},{1,1,0}};
}
if (random == 1) {
return new int[][] {{1,1,0},{1,0,0},{1,0,0}};
}
if (random == 2) {
return new int[][] {{1,1,0},{0,1,1},{0,0,0}};
}
if (random == 3) {
return new int[][] {{0,1,1},{1,1,0},{0,0,0}};
}
if (random == 4) {
return new int[][] {{1,0,0},{1,1,0},{1,0,0}};
}
if (random == 5) {
return new int[][] {{0,0,0,0},{1,1,1,1},{0,0,0,0},{0,0,0,0}};
}
if (random == 6) {
return new int[][] {{1,1,1},{1,1,1},{1,1,1}};
} else {
return new int[][] {{1,1,1},{1,1,1},{1,1,1}};
}
}


public static void reprint(int a, int b,int[][] block, int[][] mainScreen/*, int[][] block*/) {
System.out.println("Next Block");
/*for (int i = 0 ; i< length.block ; i++ )
{
for (int j = 0; j< length.block[i] ; j++ )
{
if (block[j][i] = 1)
{
System.out.print("*");
}
if (j=2)
{
System.out.print("\n");
}
else
{
System.out.print(" ");
}
}
}*/

for (int i = 0 ; i <= 14 ; i++) {
for (int j = 0; j <= 24 ; j++ ) {
if (mainScreen[j][i]==1) {
System.out.print("|");
if (j == 24) {
System.out.println();
}
continue;
}
if (mainScreen[j][i] == 2) {
System.out.print("*");
continue;
}
if (j>=a&&i>=b&&j<=(a+2)&&i<=(b+2)&&block[j-a][i-b]==1&&mainScreen[j][i] == 0) {
System.out.print("*");
continue;
} else {
System.out.print(" ");
continue;
}
}
}
System.out.println("-------------------------");
System.out.println("INSTRUCTOR: a=left b=right w=rotate s=down");
System.out.print("Command:");

}

public static int[][] border1(int random, int a, int b, int[][] block, int[][] mainScreen ) {
int temp = a;
if ( random == 0 || random == 1 || random == 2 || random == 3 || random ==4) {
for (int i = 0 ; i <= 14 ; i++) {
for (int j = 0; j <= 24 ; j++ ) {
if (j>=a && i>=b && j<=(a+2) &&i <=(b+2) && block[j-a][i-b]==1 && (mainScreen[j][i] == 2 || i == 14)) {
temp -= 1;
break;
} else {
}
}
}
}
if (random == 5) {
for (int i = 0 ; i <= 14 ; i++) {
for (int j = 0; j <= 24 ; j++ ) {
if (j>=a && i>=b && j<=(a+3) &&i <=(b+3) && block[j-a][i-b]==1 && (mainScreen[j][i] == 2 || i == 14)) {
temp -= 1;
break;
} else {
}
}
}
}
if (random == 6) {
for (int i = 0 ; i <= 14 ; i++) {
for (int j = 0; j <= 24 ; j++ ) {
if (j>=a && i>=b && j<=(a+1) && i <=(b+1) && block[j-a][i-b]==1 && (mainScreen[j][i] == 2 || i == 14)) {
temp -= 1;
break;
} else {
}
}
}
}
if (temp < a) {
a = temp;
for (int i = 0 ; i <= 14 ; i++) {
for (int j = 0; j <= 24 ; j++ ) {
if (j>=a&&i>=b&&j<=(a+2)&&i<=(b+2)&&block[j-a][i-b]==1) {
mainScreen[j+1][i]=2;
}
}
}
}
return mainScreen;
}

public static int border2(int random, int a, int b, int[][] block, int[][] mainScreen ) {
int j1 = 1;
int j2 = 24;
for (int i = 0 ; i <= 14 ; i++ ) {
if (i>=b && i <=(b+1) && block[j1-a][i-b]==1) {
return 1;
}
if (i>=b && i <=(b+1) && block[j1-a][i-b]==1) {
return 2;
}
}
return 0;
}
}
回复
C_L101_10_0 2018-11-09
int[][] mainScreen1 = mainScreen; 两个数组指向同一个地址
回复
tangyanzhi1111 2018-11-09
代码太长了,懒得看......
回复
发帖
非技术区
创建于2007-09-28

2.3w+

社区成员

Java 非技术区
申请成为版主
帖子事件
创建了帖子
2018-11-09 02:14
社区公告
暂无公告