Last active
June 14, 2018 15:28
-
-
Save rjhilgefort/ba309cc8f084a79b4eadc07d4acdecff to your computer and use it in GitHub Desktop.
A quick Connect Four app build with a single class. Has a stupid AI, simulate, and user interaction. In ramda REPL: https://goo.gl/WBd2jL
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
const { log, clear } = console | |
const reduceI = addIndex(reduce); | |
const HUMAN = 'X'; | |
const COMPUTER = 'O'; | |
class Board { | |
constructor({ | |
rows = 4, | |
columns = 4, | |
player1 = HUMAN, | |
player2 = COMPUTER, | |
} = {}) { | |
this.rows = rows; | |
this.columns = columns; | |
this.player1 = player1; | |
this.player2 = player2; | |
// Row :: Array<string|nil> | |
// Board :: Array<Rows> | |
this.board = []; | |
for (let i = 0; i < this.rows; i++) { | |
let _column = []; | |
for (let j = 0; j < this.columns; j++) { | |
_column.push(null); | |
} | |
this.board.push(_column) | |
}; | |
} | |
print() { | |
for (let i = this.rows - 1; i >= 0; i--) { | |
compose( | |
log, | |
join('|'), | |
map(when(isNil, always('-'))), | |
)(this.board[i]); | |
} | |
compose(log, join(' '), times(add(1)))(this.columns); | |
return this; | |
} | |
_traverse = (cb) => ({ player, column }) => { | |
for (let i = 0; i < this.rows; i++) { | |
for (let j = 0; j < this.columns; j++) { | |
const currColumn = j + 1; | |
if (currColumn !== column) continue; | |
const currValue = this.board[i][j]; | |
if (isNil(currValue)) { | |
return cb({ | |
r: i, | |
c: j, | |
player, | |
column, | |
}); | |
} | |
} | |
} | |
return cb(null); | |
} | |
move = this._traverse(({ r, c, player, column }) => { | |
this.board[r][c] = player; | |
}); | |
moveIsValid = this._traverse(ifElse(isNil, F, T)); | |
isFull() { | |
for (let i = 0; i < this.rows; i++) { | |
for (let j = 0; j < this.columns; j++) { | |
const currValue = this.board[i][j]; | |
if (isNil(currValue)) { | |
return false; | |
} | |
} | |
} | |
return true; | |
} | |
movePlayer = player => { | |
for (let i = 0; i < this.rows; i++) { | |
for (let j = 0; j < this.columns; j++) { | |
const currValue = this.board[i][j]; | |
if (isNil(currValue)) { | |
this.board[i][j] = player; | |
return this; | |
} | |
} | |
} | |
throw new Error('No Moves Available'); | |
} | |
movePlayer1 = () => this.movePlayer(this.player1); | |
movePlayer2 = () => this.movePlayer(this.player2); | |
movePlayer1ThenAI = ({ column }) => { | |
log('-----------Making move-----'); | |
this.move({ player: this.player1, column }); | |
this.print(); | |
this.movePlayer2(); | |
this.print(); | |
} | |
simulate() { | |
try { | |
while(!this.isFull()) { | |
this.movePlayer1(); | |
this.print(); | |
this.movePlayer2(); | |
this.print(); | |
} | |
} catch(e) { | |
log(e); | |
} | |
} | |
} | |
const game = new Board({ rows: 7, columns: 7 }); | |
window.onkeyup = (e) => { | |
const column = parseInt(e.key, 10); | |
if (column > game.columns) { | |
log('cannot make move'); | |
} else { | |
game.movePlayer1ThenAI({ column }); | |
} | |
} | |
// clear(); | |
// TYPE BELOW: | |
// 1231 | |
const foo = 'end'; | |
foo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment