-
-
Save wilmoore/84ad5d4dbb5b2e76e1af to your computer and use it in GitHub Desktop.
Tic Tac Toe Game Winning Slot & Board Generator
This file contains hidden or 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
| 'use strict' | |
| var size = 3 | |
| var length = Math.pow(size, 2) | |
| var backslash = [ 0, 4, 8 ] | |
| var board = Object.create({ | |
| grid: new Array(length), | |
| empty () { | |
| return this.grid.every(element => element == null) | |
| }, | |
| which (symbol) { | |
| var out = [] | |
| var idx = -1 | |
| var end = this.grid.length | |
| while (++idx < end) { | |
| if (this.grid[idx] === symbol) out.push(idx) | |
| } | |
| return out | |
| }, | |
| occupied (slot) { | |
| return this.grid[slot] != null | |
| }, | |
| occupiedBy (symbol, slot) { | |
| return this.get(slot) === symbol | |
| }, | |
| get (slot) { | |
| return this.grid[slot] | |
| }, | |
| set (symbol, slot) { | |
| this.grid[slot] = symbol | |
| } | |
| }) | |
| board.set('x', 4) | |
| board.set('x', 0) | |
| board.set('x', 2) | |
| board.set('x', 8) | |
| console.log('size (%d) length (%d)', size, length) | |
| console.log('board %s (%s)', board.grid, board.empty() ? 'empty' : 'not empty') // board ... (not empty) | |
| console.log('slot 4 is %s', board.occupied(4) ? `occupied by ${board.get(4)}` : 'not occupied') // slot 4 is occupied by x | |
| console.log('slot 6 is %s', board.occupied(6) ? 'occupied' : 'not occupied') // slot 6 is not occupied | |
| console.log(board.which('x')) // [ 0, 4, 8 ] | |
| var diff = backslash.filter(x => !~board.which('x').indexOf(x)) | |
| console.log('Player x %s', diff.length === 0 ? 'has won via backslash' : 'has not won yet') // Player x has won via backslash |
This file contains hidden or 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
| 'use strict' | |
| var appendUntil = require('array-append-until') | |
| var size = 3 | |
| var length = Math.pow(size, 2) | |
| var finalColumnIdx = size - 1 | |
| var firstGridIdx = 0 | |
| var finalGridIdx = length - 1 | |
| var backslashIncrement = finalGridIdx / finalColumnIdx | |
| var columns = [] | |
| var rows = [] | |
| var backslash = [] | |
| var forwardslash = [] | |
| // rows algorithm | |
| for (var r = firstGridIdx; r <= size * (finalColumnIdx); r += size) { | |
| rows.push(appendUntil(incrementBy(1), listLengthIsEqualTo(size), [r])) | |
| } | |
| // columns algorithm | |
| for (var c = firstGridIdx; c < size; c++) { | |
| columns.push(appendUntil(incrementBy(size), listLengthIsEqualTo(size), [c])) | |
| } | |
| // backslash (top-left to bottom right diagonal) algorithm | |
| backslash.push(appendUntil(incrementBy(backslashIncrement), listLengthIsEqualTo(size), [firstGridIdx])) | |
| // forwardslash (top-right to bottom left diagonal) algorithm | |
| forwardslash.push(appendUntil(incrementBy(finalColumnIdx), listLengthIsEqualTo(size), [finalColumnIdx])) | |
| console.log('rows', rows) | |
| console.log('columns', columns) | |
| console.log('backslash', backslash) | |
| console.log('forwardslash', forwardslash) | |
| function incrementBy (val) { | |
| return (list) => list.pop() + val | |
| } | |
| function listLengthIsEqualTo (equals, list) { | |
| return (list) => list.length === equals | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output for a board of size 3: