Skip to content

Instantly share code, notes, and snippets.

@abrahamsangha
Created March 16, 2024 00:33
Show Gist options
  • Save abrahamsangha/fb5065fb4cd63c3d9888e9d3b51da3da to your computer and use it in GitHub Desktop.
Save abrahamsangha/fb5065fb4cd63c3d9888e9d3b51da3da to your computer and use it in GitHub Desktop.
class TTTGame {
constructor() {
this.board = [[null,null,null],[null,null,null],[null,null,null]]
this.currentTurn = 'x'
this.gameState = true
}
reset() {
this.board = [[null,null,null],[null,null,null],[null,null,null]]
this.currentTurn = 'x'
this.gameState = true
}
checkForWin() {
const verticalWin = this.checkForVerticalWin()
const horizontalWin = this.checkForHorizontalWin()
const firstDiagonalWin = this.checkForFirstDiagonalWin()
const secondDiagonalWin = this.checkForSecondDiagonalWin()
if (verticalWin) return verticalWin
if (horizontalWin) return horizontalWin
if (firstDiagonalWin) return firstDiagonalWin
if (secondDiagonalWin) return secondDiagonalWin
return false
}
checkForMatches(val1, val2, val3) {
if (val1 === val2 && val1 === val3) {
this.gameState = false
return {result: true, winner: val1}
}
return false
}
checkForHorizontalWin() {
for (let row = 0; row < 3; row++) {
const firstValue = this.board[row][0]
const secondValue = this.board[row][1]
const thirdValue = this.board[row][2]
if (!firstValue) {
continue
}
const result = this.checkForMatches(firstValue, secondValue, thirdValue)
if (result) {
return result
}
}
return false
}
checkForVerticalWin() {
for (let col = 0; col < 3; col++) {
const firstValue = this.board[0][col]
const secondValue = this.board[1][col]
const thirdValue = this.board[2][col]
if (!firstValue) {
continue
}
const result = this.checkForMatches(firstValue, secondValue, thirdValue)
if (result) {
return result
}
}
return false
}
checkForFirstDiagonalWin() {
const firstValue = this.board[0][0]
const secondValue = this.board[1][1]
const thirdValue = this.board[2][2]
if (!firstValue) {
return false
}
return this.checkForMatches(firstValue, secondValue, thirdValue)
}
checkForSecondDiagonalWin() {
const firstValue = this.board[0][2]
const secondValue = this.board[1][1]
const thirdValue = this.board[2][0]
if (!firstValue) {
return false
}
return this.checkForMatches(firstValue, secondValue, thirdValue)
}
placeX({row, col} = {}) {
this.#play(row, col, 'x')
const check = this.checkForWin()
if (check) {
console.log(`${check.winner} wins`)
}
}
placeO({row, col} = {}) {
this.#play(row, col, 'o')
const check = this.checkForWin()
if (check) {
console.log(`${check.winner} wins`)
}
}
#play(row, col, char) {
if (!this.gameState) {
throw new Error("game over")
}
if (typeof row !== 'number' || typeof col !== 'number') {
throw new Error("that's not a valid space")
}
if (this.currentTurn != char) {
throw new Error("not your turn, player")
}
if (row < 0 || col < 0 || row > 2 || col > 2 ) {
throw new Error("that's not a valid space")
}
if (this.board[row][col]) {
throw new Error('that space is already taken')
}
this.board[row][col] = char
if (char === 'x') {
this.currentTurn = 'o'
}
if (char === 'o') {
this.currentTurn = 'x'
}
return this
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment