我会尽量简单明了地解释我得到了什么和我需要什么。
我有一个二维数组,组成一个 char 元素板。我必须从底部拍摄一张带有“·”的照片。射击从底部 (a.length-1) 到顶部 (0),根据射击方向在墙上弹跳。
if (dirDisp == 3){
for(int x = 3, y = 0; x < 5 & y < 2; x++, y++) {
if(a[(a.length-1) - y][(a[x].length-1) / 2 + x] == EMPTY)
a[(a.length-1) - y][(a[x].length-1) / 2 + x] = '·';
}
for(int x = 0, y = 1; x < 9 & y < 11; x++, y++) {
if(a[(a.length-1) - y][(a[x].length-1) - x] == EMPTY)
a[(a.length-1) - y][(a[x].length-1) - x] = '·';
}
for(int x = 0, y = 9; x < 5 & y < 14; x++, y++) {
if(a[(a.length-1) - y][x] == EMPTY)
a[(a.length-1) - y][x] = '·';
}
}
我已将射门方向 = 3 的代码放在上面。EMPTY 常量是“”。问题是我还 hace 了数组中的其他字符,例如“a”、“b”等。
我想要的是在没有 EMPTY 的情况下停止循环。它实际上做的是:
g g b g g y y r r
y o y a r y g o b
g o r a b r a a b
g y o a y a g o b
g a b b b o o b g
·
·
r r r r r r r r r
·
·
·
·
·
·
当到达第一个不同于 EMPTY 的字符时,射击必须停止。我试过使用中断并继续。也使用 firstLoop:{}。我还尝试在循环的每个部分使用 boolean 值,但我还没有让它工作。我需要一点帮助。
如果有什么不清楚的就问。
PD:镜头有 3 个“for”循环,第一个是从底部到第一面墙填充数组,第二个是墙与墙之间的空间,第三个是对面墙到顶端。 PD2:“r”行供您查看实际发生的情况。
使用 boolean 值之后:
a b a b y a r y y
y a o y g r o g g
g o y o a y y a y
y o a a g o o b a
o o r a o r g y y
r r r r r r r r r
·
·
主类
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Stack;
public class BobbleTexto {
public static Scanner sc = new Scanner(System.in);
public static char RED = 'r';
public static char GREEN = 'g';
public static char BLUE = 'b';
public static char ORANGE = 'o';
public static char YELLOW = 'y';
public static char GRAY = 'a';
public static char PURPLE = 'p';
public static char EMPTY = ' ';
public static char IZQUIERDA = 'A';
public static char DERECHA = 'D';
public static char DISPARO = 'S';
public static char SALIR = 'Q';
public static char [][] generarTablero (){
char [][] a = new char [14][9];
for (int x = 0; x < a.length; x++){
for (int y = 0; y < a[x].length;y++){
a [x][y] = EMPTY;
}
}
for(int x = 0; x < 5; x++){
for(int y = 0; y < a[y].length; y++){
a[x][y] = generarBurbuja();
a[7][y] = RED;
}
}
return a;
}
public static char [][] limpiarTablero (char [][] a){
for (int x = 0; x < a.length; x++){
for (int y = 0; y < a[x].length;y++){
if (a [x][y] == '·')
a[x][y] = EMPTY;
}
}
return a;
}
public static void imprimirDisparo (char [][] a,int dirDisp,char Burbuja){
if (dirDisp == 1){
for(int x = 1, y = 0; x < 5 & y < 4; x++, y++){
if(a[(a.length-1) - y][(a[x].length-1) / 2 + x] == EMPTY){
a[(a.length-1) - y][(a[x].length-1) / 2 + x] = '·';
}
else{
break;
}
}
for(int x = 0, y = 3; x < 9 & y < 11; x++, y++){
if(a[(a.length-1) - y][(a[x].length-1) - x] == EMPTY){
a[(a.length-1) - y][(a[x].length-1) - x] = '·';
}
else{
}
}
for(int x = 0, y = 11; x < 4 & y < 14; x++, y++){
if(a[(a.length-1) - y][x] == EMPTY){
a[(a.length-1) - y][x] = '·';
}
else{
break;
}
}
}
if (dirDisp == 2){
for(int x = 2, y = 0; x < 5 & y < 3; x++, y++){
if(a[(a.length-1) - y][(a[x].length-1) / 2 + x] == EMPTY){
a[(a.length-1) - y][(a[x].length-1) / 2 + x] = '·';
}
else {
}
}
for(int x = 0, y = 2; x < 9 & y < 11; x++, y++){
if(a[(a.length-1) - y][(a[x].length-1) - x] == EMPTY){
a[(a.length-1) - y][(a[x].length-1) - x] = '·';
}
else {
}
}
for(int x = 0, y = 10; x < 4 & y < 14; x++, y++){
if(a[(a.length-1) - y][x] == EMPTY){
a[(a.length-1) - y][x] = '·';
}
else {
}
}
}
if (dirDisp == 3){
boolean foundObstacle = false;
for(int x = 3, y = 0; x < 5 & y < 2 && !foundObstacle; x++, y++){
if(a[(a.length-1) - y][(a[x].length-1) / 2 + x] == EMPTY)
a[(a.length-1) - y][(a[x].length-1) / 2 + x] = '·';
else {
foundObstacle = true;
break;
}
}
if(!foundObstacle) {
for(int x = 0, y = 1; x < 9 & y < 11 && !foundObstacle; x++, y++){
if(a[(a.length-1) - y][(a[x].length-1) - x] == EMPTY)
a[(a.length-1) - y][(a[x].length-1) - x] = '·';
else {
foundObstacle = true;
break;
}
}
}
if(!foundObstacle) {
for(int x = 0, y = 9; x < 5 & y < 14 && !foundObstacle; x++, y++){
if(a[(a.length-1) - y][x] == EMPTY)
a[(a.length-1) - y][x] = '·';
else break;
}
}
}
if (dirDisp == 4){
for(int x = 4, y = 0; x < 5 & y < 1; x++, y++){
if(a[(a.length-1) - y][(a[x].length-1) / 2 + x] == EMPTY)
a[(a.length-1) - y][(a[x].length-1) / 2 + x] = '·';
}
for(int x = 0, y = 0; x < 9 & y < 11; x++, y++){
if(a[(a.length-1) - y][(a[x].length-1) - x] == EMPTY)
a[(a.length-1) - y][(a[x].length-1) - x] = '·';
}
for(int x = 0, y = 8; x < 6 & y < 14; x++, y++){
if(a[(a.length-1) - y][x] == EMPTY)
a[(a.length-1) - y][x] = '·';
}
}
if (dirDisp == 0) {
for (int x = 0; x < a.length; x++)
if(a[x][(a[x].length - 1) / 2] == EMPTY)
a[x][(a[x].length - 1) / 2] = '·';
}
if (dirDisp == -1){
for(int x = 1, y = 0; x < 5 & y < 4; x++, y++){
if(a[(a.length-1) - y][(a[x].length-1) / 2 - x] == EMPTY)
a[(a.length-1) - y][(a[x].length-1) / 2 - x] = '·';
}
for(int x = 1, y = 3; x < 11 & y < 12; x++, y++){
if(a[(a.length-1) - y][x-1] == EMPTY)
a[(a.length-1) - y][x-1] = '·';
}
for(int x = 0, y = 11; x < 4 & y < 14; x++, y++){
if(a[(a.length-1) - y][(a[x].length-1) - x] == EMPTY)
a[(a.length-1) - y][(a[x].length-1) - x] = '·';
}
}
if (dirDisp == -2){
for(int x = 2, y = 0; x < 5 & y < 4; x++, y++){
if( a[(a.length-1) - y][(a[x].length-1) / 2 - x] == EMPTY)
a[(a.length-1) - y][(a[x].length-1) / 2 - x] = '·';
}
for(int x = 1, y = 2; x < 10 & y < 12; x++, y++){
if(a[(a.length-1) - y][x-1] == EMPTY)
a[(a.length-1) - y][x-1] = '·';
}
for(int x = 0, y = 10; x < 4 & y < 14; x++, y++){
if(a[(a.length-1) - y][(a[x].length-1) - x] == EMPTY)
a[(a.length-1) - y][(a[x].length-1) - x] = '·';
}
}
if (dirDisp == -3){
for(int x = 3, y = 0; x < 5 & y < 4; x++, y++){
if(a[(a.length-1) - y][(a[x].length-1) / 2 - x] == EMPTY)
a[(a.length-1) - y][(a[x].length-1) / 2 - x] = '·';
}
for(int x = 1, y = 1; x < 10 & y < 12; x++, y++){
if(a[(a.length-1) - y][x-1] == EMPTY)
a[(a.length-1) - y][x-1] = '·';
}
for(int x = 0, y = 9; x < 5 & y < 14; x++, y++){
if(a[(a.length-1) - y][(a[x].length-1) - x] == EMPTY)
a[(a.length-1) - y][(a[x].length-1) - x] = '·';
}
}
if (dirDisp == -4){
for(int x = 4, y = 0; x < 5 & y < 4; x++, y++){
if(a[(a.length-1) - y][(a[x].length-1) / 2 - x] == EMPTY)
a[(a.length-1) - y][(a[x].length-1) / 2 - x] = '·';
}
for(int x = 1, y = 0; x < 10 & y < 12; x++, y++){
if(a[(a.length-1) - y][x-1] == EMPTY)
a[(a.length-1) - y][x-1] = '·';
}
for(int x = 0, y = 8; x < 6 & y < 14; x++, y++){
if(a[(a.length-1) - y][(a[x].length-1) - x] == EMPTY)
a[(a.length-1) - y][(a[x].length-1) - x] = '·';
}
}
for (int x = 0; x < a.length; x++){
System.out.println();
for (int y = 0; y < a[x].length; y++){
System.out.print(" "+a [x][y]+" ");
}
}
System.out.println("\nDirección de disparo ["+dirDisp+"]");
System.out.println("Burbuja ["+Burbuja+"]");
}
public static int getDirDisp(){
int dirDisp = sc.nextInt();
while (dirDisp < -4 || dirDisp > 4){
System.out.println("Introduzca una dirección de disparo válida(-1,-2,-3,0,1,2,3)");
dirDisp = sc.nextInt();
}
return dirDisp;
}
public static char generarBurbuja(){
char burbuja = EMPTY;
int azar = (int) (Math.random() * 6);
switch (azar){
case 0 : burbuja = RED; break;
case 1 : burbuja = GREEN; break;
case 2 : burbuja = BLUE; break;
case 3 : burbuja = ORANGE; break;
case 4 : burbuja = YELLOW; break;
case 5 : burbuja = GRAY; break;
case 6 : burbuja = PURPLE; break;
}
return burbuja;
}
public static char obtenerAccionJugador (){
System.out.println("Acción?");
String aux = sc.next();
char accion = aux.charAt(0);
while (accion != IZQUIERDA && accion != DERECHA && accion != DISPARO && accion != SALIR){
System.out.println("Introduzca una acción válida:");
aux = sc.next();
accion = aux.charAt(0);
}
System.out.println("***************************");
return accion;
}
public static void obtenerTrayectoria (char [][] a){
System.out.println("Posiciones:");
for (int x = 0; x < a.length; x++){
for (int y = 0; y < a[x].length;y++){
if(a [x][y] == '·'){
Posicion pos = new Posicion(x,y);
ArrayList<Posicion> aListPos = new ArrayList<Posicion>();
aListPos.add(pos);
for(Posicion ii : aListPos){
System.out.println("(" + pos.getFila() + "," + pos.getColumna()+")");
}
}
}
}
}
public static char [][] realizarDisparo (char [][] a,char Burbuja){
int c [] = new int [a.length];
int w = 0;
for (int x = 0; x < a.length; x++){
for (int y = 0; y < a[x].length;y++){
if (a [x][y] == '·'){
w++;
c [w]= x;
}
if(a[c[1]][y] == '·'){
a[c[1]][y] = Burbuja;
}
}
}
return a;
}
public static int moverDerecha (int dirDisp, char [][] a){
if(dirDisp == -4)
dirDisp = -3;
else if (dirDisp == -3)
dirDisp = -2;
else if (dirDisp == -2)
dirDisp = -1;
else if (dirDisp == -1)
dirDisp = 0;
else if (dirDisp == 0)
dirDisp = 1;
else if (dirDisp == 1)
dirDisp = 2;
else if (dirDisp == 2)
dirDisp = 3;
else if(dirDisp == 3)
dirDisp = 4;
return dirDisp;
}
public static int moverIzquierda (int dirDisp, char [][] a){
if(dirDisp == 4)
dirDisp = 3;
else if (dirDisp == 3)
dirDisp = 2;
else if (dirDisp == 2)
dirDisp = 1;
else if (dirDisp == 1)
dirDisp = 0;
else if (dirDisp == 0)
dirDisp = -1;
else if (dirDisp == -1)
dirDisp = -2;
else if (dirDisp == -2)
dirDisp = -3;
else if(dirDisp == -3)
dirDisp = -4;
else{}
return dirDisp;
}
public static boolean existenBurbujas (char [][] a){
boolean result = true;
int contadorBurbujas = 0;
for (int x = 0; x < a.length; x++){
for (int y = 0; y < a[x].length;y++){
if(a [x][y] != EMPTY){
contadorBurbujas++;
}
}
}
if(contadorBurbujas > 0){
return true;
}
return result;
}
public static void borrarBurbujasAgrupadas(char [][] b, Posicion p) {
if(b[p.fila][p.columna] != EMPTY)
vecinos(b, p.fila, p.columna, b[p.fila][p.columna]);
}
private static void vecinos(char [][] b, int i_comienzo, int j_comienzo, char destino) {
Stack<Integer> ic = new Stack<Integer>();
Stack<Integer> jc = new Stack<Integer>();
ic.add(i_comienzo);
jc.add(j_comienzo);
int t = 0;
boolean [][] visitados = new boolean[b.length][b[0].length];
while( !ic.isEmpty() ) {
int i = ic.pop();
int j = jc.pop();
visitados[i][j] = true;
t++;
if(j-1 >= 0) {
if(b[i][j-1] == destino && !visitados[i][j-1]) {
ic.push(i);
jc.push(j-1);
}
}
if(j+1 < b[0].length) {
if(b[i][j+1] == destino && !visitados[i][j+1]) {
ic.push(i);
jc.push(j+1);
}
}
if(i-1 >=0) {
if(b[i-1][j] == destino && !visitados[i-1][j]) {
ic.push(i-1);
jc.push(j);
}
}
if(i+1 < b.length) {
if(b[i+1][j] == destino && !visitados[i+1][j]) {
ic.push(i+1);
jc.push(j);
}
}
if(j-1 >=0 && i-1 >=0) {
if(b[i-1][j-1] == destino && !visitados[i-1][j-1]) {
ic.push(i-1);
jc.push(j-1);
}
}
if(j+1 < b[0].length && i-1 >= 0 ) {
if(b[i-1][j+1] == destino && !visitados[i-1][j+1]) {
ic.push(i-1);
jc.push(j+1);
}
}
if(j-1>=0 && i+1<b.length) {
if(b[i+1][j-1] == destino && !visitados[i+1][j-1]) {
ic.push(i+1);
jc.push(j-1);
}
}
if(j+1<b[0].length && i+1<b.length) {
if(b[i+1][j+1] == destino && !visitados[i+1][j+1]) {
ic.push(i+1);
jc.push(j+1);
}
}
}
if(t >= 3)
for(int i=0; i<visitados.length; i++)
for(int j=0; j<visitados[i].length; j++)
if(visitados[i][j])
b[i][j] = EMPTY;
}
public static void jugar (){
Posicion p = new Posicion();
char [][] a = generarTablero();
System.out.println("Introduzca la dirección de disparo(-1,-2,-3,-4,0,1,2,3,4)");
int dirDisp = getDirDisp();
char Burbuja = generarBurbuja();
imprimirDisparo(a,dirDisp,Burbuja);
char accion = obtenerAccionJugador();
while(true){
if (accion == IZQUIERDA){
dirDisp = moverIzquierda(dirDisp,a);
limpiarTablero(a);
imprimirDisparo(a,dirDisp,Burbuja);
accion = obtenerAccionJugador();
}
else if(accion == DERECHA) {
dirDisp = moverDerecha(dirDisp,a);
limpiarTablero(a);
imprimirDisparo(a,dirDisp,Burbuja);
accion = obtenerAccionJugador();
}
else if(accion == DISPARO){
realizarDisparo(a,Burbuja);
borrarBurbujasAgrupadas(a,p);
Burbuja = generarBurbuja();
imprimirDisparo(a,dirDisp,Burbuja);
obtenerTrayectoria(a);
accion = obtenerAccionJugador();
}
else if (accion == SALIR){
System.out.println("GAME OVER");
}continue;
}
}
public static void main (String[] args){
jugar();
}
}
另一个有对象的类
公共(public)课位置{
int fila;
int columna;
public Posicion(int fila,int columna) {
this.fila = fila;
this.columna = columna;
}
public int getFila(){
return this.fila;
}
public int getColumna(){
return this.columna;
}
public Posicion(){}
尝试新的拍摄方式
在您回答所有问题后,我做了一种新的拍摄方法,比第一种方法简单得多,带有 while 循环和更多常量名称,使其易于理解。
Position 类还是一样。
我将把正射击方向的代码放在这里:
public static void getTrayectory (char [][] a,int shootDirection){
int x = 0, y = 0;
START = ((RIGHT_WALL)/2) + shootDirection;
a [BOTTOM][START] = SHOT;
if(shootDirection > 0){
while (!isOut(BOTTOM-x,START+y) && emptyCell(BOTTOM-x,START+y)){
b [BOTTOM - x][START + y] = SHOT;
x++; y++;
}
y = 0; x=x-1;
while (!isOut(BOTTOM-x,RIGHT_WALL-y) && emptyCell(BOTTOM-x,RIGHT_WALL-y)){
b [BOTTOM-x][RIGHT_WALL-y] = SHOT;
x++; y++;
}
y = 0; x=x-1;
while (!isOut(BOTTOM-x,LEFT_WALL+y) && emptyCell(BOTTOM-x,LEFT_WALL+y)){
b [BOTTOM-x][LEFT_WALL+y] = SHOT;
x++; y++;
}
y = 0;
while (!isOut(BOTTOM-x,RIGHT_WALL-y) && emptyCell(BOTTOM-x,RIGHT_WALL-y)){
b [BOTTOM-x][RIGHT_WALL-y] = SHOT;
x++; y++;
}
}
}
你还需要这个才能编译它; )
public static char [][] b = new char [25][9];
public static char EMPTY = '-';
public static char SHOT = 'x';
public static Scanner sc = new Scanner(System.in);
public static int shootDirection;
public static int BOTTOM = b.length - 1;
public static int RIGHT_WALL = b[0].length - 1;
public static int LEFT_WALL = 0;
public static int TOP = 0;
public static int CENTER = ((RIGHT_WALL)/2);
public static int START = ((RIGHT_WALL)/2) + shootDirection;
public static void fill (char [][] b){
for (int x = 0; x < b.length; x++){
for (int y = 0; y < b[x].length;y++){
b [x][y] = EMPTY;
b [10][y] = 'A';
}
}
}
public static void show (char [][] b){
for (int x = 0; x < b.length; x++){
System.out.println();
for (int y = 0; y < b[x].length; y++){
System.out.print(" "+b [x][y]+" ");
}
}
}
public static boolean isOut (int x,int y){
if(x < TOP)return true;
if(y > RIGHT_WALL)return true;
if(y < LEFT_WALL)return true;
return false;
}
static boolean emptyCell(int x, int y){
return b [x-1][y] == EMPTY;
}
问题是,它有效,但是 emptyCell 方法中存在错误。它返回 b [x-1][y] == EMPTY,因为如果我使用 return b[x][y] 它会得到一个 OutOfBounds 错误。由于 x-1,循环在它应该停止之前停止了一行,我不知道如何修改它以使其正常工作。
另一件事是,我想做一个更简单的方法,但即使有下面的小例子,我也不知道如何用我的常量来做。我希望你能为我做这件事,这一定很容易,但我不知道 :S 使用 isOut 方法和 emptyCell 方法,答案一定很近。谢谢你,对不起这个长线程。
最佳答案
据我了解代码,最接近您所拥有的解决方案是在 EMPTY
检查处放置一个 else
并在其中告诉它break
从可能被中断的第一个循环开始。然后,设置一个告诉它停止的变量,然后使其余的 for
循环以该变量为条件,这样它就不会从那里选择。
所以,它可以像这样(但请记住,以下是一个糟糕的解决方法,此代码的整体结构应该在之后立即改进):
if (dirDisp == 3){
boolean foundObstacle = false;
for(int x = 3, y = 0; x < 5 & y < 2; x++, y++){
if(a[(a.length-1) - y][(a[x].length-1) / 2 + x] == EMPTY)
a[(a.length-1) - y][(a[x].length-1) / 2 + x] = '·';
else {
foundObstacle = true;
break;
}
}
if(!foundObstacle) {
for(int x = 0, y = 1; x < 9 & y < 11; x++, y++){
if(a[(a.length-1) - y][(a[x].length-1) - x] == EMPTY)
a[(a.length-1) - y][(a[x].length-1) - x] = '·';
else {
foundObstacle = true;
break;
}
}
}
if(!foundObstacle) {
for(int x = 0, y = 9; x < 5 & y < 14; x++, y++){
if(a[(a.length-1) - y][x] == EMPTY)
a[(a.length-1) - y][x] = '·';
else break;
}
}
}
一旦解决了您的问题,请考虑对您的代码进行一些更改,因为首先,如果您知道循环将过早结束,则使用 for
循环被认为是不好的做法。在这些情况下,我们通常使用 while
循环。其次,硬编码这样的东西不是一个好主意。一旦你掌握了这个窍门,你就可以想出一个算法来完成任何给定方向的工作。
希望这对您有所帮助。
编辑:
if (dirDisp == 3){
boolean foundObstacle = false;
for(int x = 3, y = 0; x < 5 & y < 2 && !foundObstacle; x++, y++){
if(a[(a.length-1) - y][(a[x].length-1) / 2 + x] == EMPTY)
a[(a.length-1) - y][(a[x].length-1) / 2 + x] = '·';
else {
foundObstacle = true;
break;
}
}
if(!foundObstacle) {
// At its first iteration, this loop finds the '·' symbol placed by the previous one.
// So I've made it start from the second.
for(int x = 1, y = 2; x < 9 & y < 11 && !foundObstacle; x++, y++){
if(a[(a.length-1) - y][(a[x].length-1) - x] == EMPTY)
a[(a.length-1) - y][(a[x].length-1) - x] = '·';
else {
foundObstacle = true;
break;
}
}
}
if(!foundObstacle) {
for(int x = 0, y = 9; x < 5 & y < 14 && !foundObstacle; x++, y++){
if(a[(a.length-1) - y][x] == EMPTY)
a[(a.length-1) - y][x] = '·';
else break;
}
}
}
请记住,正如其他人所建议的那样,您在调试时遇到问题的原因是代码非常晦涩。无论经验多么丰富,这都是一件很难调试的事情。
这就是为什么每个人都建议我们应该编写干净的代码。记住,每个人都会犯错。一旦你了解了如何用一种语言编写程序(显然你个人也这样做),最重要的是你自己对代码的理解程度,这样你就可以更容易地看到你的错误。永远不要认为你理解一段代码只是因为你自己写了一段代码。创建很多变量,给它们起有意义的名字,把你的程序分成几部分,做很多类型来对应你的程序必须处理的实体,也给它们起有意义的名字,找到重复的代码模式,把它们变成函数/方法(有或没有参数化部分)并给它们起有意义的名字。
即使到那时你还找不到自己的错误,至少其他人可以更容易地帮助你。 ;)
关于java - 试图让游戏运行,循环条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13820467/