diff --git a/src/games/reversi/core.ts b/src/games/reversi/core.ts index e724917fbff7edbf180eef91f9fe76f021b517a5..f678e4ec8bee2e5db6c694148c032a9d62010e57 100644 --- a/src/games/reversi/core.ts +++ b/src/games/reversi/core.ts @@ -76,27 +76,14 @@ export default class Reversi { this.mapHeight = map.length; const mapData = map.join(''); - this.board = mapData.split('').map(d => { - if (d == '-') return null; - if (d == 'b') return BLACK; - if (d == 'w') return WHITE; - return undefined; - }); + this.board = mapData.split('').map(d => d === '-' ? null : d === 'b' ? BLACK : d === 'w' ? WHITE : undefined); - this.map = mapData.split('').map(d => { - if (d == '-' || d == 'b' || d == 'w') return 'empty'; - return 'null'; - }); + this.map = mapData.split('').map(d => d === '-' || d === 'b' || d === 'w' ? 'empty' : 'null'); //#endregion // ゲームãŒå§‹ã¾ã£ãŸæ™‚点ã§ç‰‡æ–¹ã®è‰²ã®çŸ³ã—ã‹ãªã„ã‹ã€å§‹ã¾ã£ãŸæ™‚点ã§å‹æ•—ãŒæ±ºå®šã™ã‚‹ã‚ˆã†ãªãƒžãƒƒãƒ—ã®å ´åˆãŒã‚ã‚‹ - if (!this.canPutSomewhere(BLACK)) { - if (!this.canPutSomewhere(WHITE)) { - this.turn = null; - } else { - this.turn = WHITE; - } - } + if (!this.canPutSomewhere(BLACK)) + this.turn = this.canPutSomewhere(WHITE) ? WHITE : null; } /** @@ -117,16 +104,14 @@ export default class Reversi { * 黒石ã®æ¯”率 */ public get blackP() { - if (this.blackCount == 0 && this.whiteCount == 0) return 0; - return this.blackCount / (this.blackCount + this.whiteCount); + return this.blackCount == 0 && this.whiteCount == 0 ? 0 : this.blackCount / (this.blackCount + this.whiteCount); } /** * 白石ã®æ¯”率 */ public get whiteP() { - if (this.blackCount == 0 && this.whiteCount == 0) return 0; - return this.whiteCount / (this.blackCount + this.whiteCount); + return this.blackCount == 0 && this.whiteCount == 0 ? 0 : this.whiteCount / (this.blackCount + this.whiteCount); } public transformPosToXy(pos: number): number[] { @@ -172,13 +157,10 @@ export default class Reversi { private calcTurn() { // ターン計算 - if (this.canPutSomewhere(!this.prevColor)) { - this.turn = !this.prevColor; - } else if (this.canPutSomewhere(this.prevColor)) { - this.turn = this.prevColor; - } else { - this.turn = null; - } + this.turn = + this.canPutSomewhere(!this.prevColor) ? !this.prevColor : + this.canPutSomewhere(this.prevColor) ? this.prevColor : + null; } public undo() { @@ -199,8 +181,7 @@ export default class Reversi { */ public mapDataGet(pos: number): MapPixel { const [x, y] = this.transformPosToXy(pos); - if (x < 0 || y < 0 || x >= this.mapWidth || y >= this.mapHeight) return 'null'; - return this.map[pos]; + return x < 0 || y < 0 || x >= this.mapWidth || y >= this.mapHeight ? 'null' : this.map[pos]; } /** @@ -223,16 +204,10 @@ export default class Reversi { * @param pos ä½ç½® */ public canPut(color: Color, pos: number): boolean { - // æ—¢ã«çŸ³ãŒç½®ã„ã¦ã‚ã‚‹å ´æ‰€ã«ã¯æ‰“ã¦ãªã„ - if (this.board[pos] !== null) return false; - - if (this.opts.canPutEverywhere) { - // 挟んã§ãªãã¦ã‚‚ç½®ã‘るモード - return this.mapDataGet(pos) == 'empty'; - } else { - // 相手ã®çŸ³ã‚’1ã¤ã§ã‚‚å転ã•ã›ã‚‰ã‚Œã‚‹ã‹ - return this.effects(color, pos).length !== 0; - } + return ( + this.board[pos] !== null ? false : // æ—¢ã«çŸ³ãŒç½®ã„ã¦ã‚ã‚‹å ´æ‰€ã«ã¯æ‰“ã¦ãªã„ + this.opts.canPutEverywhere ? this.mapDataGet(pos) == 'empty' : // 挟んã§ãªãã¦ã‚‚ç½®ã‘るモード + this.effects(color, pos).length !== 0); // 相手ã®çŸ³ã‚’1ã¤ã§ã‚‚å転ã•ã›ã‚‰ã‚Œã‚‹ã‹ } /** @@ -263,19 +238,13 @@ export default class Reversi { [x, y] = nextPos(x, y); // 座標ãŒæŒ‡ã—示ã™ä½ç½®ãŒãƒœãƒ¼ãƒ‰å¤–ã«å‡ºãŸã¨ã - if (this.opts.loopedBoard) { - x = ((x % this.mapWidth) + this.mapWidth) % this.mapWidth; - y = ((y % this.mapHeight) + this.mapHeight) % this.mapHeight; - - if (this.transformXyToPos(x, y) == initPos) { + if (this.opts.loopedBoard && this.transformXyToPos( + (x = ((x % this.mapWidth) + this.mapWidth) % this.mapWidth), + (y = ((y % this.mapHeight) + this.mapHeight) % this.mapHeight)) == initPos) // 盤é¢ã®å¢ƒç•Œã§ãƒ«ãƒ¼ãƒ—ã—ã€è‡ªåˆ†ãŒçŸ³ã‚’ç½®ãä½ç½®ã«æˆ»ã£ã¦ããŸã¨ãã€æŒŸã‚るよã†ã«ã—ã¦ã„ã‚‹ (ref: Test4ã®ãƒžãƒƒãƒ—) - return found; - } - } else { - if (x == -1 || y == -1 || x == this.mapWidth || y == this.mapHeight) { - return []; // 挟ã‚ãªã„ã“ã¨ãŒç¢ºå®š (盤é¢å¤–ã«åˆ°é”) - } - } + return found; + else if (x == -1 || y == -1 || x == this.mapWidth || y == this.mapHeight) + return []; // 挟ã‚ãªã„ã“ã¨ãŒç¢ºå®š (盤é¢å¤–ã«åˆ°é”) const pos = this.transformXyToPos(x, y); if (this.mapDataGet(pos) === 'null') return []; // 挟ã‚ãªã„ã“ã¨ãŒç¢ºå®š (é…ç½®ä¸å¯èƒ½ãªãƒžã‚¹ã«åˆ°é”) @@ -300,14 +269,9 @@ export default class Reversi { * ゲームã®å‹è€… (null = 引ã分ã‘) */ public get winner(): Color { - if (!this.isEnded) return undefined; - - if (this.blackCount == this.whiteCount) return null; - - if (this.opts.isLlotheo) { - return this.blackCount > this.whiteCount ? WHITE : BLACK; - } else { - return this.blackCount > this.whiteCount ? BLACK : WHITE; - } + return this.isEnded ? + this.blackCount == this.whiteCount ? null : + this.opts.isLlotheo === this.blackCount > this.whiteCount ? WHITE : BLACK : + undefined; } }