summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2018-03-07 17:48:32 +0900
committersyuilo <syuilotan@yahoo.co.jp>2018-03-07 17:48:32 +0900
commit161fd4afab323ca6bf491def473f84bb7557b481 (patch)
tree4c8a2215dc9d3e3b817bbd82ca9b8f88fb0a0420 /src/common
parentwip (diff)
downloadmisskey-161fd4afab323ca6bf491def473f84bb7557b481.tar.gz
misskey-161fd4afab323ca6bf491def473f84bb7557b481.tar.bz2
misskey-161fd4afab323ca6bf491def473f84bb7557b481.zip
wip
Diffstat (limited to 'src/common')
-rw-r--r--src/common/othello.ts66
1 files changed, 44 insertions, 22 deletions
diff --git a/src/common/othello.ts b/src/common/othello.ts
index 858fc33158..fc27d72dcd 100644
--- a/src/common/othello.ts
+++ b/src/common/othello.ts
@@ -1,37 +1,38 @@
const BOARD_SIZE = 8;
export default class Othello {
- public board: Array<Array<'black' | 'white'>>;
+ public board: Array<'black' | 'white'>;
/**
* ゲームを初期化します
*/
constructor() {
this.board = [
- [null, null, null, null, null, null, null, null],
- [null, null, null, null, null, null, null, null],
- [null, null, null, null, null, null, null, null],
- [null, null, null, 'black', 'white', null, null, null],
- [null, null, null, 'white', 'black', null, null, null],
- [null, null, null, null, null, null, null, null],
- [null, null, null, null, null, null, null, null],
- [null, null, null, null, null, null, null, null]
+ null, null, null, null, null, null, null, null,
+ null, null, null, null, null, null, null, null,
+ null, null, null, null, null, null, null, null,
+ null, null, null, 'white', 'black', null, null, null,
+ null, null, null, 'black', 'white', null, null, null,
+ null, null, null, null, null, null, null, null,
+ null, null, null, null, null, null, null, null,
+ null, null, null, null, null, null, null, null
];
}
public setByNumber(color, n) {
const ps = this.getPattern(color);
- this.set(color, ps[n][0], ps[n][1]);
+ this.set2(color, ps[n][0], ps[n][1]);
}
private write(color, x, y) {
- this.board[y][x] = color;
+ const pos = x + (y * 8);
+ this.board[pos] = color;
}
/**
* 石を配置します
*/
- public set(color, x, y) {
+ public set2(color, x, y) {
this.write(color, x, y);
const reverses = this.getReverse(color, x, y);
@@ -89,24 +90,42 @@ export default class Othello {
});
}
+ public set(color, pos) {
+ const x = pos % BOARD_SIZE;
+ const y = Math.floor(pos / BOARD_SIZE);
+ this.set2(color, x, y);
+ }
+
+ public get(x, y) {
+ const pos = x + (y * 8);
+ return this.board[pos];
+ }
+
/**
* 打つことができる場所を取得します
*/
public getPattern(myColor): number[][] {
const result = [];
- this.board.forEach((stones, y) => stones.forEach((stone, x) => {
+ this.board.forEach((stone, i) => {
if (stone != null) return;
- if (this.canReverse(myColor, x, y)) result.push([x, y]);
- }));
+ const x = i % BOARD_SIZE;
+ const y = Math.floor(i / BOARD_SIZE);
+ if (this.canReverse2(myColor, x, y)) result.push([x, y]);
+ });
return result;
}
/**
* 指定の位置に石を打つことができるかどうか(相手の石を1つでも反転させられるか)を取得します
*/
- public canReverse(myColor, targetx, targety): boolean {
+ public canReverse2(myColor, targetx, targety): boolean {
return this.getReverse(myColor, targetx, targety) !== null;
}
+ public canReverse(myColor, pos): boolean {
+ const x = pos % BOARD_SIZE;
+ const y = Math.floor(pos / BOARD_SIZE);
+ return this.getReverse(myColor, x, y) !== null;
+ }
private getReverse(myColor, targetx, targety): number[] {
const opponentColor = myColor == 'black' ? 'white' : 'black';
@@ -117,11 +136,11 @@ export default class Othello {
return (x, y): any => {
if (breaked) {
return;
- } else if (this.board[y][x] == myColor && opponentStoneFound) {
+ } else if (this.get(x, y) == myColor && opponentStoneFound) {
return true;
- } else if (this.board[y][x] == myColor && !opponentStoneFound) {
+ } else if (this.get(x, y) == myColor && !opponentStoneFound) {
breaked = true;
- } else if (this.board[y][x] == opponentColor) {
+ } else if (this.get(x, y) == opponentColor) {
opponentStoneFound = true;
} else {
breaked = true;
@@ -210,12 +229,13 @@ export default class Othello {
public toString(): string {
//return this.board.map(row => row.map(state => state === 'black' ? '●' : state === 'white' ? '○' : '┼').join('')).join('\n');
- return this.board.map(row => row.map(state => state === 'black' ? '⚫️' : state === 'white' ? '⚪️' : '🔹').join('')).join('\n');
+ //return this.board.map(row => row.map(state => state === 'black' ? '⚫️' : state === 'white' ? '⚪️' : '🔹').join('')).join('\n');
+ return 'wip';
}
public toPatternString(color): string {
//const num = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
- const num = ['0️⃣', '1️⃣', '2️⃣', '3️⃣', '4️⃣', '5️⃣', '6️⃣', '7️⃣', '8️⃣', '9️⃣', '🔟', '🍏', '🍎', '🍐', '🍊', '🍋', '🍌', '🍉', '🍇', '🍓', '🍈', '🍒', '🍑', '🍍'];
+ /*const num = ['0️⃣', '1️⃣', '2️⃣', '3️⃣', '4️⃣', '5️⃣', '6️⃣', '7️⃣', '8️⃣', '9️⃣', '🔟', '🍏', '🍎', '🍐', '🍊', '🍋', '🍌', '🍉', '🍇', '🍓', '🍈', '🍒', '🍑', '🍍'];
const pattern = this.getPattern(color);
@@ -223,7 +243,9 @@ export default class Othello {
const i = pattern.findIndex(p => p[0] == x && p[1] == y);
//return state === 'black' ? '●' : state === 'white' ? '○' : i != -1 ? num[i] : '┼';
return state === 'black' ? '⚫️' : state === 'white' ? '⚪️' : i != -1 ? num[i] : '🔹';
- }).join('')).join('\n');
+ }).join('')).join('\n');*/
+
+ return 'wip';
}
}