如何防止“嵌套”

时间:2015-01-16 19:47:19

标签: javascript android nested

我制作了一个有效的代码,但我的老师告诉我,我正在'发明',因为他看到了很多if / else语句,我试着缩短我的代码,但我不知道如何摆脱这些如果/ else语句。

这是我的代码,简短摘要: 代码是一个nxn块的网格,只能与“空白”磁贴交换,所以我的代码看起来如下:

    grd.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            // This statement prevents the player from playing the game
            // if the timer is counting down.
            if (timer == true) {
                Toast.makeText(getApplicationContext(),
                        "The game has not started yet!", Toast.LENGTH_SHORT)
                        .show();

            } else {
                clickcounter++;

                // Because difficulty's are 0,1,2 the row/column dimensions
                // are difficulty + 3, here i create the texview of the moves
                // as well.
                size = (int) difficulty + 3;
                TextView movesText = (TextView) findViewById(R.id.textView2);

                // Checks if click is even or odd. Even is firstclick odd is
                // secondclick
                if (clickcounter % 2 == 1) {
                    firstimg = (ImageView) view;
                    firstclick = position;
                    firsttag = (int) firstimg.getTag();

                } else if (clickcounter % 2 == 0) {
                    ImageView secondimg = (ImageView) view;
                    scndtag = (int) secondimg.getTag();

                    // To prevent cheaters to press twice on the same tile
                    // we set this if statement
                    if (position == firstclick) {
                        clickcounter = 1;                       

                    // Next we check whether one of the clicked images
                    // is the blank one, if that's not the case do
                    // nothing!
                    } else if (firsttag != crops.size() - 1
                            && scndtag != crops.size() - 1) {

                    // This else statements checks what happens if one has clicked the
                    // blank tile
                    } else {

                        // This statement below is the same as an switch/case statement
                        // but because of the fact that the switch/case statement gave the
                        // following error i chose for this method:
                        // case expressions must be constant expressions
                        int i = firstclick - position;
                        if (i == -1) {
                            if(firstclick % size == size - 1){

                            } else {
                                swap(firstclick,position);
                                moves++;
                                movesText.setText("Moves: " + moves);
                                checkwin2();
                            }
                        } else if (i == 1) {
                            if(firstclick % size == 0){

                            } else {
                                swap(firstclick,position);
                                moves++;
                                movesText.setText("Moves: " + moves);
                                checkwin2();
                            }
                        } else if (i == size) {
                            swap(firstclick,position);
                            moves++;
                            movesText.setText("Moves: " + moves);
                            checkwin2();
                        } else if (i == -size) {
                            swap(firstclick,position);
                            moves++;
                            movesText.setText("Moves: " + moves);
                            checkwin2();
                        }
                    }
                }
            }
        }
    });
}

我试图制作一个switch / case语句以防止if / else语句的总和,但是它说它无法将大小标识为常量(因为难度是玩家选择的变量),所以如果我能设法做到这一点,if / else语句的重载可能会消失。

