我需要一个假想的墙壁,用于我的游戏"

时间:2016-02-14 22:37:54

标签: java eclipse

我来自德国,所以有些词可能是德语中的变数。

所以无论如何,我试图建立一个带阵列的二维房间(没有图形或类似的东西),我有一个"播放器"谁可以使用按钮1,2,3,4(右下)向左走。这是我的sourecode:

package f_ArrayUebungen;

import java.util.Scanner;

public class a_Array1 {

public static void main(String[] args)

{

    Scanner eingabe = new Scanner(System.in); 

    Scanner sc = new Scanner(System.in);

    int eingeben, w = 0, s = 0, l = 0, k = 1;

    int ort[][] = new int [s][w];
    int kasten[][] = new int [][]
    { 
        //   0 1 2 3 4 5 6 7 8 9 1 2 3
            {2,1,1,1,2,1,1,1,2,1,1,1,2},// 0
            {2,0,0,0,0,0,0,0,2,0,0,0,2},// 1
            {2,0,0,0,2,0,0,0,2,0,0,0,2},// 2
            {2,0,0,0,2,0,0,0,0,0,0,0,2},// 3
            {2,1,1,0,2,1,0,1,2,1,1,0,2},// 4
            {2,0,0,0,2,0,0,0,2,0,0,0,2},// 5
            {2,0,0,0,0,0,0,0,0,0,0,0,2},// 6
            {2,0,0,0,2,0,0,0,2,0,0,0,2},// 7
            {2,0,1,1,2,1,0,1,2,1,1,0,2},// 8
            {2,0,0,0,2,0,0,0,0,0,0,0,2},// 9
            {2,0,0,0,2,0,0,0,2,0,0,0,2},// 10
            {2,0,0,0,0,0,0,0,2,0,0,0,2},// 11
            {2,1,1,1,2,1,1,1,2,1,1,1,2} // 12
    };


      System.out.println("Wo satrten?\n1.)breite");
       w = sc.nextInt();
       System.out.println("2.) tiefe");
       s = sc.nextInt();

       kasten [s][w] = 3;

       for (int i = 0; i < kasten.length; i++ )
        {

          for (int j = 0; j < kasten[i].length; j++ )

              if ( kasten[i][j] == 2 ){
                  System.out.print("|");}

              else if ( kasten[i][j] == 1 ){
                  System.out.print("-");}

              else if (  kasten[i][j] == 0){
                System.out.print(" ");}

              else if ( kasten[i][j] == 3 ){
                  System.out.print("°");}
              System.out.println();     
        }

      while (k > l)
      {
          kasten [s][w] = 0;


      System.out.print("Welche Richtung? ");
      System.out.println("Runter = 1 | Rechts = 2 | Hoch = 3 | Links = 4");
      eingeben = eingabe.nextInt();
      int i=0,j=0;
      {
       if (eingeben == 1){
           s = s + 1;
       kasten [s][w] = 3;}
       if (eingeben == 2){
           w = w + 1;
           kasten [s][w] = 3;}
       if (eingeben == 3){
           s = s - 1;
           kasten [s][w] = 3;}
       if (eingeben == 4){
           w = w - 1;
           kasten [s][w] = 3;}

      }
      {
          if (kasten [s][w] == 0)
              System.out.println("nein");
      }


       kasten [s][w] = 3;

       for (int a = 0; a < kasten.length; a++ )
        {

          for (int q = 0; q < kasten[a].length; q++ )

              if ( kasten[a][q] == 2 ){
                  System.out.print("|");}

              else if ( kasten[a][q] == 1 ){
                  System.out.print("-");}

              else if (  kasten[a][q] == 0){
                System.out.print(" ");}

              else if ( kasten[a][q] == 3 ){
                  System.out.print("°");}
              System.out.println();  


        }
  }

}

}

所以我希望数字1和2是墙。如果我试图通过他们,程序应该阻止我,并告诉我它不会工作,我得到一个新的决定,我想要的方式。我希望你理解我。我知道程序可以写得更容易,但它是我写的最简单的方法。

感谢您的帮助: - )

1 个答案:

答案 0 :(得分:0)

欢迎使用StackOverflow!

