summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorha-dai <contact@haradai.net>2017-10-25 16:24:16 +0900
committerha-dai <contact@haradai.net>2017-10-25 16:24:16 +0900
commitfabcad6db9dff573ef1b3a634db813e8eff7d82e (patch)
treee2c312df68bdd508b7422fbe15a35351c6a589b0 /src/common
parentMerge pull request #788 from syuilo/greenkeeper/@types/morgan-1.7.33 (diff)
parentMerge branch 'master' of https://github.com/syuilo/misskey (diff)
downloadmisskey-fabcad6db9dff573ef1b3a634db813e8eff7d82e.tar.gz
misskey-fabcad6db9dff573ef1b3a634db813e8eff7d82e.tar.bz2
misskey-fabcad6db9dff573ef1b3a634db813e8eff7d82e.zip
Merge branch 'master' of https://github.com/syuilo/misskey
Diffstat (limited to 'src/common')
-rw-r--r--src/common/get-post-summary.ts39
-rw-r--r--src/common/get-user-summary.ts12
-rw-r--r--src/common/othello.ts268
3 files changed, 319 insertions, 0 deletions
diff --git a/src/common/get-post-summary.ts b/src/common/get-post-summary.ts
new file mode 100644
index 0000000000..f628a32b41
--- /dev/null
+++ b/src/common/get-post-summary.ts
@@ -0,0 +1,39 @@
+/**
+ * 投稿を表す文字列を取得します。
+ * @param {*} post 投稿
+ */
+const summarize = (post: any): string => {
+ let summary = post.text ? post.text : '';
+
+ // メディアが添付されているとき
+ if (post.media) {
+ summary += ` (${post.media.length}つのメディア)`;
+ }
+
+ // 投票が添付されているとき
+ if (post.poll) {
+ summary += ' (投票)';
+ }
+
+ // 返信のとき
+ if (post.reply_to_id) {
+ if (post.reply_to) {
+ summary += ` RE: ${summarize(post.reply_to)}`;
+ } else {
+ summary += ' RE: ...';
+ }
+ }
+
+ // Repostのとき
+ if (post.repost_id) {
+ if (post.repost) {
+ summary += ` RP: ${summarize(post.repost)}`;
+ } else {
+ summary += ' RP: ...';
+ }
+ }
+
+ return summary.trim();
+};
+
+export default summarize;
diff --git a/src/common/get-user-summary.ts b/src/common/get-user-summary.ts
new file mode 100644
index 0000000000..1bec2f9a26
--- /dev/null
+++ b/src/common/get-user-summary.ts
@@ -0,0 +1,12 @@
+import { IUser } from '../api/models/user';
+
+/**
+ * ユーザーを表す文字列を取得します。
+ * @param user ユーザー
+ */
+export default function(user: IUser): string {
+ return `${user.name} (@${user.username})\n` +
+ `${user.posts_count}投稿、${user.following_count}フォロー、${user.followers_count}フォロワー\n` +
+ `場所: ${user.profile.location}、誕生日: ${user.profile.birthday}\n` +
+ `「${user.description}」`;
+}
diff --git a/src/common/othello.ts b/src/common/othello.ts
new file mode 100644
index 0000000000..858fc33158
--- /dev/null
+++ b/src/common/othello.ts
@@ -0,0 +1,268 @@
+const BOARD_SIZE = 8;
+
+export default class Othello {
+ public board: Array<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]
+ ];
+ }
+
+ public setByNumber(color, n) {
+ const ps = this.getPattern(color);
+ this.set(color, ps[n][0], ps[n][1]);
+ }
+
+ private write(color, x, y) {
+ this.board[y][x] = color;
+ }
+
+ /**
+ * 石を配置します
+ */
+ public set(color, x, y) {
+ this.write(color, x, y);
+
+ const reverses = this.getReverse(color, x, y);
+
+ reverses.forEach(r => {
+ switch (r[0]) {
+ case 0: // 上
+ for (let c = 0, _y = y - 1; c < r[1]; c++, _y--) {
+ this.write(color, x, _y);
+ }
+ break;
+
+ case 1: // 右上
+ for (let c = 0, i = 1; c < r[1]; c++, i++) {
+ this.write(color, x + i, y - i);
+ }
+ break;
+
+ case 2: // 右
+ for (let c = 0, _x = x + 1; c < r[1]; c++, _x++) {
+ this.write(color, _x, y);
+ }
+ break;
+
+ case 3: // 右下
+ for (let c = 0, i = 1; c < r[1]; c++, i++) {
+ this.write(color, x + i, y + i);
+ }
+ break;
+
+ case 4: // 下
+ for (let c = 0, _y = y + 1; c < r[1]; c++, _y++) {
+ this.write(color, x, _y);
+ }
+ break;
+
+ case 5: // 左下
+ for (let c = 0, i = 1; c < r[1]; c++, i++) {
+ this.write(color, x - i, y + i);
+ }
+ break;
+
+ case 6: // 左
+ for (let c = 0, _x = x - 1; c < r[1]; c++, _x--) {
+ this.write(color, _x, y);
+ }
+ break;
+
+ case 7: // 左上
+ for (let c = 0, i = 1; c < r[1]; c++, i++) {
+ this.write(color, x - i, y - i);
+ }
+ break;
+ }
+ });
+ }
+
+ /**
+ * 打つことができる場所を取得します
+ */
+ public getPattern(myColor): number[][] {
+ const result = [];
+ this.board.forEach((stones, y) => stones.forEach((stone, x) => {
+ if (stone != null) return;
+ if (this.canReverse(myColor, x, y)) result.push([x, y]);
+ }));
+ return result;
+ }
+
+ /**
+ * 指定の位置に石を打つことができるかどうか(相手の石を1つでも反転させられるか)を取得します
+ */
+ public canReverse(myColor, targetx, targety): boolean {
+ return this.getReverse(myColor, targetx, targety) !== null;
+ }
+
+ private getReverse(myColor, targetx, targety): number[] {
+ const opponentColor = myColor == 'black' ? 'white' : 'black';
+
+ const createIterater = () => {
+ let opponentStoneFound = false;
+ let breaked = false;
+ return (x, y): any => {
+ if (breaked) {
+ return;
+ } else if (this.board[y][x] == myColor && opponentStoneFound) {
+ return true;
+ } else if (this.board[y][x] == myColor && !opponentStoneFound) {
+ breaked = true;
+ } else if (this.board[y][x] == opponentColor) {
+ opponentStoneFound = true;
+ } else {
+ breaked = true;
+ }
+ };
+ };
+
+ const res = [];
+
+ let iterate;
+
+ // 上
+ iterate = createIterater();
+ for (let c = 0, y = targety - 1; y >= 0; c++, y--) {
+ if (iterate(targetx, y)) {
+ res.push([0, c]);
+ break;
+ }
+ }
+
+ // 右上
+ iterate = createIterater();
+ for (let c = 0, i = 1; i <= Math.min(BOARD_SIZE - targetx, targety); c++, i++) {
+ if (iterate(targetx + i, targety - i)) {
+ res.push([1, c]);
+ break;
+ }
+ }
+
+ // 右
+ iterate = createIterater();
+ for (let c = 0, x = targetx + 1; x < BOARD_SIZE; c++, x++) {
+ if (iterate(x, targety)) {
+ res.push([2, c]);
+ break;
+ }
+ }
+
+ // 右下
+ iterate = createIterater();
+ for (let c = 0, i = 1; i < Math.min(BOARD_SIZE - targetx, BOARD_SIZE - targety); c++, i++) {
+ if (iterate(targetx + i, targety + i)) {
+ res.push([3, c]);
+ break;
+ }
+ }
+
+ // 下
+ iterate = createIterater();
+ for (let c = 0, y = targety + 1; y < BOARD_SIZE; c++, y++) {
+ if (iterate(targetx, y)) {
+ res.push([4, c]);
+ break;
+ }
+ }
+
+ // 左下
+ iterate = createIterater();
+ for (let c = 0, i = 1; i < Math.min(targetx, BOARD_SIZE - targety); c++, i++) {
+ if (iterate(targetx - i, targety + i)) {
+ res.push([5, c]);
+ break;
+ }
+ }
+
+ // 左
+ iterate = createIterater();
+ for (let c = 0, x = targetx - 1; x >= 0; c++, x--) {
+ if (iterate(x, targety)) {
+ res.push([6, c]);
+ break;
+ }
+ }
+
+ // 左上
+ iterate = createIterater();
+ for (let c = 0, i = 1; i <= Math.min(targetx, targety); c++, i++) {
+ if (iterate(targetx - i, targety - i)) {
+ res.push([7, c]);
+ break;
+ }
+ }
+
+ return res.length === 0 ? null : res;
+ }
+
+ 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');
+ }
+
+ 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 pattern = this.getPattern(color);
+
+ return this.board.map((row, y) => row.map((state, x) => {
+ 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');
+ }
+}
+
+export function ai(color: string, othello: Othello) {
+ const opponentColor = color == 'black' ? 'white' : 'black';
+
+ function think() {
+ // 打てる場所を取得
+ const ps = othello.getPattern(color);
+
+ if (ps.length > 0) { // 打てる場所がある場合
+ // 角を取得
+ const corners = ps.filter(p =>
+ // 左上
+ (p[0] == 0 && p[1] == 0) ||
+ // 右上
+ (p[0] == (BOARD_SIZE - 1) && p[1] == 0) ||
+ // 右下
+ (p[0] == (BOARD_SIZE - 1) && p[1] == (BOARD_SIZE - 1)) ||
+ // 左下
+ (p[0] == 0 && p[1] == (BOARD_SIZE - 1))
+ );
+
+ if (corners.length > 0) { // どこかしらの角に打てる場合
+ // 打てる角からランダムに選択して打つ
+ const p = corners[Math.floor(Math.random() * corners.length)];
+ othello.set(color, p[0], p[1]);
+ } else { // 打てる角がない場合
+ // 打てる場所からランダムに選択して打つ
+ const p = ps[Math.floor(Math.random() * ps.length)];
+ othello.set(color, p[0], p[1]);
+ }
+
+ // 相手の打つ場所がない場合続けてAIのターン
+ if (othello.getPattern(opponentColor).length === 0) {
+ think();
+ }
+ }
+ }
+
+ think();
+}