Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Grid ADT
- * @author J.R.Dermoudy and <<INSERT YOUR NAME HERE>>
- * @version December 2005
- This file holds the Grid ADT which represents
- the Nim board. The Grid consists of an array
- of the (linear) squares in the board.
- YOU NEED TO MAKE CHANGES TO THIS FILE!
- */
- import java.awt.*;
- public class Grid implements GridInterface
- {
- //finals
- private final int STARTDIMENSION = 1;
- private final int DIMENSION=21; // number of squares in grid
- private final int MAXIMUM_ROW=6; // number of rows in grid
- private final boolean TRACING=true; // do we want to see trace output?
- // properties
- private int dimension; // the number of rows in the grid
- private Square board[]; // all the Squares within the grid
- public Grid()
- /*
- Constructor method.
- Pre-condition: none
- Post-condition: a 21 element grid is created in
- which all squares are occupied
- Informally: create a full 6 row triangular grid
- */
- {
- int i,r,c;
- Location l;
- trace("Grid: constructor begins");
- dimension=MAXIMUM_ROW;
- board=new Square[DIMENSION];
- trace("Grid: building squares");
- i=0;
- for (r=0; r<MAXIMUM_ROW; r++)
- {
- for (c=0; c<=r; c++)
- {
- l=new Location(r+1,c+1);
- board[i]=new Square(l);
- i++;
- }
- }
- trace("Grid: constructor ends");
- }
- private int findIndex(Location l)
- /*
- Locate index in grid which corresponds to square at
- given location
- Pre-condition: location is within the current grid
- Post-condition: the index of the element with the given
- location is returned
- Informally: identify which array element is at the given
- location
- */
- {
- int i,r,c;
- trace("findIndex: findIndex starts");
- i=0;
- for (r=1;r<l.getRow();r++)
- {
- for (c=1;c<=r;c++)
- {
- i++;
- }
- }
- i+=(l.getColumn()-1);
- trace("findIndex: findIndex ends");
- return i;
- }
- public void setSquare(Location l, Square s) throws IllegalGridException
- /*
- Set method for an element of the "board" instance variable.
- Pre-condition: the given Square object and the board array
- are defined
- Post-condition: the given square is assigned to an element
- of the Grid object selected according to
- the given location within the grid
- Informally: insert the given square into the grid at the
- appropriate location, an exception is thrown if
- the location is not within the grid
- */
- {
- int i;
- trace("setSquare: setSquare starts");
- i = findIndex(l);
- if((i < STARTDIMENSION)|| (i > DIMENSION))
- {
- throw new IllegalGridException();
- }
- else
- {
- board[i]= s;
- }
- trace("setSquare: setSquare ends");
- }
- public Square getSquare(Location l) throws IllegalGridException
- /*
- Get method for an element of the "board" instance variable.
- Pre-condition: the board array is defined
- Post-condition: the Square object at the appropriate
- element of the "board" selected according
- to the given Location value is returned
- Informally: return the square of the grid at the given
- location, an exception is thrown if the
- location is not within the grid
- */
- {
- int i;
- trace("getSquare: getSquare starts");
- i = findIndex(l);
- if((i < STARTDIMENSION)|| (i > DIMENSION))
- {
- throw new IllegalGridException();
- }
- trace("getSquare: getSquare ends");
- return board[i];
- }
- public void setDimension(int d)
- /*
- Set method for the "dimension" instance variable.
- Pre-condition: the given Dimension value is defined and
- valid
- Post-condition: the instance variable "dimension" is
- assigned the given Dimension value
- Informally: assign the given dimension to the Grid object
- */
- {
- trace("setDimension: setDimension starts");
- dimension = d;
- trace("setDimension: setDimension ends");
- }
- public int getDimension()
- /*
- Get method for "dimension" instance variable.
- Pre-condition: none
- Post-condition: the value of the Grid object's dimension
- field is returned
- Informally: return the current grid's dimension
- */
- {
- trace("getDimension: getDimension starts and ends");
- return dimension;
- }
- public void emptySquare(Location l) // ******************* may need checking *************************
- /*
- Pre-condition: the given Location value is within
- the bounds of the current Grid
- object
- Post-condition: the square at the position in the
- grid indicated by the given Location
- value is altered to be empty
- Informally: update the square at the nominated location
- of the grid with the empty symbol
- */
- {
- Square q = new Square(l, " "); // not sure what im doing here.... XD
- trace("emptySquare: emptySquare starts");
- int i = findIndex(l);
- board[i] = q;
- trace("emptySquare: emptySquare ends");
- }
- public boolean isEmpty(Location l)
- /*
- Pre-condition: the given Location value is within
- the bounds of the current grid
- Post-condition: a Boolean value is returned which
- represents whether the symbol of
- the square of the current Grid
- object with the given Location
- value is empty
- Informally: return whether or not the square at
- the given location is empty
- */
- {
- int i;
- trace("isEmpty: isEmpty starts and ends");
- i = findIndex(l);
- if(board[i] == null)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public boolean validMove(Location l) // **********************************
- /*
- Pre-condition: none
- Post-condition: true is returned if the given
- Location is within the bounds of
- the current Grid object, false is
- returned if it is not
- Informally: return whether or not the given
- location lies within the current grid
- */
- {
- int r;
- int c;
- trace("validMove: validMove starts and ends");
- return false;
- }
- public boolean gameOver() // ******************************
- /*
- Pre-condition: none
- Post-condition: true is returned if the game is
- over because the grid is empty
- Informally: return whether or not the game is over
- */
- {
- int r,c;
- Location l;
- trace("gameOver: gameOver starts");
- for (r=0; r<MAXIMUM_ROW; r++)
- {
- for (c=0; c<=r; c++)
- {
- }
- }
- trace("gameOver: gameOver ends");
- return true;
- }
- public String toString()
- /*
- Pre-condition: none
- Post-condition: a String representation of the grid
- is returned
- Informally: find a String representation of the grid
- */
- {
- String s="[";
- int r,c;
- Location l;
- trace("toString: toString starts");
- for (r=1;r<=dimension;r++)
- {
- for (c=1;c<=r;c++)
- {
- l=new Location(r,c);
- if (isEmpty(l))
- {
- s+=" ";
- }
- else
- {
- s+="X";
- }
- }
- if (r != dimension)
- {
- s+=",";
- }
- }
- s+="]";
- trace("toString: toString ends");
- return s;
- }
- public void showGrid(Display s)
- /*
- Pre-condition: the Screen parameter is correctly defined
- Post-condition: the screen representation of the Grid
- object is displayed on the given Screen
- Informally: display the current grid
- */
- {
- int i;
- trace("showGrid: showGrid starts");
- for (i=0; i<DIMENSION; i++)
- {
- board[i].showSquare(s,getDimension());
- }
- trace("showGrid: grid is " + toString());
- trace("showGrid: showGrid ends");
- }
- public void trace(String s)
- /*
- Provide trace output.
- Pre-condition: none
- Post-condition: if trace output is desired then the given String
- parameter is shown on the console
- Informally: show the given message for tracing purposes
- */
- {
- if (TRACING)
- {
- System.out.println("Grid: " + s);
- }
- }
- }
Add Comment
Please, Sign In to add comment