Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

java- FloodIt game The model The first step is to build the model of the game. T

ID: 3826807 • Letter: J

Question

java- FloodIt game

The model

The first step is to build the model of the game. This will be done via the class GameModel and the helper class DotInfo. Our unique instance of the class GameModel will have to store the current state of the game. This includes

The colour and status (captured or not) of each dot on the board. The class DotInfo helps with this.

The size of the board.

The number of steps taken by the player so far

The number of dots captured so far

the currently selected colour

It also provides the necessary setters and getters, so the the controller and the view can check the status of any dot, and the controller can change the status of a dot or set the current colour to a new value. Finally, it provides a way to (re)initialize the game, reallocating the initial colours randomly.

A detailed description of the classes can be found here:

// ADD YOUR IMPORTS HERE

/**
* The class <b>GameModel</b> holds the model, the state of the systems.
* It stores the followiung information:
* - the state of all the ``dots'' on the board (color, captured or not)
* - the size of the board
* - the number of steps since the last reset
* - the current color of selection
*
* The model provides all of this informations to the other classes trough
* appropriate Getters.
* The controller can also update the model through Setters.
* Finally, the model is also in charge of initializing the game
public class GameModel {


    /**
     * predefined values to capture the color of a DotInfo
     */
    public static final int COLOR_0           = 0;
    public static final int COLOR_1           = 1;
    public static final int COLOR_2           = 2;
    public static final int COLOR_3           = 3;
    public static final int COLOR_4           = 4;
    public static final int COLOR_5           = 5;
    public static final int NUMBER_OF_COLORS = 6;


// ADD YOUR INSTANCE VARIABLES HERE

    /**
     * Constructor to initialize the model to a given size of board.
     *
     * @param size
     *            the size of the board
     */
    public GameModel(int size) {

// ADD YOUR CODE HERE

    }


    /**
     * Resets the model to (re)start a game. The previous game (if there is one)
     * is cleared up .
     */
    public void reset(){

// ADD YOUR CODE HERE

    }


    /**
     * Getter method for the size of the game
     *
     * @return the value of the attribute sizeOfGame
     */  
    public int getSize(){

// ADD YOUR CODE HERE

    }

    /**
     * returns the current color of a given dot in the game
     *
     * @param i
     *            the x coordinate of the dot
     * @param j
     *            the y coordinate of the dot
     * @return the status of the dot at location (i,j)
     */  
    public int getColor(int i, int j){

// ADD YOUR CODE HERE

    }

    /**
     * returns true is the dot is captured, false otherwise
    *
     * @param i
     *            the x coordinate of the dot
     * @param j
     *            the y coordinate of the dot
     * @return the status of the dot at location (i,j)
     */  
    public boolean isCaptured(int i, int j){

// ADD YOUR CODE HERE

    }

    /**
     * Sets the status of the dot at coordinate (i,j) to captured
     *
     * @param i
     *            the x coordinate of the dot
     * @param j
     *            the y coordinate of the dot
     */  
    public void capture(int i, int j){

// ADD YOUR CODE HERE

   }


    /**
     * Getter method for the current number of steps
     *
     * @return the current number of steps
     */  
    public int getNumberOfSteps(){

// ADD YOUR CODE HERE

    }

    /**
     * Setter method for currentSelectedColor
     *
     * @param val
     *            the new value for currentSelectedColor
    */  
    public void setCurrentSelectedColor(int val) {

// ADD YOUR CODE HERE

    }

    /**
     * Getter method for currentSelectedColor
     *
     * @return currentSelectedColor
     */  
    public int getCurrentSelectedColor() {

// ADD YOUR CODE HERE

    }


    /**
     * Getter method for the model's dotInfo reference
     * at location (i,j)
     *
      * @param i
     *            the x coordinate of the dot
     * @param j
     *            the y coordinate of the dot
     *
     * @return model[i][j]
     */  
    public DotInfo get(int i, int j) {

// ADD YOUR CODE HERE

    }


   /**
     * The metod <b>step</b> updates the number of steps. It must be called
     * once the model has been updated after the payer selected a new color.
     */
     public void step(){

// ADD YOUR CODE HERE

    }

   /**
     * The metod <b>isFinished</b> returns true iff the game is finished, that
     * is, all the dats are captured.
     *
     * @return true if the game is finished, false otherwise
     */
    public boolean isFinished(){

// ADD YOUR CODE HERE

    }


   /**
     * Builds a String representation of the model
     *
     * @return String representation of the model
     */
    public String toString(){

// ADD YOUR CODE HERE

    }
}

