Home > Software engineering >  How to create a function to evaluate 2D arrays?
How to create a function to evaluate 2D arrays?

Time:01-05

So for example, giving this variables:

const rowWin = [
["O", "O", "O"],
["-", "-", "-"],
["-", "-", "-"]
> ];
const colWin = [
["-", "X", "-"],
["-", "X", "-"],
["-", "X", "-"]
> ];
const diagonalWin = [
["-", "-", "O"],
["-", "O", "-"],
["O", "-", "-"]
> ]
const diagonalWinInverse = [
["X", "-", "-"],
["-", "X", "-"],
["-", "-", "X"]
> ];

How I write the code that will determine whether a winning play has been made or not? I would like the function to log the results to the console (that X has won and O has lost, or vice versa). So if the output for example is: console.log(diagonalWin); the console should print: X Won and O Lost I am trying to figure out the function but I can get the desired result. I am just a beginner so I will really appreciate any help or guides in how to formulate this function. Thank you very much.

I hard code it but I really struggling to get the exact function.


if(rowWin[0][0] && rowWin[0][1] && rowWin[0][2]){
    console.log("O wins, X lost")
}else console.log("X wins")

if(colWin[0][1] && colWin[1][1] && colWin[2][1]){
    console.log("X wins, O lost")
}else console.log("O wins")

if(diagonalWin[0][2] && diagonalWin[1][1] && diagonalWin[2][0]){
    console.log("O wins, X lost")
}else console.log("X wins")

if(diagonalWinInverse[0][0] && diagonalWinInverse[1][1] && diagonalWinInverse[2][2]){
    console.log("X wins, X lost")
}else console.log("O wins")

CodePudding user response:

the easiest way is to split it to small steps:

  1. check row (array of three)
  2. apply step 1. on all rows and main diagonal
  3. rotate matrix
  4. repeat steps 1. and 2.

/**
 * @typedef {'X'|'O'|'-'} Mark
 * @typedef {'X'|'O'|null} Winner
 */

/**
 * @param {Mark[]} row
 * @returns {Winner}
 */
function checkRow(row) {
    const first = row[0];
    if (first !== '-' && first.repeat(3) === row.join('')) return first;

    return null;
}

/**
 * @param {Mark[][]} matrix
 * @returns {Winner}
 */
function checkRows(matrix) {
    for (const row of matrix) {
        const winner = checkRow(row);
        if (winner) return winner;
    }

    return null;
}

/**
 * @param {Mark[][]} matrix
 * @returns {Winner}
 */
function getPartialWinner(matrix) {
    const rowsWinner = checkRows(matrix);
    if (rowsWinner) return rowsWinner;

    const diagonalWinner = checkRow([matrix[0][0], matrix[1][1], matrix[2][2]]);
    if (diagonalWinner) return diagonalWinner;

    return null;
}

/**
 * @param {Mark[][]} matrix
 * @returns {Mark[][]}
 */
function rotateMatrix(matrix) {
    return matrix[0].map((_, index) => matrix.map(row => row[index]).reverse())
}

/**
 * @param {Mark[][]} matrix
 */
function printMatrix(matrix) {
    console.log('\n'   matrix.map(row => row.join(' ')).join('\n'))
}

/**
 * @param {Mark[][]} matrix
 */
function printWinner(matrix) {
    printMatrix(matrix);
    let winner = getPartialWinner(matrix);

    if (!winner) {
        const rotatedMatrix = rotateMatrix(matrix);
        printMatrix(rotatedMatrix);
        winner = getPartialWinner(rotatedMatrix);
    }

    if (winner)
        console.log(`Winner is ${ winner }.`);
    else
        console.log('No winner');
}

const rowWin = [
    ["O", "O", "O"],
    ["-", "-", "-"],
    ["-", "-", "-"]
];
const colWin = [
    ["-", "X", "-"],
    ["-", "X", "-"],
    ["-", "X", "-"]
];
const diagonalWin = [
    ["-", "-", "O"],
    ["-", "O", "-"],
    ["O", "-", "-"]
]
const diagonalWinInverse = [
    ["X", "-", "-"],
    ["-", "X", "-"],
    ["-", "-", "X"]
];

printWinner(rowWin);
printWinner(colWin);
printWinner(diagonalWin);
printWinner(diagonalWinInverse);

  • Related