Suhan Gui Suhan Gui - 3 days ago 5
jQuery Question

Detecting 5 in a row

I am making a game of tic tac toe 5 in a row. I have the grid where whenever you click on a square, it records a "coordinate" of [row,column] in the certain color of the dot. I'm currently not sure how to use the 'coordinates' to detect a five in a row of either color and just prints out a message.

Note: If 5 in a row gets tedious with the copy and pasting of previous code or such, a 3 in a row will also work for me and I will just modify it into a 5 in a row. Also when viewing the code snippet below, use the full screen mode.

Code I have so far:



var white=true;
function generateGrid( rows, cols ) {
var grid = "<table>";
for ( row = 1; row <= rows; row++ ) {
grid += "<tr>";
for ( col = 1; col <= cols; col++ ) {
grid += "<td></td>";
}
grid += "</tr>";
}
return grid;
}

$( "#tableContainer" ).append( generateGrid( 10, 10) );

$( "td" ).click(function() {
$(this).css('cursor','default');
var index = $( "td" ).index( this );
var row = Math.floor( ( index ) / 10) + 1;
var col = ( index % 10) + 1;
var $td = $(this);
if ($td.data('clicked'))
return;
if (white===true){
var whi=[row,col];
console.log("white coord is "+whi);
} else {
var bla=[row,col];
console.log("black coord is "+bla);
}

$td.data('clicked', true);

$td.css('background-color', white ? 'white' : 'black');
white = !white;
});

html{
background-color:#7189ea;
}
td {
border: 1px solid;
width: 25px;
height: 25px;
border-radius:100%;
}

table {
border-collapse: collapse;
}

<link type="text/css" rel="stylesheet" href="stylesheet.css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<div id="tableContainer"></div>




Answer

I've written a function that checks whether the last move wins the game. It basically loops the squares in every direction (and backwards) and looks for 5 in a row (the required length of a line).

var board = new Array();
var boardSize = 5;
var requiredLineLength = 5;
for (var r = 0; r < boardSize; r++) {
    board[r] = new Array();
    for (var c = 0; c < boardSize; c++) {
        board[r][c] = 0;
    }
}

var lineDirections = [
    [0, 1], //horizontal
    [1, 0], //vertical
    [1, -1], //diagonal 1
    [1, 1] //diagonal 2
];

//example usage:
board[0][0] = 1;
board[1][0] = 1;
board[2][0] = 1;
board[3][0] = 1;
board[4][0] = 1;

console.log(checkWin(1, [0, 0]));

// an empty square is marked with 0
// the players are marked with 1 and 2
// pl is the id of the player: either 1 or 2
// lastMove is an array of size 2, with the coordinates of the last move played, for example: [3, 1]
function checkWin(pl, lastMove) {
    var boolWon = false;
    for (var i = 0; i < lineDirections.length && !boolWon; i++) {
        var shift = lineDirections[i];
        var currentSquare = [lastMove[0] + shift[0], lastMove[1] + shift[1]];
        var lineLength = 1;

        while (lineLength < requiredLineLength && legalSquare(currentSquare) && board[currentSquare[0]][currentSquare[1]] === pl) {
            lineLength++;
            currentSquare[0] += shift[0];
            currentSquare[1] += shift[1];
        }

        currentSquare = [lastMove[0] - shift[0], lastMove[1] - shift[1]];
        while (lineLength < requiredLineLength && legalSquare(currentSquare) && board[currentSquare[0]][currentSquare[1]] === pl) {
            lineLength++;
            currentSquare[0] -= shift[0];
            currentSquare[1] -= shift[1];
        }
        if (lineLength >= requiredLineLength)
            boolWon = true;
    }
    return boolWon;
}

function legalSquare(square) {
    return square[0] < boardSize && square[1] < boardSize && square[0] >= 0 && square[1] >= 0;
}

It's not fully tested so let me know if you encounter any problems or if you need any clarification on how this works.

Comments