扫雷游戏的算法

新手上路,请多包涵

有没有人知道在扫雷游戏中,如果点击了一个没有雷的区域,周围没有雷的区域是怎么搜索的,什么时候停止搜索?

阅读 2.3k
4 个回答

遍历即可,上下左右咯。不需要什么特殊算法

写好了,欢迎去玩,哈哈哈:http://jsrun.net/sfQKp/edit


image.png



<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.14/vue.js"></script>
<style class="">
    .game-row{
        display: flex;
    }
    .game-col{
        width: 30px;
        height: 30px;
        display: flex;
        align-items: center;
        justify-content: center;
        border: 1px solid #cef;
        cursor: pointer;
    }
    .game-col[data-state="2"]{
        background: #fcc;
    }
</style>
<div id="app">
    <!-- {{gameMap}} -->
    <div v-for="(row, rowIdx) in gameMap" class="game-row">
        <div v-for="(col, colIdx) in row"  
            class="game-col" 
            :data-state="gameMap[rowIdx][colIdx]"
            @click="clickHandler(rowIdx, colIdx)">
                {{col}}
        </div>
    </div>
</div>
<script>
    new Vue({
        el: '#app',
        data(){
            return {
                // 游戏状态 0未开始 1已开始 2胜利 3失败
                state: 0, 
                // 最后单击的元素
                active: [-1, -1],
                // 
                gameLen: 10,
                gameLevel: 4,
                gameMap: Array.from({length: 10}).map(v=>Array.from({length: 10}).map(v=>0))
            }
        },
        created(){
            this.init();
        },
        methods: {
            init(){
                this.initMap();
                this.initMine();
                this.state = 1;
            },
            initMap(){
                this.gameMap = Array.from({length: this.gameLen}).map(v=>Array.from({length: this.gameLen}).map(v=>0))
            },
            initMine(){
                let i = 0;
                let end = this.gameLen * this.gameLevel;
                while(i < end){
                    let x = Math.random() * this.gameLen >>> 0;
                    let y = Math.random() * this.gameLen >>> 0;
                    if(this.gameMap[x][y] === 0){
                        this.gameMap[x][y] = 2;
                        i++;
                    }
                }
            },
            clickHandler(rowIdx, colIdx, isAuto = false){
                if(this.gameMap[rowIdx]?.[colIdx] != undefined){
                    if(this.state == 0){
                        this.init();
                    }else if(this.state == 1){
                        if(this.gameMap[rowIdx][colIdx] === 2 && !isAuto){

                        }else if(this.gameMap[rowIdx][colIdx] === 0){
                            this.gameMap[rowIdx][colIdx] = 1;
                            this.clickHandler(rowIdx+1, colIdx, true);
                            this.clickHandler(rowIdx-1, colIdx, true);
                            this.clickHandler(rowIdx, colIdx+1, true);
                            this.clickHandler(rowIdx, colIdx-1, true);
                        }
                    }
                    this.gameMap = JSON.parse(JSON.stringify(this.gameMap))
                }
            }
        }
    })
</script>

上下左右递归查看数字即可,遇到0继续递归,遇到>0的数字停止

就遍历吧,设置一个在一定范围内的随机值来控制遍历停止

没太看懂您说的搜索的含义,我这里列举一个大致demo,关于扫雷中查找雷和找到类后周围展现的过程

首先,定义了横纵坐标后,这里是查找雷的过程

int CheckShow(char show[ROWS][COLS], int row, int col)
{
 int win = 0;
 int i = 0;
 int j = 0;
 for (i = 1; i <= row; i++)
 {
 for (j = 1; j <= col; j++)
 {
 if (show[i][j] == '#')
 win++;
 }
 }
 return win;
}

这是展开的过程

int get_mine_count(char mine[ROWS][COLS], int x, int y)
{
 return mine[x - 1][y] +
 mine[x - 1][y - 1] +
 mine[x][y - 1] +
 mine[x + 1][y - 1] +
 mine[x + 1][y] +
 mine[x + 1][y + 1] +
 mine[x][y + 1] +
 mine[x - 1][y + 1] - 8 * '0';
}

void ExcludeMine(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
 int count = get_mine_count(mine, x, y);
 if (count != 0)
 {
 show[x][y] = count + '0';
 }
 else
 {
 show[x][y] = ' ';
 if (show[x - 1][y] == '#')
 ExcludeMine(mine, show, x - 1, y);
 if (show[x - 1][y - 1] == '#')
 ExcludeMine(mine, show, x - 1, y - 1);
 if (show[x][y - 1] == '#')
 ExcludeMine(mine, show, x, y - 1);
 if (show[x + 1][y - 1] == '#')
 ExcludeMine(mine, show, x + 1, y - 1);
 if (show[x + 1][y] == '#')
 ExcludeMine(mine, show, x + 1, y);
 if (show[x + 1][y + 1] == '#')
 ExcludeMine(mine, show, x + 1, y + 1);
 if (show[x][y + 1] == '#')
 ExcludeMine(mine, show, x, y + 1);
 if (show[x - 1][y + 1] == '#')
 ExcludeMine(mine, show, x - 1, y + 1);
 }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题