首先,您可以采取一些措施来改进代码:

  1. 请在您的软件中使用英文作为所有名称。即使你认为没有人会看到它。 (但既然你已经使用过德语,我也这样做了)

  2. 不要重复代码。在屏幕上打印地图将始终如一,他们为此发明了功能(或方法)。请使用它们!

  3. 当您不需要时,请勿打印“\ n”等新行字符。这些在其他平台上可能不同。请使用System.out.println()打印出来。

  4. 缩进很重要。你已经做了一个不错的工作,但我注意到有些事情会让我感到沮丧,就像下面片段中的最后几行一样:

  5.       for (int j = 0; j < kasten[i].length; j++ )
    
              if ( kasten[i][j] == 2 ){
                  System.out.print("|");}
    
              else if ( kasten[i][j] == 1 ){
                  System.out.print("-");}
    
              else if ( kasten[i][j] == 0 ){
                System.out.print(" ");}
    
              else if ( kasten[i][j] == 3 ){
                  System.out.print("°");}
              System.out.println();     
    

    根据我之前评论中的这些改进和建议,请查看代码的更新版本:

    package f_ArrayUebungen;
    
    import java.util.Scanner;
    
    public class a_Array1
    {
        private static void kastenDrucken( int[][] kasten )
        {
            for (int i = 0; i < kasten.length; i++ )
            {
                for (int j = 0; j < kasten[i].length; j++ )
                {
                    if ( kasten[i][j] == 2 )
                    {
                        System.out.print("|");
                    }
                    else if ( kasten[i][j] == 1 )
                    {
                        System.out.print("-");
                    }
                    else if ( kasten[i][j] == 0 )
                    {
                        System.out.print(" ");
                    }
                    else if ( kasten[i][j] == 3 )
                    {
                        System.out.print("°");
                    }
                }
                System.out.println();
            }
        }
    
        private static boolean bewegenMoglich( int[][] kasten, int s, int w )
        {
            if (kasten[s][w] != 0)
            {
                System.out.println("nein");
                return false;
            }
            return true;
        }
    
        public static void main(String[] args)
        {
            Scanner eingabe = new Scanner(System.in);
            Scanner sc = new Scanner(System.in);
            int eingeben, w = 0, s = 0, l = 0, k = 1;
            int ort[][] = new int[s][w]; // ort is never used!
            int kasten[][] = new int[][]
            { 
            //   0 1 2 3 4 5 6 7 8 9 1 2 3       <- this line skips number 10
                {2,1,1,1,2,1,1,1,2,1,1,1,2},// 0
                {2,0,0,0,0,0,0,0,2,0,0,0,2},// 1
                {2,0,0,0,2,0,0,0,2,0,0,0,2},// 2
                {2,0,0,0,2,0,0,0,0,0,0,0,2},// 3
                {2,1,1,0,2,1,0,1,2,1,1,0,2},// 4
                {2,0,0,0,2,0,0,0,2,0,0,0,2},// 5
                {2,0,0,0,0,0,0,0,0,0,0,0,2},// 6
                {2,0,0,0,2,0,0,0,2,0,0,0,2},// 7
                {2,0,1,1,2,1,0,1,2,1,1,0,2},// 8
                {2,0,0,0,2,0,0,0,0,0,0,0,2},// 9
                {2,0,0,0,2,0,0,0,2,0,0,0,2},// 10
                {2,0,0,0,0,0,0,0,2,0,0,0,2},// 11
                {2,1,1,1,2,1,1,1,2,1,1,1,2} // 12
            };
    
            System.out.println("Wo satrten?");
            System.out.println("1.)breite");
            w = sc.nextInt();
            System.out.println("2.) tiefe");
            s = sc.nextInt();
    
            kasten[s][w] = 3;
    
            kastenDrucken( kasten );
    
            while (k > l) // Note: k and l are never updated, so this is always true!
            {
                System.out.print("Welche Richtung? ");
                System.out.println("Runter = 1 | Rechts = 2 | Hoch = 3 | Links = 4");
    
                eingeben = eingabe.nextInt();
                int i=0,j=0;
    
                if (eingeben == 1 && bewegenMoglich(kasten, s + 1, w))
                {
                    kasten[s][w] = 0;
                    s = s + 1;
                    kasten[s][w] = 3;
                }
                if (eingeben == 2 && bewegenMoglich(kasten, s, w + 1))
                {
                    kasten[s][w] = 0;
                    w = w + 1;
                    kasten[s][w] = 3;
                }
                if (eingeben == 3 && bewegenMoglich(kasten, s - 1, w))
                {
                    kasten[s][w] = 0;
                    s = s - 1;
                    kasten[s][w] = 3;
                }
                if (eingeben == 4 && bewegenMoglich(kasten, s, w - 1))
                {
                    kasten[s][w] = 0;
                    w = w - 1;
                    kasten[s][w] = 3;
                }
    
                kastenDrucken( kasten );
            }
        }
    }