尝试让游戏运行,循环条件。

3

我将尝试简明清晰地说明我的现有情况和需求。

我有一个由字符元素组成的二维数组,形成一个棋盘。我需要从底部进行一次射击,并用“·”来表示它。射击从底部(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] = '·';
            }
        }

我将提供shot direction = 3的代码。 EMPTY常量为' '。事实上,数组中还有其他字符,例如'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不同的字符时,射击必须停止。我试过使用break和continue。还尝试了使用firstLoop:{},在循环的每个部分中使用布尔值,但都没有成功。我需要一点帮助。
如果有不清楚的地方,请问。
注:shot有三个'for'循环,第一个是从底部到第一堵墙填充数组,第二个是用于墙之间的空间,第三个是用于相反的墙到顶部。 注2:'r'行是为了让您看到实际发生的情况。
使用布尔值后:
 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 class Posicion {
    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循环和更加容易理解的常量名称。

位置类仍然保持不变。

下面是正方向发射代码:

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,循环停止比应该早一行,我不知道如何修改它使其正常工作。
另一件事是,我想做一个更简单的方法,但即使在下面的小例子中,我也不能通过我的常量弄清楚如何做到这一点。我希望你能帮我做到这一点,它一定很容易,但我不知道怎么做。通过isOut方法和emptyCell方法,答案必须接近。谢谢,对于这个长串的线程感到抱歉。

你似乎在使用按位与运算符&,但应该使用逻辑与&&。在这种情况下,我认为它们的作用是相同的,但你仍然应该进行更正。 - hyde
3个回答

1
据我理解你的代码,最接近你的解决方案是在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;
                }
            }
        }

请记住,你在调试时遇到困难的原因,正如其他人所建议的那样,是因为代码非常难懂。无论一个人有多少经验,这都是一件非常困难的事情。

这就是为什么每个人都建议我们应该编写清晰易懂的代码。记住,每个人都会犯错误。一旦你理解了如何用某种语言编写程序(而你显然已经掌握了),最重要的是你自己对代码的理解程度,这样你就可以更容易地发现自己的错误。永远不要认为你理解了一段代码,只是因为你自己写过它。创建许多变量,给它们有意义的名称,将程序分成几个部分,创建许多类型,与程序需要处理的实体相对应,也给它们有意义的名称,找出重复的代码模式,将它们转换为函数/方法(带或不带参数化部分),同样给它们有意义的名称。

即使你在这个阶段找不到错误,至少对于其他人来说,帮助你也会更容易。;)


我尝试了这段代码,和我做的完全一样,但它不起作用:S 它只显示在撞墙之前的镜头。什么都没有。在让它工作后,我将更改for循环为while循环,感谢您的建议;) - AgusDG
@AgusDG 很抱歉之前我误解了。我以为你想让“.”停在“r”块并且不再移动。那么你是不是不想让它停下来呢?如果是这样,你期望的结果是什么,而我的建议又做了什么呢? - Theodoros Chatzigiannakis
那就是我想要的,但是那段代码不起作用。它执行了完整的第一个循环,但甚至没有开始第二个循环:S 我知道它应该能工作,但它却没有。 - AgusDG
@AgusDG 你觉得把 for 循环的条件改成 && !foundObstacle 怎么样? - Theodoros Chatzigiannakis
完成。它是用西班牙语编写的,但是一样的,只需运行并使用字母“A”(向左移动),“D”(向右移动),“S”(射击)和“Q”(退出游戏)。如果dirDisp == 3,则布尔值为ON;) - AgusDG
显示剩余4条评论

1
如果您的中断条件很复杂,建议改用while循环。使用良好的方法名也会使您的代码更易读。
while (!outOfBounds(x,y) && emptyCell(x,y) {
  a[x][y] = '.';
  moveCursor(direction);
}

...

boolean emptyCell(int x, int y){
  return a[x][y] == EMPTY;
}

boolean outOfBounds(int x, int y){
  if (x<0) return true;
  if (y<0) return true;
  if (x>LIMIT_X) return true;
  if (y>LIMIT_Y) return true;
  return false;
}

void moveCursor(direction){
  if (direction==DOWN_RIGHT){
    x++;
    y++;
    if (x > X_LIMIT){
      x-=2;
      direction = DOWN_LEFT;
    }
    ...
  }

我想过这么做,但我没有意识到如何去做。你能在这里写出“outOfBounds”和“emptyCell”的方法吗?我想我知道如何做,但看到它们会更好;) - AgusDG
我不明白DOWN_RIGHT、LEFT和X_LIMIT是什么。 - AgusDG
@AgusDG outOfBounds 是一个假设的方法,用于检查 xy 是否在限制范围内。大写字母中的 DOWN_RIGHT 和其他标识符应该是常量,其中存储了您场地的边界。 - Theodoros Chatzigiannakis
1
@AgusDG 我稍微填了一下空。大写的单词应该是常量。我想你比我更清楚它们的值 :) 如果你使用像这样小型、简单和命名良好的方法来因式分解你的代码,那么工作将会立即变得清晰明了,你也会更容易地发现错误。 - Joeri Hendrickx

0

我刚写了这个小方法,它可以做你想要的事情。但是它填充了一个随机字符数组。请看我的中断条件。

private void shot() {
    char charArray[][] = new char[100][20];
    Random r = new Random();
    for(int i = 0;i<charArray.length;++i) {
        for(int j = 0;j<charArray[0].length;++j) {
            if(r.nextInt(5) == 0) {
                charArray[i][j] = (char) (r.nextInt(26)+65);
            }
        }
    }
    boolean shotRight = false;
    for(int i = charArray.length-1,j = charArray[0].length-1;i>0;--i) {
        System.out.println(i + " " + j);
                    // break condition
        if(charArray[i][j] != '\u0000') break;
        if(shotRight) {
            charArray[i][j] = '.';
            ++j;
        }
        else {
            charArray[i][j] = '.';
            --j;
        }
        if(j <= 0) {
            shotRight = true;
        }
        if(j >= charArray[0].length-1) {
            shotRight = false;
        }

    }
    for(int i = 0;i<charArray.length;++i) {
        for(int j = 0;j<charArray[0].length;++j) {
            System.out.print(((charArray[i][j] == '\u0000') ? "  ":charArray[i][j] + " ") + "  ");
        }
        System.out.println();
    }

}

它会中断,因为 char 的默认值为 '\u0000'


我没有Random可以编译它:S - AgusDG
@AgusDG Randomjava.util 包中的一个类。您可以在源文件顶部执行 import java.util.Random;,或者使用其完整名称。 - Theodoros Chatzigiannakis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接