Created
July 3, 2020 14:35
-
-
Save marcoemrich/81db4a27199e5fa93c35360095b295f9 to your computer and use it in GitHub Desktop.
JS-Testing: Model of Tic Tac Toe
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
import * as R from 'ramda' | |
export default class TicTacToe { | |
constructor(fields, currentPlayer = 'X') { | |
this.fields = fields; | |
this.currentPlayer = currentPlayer; | |
}; | |
static startWithSize(width, height) { | |
return new TicTacToe(Array(width * height).fill(" ")); | |
} | |
oppositePlayer(player) { | |
return player === 'X' ? 'O' : 'X'; | |
} | |
toString() { return this.fields.join('') }; | |
mark(cellNr) { | |
if (R.nth(cellNr, this.fields) !== ' ') return this; | |
return new TicTacToe( | |
R.update(cellNr, this.currentPlayer, this.fields), | |
this.oppositePlayer(this.currentPlayer) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment