Last active
February 28, 2018 06:10
-
-
Save guchimon99/a3d8f66f56fdbf834696c6734e584953 to your computer and use it in GitHub Desktop.
リバーシの開いているセルを探す
This file contains hidden or 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
| function moveIndex (index, x, y) { | |
| index += x | |
| index += y * 8 | |
| return index | |
| } | |
| function isTopSide (index) { | |
| return index < 8 | |
| } | |
| function isLeftSide(index) { | |
| return index % 8 == 0 | |
| } | |
| function isRightSide(index) { | |
| return index % 8 == 7 | |
| } | |
| function isBottomSide (index){ | |
| return index > 55 | |
| } | |
| function getPuttableInexes ( cells, putColor ) { | |
| var indexes = [] | |
| cells.forEach((cell, index) => { | |
| if (cell != 0) return | |
| var lines = [], line, cursor | |
| cursor = index | |
| line = [] | |
| while (!isTopSide(cursor) && !isLeftSide(cursor)) { | |
| cursor = moveIndex(cursor, -1, -1) | |
| line.push(cursor) | |
| } | |
| lines.push(line) | |
| cursor = index | |
| line = [] | |
| while (!isTopSide(cursor)) { | |
| cursor = moveIndex(cursor, 0, -1) | |
| line.push(cursor) | |
| } | |
| lines.push(line) | |
| cursor = index | |
| line = [] | |
| while (!isTopSide(cursor) && !isRightSide(cursor)) { | |
| cursor = moveIndex(cursor, 1, -1) | |
| line.push(cursor) | |
| } | |
| lines.push(line) | |
| cursor = index | |
| line = [] | |
| while (!isLeftSide(cursor)) { | |
| cursor = moveIndex(cursor, -1, 0) | |
| line.push(cursor) | |
| } | |
| lines.push(line) | |
| cursor = index | |
| line = [] | |
| while (!isRightSide(cursor)) { | |
| cursor = moveIndex(cursor, 1, 0) | |
| line.push(cursor) | |
| } | |
| lines.push(line) | |
| cursor = index | |
| line = [] | |
| while (!isBottomSide(cursor) && !isLeftSide(cursor)) { | |
| cursor = moveIndex(cursor, -1, 1) | |
| line.push(cursor) | |
| } | |
| lines.push(line) | |
| cursor = index | |
| line = [] | |
| while (!isBottomSide(cursor)) { | |
| cursor = moveIndex(cursor, 0, 1) | |
| line.push(cursor) | |
| } | |
| lines.push(line) | |
| cursor = index | |
| line = [] | |
| while (!isBottomSide(cursor) && !isRightSide(cursor)) { | |
| cursor = moveIndex(cursor, 1, 1) | |
| line.push(cursor) | |
| } | |
| lines.push(line) | |
| var isPuttable = lines.some((line) => { | |
| var count = 0 | |
| line.some((i) => { | |
| if (cells[i] == 0) { | |
| count = 0 | |
| return true | |
| } else if (cells[i] == putColor) { | |
| return true | |
| } else { | |
| count += 1 | |
| } | |
| }) | |
| return count > 0 | |
| }) | |
| if (isPuttable) { indexes.push(index) } | |
| }) | |
| return indexes | |
| } | |
| // example: | |
| /* | |
| function renderCells (cells) { | |
| var str = "" | |
| cells.forEach((cell, index) => { | |
| switch(cell) { | |
| case 0 : | |
| str += "_" | |
| break | |
| case 1: | |
| str += "b" | |
| break | |
| case 2: | |
| str += "w" | |
| break | |
| case 3: | |
| str += "@" | |
| break | |
| } | |
| str += " " | |
| if (index % 8 == 7) str += "\n" | |
| }) | |
| console.log(str) | |
| } | |
| var cells = [ | |
| 0,0,0,0,0,0,0,0, | |
| 0,0,0,0,0,0,0,0, | |
| 0,0,0,0,0,0,0,0, | |
| 0,0,0,1,2,0,0,0, | |
| 0,0,0,2,1,0,0,0, | |
| 0,0,0,0,0,0,0,0, | |
| 0,0,0,0,0,0,0,0, | |
| 0,0,0,0,0,0,0,0 | |
| ] | |
| var indexes = getPuttableInexes(cells, 1) | |
| indexes.forEach((index) => cells[index] = 3) | |
| renderCells(cells) | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment