当前位置: 代码迷 >> java >> BlueJ Java Puzzle中的move方法的条件语句
  详细解决方案

BlueJ Java Puzzle中的move方法的条件语句

热度:101   发布时间:2023-08-02 10:34:58.0

我必须完成一些方法才能跨房间退出。 如果您踩到每个点,它将打开或关闭力场。 我将如何为move方法编写条件语句? 我的一直在告诉我,即使我应该能够采取行动,我也无法做到。

我了解如何移动,但不了解如何格式化条件语句以允许我越过关闭的障碍(错误)而打开其他障碍(正确)。 也就是说,如果红色障碍打开了,但黄色关闭了,我应该可以移动到黄色,但事实并非如此,因为它告诉我路径被阻塞了。

 /*
 * 
 * This class allows a user to walk around in a virtual 3x3 room 
 * and solve a puzzle.
 * 
 * The room has nine locations with the following (x, y) coordinates.
 * 
 *                      (0, 0)  (1, 0)  (2, 0)
 *                      (0, 1)  (1, 1)  (2, 1)
 *                      (0, 2)  (1, 2)  (2, 2)
 *                      
 * The user starts at location (1, 2).  Directly across the room from the 
 * user is a door.  The door may be blocked by 0, 1, or 2 force fields.  
 * The frame of the door identifies which force field is currently blocking 
 * it: a red frame means that only the red force field is on, a yellow frame 
 * means that only the yellow force field is on, an orange frame means that 
 * both force fields are on, and a green frame means that both fields are off. 
 * 
 * Depending where the user steps in the room one of the force fields will 
 * either turn on or off. The goal of the puzzle is to cross the room (i.e., 
 * reach position (1,0)) facing the door with both force fields off (i.e., a 
 * green frame).  
 * 
 * The constructor and the methods of this class are incomplete.  The comments 
 * tell you what is missing.  You do not need to add any fields to this class.  
 * You do not need to change any of the other classes in this project.
 */

public class Puzzle
{

    //Your current xy position in the room
    private int xPosition;
    private int yPosition;

    //Boolean variables that tell you whether or not the red and yellow
    //force fields are currently turned on (true) or off (false).
    private boolean yellowField;
    private boolean redField;

    private View view;

    public Puzzle()
    {

        /*
         * Finish: Initialize xPosition and yPosition to reflect that your 
         * initial position in the puzzle is (1,2) and that both force fields 
         * are initially off.
         */
        xPosition = 1;
        yPosition = 2;
        yellowField = false;
        redField = false;
        view = new View();
        view.display(xPosition, yPosition, yellowField, redField);
    }

    public void moveForward() 
    { 
        /*
         * Finish: Implement this method that moves you forward in the room
         * if there is not a wall blocking you.  Otherwise print a message
         * alerting the user that this move is not permitted now.
         * 
         * If the move is allowed then you should call toggleFields() and
         * view.display(xPosition, yPosition, yellowField, redField) afterward.
         */

     }

    public void moveBackward()
    {
        /*
         * Finish: Implement this method that moves you backward in the room
         * if there is not a wall blocking you.  Otherwise print a message
         * alerting the user that this move is not permitted now.
         * 
         * If the move is allowed then you should call toggleFields() and
         * view.display(xPosition, yPosition, yellowField, redField) afterward.
         */

        if (yellowField)
        {
            if (yellowField == false)
                {
                    yPosition += 1;
                    toggleFields();
                    view.display(xPosition, yPosition, yellowField, redField);
                }
            else
                {
                    System.out.println("Your path is blocked pick another move");
                }
        }
        else
        {
            if (redField == false)
                {
                    yPosition += 1;
                    toggleFields();
                    view.display(xPosition, yPosition, yellowField, redField);
                }
            else
                {
                    System.out.println("Your path is blocked pick another move");
                }
        }
    }

    public void moveRight()
    {

    }

    public void moveLeft()
    {
        /*
         * Finish: Implement this method that moves you to the left in the room
         * if there is not a wall blocking you.  Otherwise print a message
         * alerting the user that this move is not permitted now.
         * 
         * If the move is allowed then you should call toggleFields() and
         * view.display(xPosition, yPosition, yellowField, redField) afterward.
         * 
         */

    }

    private void toggleFields()
    {
        /*
         * Finish: Implement this method that turns a field on or off depending 
         * on where you step.  The following table describes the coordinate system 
         * and the effect of stepping in a particular location.
         * 
         * The x coordinate of a location is its row number, and the y coordinate 
         * is its column number.
         * 
         *     0   1   2  
         *  ------------
         *  0| r   r   r
         *   |
         *  1| r   y   r
         *   |
         *  2| y   y   y
         *  
         *  
         *  By stepping in a location labeled 'r' the red force field will turn 
         *  'on' if it is currently 'off' and 'off' if it is currently turned 'on'.
         *  
         *  By stepping in a location labeled 'y' the yellow force field will turn 
         *  'on' if it is currently 'off' and 'off' if it is currently turned 'on'.
         */
        if(redField)
        {
            if (redField == true)
            {
                redField = false;
            }
            else 
            {
                redField = true;
            }
        }
        else
        {
            if (yellowField == true)
            {   
                yellowField = false;
            }
            else
            {
                yellowField = true;
            }
        }
    }  