/**
* The class <b>DotInfo</b> is a simple helper class to store the initial color and state
* (captured or not) at the dot position (x,y)
*
*/

public class DotInfo {


// ADD YOUR INSTANCE VARIABLES HERE



    /**
     * Constructor
     *
     * @param x
     *            the x coordinate
     * @param y
     *            the y coordinate
     * @param color
     *            the initial color
     */
    public DotInfo(int x, int y, int color){

// ADD YOUR CODE HERE

    }

    /**
     * Getter method for the attribute x.
     *
     * @return the value of the attribute x
     */
    public int getX(){

// ADD YOUR CODE HERE

    }
   
    /**
     * Getter method for the attribute y.
     *
     * @return the value of the attribute y
     */
    public int getY(){

// ADD YOUR CODE HERE

    }
   

    /**
     * Setter for captured
     * @param captured
     *            the new value for captured
     */
    public void setCaptured(boolean captured) {

// ADD YOUR CODE HERE

    }

    /**
     * Get for captured
     *
     * @return captured
     */
    public boolean isCaptured(){

// ADD YOUR CODE HERE

    }

    /**
     * Get for color
     *
     * @return color
     */
    public int getColor() {

// ADD YOUR CODE HERE

    }

}

Explanation / Answer

package floodit;

/**
* The class <b>DotInfo</b> is a simple helper
* class to store the initial color and state
* (capture or not) at the dot position (x,y)
*/
public class DotInfo {
   private int x;
   private int y;
   private int color;
   private boolean captured;
  
   /**
   * Constructor
   *
   * @param x
   *    the x coordinate
   * @param y
   *    the y coordinate
   * @param color
   *    the initial color
   */
   public DotInfo(int x, int y, int color) {
       this.x = x;
       this.y = y;
       this.color = color;
       this.captured = false;
   }
  
   /**
   * Getter method of the attribute x
   *
   * @return the value of the attribute x
   */
   public int getX() {
       return x;
   }
  
   /**
   * Getter method of the attribute y
   *
   * @return the value of the attribute y
   */
   public int getY() {
       return y;
   }
  
   /**
   * Getter method of the attribute color
   *
   * @return the value of the attribute color
   */
   public int getColor() {
       return color;
   }
  
   /**
   * Setter for captured
   *
   * @param the new value for captured
   */
   public void setCaptured(boolean captured) {
       this.captured = captured;
   }
  
   /**
   * Getter for captured
   *
   * @return captured
   */
   public boolean isCaptured() {
       return captured;
   }
  
   /*
   * Returns String representation of DotInfo
   *
   * @return the String representation of DotInfo in [x,y,color,captured]
   */
   @Override
   public String toString() {
       StringBuilder sbr = new StringBuilder();
       sbr.append("[");
       sbr.append(x);
       sbr.append(",");
       sbr.append(y);
       sbr.append(",");
       sbr.append(color);
       sbr.append(",");
       sbr.append(captured);
       sbr.append("]");
      
       return sbr.toString();
   }
}

package floodit;

/**
* The class <b>GameModel</b> holds the model,
* the state of the systems
* It stores the following information:
* - the state of all the "dots" on the board
* (color, capture or not)
* - the size of the board
* - the number of steps since the last reset
* - the current color of selection
*
* The model provides all the informations to other classes
* through appropriate getters.
* The controller can also update the model through setters
* Finally, the model is also in charge of
* initializing the game
*/
public class GameModel {
   /**
   * predefined values to capture the color of a DotInfo
   */
   public static final int COLOR_0 = 0;
   public static final int COLOR_1 = 1;
   public static final int COLOR_2 = 2;
   public static final int COLOR_3 = 3;
   public static final int COLOR_4 = 4;
   public static final int COLOR_5 = 5;
   public static final int NUMBER_OF_COLORS = 6;
  
