var Card = require('./Card.js'); class GameMachine { constructor() { this.state = { board: new Board(), players: [], hash: "0123456789" }; } setState(state) { this.state = { ...this.state, ...state}; } setBoard(board) { this.state = { ...this.state, board}; } setPlayers(players) { this.state = { ...this.state, players}; } runMove(move) { move.performMove(this.state.board); } } class Board { constructor() { this.data = []; this.owners = []; // These 2 to be updated after every move this.triggerPaths = []; // like attackVectors this.plusPaths = []; // like attackVectors for (var i=0; i < 9; i += 1) { this.triggerPaths[i] = []; this.plusPaths[i] = { winner: -1, sums: [] } } } debug() { console.log('BOARD'); console.log('---------------------------'); console.log('Cards :'); let s = ''; for(var i=0,j=0;i 0 ) { this._flipCard(position + dx, false, this.owners[position]); } } } _applySameRule(position) { for (let j = 0; j < 4; j += 1) { if ( Board.ATTACK_VECTORS[position][j] === 0 ) continue; const dx = this._getDisplacement(j); if ( this.triggerPaths[position][j] === 0 ) { this._flipCard(position + dx, true, this.owners[position]); } } } _applyPlusRule(position) { for (let j = 0; j < 4; j += 1) { // this for loop can be fixed seems obsolete & slow if ( Board.ATTACK_VECTORS[position][j] === 0 ) continue; const dx = this._getDisplacement(j); if ( this.plusPaths[position].sums[j] === this.plusPaths[position].winner ) { this._flipCard(position + dx, true, this.owners[position]); } } } _checkSameRule(position) { const card = this.data[position]; let sames = 0; for (let j = 0; j < 4; j += 1) { if ( Board.ATTACK_VECTORS[position][j] === 0 ) continue; if (this.triggerPaths[position] && this.triggerPaths[position][j] === 0) { sames += 1; } } return sames > 1; } _checkPlusRule(position) { // refactor plz // need fix (problem double plus a.k.a. four side attack) const card = this.data[position]; let pluses = {}; let sum = -1; for (let j = 0; j < 4; j += 1) { if ( Board.ATTACK_VECTORS[position][j] === 0 ) continue; if(sum = this.plusPaths[position].sums[j]) pluses[sum] = (pluses[sum] || 0) + 1; } for (var i in pluses) { if (pluses[i] > 1) { this.plusPaths[position].winner = parseInt(i); console.log(i); return true; } } this.plusPaths[position].winner = -1; return false; } isEmpty(position) { return !!this.data[position]; } } // START From top left goin row row Board.ATTACK_VECTORS = [ // [R, U, L, D] // right up left down [1, 0, 0, 1], [1, 0, 1, 1], [0, 0, 1, 1], [1, 1, 0, 1], [1, 1, 1, 1], [0, 1, 1, 1], [1, 1, 0, 0], [1, 1, 1, 0], [0, 1, 1, 0] ]; module.exports = { GameMachine };