Javaでオセロを作ったのでソースコードを置いておく

import java.util.Scanner;
import java.io.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Othello {

    final int BLACK = 1;
    final int WHITE = 2;

    final int EMPTY = 0;
    final int WALL = 3;
    final int ENTITY = 100;

    final int LEFT = -1;
    final int RIGHT = 1;
    final int UPPER = 10;
    final int DOWN = -10;
    final int LEFT_UPPER = 11;
    final int LEFT_DOWN = -9;
    final int RIGHT_UPPER = 9;
    final int RIGHT_DOWN = -11;

    final int[] DIRS = { LEFT, RIGHT, UPPER, DOWN, LEFT_UPPER, LEFT_DOWN, RIGHT_UPPER, RIGHT_DOWN };

    int[] board;
    int turn;

    public Othello() {
        turn = BLACK;
        board = new int[ENTITY];

        for (int i = 0; i < ENTITY; i++) {
            board[i] = WALL;
        }

        for (int rank = 1; rank <= 8; rank++) {
            for (int file = 1; file <= 8; file++) {
                board[rank * 10 + file] = EMPTY;
            }
        }

        board[44] = BLACK;
        board[55] = BLACK;
        board[45] = WHITE;
        board[54] = WHITE;
    }

    public int stdinPos() {

        while (true) {

            Scanner scan = new Scanner(System.in);
            System.out.print("Enter pos\n");
            String input = scan.nextLine();

            char r = input.charAt(0);
            r = Character.toUpperCase(r);
            int rank = r - 'A' + 1;

            char c = input.charAt(1);
            int file = Character.getNumericValue(c);
            scan.close();
            if (rank >= 1 && rank <= 8 && file >= 1 && file <= 8) {

                return file * 10 + rank;
            } else {
                System.out.print("unvalid num\n ");
            }
        }
    }

    public void printBoard() {
        System.out.print("  A B C D E F G H\n");

        for (int rank = 1; rank <= 8; rank++) {
            System.out.print(rank);
            System.out.print(" ");
            for (int file = 1; file <= 8; file++) {
                switch (board[rank * 10 + file]) {
                case BLACK:
                    System.out.print("o ");
                    break;
                case WHITE:
                    System.out.print("x ");
                    break;
                case EMPTY:
                    System.out.print("- ");
                    break;
                default:
                    System.out.print("# ");
                    break;
                }
            }
            System.out.print("\n");
        }

    }

    public int[] makeLegalBoard() {
        int[] tmp_board = new int[ENTITY];

        int player = turn == BLACK ? BLACK : WHITE;

        for (int rank = 1; rank <= 8; rank++) {
            for (int file = 1; file <= 8; file++) {
                int pos = rank * 10 + file;
                if (this.canFlipLines(pos, player)) {
                    tmp_board[pos] = 1;
                }
            }
        }

        return tmp_board;
    }

    public boolean canFlipLines(int pos, int player) {
        if (board[pos] != EMPTY) {
            return false;
        }

        for (int dir : DIRS) {
            if (this.canFlipLine(pos, player, dir)) {
                return true;
            }
        }

        return false;
    }

    public boolean canFlipLine(int pos, int player, int dir) {

        int opponent = 3 - player;
        boolean isFliped = false;
        int curr_pos = pos;

        while (true) {
            curr_pos += dir;

            if (board[curr_pos] == opponent) {
                isFliped = true;
                continue;
            }

            if (board[curr_pos] == player && isFliped) {
                return true;
            }

            break;
        }

        return false;

    }

    public void flipLines(int pos) {
        for (int dir : DIRS) {
            this.flipLine(pos, dir);
        }

        board[pos] = turn;
    }

    public void flipLine(int pos, int dir) {
        int opponent = 3 - turn;
        int curr_pos = pos;
        ArrayList<Integer> flip_pos = new ArrayList<>(0);

        while (true) {
            curr_pos += dir;

            if (board[curr_pos] == opponent) {
                flip_pos.add(curr_pos);
                continue;
            }

            if (board[curr_pos] == turn) {
                for (int i = 0, size = flip_pos.size(); i < size; i++) {
                    curr_pos = flip_pos.get(i);
                    board[curr_pos] = turn;
                }
            }

            break;
        }
    }

    public void changeTurn() {
        turn = 3 - turn;
    }

    public static void main(String[] args) {
        Othello othello = new Othello();
        while (true) {

            othello.printBoard();
            int[] legalBoard = othello.makeLegalBoard();
            int pos = othello.stdinPos();

            if (legalBoard[pos] == 1) {
                othello.flipLines(pos);
                othello.changeTurn();
            } else {
                System.out.println("restar");
                continue;
            }

        }

    }

}

