【问题标题】:Conditional statement for move methods in BlueJ Java puzzleBlueJ Java 谜题中移动方法的条件语句
【发布时间】:2019-07-08 00:54:24
【问题描述】:

我必须完成一些方法才能穿过房间才能离开。如果你踩到每个点,它会打开或关闭一个力场。如何为 move 方法编写条件语句?我的一直告诉我,即使我应该能够采取行动,我也不能。

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

 /*
 * 
 * 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.
         * 
         */   
    }
}

【问题讨论】:

  • 欢迎来到 Stack Overflow。请重新格式化您的问题以包含正确的标点符号。即使你在问什么也很难理解。尝试隔离您的问题和相关代码,而不是粘贴完整的问题和所有代码。

标签: java conditional bluej


【解决方案1】:

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

我知道如何移动,但我不知道如何格式化 条件语句,允许我越过关闭的障碍(假) 而其他障碍开启(真)。也就是说,如果红色障碍是 开,但黄色已关闭 我应该能够移动到黄色,但这是 不是这样,因为它告诉我路径被阻塞了。

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

这将是地图:

0 1 2 -------------- 0| rrr 1| r y r 2|是的

那么这将是状态:
从坐标 (1,2) 开始(redField 关闭,yellowField 关闭)
移动到坐标 (1,1)(redField 关闭,yellowField 开启)
移动到坐标 (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; } }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-24
    • 2014-01-06
    • 2012-03-09
    • 1970-01-01
    相关资源
    最近更新 更多