Skip to main content

JS Function for Hardcoded TicTacToe check for win with a function

This time I am making a tictactoe game for my work in vanilla JS. I can get it to work by using if statements, but I now need it to work with an added function I am struggling to get working (which is a requirement). The code is what I have come up with so far.

This is my code:

const userGame = [
    row1 = ["O", "O", "O"],
    row2 = ["", "", ""],
    row3 = ["", "", ""]
]

// Made this for easier referencing
cells = [
    cell1 = row1[0],
    cell2 = row1[1],
    cell3 = row1[2],
    cell4 = row2[0],
    cell5 = row2[1], // C = Center
    cell6 = row2[2],
    cell7 = row3[0],
    cell8 = row3[1],
    cell9 = row3[2]
]

// Defining win conditions (row, column, diagonal)
const rowWinO = [
    ["O", "O", "O"]
]

const rowWinX = [
    ["X", "X", "X"]
]

const colWinO = [
    ["O"],
    ["O"],
    ["O"]
]

const colWinX = [
    ["X"],
    ["X"],
    ["X"]
]

const diagonalWinO = [
    ["", "", "O"],
    ["", "O", ""],
    ["O", "", ""]
]

const diagonalWinX = [
    ["", "", "X"],
    ["", "X", ""],
    ["X", "", ""]
]

const diagonalWinInverseO = [
    ["O", "", ""],
    ["", "O", ""],
    ["", "", "O"]
]

const diagonalWinInverseX = [
    ["X", "", ""],
    ["", "X", ""],
    ["", "", "X"]
]

// Placement of X and O
Xcell = 'X'
Ocell = 'O'
let gameWin = false

// Where the struggle is. 
// The function isn't calling (for example)
// rowWinO for the if statements below this function. 
// This program should be returning rowWinO and 
// declaring that 'O has won on a row'

function evaluatePlay(board) {
    board = userGame
    if (cell1 == Ocell && cell2 == Ocell && cell3 == Ocell
    || cell4 == Ocell && cell5 == Ocell && cell6 == Ocell
    || cell7 == Ocell && cell8 == Ocell && cell9 == Ocell) {
        rowWinO == true
    }

    else if (cell1 == Xcell && cell2 == Xcell && cell3 == Xcell
        || cell4 == Xcell && cell5 == Xcell && cell6 == Xcell
        || cell7 == Xcell && cell8 == Xcell && cell9 == Xcell) {
            rowWinX == true
        }

    else if (cell1 == Ocell && cell4 == Ocell && cell7 == Ocell
        || cell2 == Ocell && cell5 == Ocell && cell8 == Ocell
        || cell3 == Ocell && cell6 == Ocell && cell9 == Ocell) {
            colWinO == true
        }

    else if (cell1 == Xcell && cell4 == Xcell && cell7 == Xcell
        || cell2 == Xcell && cell5 == Xcell && cell8 == Xcell
        || cell3 == Xcell && cell6 == Xcell && cell9 == Xcell) {
            colWinX == true
        }

    else if (board == diagonalWinO ) {
        // Done this way because the board can only get
        // a win like this in one way
        diagonalWinO == true 
    }

    else if (board == diagonalWinInverseO) {
        diagonalWinInverseO == true
    }
    
    else if (board == diagonalWinX) {
        diagonalWinX == true
    }

    else if (board == diagonalWinInverseX) {
        diagonalWinInverseX == true
    }

    if (rowWinO == true || rowWinX == true || colWinO == true
        || colWinX == true || diagonalWinO == true || diagonalWinX == true
        || diagonalWinInverseO == true || diagonalWinInverseX == true) {
        // If the gameboard matches a win state, return that we have a 
        // winner
        gameWin == true
    }

    return;
}

evaluatePlay(userGame)

if (gameWin == true) {
    if (rowWinO == true ) {
        // O wins on a row
        console.log('O wins on a row!')
    }

    // X Wins on a row
    else if(rowWinX == true) {
        console.log('X wins on a row! ')
    }

    // O Wins on a column
    else if(colWinO == true) {
        console.log('O wins on a column!')
    }

    // X Wins on a column
    else if(colWinX == true) {
        console.log('X wins on a column!')
    }

    // O wins on a diagonal
    else if(diagonalWinO == true) {
        console.log('O wins on a diagonal!')
    }

    else if(diagonalWinInverseO == true) {
        console.log('O wins on a diagonal!')
    }

    // X wins on a diagonal
    else if(diagonalWinX == true) {
        console.log('X wins on a diagonal!')
    }

    else if(diagonalWinInverseX == true) {
        console.log('X wins on a diagnoal!')
    }
}

