Created
May 18, 2018 23:58
-
-
Save TimurM/2383afd73d51efe510681340c185eea7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//boardState is 2d array | |
//action is 2 length number string ex: "11" | |
//player is string of either "x" or "o" | |
//took longer time than allotted time | |
// Timur's comments: | |
// - Please indent your code to make it easier to read it! | |
// - Don't switch between es5 and es6. (var vs let) | |
// - break up your code into function. | |
// - like check for win condition, should be its own function. | |
// - That will make your code more readable | |
function ticTacToe( boardState, action, player ){ | |
if(boardState == null){ | |
// You should declare a varable outside and above the if statement and only use 'var board' once. | |
// otherwise it's easy to overwrite and break your code. | |
var board = [ | |
[null, null, null], | |
[null, null, null], | |
[null, null, null] | |
]; | |
}else{ | |
var board = boardState; | |
} | |
const numbers = action.split(""); | |
let a = parseInt(numbers[0]); | |
let b = parseInt(numbers[1]); | |
//check for if something is already there | |
if(board[a][b] != null){ | |
return "INVALID MOVE"; | |
}else{ | |
board[a][b] = player; | |
//check for win condition | |
if((board[0][0] == board[1][1]) && (board[1][1] == board[2][2])){ | |
if(board[0][0] != null){ | |
return ("player " + board[0][0] + " is the winner") | |
} | |
} | |
if((board[0][0] == board[1][0]) && (board[1][0] == board[2][0])){ | |
if(board[0][0] != null){ | |
return ("player " + board[0][0] + " is the winner") | |
} | |
} | |
if((board[0][1] == board[1][1]) && (board[1][1] == board[2][1])){ | |
if(board[0][1] != null){ | |
return ("player " + board[0][1] + " is the winner") | |
} | |
} | |
if((board[0][2] == board[1][2]) && (board[1][2] == board[2][2])){ | |
if(board[0][2] != null){ | |
return ("player " + board[0][2] + " is the winner") | |
} | |
} | |
if((board[0][2] == board[1][1]) && (board[1][1] == board[2][0])){ | |
if(board[0][2] != null){ | |
return ("player " + board[0][2] + " is the winner") | |
} | |
} | |
if((board[1][0] == board[1][1]) && (board[1][1] == board[1][2])){ | |
if(board[1][0] != null){ | |
return ("player " + board[1][0] + " is the winner") | |
} | |
} | |
if((board[2][0] == board[2][1]) && (board[2][1] == board[2][2])){ | |
if(board[2][0] != null){ | |
return ("player " + board[2][0] + " is the winner") | |
} | |
} | |
if((board[0][0] == board[0][1]) && (board[0][1] == board[0][2])){ | |
if(board[0][0] != null){ | |
return ("player " + board[0][0] + " is the winner") | |
} | |
} | |
//if no winconditions are met, return board and | |
return board; | |
} | |
} | |
var daBoard = [["x", null, null], | |
[null, null, null], | |
[null, null, "x"] | |
]; | |
console.log(ticTacToe(daBoard, "11", "x")); | |
view rawtic tac toe hosted with ❤ by GitHub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment