Javascript 在扫雷艇上放置自定义地雷

Javascript 在扫雷艇上放置自定义地雷,javascript,Javascript,我想搞乱扫雷舰的密码,我想做的是把地雷放在特定的位置[x,y],这样一旦引爆,它们就可以拼出一个字母。我用这个页面来练习,但由于某些原因,我无法让它工作。我试图修改randIndex并将其设置为[1,1],以便只在角落中放置一个地雷,但没有成功。我该怎么做呢 layMines: function() { var rowCol, cell, i; // designate mine spots this.desi

我想搞乱扫雷舰的密码,我想做的是把地雷放在特定的位置[x,y],这样一旦引爆,它们就可以拼出一个字母。我用这个页面来练习,但由于某些原因,我无法让它工作。我试图修改randIndex并将其设置为[1,1],以便只在角落中放置一个地雷,但没有成功。我该怎么做呢

layMines: function() {
        var rowCol,
            cell,   

  i;

        // designate mine spots
        this.designateMineSpots();

        for ( i = 0; i < this.numMines; i++ ) {
            rowCol = this.numToRowCol( this.mineCells[i] );
            cell = this.cells[ rowCol[0] ][ rowCol[1] ];            
            cell.hasMine = true;
            cell.classUncovered = 'mine';
        }
    }, // end layMines()

//-----------------------------------

    // designate unique random mine spots and store in this.mineCells
    designateMineSpots: function() {
        this.safeCells = [];
        this.mineCells = []

        var i,
            randIndex;

        i = this.numCells;
        while ( i-- ) {
            this.safeCells.push( i + 1 );
        }

        i = this.numMines;
        while ( i-- ) {
            randIndex= 
              -~( Math.random() * this.safeCells.length ) - 1;
            this.mineCells.push( this.safeCells[randIndex] );
            this.safeCells.splice( randIndex, 1 ); // remove cell from array of safe cells
        }        
    }, // end designateMineSpots

//-----------------------------------    

    // calculate and set surrounding mine count for a cell
    calcMineCount: function( row, col ) {
        var count = 0,
            cell = this.cells[row][col],
            i, 
            j;

        for (i = row - 1; i <= row + 1; i++) {
            for (j = col - 1; j <= col + 1; j++) {
                // applying to surrounding cells, but we skip actual cell
                if (i == row && j == col) { continue; }

                if (this.cells[i][j].hasMine) { count++; }
            }
        }

        cell.numSurroundingMines = count;

        if (!cell.hasMine) { 
            cell.classUncovered = 'mines' + count;
        }
    },

//-----------------------------------
layMines:function(){
瓦罗科尔,
细胞,
我
//指定雷区
这是。指定的点();
对于(i=0;i对于(i=row-1;i您说您正在将
randIndex
修改为
[1,1]
,但是
randIndex
应该是一个整数。下面的代码被修改为沿底部放置所有地雷

layMines: function() {
        var rowCol,
            cell,   

  i;

        // designate mine spots
        this.designateMineSpots();

        for ( i = 0; i < this.numMines; i++ ) {
            rowCol = this.numToRowCol( this.mineCells[i] );
            cell = this.cells[ rowCol[0] ][ rowCol[1] ];            
            cell.hasMine = true;
            cell.classUncovered = 'mine';
        }
    }, // end layMines()

//-----------------------------------

    // designate unique random mine spots and store in this.mineCells
    designateMineSpots: function() {
        this.safeCells = [];
        this.mineCells = []

        var i,
            randIndex;

        i = this.numCells;
        while ( i-- ) {
            this.safeCells.push( i + 1 );
        }

        i = this.numMines;
        while ( i-- ) {
            randIndex= 
              -~( Math.random() * this.safeCells.length ) - 1;
            this.mineCells.push( this.safeCells[randIndex] );
            this.safeCells.splice( randIndex, 1 ); // remove cell from array of safe cells
        }        
    }, // end designateMineSpots

//-----------------------------------    

    // calculate and set surrounding mine count for a cell
    calcMineCount: function( row, col ) {
        var count = 0,
            cell = this.cells[row][col],
            i, 
            j;

        for (i = row - 1; i <= row + 1; i++) {
            for (j = col - 1; j <= col + 1; j++) {
                // applying to surrounding cells, but we skip actual cell
                if (i == row && j == col) { continue; }

                if (this.cells[i][j].hasMine) { count++; }
            }
        }

        cell.numSurroundingMines = count;

        if (!cell.hasMine) { 
            cell.classUncovered = 'mines' + count;
        }
    },

//-----------------------------------
// designate unique random mine spots and store in this.mineCells
designateMineSpots: function() {
    this.safeCells = [];
    this.mineCells = []

    var i,
        randIndex;

    i = this.numCells;
    while ( i-- ) {
        this.safeCells.push( i + 1 );
    }

    i = this.numMines;
    while ( i-- ) {
        randIndex= i; // This will put all mines along the bottom rows. 
        this.mineCells.push( this.safeCells[randIndex] );
        this.safeCells.splice( randIndex, 1 ); // remove cell from array of safe cells
    }        
}, // end designateMineSpots
听起来你也想更改地雷数量?也许在“新游戏”中,将地雷数量更改为你需要的任何数字?结合上面的代码,这将在最下面一行放置一枚地雷

layMines: function() {
        var rowCol,
            cell,   

  i;

        // designate mine spots
        this.designateMineSpots();

        for ( i = 0; i < this.numMines; i++ ) {
            rowCol = this.numToRowCol( this.mineCells[i] );
            cell = this.cells[ rowCol[0] ][ rowCol[1] ];            
            cell.hasMine = true;
            cell.classUncovered = 'mine';
        }
    }, // end layMines()

//-----------------------------------

    // designate unique random mine spots and store in this.mineCells
    designateMineSpots: function() {
        this.safeCells = [];
        this.mineCells = []

        var i,
            randIndex;

        i = this.numCells;
        while ( i-- ) {
            this.safeCells.push( i + 1 );
        }

        i = this.numMines;
        while ( i-- ) {
            randIndex= 
              -~( Math.random() * this.safeCells.length ) - 1;
            this.mineCells.push( this.safeCells[randIndex] );
            this.safeCells.splice( randIndex, 1 ); // remove cell from array of safe cells
        }        
    }, // end designateMineSpots

//-----------------------------------    

    // calculate and set surrounding mine count for a cell
    calcMineCount: function( row, col ) {
        var count = 0,
            cell = this.cells[row][col],
            i, 
            j;

        for (i = row - 1; i <= row + 1; i++) {
            for (j = col - 1; j <= col + 1; j++) {
                // applying to surrounding cells, but we skip actual cell
                if (i == row && j == col) { continue; }

                if (this.cells[i][j].hasMine) { count++; }
            }
        }

        cell.numSurroundingMines = count;

        if (!cell.hasMine) { 
            cell.classUncovered = 'mines' + count;
        }
    },

//-----------------------------------
我不确定你是否想遵守某一关卡的默认地雷数量?如果不想,你可能甚至不需要将
numMines
传递给
newGame
构造函数。你可以删除设置它的所有引用,并用代码替换它来计算你需要拼出字母的地雷数量

layMines: function() {
        var rowCol,
            cell,   

  i;

        // designate mine spots
        this.designateMineSpots();

        for ( i = 0; i < this.numMines; i++ ) {
            rowCol = this.numToRowCol( this.mineCells[i] );
            cell = this.cells[ rowCol[0] ][ rowCol[1] ];            
            cell.hasMine = true;
            cell.classUncovered = 'mine';
        }
    }, // end layMines()

//-----------------------------------

    // designate unique random mine spots and store in this.mineCells
    designateMineSpots: function() {
        this.safeCells = [];
        this.mineCells = []

        var i,
            randIndex;

        i = this.numCells;
        while ( i-- ) {
            this.safeCells.push( i + 1 );
        }

        i = this.numMines;
        while ( i-- ) {
            randIndex= 
              -~( Math.random() * this.safeCells.length ) - 1;
            this.mineCells.push( this.safeCells[randIndex] );
            this.safeCells.splice( randIndex, 1 ); // remove cell from array of safe cells
        }        
    }, // end designateMineSpots

//-----------------------------------    

    // calculate and set surrounding mine count for a cell
    calcMineCount: function( row, col ) {
        var count = 0,
            cell = this.cells[row][col],
            i, 
            j;

        for (i = row - 1; i <= row + 1; i++) {
            for (j = col - 1; j <= col + 1; j++) {
                // applying to surrounding cells, but we skip actual cell
                if (i == row && j == col) { continue; }

                if (this.cells[i][j].hasMine) { count++; }
            }
        }

        cell.numSurroundingMines = count;

        if (!cell.hasMine) { 
            cell.classUncovered = 'mines' + count;
        }
    },

//-----------------------------------
newGame: function( level, numRows, numCols, numMines, resetting ) {
    var resetting = resetting || false;
    // Note most of the code below was removed to make it clear which change you need. 
   // You only need to add the one line at the bottom of this. 
    ...
    if ( resetting ) {
        ...

        // reset cells    
        for ( i = 1; i <= this.numRows; i++ ) {
            for ( j = 1; j <= this.numCols; j++ ) {
                ...
            }
        }
    } else { // new game (not resetting)

        if ( level == 'custom' ) {
           ...
        } else {
           ...
        }

        this.numMines = 1; // HERE IS THE MODIFICATION TO MINECOUNT YOU NEED
        ...
    }
 }
在上面的3x3雷区中,要生成字母“T”,您需要从
safeCells
中删除元素8、7、6、4和1,并将它们移动到
minecell
。请注意,按降序删除它们可能很重要,否则
safeCells[8]
可能不包含整数8。例如,如果先移除
安全单元[1]
并拼接阵列以移除该元素,则
安全单元[8]
不再存在,整数8包含在
安全单元[7]

layMines: function() {
        var rowCol,
            cell,   

  i;

        // designate mine spots
        this.designateMineSpots();

        for ( i = 0; i < this.numMines; i++ ) {
            rowCol = this.numToRowCol( this.mineCells[i] );
            cell = this.cells[ rowCol[0] ][ rowCol[1] ];            
            cell.hasMine = true;
            cell.classUncovered = 'mine';
        }
    }, // end layMines()

//-----------------------------------

    // designate unique random mine spots and store in this.mineCells
    designateMineSpots: function() {
        this.safeCells = [];
        this.mineCells = []

        var i,
            randIndex;

        i = this.numCells;
        while ( i-- ) {
            this.safeCells.push( i + 1 );
        }

        i = this.numMines;
        while ( i-- ) {
            randIndex= 
              -~( Math.random() * this.safeCells.length ) - 1;
            this.mineCells.push( this.safeCells[randIndex] );
            this.safeCells.splice( randIndex, 1 ); // remove cell from array of safe cells
        }        
    }, // end designateMineSpots

//-----------------------------------    

    // calculate and set surrounding mine count for a cell
    calcMineCount: function( row, col ) {
        var count = 0,
            cell = this.cells[row][col],
            i, 
            j;

        for (i = row - 1; i <= row + 1; i++) {
            for (j = col - 1; j <= col + 1; j++) {
                // applying to surrounding cells, but we skip actual cell
                if (i == row && j == col) { continue; }

                if (this.cells[i][j].hasMine) { count++; }
            }
        }

        cell.numSurroundingMines = count;

        if (!cell.hasMine) { 
            cell.classUncovered = 'mines' + count;
        }
    },

//-----------------------------------

希望有帮助!

谢谢你,伟大的解释!但是为什么为什么(我){RANDQUALZY= I?……把地雷放在底部。你怎么把它放在中间的顶部或右边??感谢你的帮助:@Cesar我更新了我的答案,试图帮助回答这个问题。基本上,
safeCells
是一个数组,它包含映射到网格中某个单元格的整数,0表示右下角。因此,您需要遍历该数组,删除映射到要放入地雷的单元格的任何元素,然后将该元素放入
mineCells
相反。希望这对你有帮助。如果你仍然卡住了,请告诉我。哇,谢谢!这成功了。我在7X7矩阵上试用了它。我取出了元素This.minecell.push(This.safeCells[49,48,47,46,45,44,43,39,32,25,18,11,4]),并将其放入minecell=[];我得到了一个字母T:D…..但是它是颠倒的lol.知道为什么吗?PS.再次感谢你的帮助!只是把它改成了这个。MineCell=[46,39,32,25,18,11,7,6,5,4,3,2,1]现在它工作得很好!我得到了一个字母T:)
layMines: function() {
        var rowCol,
            cell,   

  i;

        // designate mine spots
        this.designateMineSpots();

        for ( i = 0; i < this.numMines; i++ ) {
            rowCol = this.numToRowCol( this.mineCells[i] );
            cell = this.cells[ rowCol[0] ][ rowCol[1] ];            
            cell.hasMine = true;
            cell.classUncovered = 'mine';
        }
    }, // end layMines()

//-----------------------------------

    // designate unique random mine spots and store in this.mineCells
    designateMineSpots: function() {
        this.safeCells = [];
        this.mineCells = []

        var i,
            randIndex;

        i = this.numCells;
        while ( i-- ) {
            this.safeCells.push( i + 1 );
        }

        i = this.numMines;
        while ( i-- ) {
            randIndex= 
              -~( Math.random() * this.safeCells.length ) - 1;
            this.mineCells.push( this.safeCells[randIndex] );
            this.safeCells.splice( randIndex, 1 ); // remove cell from array of safe cells
        }        
    }, // end designateMineSpots

//-----------------------------------    

    // calculate and set surrounding mine count for a cell
    calcMineCount: function( row, col ) {
        var count = 0,
            cell = this.cells[row][col],
            i, 
            j;

        for (i = row - 1; i <= row + 1; i++) {
            for (j = col - 1; j <= col + 1; j++) {
                // applying to surrounding cells, but we skip actual cell
                if (i == row && j == col) { continue; }

                if (this.cells[i][j].hasMine) { count++; }
            }
        }

        cell.numSurroundingMines = count;

        if (!cell.hasMine) { 
            cell.classUncovered = 'mines' + count;
        }
    },

//-----------------------------------