Guest User

Untitled

a guest
Jun 18th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.81 KB | None | 0 0
  1. /**
  2. *   Grid ADT
  3. *   @author J.R.Dermoudy and <<INSERT YOUR NAME HERE>>
  4. *   @version December 2005
  5.  
  6.     This file holds the Grid ADT which represents
  7.     the Nim board.  The Grid consists of an array
  8.     of the (linear) squares in the board.
  9.  
  10.     YOU NEED TO MAKE CHANGES TO THIS FILE!
  11. */
  12.  
  13. import java.awt.*;
  14.  
  15.  
  16. public class Grid implements GridInterface
  17. {
  18.     //finals
  19.     private final int STARTDIMENSION = 1;
  20.     private final int DIMENSION=21;                     // number of squares in grid
  21.     private final int MAXIMUM_ROW=6;                    // number of rows in grid
  22.     private final boolean TRACING=true;             // do we want to see trace output?
  23.  
  24.     // properties
  25.     private int dimension;          // the number of rows in the grid
  26.     private Square board[];         // all the Squares within the grid
  27.  
  28.  
  29.     public Grid()
  30.     /*
  31.         Constructor method.
  32.         Pre-condition: none
  33.         Post-condition: a 21 element grid is created in
  34.                         which all squares are occupied
  35.         Informally: create a full 6 row triangular grid
  36.     */
  37.     {
  38.         int i,r,c;
  39.         Location l;
  40.  
  41.         trace("Grid: constructor begins");
  42.  
  43.         dimension=MAXIMUM_ROW;
  44.         board=new Square[DIMENSION];
  45.  
  46.         trace("Grid: building squares");
  47.         i=0;
  48.         for (r=0; r<MAXIMUM_ROW; r++)
  49.         {
  50.             for (c=0; c<=r; c++)
  51.             {
  52.                 l=new Location(r+1,c+1);
  53.                 board[i]=new Square(l);
  54.                 i++;
  55.             }
  56.         }
  57.  
  58.         trace("Grid: constructor ends");
  59.     }
  60.  
  61.     private int findIndex(Location l)
  62.     /*
  63.         Locate index in grid which corresponds to square at
  64.         given location
  65.         Pre-condition: location is within the current grid
  66.         Post-condition: the index of the element with the given
  67.                         location is returned
  68.         Informally: identify which array element is at the given
  69.                     location
  70.     */
  71.     {
  72.         int i,r,c;
  73.  
  74.         trace("findIndex: findIndex starts");
  75.  
  76.         i=0;
  77.         for (r=1;r<l.getRow();r++)
  78.         {
  79.             for (c=1;c<=r;c++)
  80.             {
  81.                 i++;
  82.             }
  83.         }
  84.  
  85.         i+=(l.getColumn()-1);
  86.  
  87.         trace("findIndex: findIndex ends");
  88.         return i;
  89.     }
  90.  
  91.  
  92.     public void setSquare(Location l, Square s) throws IllegalGridException
  93.     /*
  94.         Set method for an element of the "board" instance variable.
  95.         Pre-condition: the given Square object and the board array
  96.                        are defined
  97.         Post-condition: the given square is assigned to an element
  98.                         of the Grid object selected according to
  99.                         the given location within the grid
  100.         Informally: insert the given square into the grid at the
  101.                     appropriate location, an exception is thrown if
  102.                     the location is not within the grid
  103.     */
  104.     {
  105.         int i;
  106.  
  107.         trace("setSquare: setSquare starts");
  108.  
  109.         i = findIndex(l);
  110.         if((i < STARTDIMENSION)|| (i > DIMENSION))
  111.         {
  112.             throw new IllegalGridException();
  113.         }
  114.         else
  115.         {
  116.             board[i]= s;
  117.         }
  118.         trace("setSquare: setSquare ends");
  119.     }
  120.  
  121.  
  122.     public Square getSquare(Location l) throws IllegalGridException
  123.     /*
  124.         Get method for an element of the "board" instance variable.
  125.         Pre-condition: the board array is defined
  126.         Post-condition: the Square object at the appropriate
  127.                         element of the "board" selected according
  128.                         to the given Location value is returned
  129.         Informally: return the square of the grid at the given
  130.                     location, an exception is thrown if the
  131.                     location is not within the grid
  132.     */
  133.     {
  134.         int i;
  135.  
  136.         trace("getSquare: getSquare starts");
  137.  
  138.         i = findIndex(l);
  139.         if((i < STARTDIMENSION)|| (i > DIMENSION))
  140.         {
  141.             throw new IllegalGridException();
  142.         }
  143.  
  144.         trace("getSquare: getSquare ends");
  145.  
  146.         return board[i];
  147.  
  148.  
  149.     }
  150.  
  151.  
  152.     public void setDimension(int d)
  153.     /*
  154.         Set method for the "dimension" instance variable.
  155.         Pre-condition: the given Dimension value is defined and
  156.                        valid
  157.         Post-condition: the instance variable "dimension" is
  158.                         assigned the given Dimension value
  159.         Informally: assign the given dimension to the Grid object
  160.     */
  161.     {
  162.         trace("setDimension: setDimension starts");
  163.  
  164.         dimension = d;
  165.  
  166.         trace("setDimension: setDimension ends");
  167.     }
  168.  
  169.  
  170.     public int getDimension()
  171.     /*
  172.         Get method for "dimension" instance variable.
  173.         Pre-condition: none
  174.         Post-condition: the value of the Grid object's dimension
  175.                         field is returned
  176.         Informally: return the current grid's dimension
  177.     */
  178.     {
  179.         trace("getDimension: getDimension starts and ends");
  180.  
  181.         return dimension;
  182.     }
  183.  
  184.  
  185.     public void emptySquare(Location l) // ******************* may need checking *************************
  186.     /*
  187.         Pre-condition: the given Location value is within
  188.                        the bounds of the current Grid
  189.                        object
  190.         Post-condition: the square at the position in the
  191.                         grid indicated by the given Location
  192.                         value is altered to be empty
  193.         Informally: update the square at the nominated location
  194.                     of the grid with the empty symbol
  195.     */
  196.     {
  197.         Square q = new Square(l, " "); // not sure what im doing here.... XD
  198.  
  199.         trace("emptySquare: emptySquare starts");
  200.  
  201.         int i = findIndex(l);
  202.         board[i] = q;
  203.  
  204.         trace("emptySquare: emptySquare ends");
  205.     }
  206.  
  207.  
  208.     public boolean isEmpty(Location l)
  209.     /*
  210.         Pre-condition: the given Location value is within
  211.                        the bounds of the current grid
  212.         Post-condition: a Boolean value is returned which
  213.                         represents whether the symbol of
  214.                         the square of the current Grid
  215.                         object with the given Location
  216.                         value is empty
  217.         Informally: return whether or not the square at
  218.                     the given location is empty
  219.     */
  220.     {
  221.         int i;
  222.  
  223.         trace("isEmpty: isEmpty starts and ends");
  224.  
  225.         i = findIndex(l);
  226.         if(board[i] == null)
  227.         {
  228.             return true;
  229.         }
  230.         else
  231.         {
  232.             return false;
  233.         }
  234.     }
  235.  
  236.  
  237.     public boolean validMove(Location l) // **********************************
  238.     /*
  239.         Pre-condition: none
  240.         Post-condition: true is returned if the given
  241.                         Location is within the bounds of
  242.                         the current Grid object, false is
  243.                         returned if it is not
  244.         Informally: return whether or not the given
  245.                     location lies within the current grid
  246.     */
  247.     {
  248.         int r;
  249.         int c;
  250.  
  251.         trace("validMove: validMove starts and ends");
  252.  
  253.         return false;
  254.     }
  255.  
  256.  
  257.     public boolean gameOver() // ******************************
  258.     /*
  259.         Pre-condition: none
  260.         Post-condition: true is returned if the game is
  261.                         over because the grid is empty
  262.         Informally: return whether or not the game is over
  263.     */
  264.     {
  265.         int r,c;
  266.         Location l;
  267.  
  268.         trace("gameOver: gameOver starts");
  269.  
  270.         for (r=0; r<MAXIMUM_ROW; r++)
  271.         {
  272.             for (c=0; c<=r; c++)
  273.             {
  274.  
  275.             }
  276.         }
  277.  
  278.         trace("gameOver: gameOver ends");
  279.         return true;
  280.     }
  281.  
  282.  
  283.     public String toString()
  284.     /*
  285.         Pre-condition: none
  286.         Post-condition: a String representation of the grid
  287.                         is returned
  288.         Informally: find a String representation of the grid
  289.     */
  290.     {
  291.         String s="[";
  292.         int r,c;
  293.         Location l;
  294.  
  295.  
  296.         trace("toString: toString starts");
  297.  
  298.         for (r=1;r<=dimension;r++)
  299.         {
  300.             for (c=1;c<=r;c++)
  301.             {
  302.                 l=new Location(r,c);
  303.                 if (isEmpty(l))
  304.                 {
  305.                     s+=" ";
  306.                 }
  307.                 else
  308.                 {
  309.                     s+="X";
  310.                 }
  311.             }
  312.             if (r != dimension)
  313.             {
  314.                 s+=",";
  315.             }
  316.         }
  317.  
  318.         s+="]";
  319.  
  320.         trace("toString: toString ends");
  321.         return s;
  322.     }
  323.  
  324.  
  325.     public void showGrid(Display s)
  326.     /*
  327.         Pre-condition: the Screen parameter is correctly defined
  328.         Post-condition: the screen representation of the Grid
  329.                         object is displayed on the given Screen
  330.         Informally: display the current grid
  331.     */
  332.     {
  333.         int i;
  334.  
  335.         trace("showGrid: showGrid starts");
  336.  
  337.         for (i=0; i<DIMENSION; i++)
  338.         {
  339.             board[i].showSquare(s,getDimension());
  340.         }
  341.  
  342.         trace("showGrid: grid is " + toString());
  343.  
  344.         trace("showGrid: showGrid ends");
  345.     }
  346.  
  347.     public void trace(String s)
  348.     /*
  349.         Provide trace output.
  350.         Pre-condition: none
  351.         Post-condition: if trace output is desired then the given String
  352.                         parameter is shown on the console
  353.         Informally: show the given message for tracing purposes
  354.     */
  355.     {
  356.         if (TRACING)
  357.         {
  358.             System.out.println("Grid: " + s);
  359.         }
  360.     }
  361. }
Add Comment
Please, Sign In to add comment