【发布时间】: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