java - 扫雷哈希集

标签 java variables hashset minesweeper

我正在编写扫雷游戏,并且正在尝试使用哈希集。我的问题是哈希集可以同时包含整数和字符串吗?我使用的 hashset 主要有字符串和一个整数。这可以做到吗?当我编译它时,它给出了错误:找不到变量 _minecount ,但它不是一个字符串。这是我的代码:

    import java.util.Set;
import java.util.HashSet;

/**
 * Represents a single square in Minesweeper.
 * 
 * @author Sophia Ali
 */
public class MineSquare
{
    //Constants:
    // 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
    private static final String[] VALID_SHOWN_TEXT = {"X", 0 < minecount && _minecount < 8, " ", "F", "?"};
    private HashSet<String> validShownTextSet = 
    new HashSet<String>(java.util.Arrays.asList(VALID_SHOWN_TEXT));

    // Constructors & Methods:

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





    /**
     * 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, boolean opened) {
        _flagged = flagged;
        _opened = opened;

        // If Minesquare opened do nothing:
        if (opened == true)
        setShown(" ");

        // 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;

        if ( 0 < minecount && minecount < 8 ) 
        {
       setShown("Error:" + 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, boolean opened) {
        _questioned = questioned;
        _opened = opened;
        // If Minesquare opened do nothing:
        if (opened == true)
        setShown(" ");

        // If Questioned, square should show "F":
        if ( isQuestioned() == true )
            setShown("F");
        else
            setShown(" ");  
    }





    public String getShown() {
        return _shown;
    }





    public void setShown(String shown) {
        _shown = shown;
        /*if (shown.equals("X")) {
            this.shown = shown;
        } else if (shown.equals( 0 < minecount && minecount < 8 )) {
            this.shown = shown;
        } else if (shown.equals(" ")) {
            this.shown = shown;
        } else if (shown.equals("F")) {
            this.shown = shown;
        } else if (shown.equals("?")) {
            this.shown = shown;
        } else {
            this.shown = "Error + shown";
        }
        */
       if (validShownTextSet.contains(shown)) {
           this.shown = shown;
        } else {
            this.shown = "Error" + shown;
        }
    }





    }

最佳答案

这一行

private static final String[] VALID_SHOWN_TEXT = {"X", 0 < minecount && _minecount < 8, " ", "F", "?"};

有一些问题。没有minecount此处范围内的变量。变量_minecount不是静态变量,因此无法从您通过创建 VALID_SHOWN_TEXT 所创建的静态范围访问它。静态。

正如您所猜测的,您无法存储 boolean来自表达式0 < minecount && _minecount < 8进入String大批。即使你能得到boolean表达式工作时,您必须将其转换为 String将其存储到 String数组。

// or a working boolean expression here.
..., String.valueOf(0 < minecount && _minecount < 8), ...

一个HashSet<Object>可以同时包含整数和字符串,但是在这里,看起来你可以简单地将你需要的内容转换为字符串数组,它应该能够转换为 HashSet<String> .

关于java - 扫雷哈希集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17158928/

相关文章:

javascript - 声明 JS 变量时使用 "in"

variables - ExtJS 4.2 - 动态加载日期选择器值 - 新手 Q

c# - 使用现有的哈希集作为键来创建新字典

java - 如何使用 QueryStringQueryBuilder

java - 在 Java 中使用嵌套类的好处

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

java - HashSet 中的随机数始终相同

java - 如何在不更改 setAutoCreateRowSorter 完成的顺序的情况下刷新表(JTable)?

java - Android Twilio 如何使用这些示例类?

java - Java 中的 HashSet 冲突