WordPressのエディタ関連のフィルター(Hook)をまとめていく

WordPressのエディタ関連のHookをまとめていくよ

wp_editor_settings

wp_editor()の設定を行う前に呼ばれるフィルター。

具体的には、tinymceを有効にするかとか、エディタの高さ、幅、quicktagの有無などを指定できる。

wp_editor_settings | Hook | WordPress Developer Resources

class-wp-editor.php in tags/5.3/src/wp-includes – WordPress Trac

edit_page_form

エディタ画面のメタボックス(右側にある公開ボタンとか、カテゴリーが選べるボックスのやつ)が表示された後に、起動するフィルター。

page(固定)ページのみ発火する。

edit_page_form | Hook | WordPress Developer Resources

edit-form-advanced.php in tags/5.3/src/wp-admin – WordPress Trac

edit_form_advanced

先ほどのedit_page_formの親戚みたいなやつ。固定ページ以外の編集時に発火する。

WordPressのデーターベースのメモ、post dataとかの

そもそも、WordPressのデータベースはどうなっているのか?

データベース構造 - WordPress Codex 日本語版

以上の記事を参考に見ていく。

wp post table

postの基本情報。以下の情報がテーブルに含まれる。

  • content
  • date
  • author
  • post_status
  • post_type

他にも色々あるけど、主要なのは上記の5つ。特に、post_statuspost_typeは以下の値が入る。

post_status

  • 'publish': 公開済み
  • 'pending': ペンディング
  • 'draft': 草稿
  • 'private': プライベート(非公開)
  • 'static':(2.0.x 以前はページ)
  • 'object':
  • 'attachment':
  • 'inherit': 継承(添付ファイル、改訂履歴・自動保存のとき)
  • 'future': 予約投稿

post_type

  • 'post': 投稿
  • 'page': ページ
  • 'attachment': 添付ファイル
  • 'revision': 改訂履歴・自動保存

wp_postmeta の面白い所。

普通、データベースのテーブルのcolumnを追加したい場合は、普通は既存のテーブルにカラムを追加していく。

しかし、WordPressではwp_postmetaと言うテーブルが用意されており、こいつに新しいカラムを追加していく、と言う形式を取っている。

WordPressのデータベースの関連図として、「データベース構造 - WordPress Codex 日本語版」のページが詳しい。

postmetaに関する関数

get post meta

<?php $meta_values = get_post_meta($post_id, $key, $single); ?>

postmetaの値を取得する。

関数リファレンス/get post meta - WordPress Codex 日本語版

update post meta / add post meta

<?php update_post_meta( $post_id, $meta_key, $meta_value, $prev_value ); ?> 

カルムを一行更新するか、なければ追加する。update post metaはカラムが存在しなければadd update metaをするので、基本的にupdateの方が使えば良い。

関数リファレンス/update post meta - WordPress Codex 日本語版

関数リファレンス/add post meta - WordPress Codex 日本語版

特定の記事のカラムを全部取得する

<?php get_post_custom( $post_id ); ?>

関数リファレンス/get post custom - WordPress Codex 日本語版