Minesweeper Square Class Example


The following is an early version of a Minesweeper square class. It goes with the Minesweeper square description at http://tinyurl.com/kkepryl.


/**
 * Represents a single square in Minesweeper.
 * 
 * @author J. Chung
 * @version 5-29-2013
 */
public class MineSquare
{
    // Fields:
    private  String  _shown;      // What a square is showing now
    private  boolean _mined;      // Square is mined or not
    private  boolean _flagged;    // Square is flagged or not
    private  boolean _questioned; // Square is question marked or not
    private  int     _minecount;  // Square's surrounding mine count
    private  boolean _opened;     // Player has opened this square or not
    
    
    
    
    // Constructors & Methods:

    /**
     * Default constructor
     * Sets _mined and _opened to false.
     */
    public MineSquare()
    {
        _mined = false;
        _opened = false;
    }



    
    
    /**
     * Returns flagged status of square.
     * @return  _flagged    Flagged status
     */
    public boolean isFlagged() {
        return _flagged;
    }





    /**
     * Sets or unsets flag on a square.
     * @param   flagged    True or false (square is flagged or not)
     */
    public void setFlagged(boolean flagged) {
        _flagged = flagged;
        
        // If flagged, square should show "F":
        if ( isFlagged() == true )
            setShown("F");
        else
            setShown(" ");            
    }






    public int getMinecount() {
        return _minecount;
    }





    public void setMinecount(int minecount) {
        _minecount = minecount;
    }





    public boolean isMined() {
        return _mined;
    }





    public void setMined(boolean mined) {
        _mined = mined;
    }





    public boolean isOpened() {
        return _opened;
    }





    /**
     * Open a square.
     * (Once opened, a square can't be unopened.)
     */
    public void setOpened() {
        _opened = true;
        
        if ( isMined() == true )
            setShown("X");
        else if ( getMinecount() > 0 )
            setShown(_minecount + "");
        else // blank space for _minecount = 0
            setShown(" ");
    }





    public boolean isQuestioned() {
        return _questioned;
    }





    public void setQuestioned(boolean questioned) {
        _questioned = questioned;
    }





    public String getShown() {
        return _shown;
    }





    public void setShown(String shown) {
        _shown = shown;
    }
    

}