Мастер должен перемещаться влево или вправо по мере необходимости на одну ячейку за раз.
Если значение cellX мастера в настоящее время равно LAST_CELL, оно должно двигаться так, чтобы его значение cellX было равно 0. В противном случае оно должно двигаться так, чтобы его значение LAST_CELL.
Внимательно прочитайте первую строку вопроса. Какой метод вас просят написать? Это будет определять, для чего вы вызываете сообщение.
Помните, что за пределами класса, где объявлена закрытая переменная экземпляра, доступ к нему должен осуществляться с помощью метода getli publi c. Так что если у вас есть личная переменная экземпляра в Wizard, к ней нельзя получить прямой доступ в WizardController.
Вам также следует внимательно прочитать вопрос, чтобы убедиться, что вы кодируете правильное поведение, когда cellX равен LAST_CELL.
Пока что я сделал это, но он не работает, так как он еще не завершен, плюс cellX, потому что его приват в Wizard говорит, что не может найти переменную. Я уверен, что для этого мне нужно использовать метод get из класса wizardcontroller. Но изо всех сил! Пожалуйста, помогите большое спасибо
public void travel(Wizard w) {
if (LAST_CELL) {
intcellNum = 0;
} else {
while (w.cellX < LAST_CELL) {
w.cellX++;
}
}
}
Вот класс контроллера Wizard:
public class WizardController
{
/**
* instance variables
*/
private Wizard wizard1;
private Wizard wizard2;
private int numOfRepeats;
private static final int MIN_NUM_REPEATS = 1;
private static final int MAX_NUM_REPEATS = 3;
public static final int LAST_CELL = 11;
/**
* Constructor for objects of class WizardController.
*/
public WizardController(Wizard aWizard1, Wizard aWizard2)
{
super();
this.wizard1 = aWizard1;
this.wizard2 = aWizard2;
this.numOfRepeats = 0;
}
/* instance methods */
/**
* Prompts the user for a number of action repeats
* in the range 1 to 3 inclusive, and returns this number.
*/
public int promptForNumOfRepeats()
{
int moves;
moves = Integer.parseInt(OUDialog.request("Please enter the number of"
+ " action repeats to be performed - between 1 and 3 (inclusive)"));
try
{
moves = Integer.parseInt(OUDialog.request("Please enter the number of"
+ " action repeats to be performed - between 1 and 3 (inclusive)"));
}
catch (NumberFormatException anException)
{
moves = 0;
}
return moves;
}
/**
* Returns true if the argument is in the range 1 to 3 (inclusive),
* otherwise false.
*/
public boolean isValidNumOfRepeats(int aNumber)
{
return ((aNumber >= WizardController.MIN_NUM_REPEATS)
&& (aNumber <= WizardController.MAX_NUM_REPEATS));
}
/**
* Repeatedly prompts the user for a number of repeats of the moves,
* until they enter a valid input representing a number in the range 1 to 3
* inclusive, and then returns this number.
*/
public int getNumOfRepeats()
{
int repeats = this.promptForNumOfRepeats();
while (!this.isValidNumOfRepeats(repeats))
{
OUDialog.alert("That is not a valid number of game repeats");
repeats = this.promptForNumOfRepeats();
}
return repeats;
}
[code=java]
public static void delay(int ms)
{
try{
Thread.sleep(ms);
}
catch(Exception e)
{
System.out.println("Problem in delay methods");
}
}
}
и класс Wizard:
public Wizard(OUColour aColour, int cellY)
{
super();
this.persona = new Triangle(30, 30, aColour);
this.persona.setXPos(0);
this.persona.setYPos(cellY * Wizard.CELL_SIZE_Y);
this.startCellX = 0;
this.startCellY = cellY;
this.startColour = aColour;
}
/* instance methods */
/**
* returns the persona of the Wizard - so that it can be displayed
* in the graphical display window
*/
public Triangle getPersona()
{
return this.persona;
}
/**
* Resets the receiver to its "home" X position of 0.
*/
public void homeX()
{
this.persona.setXPos(this.startCellX * Wizard.CELL_SIZE_X);
}
/**
* Resets the receiver to its "home" Y position of startCellY * CELL_SIZE.
*/
public void homeY()
{
this.persona.setYPos(this.startCellY * Wizard.CELL_SIZE_Y);
}
/**
* Decrements the X position of the receiver by CELL_SIZE.
*/
public void left()
{
this.persona.setXPos(this.persona.getXPos() - Wizard.CELL_SIZE_X);
}
/**
* Increments the X position of the receiver by CELL_SIZE.
*/
public void right()
{
this.persona.setXPos(this.persona.getXPos() + Wizard.CELL_SIZE_X);
}
/**
* Decrements the Y position of the receiver by CELL_SIZE.
*/
public void up()
{
this.persona.setYPos(this.persona.getYPos() - Wizard.CELL_SIZE_Y);
}
/**
* Increments the Y position of the receiver by CELL_SIZE.
*/
public void down()
{
this.persona.setYPos(this.persona.getYPos() + Wizard.CELL_SIZE_Y);
}
/**
* Decrements the X position of the receiver by CELL_SIZE
* times the value of the argument.
*/
public void leftBy(int numCells)
{
this.persona.setXPos(this.persona.getXPos() - Wizard.CELL_SIZE_X * numCells);
}
/**
* Increments the X position of the receiver by CELL_SIZE
* times the value of the argument.
*/
public void rightBy(int numCells)
{
this.persona.setXPos(this.persona.getXPos() + Wizard.CELL_SIZE_X * numCells);
}
/**
* Decrements the Y position of the receiver by CELL_SIZE
* times the value of the argument.
*/
public void upBy(int numCells)
{
this.persona.setYPos(this.persona.getYPos() - Wizard.CELL_SIZE_Y * numCells);
}
/**
* Increments the Y position of the receiver by CELL_SIZE
* times the value of the argument.
*/
public void downBy(int numCells)
{
this.persona.setYPos(this.persona.getYPos() + Wizard.CELL_SIZE_Y * numCells);
}
/**
* returns the cellX the wizard is currently on
*/
public int getCellX()
{
return this.persona.getXPos()/ Wizard.CELL_SIZE_X;
}
/**
* returns the cellY the wizard is currently on
*/
public int getCellY()
{
return this.persona.getYPos()/ Wizard.CELL_SIZE_Y;
}
/**
* sets the cellY the wizard is on to the argument
*/
public void setCellY(int aCellNumber)
{
this.persona.setYPos(aCellNumber * Wizard.CELL_SIZE_Y);
}
/**
* Returns a string representation of the receiver.
*/
@Override
public String toString()
{
return "An instance of class " + this.getClass().getName()
+ " in cell X:" + this.getCellX() + ", Y:" + this.getCellY()
+ ", colour " + this.persona.getColour();
}
}