// Class: Example6SteppedController
//
// Author: Alyce Brady
//
// License Information:
// This class is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation.
//
// This class is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
import edu.kzoo.grid.Grid;
import edu.kzoo.grid.GridObject;
import edu.kzoo.grid.Location;
import edu.kzoo.grid.gui.SteppedGridAppController;
/**
* Grid Container Package Examples:
*
* A Example6SteppedController
object keeps track
* of a queen's location and moves the queen diagonally in each
* timestep. A queen is represented as a generic
* GridObject
instance.
*
* @author Alyce Brady
* @version 20 December 2003
**/
public class Example6SteppedController extends SteppedGridAppController
{
// Encapsulated data for each Example6SteppedController object
private Location queenLoc; // the location of a queen
private int minRowsCols; // smaller grid dimension
/** Constructs a queen animation. **/
public Example6SteppedController()
{
queenLoc = new Location(0, 0);
}
/** Sets the application's grid.
* (Precondition: grid is not null.)
* @param grid the Grid to act on
**/
public void setGrid(Grid grid)
{
super.setGrid(grid);
// Determine how many rows or columns the queen can move.
if( grid.numRows() < grid.numCols() )
minRowsCols = grid.numRows();
else
minRowsCols = grid.numCols();
init();
}
/** Initializes or re-initializes the state of the grid application.
**/
public void init()
{
// If a queen is already on the board, remove her.
getGrid().remove(queenLoc);
// Insert queen at location (0, 0).
queenLoc = new Location(0, 0);
new GridObject(getGrid(), queenLoc);
}
/** Advances the animation one step.
* (Precondition: the grid has been set and
* has at least as many columns as rows.)
**/
public void step()
{
// Remove queen from current queen location.
getGrid().remove(queenLoc);
// Insert queen at diagonal location to the lower-right, or
// cycle back to location (0, 0) if the location to the
// lower-right would be invalid.
int newRowCol = (queenLoc.row() + 1) % minRowsCols;
queenLoc = new Location(newRowCol, newRowCol);
new GridObject(getGrid(), queenLoc);
}
}