else if (gameWin == false) {
    console.log('Nothing happens')
}

I defined a game board of hard coded tic-tac-toe.

const userGame = [
    row1 = ["O", "O", "O"],
    row2 = ["", "", ""],
    row3 = ["", "", ""]
]

Because I have to check it against multiple variables I am sadly getting confused with other people's work as they define a winning state in a single array.

The program's goal is to compare this with the winning game states like winning on a row or diagonal. However instead of using an array to define all the win cons I have to pre-define these win cons for my work, like

const rowWinO = [ ["O", "O", "O"] ]

const diagonalWinX = [
    ["", "", "X"],
    ["", "X", ""],
    ["X", "", ""]
]

My issue is the function I have. It's long, so the gist of it is to check if the indexes of the original game array and compare it the game state. As you can see above the gameBoard wins on a row, so...

Expected Output

The function should recognise this and return true. If any win condition is met, then it should recognise the game has been won (gamewin == true). Using that, the below if statement should check if (rowWinO == true && gameWin == true) display the message ('X won on a row.')

Note the win conditions are nested in gameWin for the if statements

 else if <the game is not a winner> `console.log('Nothing happens')`

I have tried tweaking it by changing X = 'X' to Xcell = 'X' but I am honestly not sure what to change anymore.

The previous program worked with if statements like this:

else if(row1[0] == 'O' && row2[0] == 'O' && row3[0] == 'O') {
    console.log('O wins on column')
    gameWin == true
}

The program does not need to check for draws, only for wins.

Via Active questions tagged javascript - Stack Overflow https://ift.tt/6vIHQ4Z

Comments

Popular posts from this blog

Where and how is this Laravel kernel constructor called? [closed]

Where and how is this Laravel kernel constructor called? public fucntion __construct(Application $app, $Router $roouter) { } I have read the documentation and some online tutorial but I can find any clear explanation. I am learning Laravel and I am wondering where does this kernel constructor receives its arguments from. "POSTMOTERM" CLARIFICATION: Here is more clarity.I have checked the boostrap/app.php and it is only used for boostrapping the interfaces into the container class. What is not clear to me is where and how the Kernel class is instatiated and the arguments passed to the object calling the constructor.Something similar to; obj = new kernel(arg1,arg2) or, is the framework using some magic functions somewhere? Special gratitude to those who burn their eyeballs and brain cells on this trivia before it goes into a full blown menopause alias "MARKED AS DUPLICATE". To some of the itchy-finger keyboard warriors, a.k.a The mods,because I believe in th...

Why is my reports service not connecting?

I am trying to pull some data from a Postgres database using Node.js and node-postures but I can't figure out why my service isn't connecting. my routes/index.js file: const express = require('express'); const router = express.Router(); const ordersCountController = require('../controllers/ordersCountController'); const ordersController = require('../controllers/ordersController'); const weeklyReportsController = require('../controllers/weeklyReportsController'); router.get('/orders_count', ordersCountController); router.get('/orders', ordersController); router.get('/weekly_reports', weeklyReportsController); module.exports = router; My controllers/weeklyReportsController.js file: const weeklyReportsService = require('../services/weeklyReportsService'); const weeklyReportsController = async (req, res) => { try { const data = await weeklyReportsService; res.json({data}) console...

How to show number of registered users in Laravel based on usertype?

i'm trying to display data from the database in the admin dashboard i used this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count(); echo $users; ?> and i have successfully get the correct data from the database but what if i want to display a specific data for example in this user table there is "usertype" that specify if the user is normal user or admin i want to user the same code above but to display a specific usertype i tried this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count()->WHERE usertype =admin; echo $users; ?> but it didn't work, what am i doing wrong? source https://stackoverflow.com/questions/68199726/how-to-show-number-of-registered-users-in-laravel-based-on-usertype