BlueJ Java拼图中移动方法的条件语句

问题描述 投票:1回答:1

我必须完成一些方法来穿过房间才能退出。如果您踩到每个点,它会打开或关闭力场。我如何为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.
         * 
         */   
    }
}
java conditional bluej
1个回答
0
投票

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

我理解如何移动,但我不明白如何格式化条件语句以允许我在其他障碍开启时跳过关闭(错误)的障碍(真实)。也就是说,如果红色障碍打开但黄色已关闭,我应该能够移动到黄色,但事实并非如此,因为它告诉我路径被阻挡。

基于你上面所说的我认为你误解了这个问题。您假设每个位置都有一个力场,它取决于地图上的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关闭,黄色字段打开) 移动到坐标(1,0)(redField打开,yellowField打开)

您位于目标位置但所有字段都已打开,因此您尚未完成,但是在此状态下,您无法移动,因为所有字段都已打开。如果你假设要完成的算法就像你说的那样,那么这个问题就无法解决了。

从那里你可以参考我原来的答案如何解决问题。

原答案: 首先,我将有这些假设,因为你对游戏如何运作的描述尚不清楚。

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

鉴于上述假设,您将如何实施游戏。

您必须实施这些检查:

  1. 首先,你必须检查边界。 在你的情况下这个: (0,0)(1,0)(2,0) (0,1)(1,1)(2,1) (0,2)(1,2)(2,2) 因此,您的x和y坐标不应小于0或大于2
  2. 检查红色区域是否打开。
  3. 检查黄色区域是否打开。
  4. 检查您是否正对着门并且绿框已打开

这是要点:

在移动方法的开始(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| r r r * | * 1| r y r * | * 2| y y y */ // 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; } }
© www.soinside.com 2019 - 2024. All rights reserved.