    public void walkthrough()
    {
        /*
         * Extra Credit:  Provide a sequence of calls to moveForward(), moveLeft(), 
         * moveRight(), and moveBackward() that solves the puzzle.  The puzzle is
         * solved if these moves take the user from location (1, 2) to location (1, 0)
         * facing a green door.
         * 
         */   
    }
}

编辑:
您的方法的错误是您没有注意黄色或红色字段将打开/关闭的坐标。 您只是在检查它们是否打开/关闭。 因此,首先您必须注意那些不同的坐标,因为正如您所说的那样,某些坐标将使yellow / re字段处于打开/关闭状态。 一旦知道了这些坐标,就可以检查用户是否处于黄色/红色位置,然后检查该字段的状态(例如,如果该字段当前处于打开状态,则将其关闭)。

我了解如何移动,但不了解如何格式化条件语句以允许我越过关闭的障碍(错误)而打开其他障碍(正确)。 也就是说,如果红色障碍打开了,但黄色关闭了,我应该可以移动到黄色,但事实并非如此,因为它告诉我路径被阻塞了。

根据您上面所说的,我认为您误解了这个问题。 您假设每个位置都有一个力场,并且取决于地图上的y / r字母,但我认为并非如此。 只有门具有力场,而您的目标是在所有力场均关闭的情况下到达门所在的位置。 因为如果您坚持自己的理解并且目标是使用户从位置(1、2)移向位置(1、0),且朝向绿色的门(所有字段均已关闭),则这是不可能的。

这将是地图:

0   1   2
            --------------  
            0| r   r   r  
            1| r   y   r  
            2| y   y   y

然后将是以下状态:
从坐标(1,2)开始(redField关闭,yellowField关闭)
移至坐标(1,1)(redField关闭,yellowField打开)
移至坐标(1,0)(redField打开,yellowField打开)

您位于目标位置,但所有字段均处于打开状态,因此您仍未完成操作,但是在此状态下,您无法移动,因为所有字段均处于打开状态。 如果您假设要完成的算法与您所说的一样,则使此问题无法解决。

从中您可以参考我有关如何解决问题的原始答案。

原始答案:
首先,我将有这些假设,因为您对游戏运行方式的描述不清楚。

  1. 游戏的目标是前往“特定”位置,但您必须关闭该位置的所有力场才能进入。
  2. 该“特定位置”从未在程序中声明,并且没有方法参数声明它,因此我假设它是一个静态变量。 因此,现在我将使用演练方法的注释并将用户位置分配给(1,2),将“某些”地方位置分配给(1,0)

鉴于以上假设,此处是您如何实施游戏。

您必须实施以下检查:

  1. 首先,您必须检查边界。
     
           您的情况是: 
    \n (0,0)(1,0)(2,0)
    \n (0,1)(1,1)(2,1)
    \n (0,2)(1,2)(2,2)
    \n 因此,您的x和y坐标不应小于0或大于2\n
  2. 检查红色区域是否打开。
  3. \n
  4. 检查黄色字段是否打开。
  5. \n
  6. 检查您是否面对门并且绿色框是否打开
  7. \n

要点如下:

在开始移动方法(moveLeft,moveRight,moveBackward,moveForward)之前,您必须计算用户的去向。 然后从该新计算的位置进行上述检查(仅检查1)。

例:

public void moveBackward() {
    int tmpY = yPosition + 1; // Subtract 1 from y position since the user is about to move backward

    // Check # 1
    if (tmpY > 2) { // you only need this check since you know that you are only incrementing yPosition
        System.out.println("Your path is blocked pick another move");
        return;
    }

    // If a valid move but the user did not win
    yPosition = tmpY;
    toggleFields();
    view.display(xPosition, yPosition, yellowField, redField);
}

现在,您必须在toggleFields方法中实现其余检查。

public void toggleFields(){ /* You can store all the locations of the red and yellow fields in a matrix but for simplicity sake I will just hard code it to the if/else statements. * Also the goal location is hard coded since there is no mention from your post where the goal location is. The goal location is (1.0) * 0 1 2 * ------------ * 0| rrr * | * 1| ryr * | * 2| yyy */ // Check # 2 if ((xPosition == 0 && yPosition == 0) || (xPosition == 1 && yPosition == 0) || (xPosition == 2 && yPosition == 0) || (xPosition == 0 && yPosition == 1) || (xPosition == 2 && yPosition == 1) ) { if (redField) { redField = false; } else { redField = true; } } // Check # 3 if ((xPosition == 1 && yPosition == 1) || (xPosition == 0 && yPosition == 2) || (xPosition == 1 && yPosition == 2) || (xPosition == 2 && yPosition == 2) ) { if (yellowField) { yellowField= false; } else { yellowField= true; } } // Check # 4 if (!redField && !yellowField && xPosition == 1 && yPosition == 0) { System.out.println("You win"); return; } }
  相关解决方案