Providing a straightforward solution without extensive loops (only presenting pseudocode, feel free to request further clarification):
If we assume the 2-dimensional array is structured as follows:
board = [
[...],
[...],
[...],
...
];
In this arrangement, the inner arrays represent the horizontal rows of the board.
We can also assume that the array comprises of "b", "w", and "x", denoting black pieces, white pieces, and empty squares, respectively.
My approach involves somewhat of a divide-and-conquer strategy, categorized into the three scenarios below. Although it may initially appear more intricate than utilizing nested loops, the underlying concept is straightforward, comprehensible, and ultimately simplistic to code.
Horizontal lines
To begin with, let's address identifying a win scenario only when the line is horizontal - which is the simplest case. Initially, concatenate a row into a single string using something like board[0].join("")
. Proceed with this process for each row. This results in an array structure such as:
rows = [
"bxwwwbx...",
"xxxwbxx...",
"wwbbbbx...",
...
]
Next, merge THIS array while inserting an "x" between elements to separate each row: rows.join("x")
.
Now you possess a comprehensive string representing your board, making it effortless to apply a regexp search for consecutive "w" or "b" characters precisely five units long:
superString.test(/(b{5,5})|(w{5,5})/)
. Upon receiving a true outcome, a win situation is detected. If not, proceed to vertical lines.
Vertical lines
To reuse the above code, establish a function testRows
for this purpose. The procedure for testing vertical lines mirrors that of horizontal detection. However, it necessitates transposing the board so that rows are transformed into columns and vice versa. Subsequently, apply the identical testRows
function. This transposition can be executed by duplicating values into a new 2-dimensional array or devising a simple getCol
function and integrating it within testRows
.
Diagonal lines
Similarly, we aim to reuse the 'testRows' function. A diagonal configuration like the one depicted:
b x x x x
x b x x x
x x b x x
x x x b x
x x x x b
Can be converted into a vertical setup as illustrated:
b x x x x
b x x x
b x x
b x
b
This transformation involves shifting row i
by i
positions. Following this alteration, transpose the matrix and resume testing for horizontals. Comparable adjustments need to occur for diagonals progressing in the opposite direction, where row i
should shift by length - 1 - i
positions, or in this context, 18 - i
locations.
Functional javascript
On a supplementary note, my solution seamlessly aligns with functional programming, enabling a relatively straightforward implementation provided you have functional programming tools on hand, although they are not mandatory. I suggest considering the utilization of underscore.js, as basic functionalities such as map
, reduce
, and filter
are frequently required across various game algorithms. For instance, the segment pertaining to testing horizontal lines could be condensed into a single line of javascript employing the map
method:
_(board).map(function (row) {return row.join("")}).join("x").test(/(b{5,5})|(w{5,5})/);