有什么建议吗?我已经做了一些工作来使代码更具可读性,原始代码如下:

    grd.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            if (timer == true) {
                Toast toast = new Toast(getApplicationContext());
                toast.makeText(getApplicationContext(),
                        "The game has not started yet!", Toast.LENGTH_SHORT)
                        .show();

            } else {
                clickcounter++;

                // Because difficulty's are 0,1,2 the row/column dimensions
                // are difficulty + 3
                size = (int) difficulty + 3;
                TextView movesText = (TextView) findViewById(R.id.textView2);

                // Checks if click is even or odd. Even is firstclick odd is
                // secondclick
                if (clickcounter % 2 == 1) {
                    firstimg = (ImageView) view;
                    firstclick = position;
                    firsttag = (int) firstimg.getTag();

                } else if (clickcounter % 2 == 0) {
                    ImageView secondimg = (ImageView) view;
                    Bitmap swapImage = crops.get(position);
                    Integer swapID = ID.get(position);
                    scndtag = (int) secondimg.getTag();

                    // To prevent cheaters to press twice on the same tile
                    // we set this if statement
                    if (position == firstclick) {
                        clickcounter = 1;

                    // These next if/else if statements are a summation
                    // of all kinds of
                    // rules that need to be implemented in the game!

                    // First we check whether one of the clicked images
                    // is the blank one
                    } else if (firsttag != crops.size() - 1
                            && scndtag != crops.size() - 1) {

                    // Here we check if the tiles are neighbors of each
                    // other
                    } else if ((position - 1 != firstclick)
                            && (position + 1 != firstclick)
                            && (position + size) != firstclick
                            && ((position - size) != firstclick)) {

                    // Check when secondclick on the right of firstclick
                    } else if (position - 1 == firstclick) {

                        // This prevents the game to swap tiles on the
                        // edges, like
                        // starting from 0, 2 with 3.
                        if (position % size != 0) {
                            crops.set(position, crops.get(firstclick));
                            ID.set(position, ID.get(firstclick));
                            crops.set(firstclick, swapImage);
                            ID.set(firstclick, swapID);
                            moves++;
                            movesText.setText("Moves: " + moves);
                            checkwin2();
                        }

                    // Check when secondclick on the left of firstclick
                    } else if (position + 1 == firstclick) {
                        if (position % size != (size - 1)) {
                            crops.set(position, crops.get(firstclick));
                            ID.set(position, ID.get(firstclick));
                            crops.set(firstclick, swapImage);
                            ID.set(firstclick, swapID);
                            moves++;
                            movesText.setText("Moves: " + moves);
                            checkwin2();
                        }

                    // Check when secondclick is above first
                    } else if ((position + size) == firstclick) {
                        crops.set(position, crops.get(firstclick));
                        ID.set(position, ID.get(firstclick));
                        crops.set(firstclick, swapImage);
                        ID.set(firstclick, swapID);
                        moves++;
                        movesText.setText("Moves: " + moves);
                        checkwin2();

                    // Check when secondclick is above first
                    } else if ((position - size) == firstclick) {
                        crops.set(position, crops.get(firstclick));
                        ID.set(position, ID.get(firstclick));
                        crops.set(firstclick, swapImage);
                        ID.set(firstclick, swapID);
                        moves++;
                        movesText.setText("Moves: " + moves);
                        checkwin2();
                    }

                    // This useful command rebuilds the imageviews
                    grd.invalidateViews();
                }
            }

        }
    });
}

亲切的问候,

Kees Til

1 个答案:

答案 0 :(得分:2)

首先,&#34;嵌套过多的固有问题&#34;是代码维护。因此,学习如何减少它是一种好习惯。

当你有嵌套循环/ if-else时,你可以做一些事情来试图消除它。以下是一些指示:

  1. 如果你在代码中看到重复的模式,在你的情况下是这样的:

    交换(firstclick,位置); 移动++; movesText.setText(&#34; Moves:&#34; + move); checkwin2();

  2. 您应该创建一个方法。然后,如果由于任何原因它改变了,它就在一个地方。此外,这允许您分离&#34;逻辑&#34; (if-else)来自程序。

    1. 您的代码非常&#34;程序性&#34;这有时是必要的,但却失去了面向对象编程的好处。要更改此设置,请考虑创建一个对象来处理此问题。制作&#34;逻辑变量&#34;像这样:

      int i = firstclick - position;

    2. 并使用此示例,make&#34; firstclick&#34;和&#34;位置&#34;进入私人对象属性并创建&#34; getters&#34;和#34; setters&#34;。然后根据get / set执行逻辑。这将使您能够专注于对象&#34;受到影响而受到影响。然后,如果您执行了第1步,则将这些方法合并到新对象中。

      1. 此外,您可以尝试采用最深层的嵌套逻辑,将其转换为&#34;对象&#34;然后尝试对下一个最深处做同样的事情,依此类推。这可以揭示不明显的逻辑模式,同时也有助于使代码更易于阅读和维护。

      2. 检查你的&#34;标志&#34;变量。也许他们应该被分成一个或多个对象。您甚至可以将对象用作&#34;标记&#34; - 甚至标志可以成为代码中的两个或多个变量,而不是正确地成为单个对象。

      3. 注意:不要害怕&#34;害怕&#34;只有一种或两种方法的小型或简单物体。此外,不要犹豫,有单行方法。重要的是动作(方法)和属性(变量)放在对象中,因为它们是相关的

相关问题