-2

I apologise if my title is poor, I'm struggling to visualise what to do here.

I have been self teaching myself JavaScript.

Currently I am trying to make a tic-tac-toe.

I am only using vanilla javascript and lodash.

I have the board currently stored as an array using lodash.chunk to split it into 3 rows:

eg.

array = [
  [x,x,x],
  [x,o,x],
  [o,o,x],
]

I already have it finding a winner in the same row, but I am trying to determine if someone wins diagonally, up/down etc.

I'm just having a lot of trouble understanding what I need to do next or how to visualise it.

Hoping someone can show me how to do this using best practices?

Thank you very much

macabeus
  • 4,156
  • 5
  • 37
  • 66
  • 1
    There are hundreds of tic-tac-toe questions here, you should be able to find the algorithm – Barmar Nov 17 '20 at 02:42
  • Check that question https://stackoverflow.com/questions/1056316/algorithm-for-determining-tic-tac-toe-game-over – wjatek Nov 17 '20 at 02:42
  • Checking a column is essentially the same as checking a row, just swap the roles of row and column. – Barmar Nov 17 '20 at 02:43
  • Does this answer your question? [Algorithm for Determining Tic Tac Toe Game Over](https://stackoverflow.com/questions/1056316/algorithm-for-determining-tic-tac-toe-game-over) – wjatek Nov 17 '20 at 02:43

2 Answers2

0

This could be one of those questions you will get in a technical interview, you're going to have to compare a multidimensional array. https://www.javascripttutorial.net/javascript-multidimensional-array/

One way would be to loop over each element in array[0][x] and see if it matches array[1][x]

YellowGuy
  • 458
  • 1
  • 5
  • 12
  • 1
    Hi Will, Thank you very much for your answer. Knowing it's called a multidimensional array gives me a great path to go down to figure this out and learn. I know there's many "determine tic-tac-toe winner" articles out there, but mostly they end up showing the answer without giving much more insight. This is great. – JoyousPeanut Nov 17 '20 at 02:53
0

here is solution....

let array = [
  ["x","x","x"],
  ["x","o","x"],
  ["o","o","x"],
]

for(let i = 0; array.length > i; i++){
  if(array[i][0].includes("x") && array[i][1].includes("x") && array[i][2].includes("x")) {
      console.log("X is win")
  }
  
}


let array2 = [
  ["x","x","o"],
  ["x","x","o"],
  ["o","o","x"],
]


for(let i = 0; array2.length > i; i++){
  if(array2[i][0].includes("x") && array2[i+1][1].includes("x") && array2[i+2][2].includes("x")) {
      console.log("X is win")
  }
  
}
AkkiJS
  • 1
  • 3