连接四个游戏检查以获取JS [英] Connect Four Game Checking for Wins JS

查看:322
本文介绍了连接四个游戏检查以获取JS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写自己的第一个完整程序,需要花两个星期的时间进行编程,但遇到了我似乎无法弄清的障碍.我正在制作一个Connect 4游戏,并且在推送到DOM之前先在JavaScript中构建逻辑.我已经开始使用构造函数创建的单元对象来制作它,然后将其以2D数组的形式推送到游戏对象中.我设法创建了一个函数,使每次播放,并使用2天数组更改该列最低点的单元格值.但是,我不确定如何获取赢钱支票功能.

I am working on my first full program with two weeks of programming under my belt, and have run into a road block I can't seem to figure out. I am making a connect 4 game, and have started by building the logic in JavaScript before pushing to the DOM. I have started to make it with cell objects made by a constructor, that are then pushed into a game object in the form of a 2D array. I have managed to create a function that makes the play each time, and changes the value of the cell at the lowest point of that column with a 2 day array. However, I am not sure how to get my check for wins function to operate.

到目前为止,我的逻辑是,对于2D数组中的每个点,您都可以按行,按列和对角线进行检查.我了解如何检查获胜的逻辑,但不了解如何按行和列遍历数组.在下面的示例中,this.cellsArray是Board构造函数中单元对象的数组.当我翻转典型的行列逻辑以说明Connect Four基于列的性质时,该数组具有7个列数组,每个数组有6行.但是我无法访问像this.cellsArray [col] [row]这样的数组,因为没有定义col和row,而且我不确定如何定义索引值?任何帮助将不胜感激!

So far my logic is that, for each point in the 2D array, you can check by row, by column, and by diagonals. I understand the logic of how to check for win, but I don't understand how to traverse through the arrays by row and column. In the example below, this.cellsArray is an array of cell objects in the Board Constructor. The array has 7 column arrays, with 6 rows each, as I flipped the typical row column logic to account for Connect Four's column based nature. However I can't access the array like this.cellsArray[col][row], as col and row aren't defined, and I'm not sure how to define an index value? Any help would be appreciated!

连接4

示例:

//array location is equal to an instance of this.cellsArray[col][row]
Board.prototype.checkRowRight = function (arrayLocation) {

    if ((arrayLocation[i+1][i].value === arrayLocation.value) && (arrayLocation[i+2][i]=== arrayLocation.value)  && (arrayLocation[i+3][i].value === arraylocation.value)){
        this.winner = this.currentPlayer;
        this.winnerFound = true;
        console.log('Winner has been found!')
    }
};

推荐答案

返回我的逻辑,发现

Referencing back to my logic found here and refactoring out the winning line detection code, this can easily be converted into Javascript as follows:

function chkLine(a,b,c,d) {
    // Check first cell non-zero and all cells match
    return ((a != 0) && (a ==b) && (a == c) && (a == d));
}

function chkWinner(bd) {
    // Check down
    for (r = 0; r < 3; r++)
        for (c = 0; c < 7; c++)
            if (chkLine(bd[r][c], bd[r+1][c], bd[r+2][c], bd[r+3][c]))
                return bd[r][c];

    // Check right
    for (r = 0; r < 6; r++)
        for (c = 0; c < 4; c++)
            if (chkLine(bd[r][c], bd[r][c+1], bd[r][c+2], bd[r][c+3]))
                return bd[r][c];

    // Check down-right
    for (r = 0; r < 3; r++)
        for (c = 0; c < 4; c++)
            if (chkLine(bd[r][c], bd[r+1][c+1], bd[r+2][c+2], bd[r+3][c+3]))
                return bd[r][c];

    // Check down-left
    for (r = 3; r < 6; r++)
        for (c = 0; c < 4; c++)
            if (chkLine(bd[r][c], bd[r-1][c+1], bd[r-2][c+2], bd[r-3][c+3]))
                return bd[r][c];

    return 0;
}

和一个测试电话:

x =[ [0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 1, 1, 0, 0],
     [0, 0, 0, 1, 1, 0, 0],
     [0, 0, 1, 2, 2, 2, 0],
     [0, 1, 2, 2, 1, 2, 0] ];
alert(chkWinner(x));

chkWinner 函数将在与董事会一起调用时返回第一个(也是唯一一个,假设每个动作仅改变一个单元格,并且您在每个动作之后都要检查)获胜的玩家.

The chkWinner function will, when called with the board, return the first (and only, assuming each move changes only one cell and you're checking after every move) winning player.

想法是将检查基本上限制在有意义的检查范围内.例如,当检查右侧的单元格时(请参阅第二个循环),您只需要检查从最左四列 0-3 0-6 >.

The idea is to basically limit the checks to those that make sense. For example, when checking cells to the right (see the second loop), you only need to check each row 0-6 starting in each of the leftmost four columns 0-3.

那是因为在寻找可能的胜利之前,从其他任何地方开始都将在板子的右边进行.换句话说,列集 {0,1,2,3} {1,2,3,4} {2,3,4,5} {3,4,5,6} 有效,但 {4,5,6,7} 无效(七个有效列是 0-6 ).

That's because starting anywhere else would run off the right hand side of the board before finding a possible win. In other words, column sets {0,1,2,3}, {1,2,3,4}, {2,3,4,5} and {3,4,5,6} would be valid but {4,5,6,7} would not (the seven valid columns are 0-6).

这篇关于连接四个游戏检查以获取JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