   private int sizeOfGame;
   private DotInfo[] dots;
   private int stepsTaken;
   private int currentSelectedColor;
  
   /**
   * Constructor to initialize the model to a given size of board
   *
   * @param size
   */
   public GameModel(int size) {
       this.sizeOfGame = size;
       dots = new DotInfo[sizeOfGame];
       stepsTaken = 0;
   }
  
   /**
   * Resets the model to (re)start a game.
   * The previous game (if there is one) is cleared up.
   */
   public void reset() {
       if(dots != null)
           dots = new DotInfo[sizeOfGame];
   }
  
   /**
   * Getter method for the size of the game
   * @return the value of the attribute sizeOfGame
   */
   public int getSize() {
       return sizeOfGame;
   }
  
   /**
   * Returns the current color of a given dot in the game
   * @param x
   *    the x coordinate of the dot
   * @param y
   *    the y coordinate of the dot
   * @return
   *    the status of the dot at location (x,y)
   */
   public int getColor(int x, int y) {
       for(DotInfo dot : dots) {
           if(dot.getX() == x && dot.getY() == y) {
               return dot.getColor();
           }
       }
      
       return -1;
   }
  
   /**
   * Returns true if the dot is captured, false otherwise
   * @param x
   *    the x coordinate of the dot
   * @param y
   *    the y coordinate of the dot
   * @return
   *   the status of the dot at location (x,y)
   */
   public boolean isCaptured(int x, int y) {
       for(DotInfo dot : dots) {
           if(dot.getX() == x && dot.getY() == y) {
               return dot.isCaptured();
           }
       }
      
       return false;
   }
  
   /**
   * Set the status of the dot at coordinate (x,y) to be captured
   * @param x
   *    the x coordinate of the dot
   * @param y
   *    the y coordinate of the dot
   */
   public void capture(int x, int y) {
       for(DotInfo dot : dots) {
           if(dot.getX() == x && dot.getY() == y) {
               dot.setCaptured(true);
               stepsTaken++;
           }
       }
   }
  
   /**
   * Getter method for the current number of steps
   * @return the current number of steps
   */
   public int getNumberOfSteps() {
       return stepsTaken;
   }
  
   /**
   * Setter method for currentSelectedColor
   * @param the new value for currentSelectedColor
   */
   public void setCurrentSelectedColor(int val) {
       currentSelectedColor = val;
   }
  
   /**
   * Getter method for currentSelectedColor
   * @return currentSelectedColor
   */
   public int getCurrentSelectedColor() {
       return currentSelectedColor;
   }
  
   /**
   * Getter method for the model's dotinfo reference at location (x,y)
   * @param x
   *    the x coordinate of the dot
   * @param y
   *    the y coordinate of the dot
   * @return
   *    dotinfo(x,y)
   */
   public DotInfo get(int x, int y) {
       for(DotInfo dot : dots) {
           if(dot.getX() == x && dot.getY() == y) {
               return dot;
           }
       }
      
       return null;
   }
  
   /**
   * The method <b>step</b> updates the number of steps.
   * It must be called once the model has been updated after the player
   * selected a new color
   */
   public void step() {
       stepsTaken++;
   }
  
   /**
   * The method <b<>isFinished</b> returns true if
   * the game is finished, this is,
   * all the dots are captured
   *
   * @return true if the game is finished, false otherwise
   */
   public boolean isFinished() {
       boolean isFinished = true;
       for(DotInfo dot : dots) {
           if(!dot.isCaptured()) {
               isFinished = false;
               break;
           }
       }
      
       return isFinished;
   }
  
   /*
   * Builds a String representation of the model
   *
   * @return String representation of the model
   */
   @Override
   public String toString() {
       StringBuilder sbr = new StringBuilder();
       sbr.append("[");
       for(DotInfo dot : dots)
           sbr.append(dot.toString());
       sbr.append("]");
       return sbr.toString();
   }
}