Skip to content

Instantly share code, notes, and snippets.

@marcoemrich
Created July 3, 2020 14:35
Show Gist options
  • Save marcoemrich/81db4a27199e5fa93c35360095b295f9 to your computer and use it in GitHub Desktop.
Save marcoemrich/81db4a27199e5fa93c35360095b295f9 to your computer and use it in GitHub Desktop.
JS-Testing: Model of Tic Tac Toe
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