不知道为什么一直不能出while循环
做了一个在控制台运行的俄罗斯方块的游戏
但现在卡住了
运行这个代码的时候,方块的循环一直出不去
我的想法大致是方块触底或接触方块时,将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;
}
}
但现在卡住了
运行这个代码的时候,方块的循环一直出不去
我的想法大致是方块触底或接触方块时,将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;
}
}
...全文
相关推荐
do-while循环——while循环
有详细的讲解和对应的案例注释,对开发人员有很打的帮助!
python中使用while循环的实例
在本篇内容里小编给各位整理的是关于python中使用while循环的实例以及相关知识点,需要的朋友们学习下。
MySQL循环语句之while循环测试
mysql 操作同样有循环语句操作,网上说有3中标准的循环方式: while 循环 、 loop 循环和repeat循环。还有一种非标准的循环: goto。 鉴于goto 语句的跳跃性会造成使用的的思维混乱,所以不建议使用。 这几个循环语句的格式如下: WHILE……DO……END WHILE REPEAT……UNTIL END REPEAT LOOP……END LOOP GOTO。 目前我只测试了 while 循环: delimiter $$ // 定义结束符为 $$ drop procedure if exists wk; // 删除 已有的 存储过程 create procedu
典型while循环实例
一个典型while循环实例,窗口台输出结果。
for while dowhile_javado-while循环_
for、while、do-while循环的使用。包括循环结构的4个要素,一级介绍do-while循环结构


非技术区
