#artificial-intelligence #computer #strategy #evaluation #go #connect-four #thinking

app kifuwarabe_connect_four

WIP. Connect-four AI. Currently, does not work.

1 unstable release

0.1.0 Sep 1, 2020

#747 in Game dev

MIT license

375KB
3K SLoC

kifuwarabe-connect-four

Note: Currently, the thinking part is not working yet. I don't know if I can make it.
注:現在、思考部はまだ 機能していません 。作れるかわかりません。

Programming computer shogi (Japanese chess) is difficult.
コンピュータ将棋のプログラミングは難しいです。

How can I improve my programming skills?
プログラミングスキルを向上させるにはどうすればよいですか?

A strategy of gradually stepping up from a simple game is not appropriate.
単純なゲームから徐々にステップアップする戦略は適切ではありません。

The reason is that the right program to solve the problem depends entirely on the game. Unless you have general artificial intelligence.
その理由は、問題を解決するための適切なプログラムが完全にゲームに依存しているためです。汎用人工知能を持っていない限り。

I recommend that you write computer shogi software directly, rather than bypassing it.
迂回するのではなく、コンピューター将棋ソフトウェアを直接作成することをお勧めします。

Of course it's best to start by modifying the strongest library. Helps to find out what is missing.
もちろん、最強のライブラリを変更することから始めるのが最善です。 何が欠けているかを知るのに役立ちます。

However, if you want to overcome the weaknesses of basic programming skills unrelated to computer shogi, it makes sense to start with a simple program.
ただし、コンピュータ将棋とは関係のない基本的なプログラミングスキルの弱点を克服したい場合は、単純なプログラムから始めるのが理にかなっています。

Connect-four games, for example, are so vast that they can't be fully explored on home computers, but they're easy to run and their skills are easy to check.
たとえば、四目並べゲームは非常に広大なので、家庭用コンピューターで完全探索することはできませんが、実行は簡単で、スキルのチェックは簡単です。

Computer shogi, computer go program Kifuwarabe has been converted into connect-four.
コンピュータ将棋、コンピュータ囲碁プログラム「きふわらべ」を4目並べに変換しました。

It was implemented with an unspecified UXI protocol that imitates the UCI / USI protocol. X has no meaning.  UCI / USIプロトコルを模した未指定のUXIプロトコルで実装しました。 X に意味はありません。

Come see the repository.
リポジトリをご覧ください。

There is no GUI (Graphical user interface). Thought engine only.
GUIは ありません。思考エンジンのみです。

Run

Note: Not currently a library . It will be converted to a library in a later update.
注: 現在ライブラリではありません 。後のアップデートでライブラリに変換します。

Terminal:

cargo run

In game:

casual_logger   | Remove 0 log file(s).
                | If you don't want this message, set `Log::set_opt(Opt::Release);`.
Hello, world!
こんにちわ、世界!!
Nought=|O|

... Test run. Omitted ...

Kifuwarabe's connect-four
きふわらべのコネクト・フォー

Command:
コマンド:
`do d`      - Mark the d file.
                手番のプレイヤーが、 7 列目に印を付けます。
`go`        - The computer shows the next move.
                コンピューターが次の1手を示します。
`info-off`  - no info output.
                info出力なし。
`info-on`   - There is info output.(Default)
                info出力あり(既定)。
`learn`     - Learning.
                学習。
`pos`       - Position display.
                局面表示。
`position xfen 7/7/7/7/7/7 O moves d c`
            - Starting position and moves.
                初期局面と棋譜を入力。
`uh`        - Step of learning.
                学習のステップ。
`undo`      - 1 back.
                1手戻します。
`uxi`       - Returns 'uxiok connect-four {protocol-version}'. It is a version of the protocol, not software.
                'uxiok connect-four {protocol-version}' を返します。ソフトではなくプロトコルのバージョンです。
`xfen`      - The current xfen string display.
                現局面のxfen文字列表示。

Let's input from `pos`.
`pos` から入力してみましょう。

Input in game:

A file of about 262 KB is automatically generated, so be careful of free disk space.
約262KB のファイルが自動生成されますので、空き容量にご注意ください。

Oops, a large log file will also be output, so make sure you have about 100 MB or more free. It grows bigger and bigger. Erase it manually if you think it is dangerous.
おっと、大きなログファイルも出力されるので、 約100MB以上 の空きがあることを確認してください。 それはますます大きくなります。 危険と思われる場合は手動で消去してください。

learn

Output in game:

... Omitted ...

[Learn]
     | Feature number      | Current evaluation        | Choice way    | Give                           | Take                                | Refund
File | Vert Hori Baro Sini | Vert Hori Baro Sini Total | File   Result | Val  Vert Hori Baro Sini Total | Val  Rest Vert Hori Baro Sini Total | Val  Vert Hori Baro Sini Total
---- + ---- ---- ---- ---- + ---- ---- ---- ---- ----- + ------ ------ + ---- ---- ---- ---- ---- ----- + ---- ---- ---- ---- ---- ---- ----- + ---- ---- ---- ---- ---- -----
   a |    1    8   16    . |    6   57   60    .   123 | a      Win    |    4    4   49   59    .   112 |    7    .    7   57   61    .   125 |    .    7   57   61    .   125
   b |    2    8   17    . |    .   57    2    .    59 | b      Win    |    4    .   49    .    .    49 |    7    .    3   57    2    .    62 |    .    3   57    2    .    62
   c |    3    8   18    . |    3   57   59    .   119 | resign Lose   |    4    1   49   58    .   108 |    .    .    1   57   58    .   116 |    .    1   57   58    .   116
   d |    4    8   19   20 |   69   57   34    5   165 | d      Win    |    4   68   49   33    4   154 |    7    .   70   57   35    5   167 |    .   70   57   35    5   167
   e |    5    8    .   21 |    3   57    .   12    72 | e      Win    |    4    1   49    .   11    61 |    7    .    4   57    .   13    74 |    .    4   57    .   13    74
   f |    6    8    .   22 |    6   57    .   41   104 | resign Lose   |    4    4   49    .   40    93 |    .    .    4   57    .   40   101 |    .    4   57    .   40   101
   g |    7    8    .   23 |    3   57    .    3    63 | resign Lose   |    4    1   49    .    2    52 |    .    .    1   57    .    2    60 |    .    1   57    .    2    60

... Omitted ...

Note: I want a strong thinking part, but I don't think it helps.
The evaluation points are distributed by adding and subtracting integers without using differentiation or real numbers.
注意: 強力な思考部が欲しいのですが、それは 役に立たないと思います。
評価値は、微分や実数を使用せずに、整数を加算および減算することによって分配されます。

Input in game:

pos

Output in game:

[Next 1 piece(s) | Go O]

  +---+---+---+---+---+---+---+ Please select a file. Example `do d`
6 |   |   |   |   |   |   |   | 列を選んでください。例 `do d`
  +---+---+---+---+---+---+---+
5 |   |   |   |   |   |   |   |    a b c d e f g
  +---+---+---+---+---+---+---+
4 |   |   |   |   |   |   |   |
  +---+---+---+---+---+---+---+
3 |   |   |   |   |   |   |   |
  +---+---+---+---+---+---+---+
2 |   |   |   |   |   |   |   |
  +---+---+---+---+---+---+---+
1 |   |   |   |   |   |   |   |
  +---+---+---+---+---+---+---+
    a   b   c   d   e   f   g

Input in game:

do d
pos

Output in game:

[Next 2 piece(s) | Go X]

  +---+---+---+---+---+---+---+ Please select a file. Example `do d`
6 |   |   |   |   |   |   |   | 列を選んでください。例 `do d`
  +---+---+---+---+---+---+---+
5 |   |   |   |   |   |   |   |    a b c d e f g
  +---+---+---+---+---+---+---+
4 |   |   |   |   |   |   |   |
  +---+---+---+---+---+---+---+
3 |   |   |   |   |   |   |   |
  +---+---+---+---+---+---+---+
2 |   |   |   |   |   |   |   |
  +---+---+---+---+---+---+---+
1 |   |   |   | O |   |   |   |
  +---+---+---+---+---+---+---+
    a   b   c   d   e   f   g

Input in game:

go

Output in game:

info json { "nps":     1, "nodes":     1, "push":"f"                                                   , "O":"Search.", "channel1":Win, "choose":"f", "total":475, "a":28, "b":70, "c":69, "d":147, "e":64, "f":59, "g":38, "pv":["d","f"] }
info json { "nps":     2, "nodes":     2, "push":"d"                                                   , "X":"Search.", "channel1":Win, "choose":"d", "total":470, "a":93, "b":30, "c":84, "d":139, "e":42, "f":54, "g":28, "pv":["d","f","d"] }
info json { "nps":     3, "nodes":     3, "push":"d"                                                   , "O":"Search.", "channel1":Win, "choose":"d", "total":463, "a":29, "b":71, "c":54, "d":147, "e":62, "f":61, "g":39, "pv":["d","f","d","d"] }
i

... omitted ...

info string pred_result=Win nps=23
bestmove f

Input in game:

do f
pos

Output in game:

[Next 3 piece(s) | Go O]

  +---+---+---+---+---+---+---+ Please select a file. Example `do d`
6 |   |   |   |   |   |   |   | 列を選んでください。例 `do d`
  +---+---+---+---+---+---+---+
5 |   |   |   |   |   |   |   |    a b c d e f g
  +---+---+---+---+---+---+---+
4 |   |   |   |   |   |   |   |
  +---+---+---+---+---+---+---+
3 |   |   |   |   |   |   |   |
  +---+---+---+---+---+---+---+
2 |   |   |   |   |   |   |   |
  +---+---+---+---+---+---+---+
1 |   |   |   | O |   | X |   |
  +---+---+---+---+---+---+---+
    a   b   c   d   e   f   g

Input in game:

xfen

Output in game:

xfen 7/7/7/7/7/7 O moves d f

Input in game:

position xfen 7/7/7/7/XOXOXOX/OXOXOXO O moves d f
pos

Output in game:

[Next 17 piece(s) | Go O]

  +---+---+---+---+---+---+---+ Please select a file. Example `do d`
6 |   |   |   |   |   |   |   | 列を選んでください。例 `do d`
  +---+---+---+---+---+---+---+
5 |   |   |   |   |   |   |   |    a b c d e f g
  +---+---+---+---+---+---+---+
4 |   |   |   |   |   |   |   |
  +---+---+---+---+---+---+---+
3 |   |   |   | O |   | X |   |
  +---+---+---+---+---+---+---+
2 | X | O | X | O | X | O | X |
  +---+---+---+---+---+---+---+
1 | O | X | O | X | O | X | O |
  +---+---+---+---+---+---+---+
    a   b   c   d   e   f   g

Input in game:

quit

Evaluation design

Does not work. You should make the thinking part.
働きません。 あなたは考える部分を作るべきです。

Elemental features

Numbered the lines that intersect the squares.
マスに交差する線に番号を付けました。

elemental features:

elemental-features.png

features square:

features-square.png

square value:

square-value.png

feature size:

feature-size.png

How to program a connect-four game?

During development, you may need to reproduce the behavior of your computer.
It is difficult to compare the behavior. Instead, it is useful to get the logs and compare the logs.
But logger's difficult to make, so use library.

  • Step 1. Use logger library.
    • Use casual_logger library at 'Cargo.toml', 'main.rs'.
    • Create the 'log.rs' file.
      • Extend the logger.

Let's proceed with development while testing.

  • Step 2. Create the test.rs file.
    • Add little by little as you progress through the steps.

The first thing you have to create is your motive.
It is important to start with the appearance.

  • Step 3. Create the 'main.rs' file and 'look_and_model.rs' file.
    • Engine - Title screen.
    • Piece - "O", "X".
    • Game result - Win/Draw/Lose.
    • Position - It's the board.
    • Search - Computer player search.
    • Search info - Computer player search info.

If you want to play immediately, you have the talent of a game creator.
Being able to control your position means being able to play.

  • Step 4. Create the 'position.rs' file.
    • do_move
    • undo_move
    • opponent

Let's enter commands into the computer. Create a command line parser.

  • Step 5. Create the 'command_line_seek.rs' file.
    • Starts with.
    • Go next to.
    • Rest.

People who are looking for something 10 minutes a day are looking for something for a week in a year.
Before creating the game itself, let's first create the replay function. Let's get it for a week.

  • Step 6. Create the 'uxi_protocol.rs' file.
    • Do. (Before 'From XFEN') Excludes legal moves and winning/losing decisions.
    • To XFEN.
    • From XFEN.
    • Undo.

Let's make a principal command.

  • Step 7. Create the 'engine.rs' file. Command line.
    • position.
    • pos.
    • do.
    • undo.
    • uxi.
    • xfen.
  • Step 8. 'src/main.rs' or 'examples/main.rs'.

Before you make a computer player, let's judge the outcome. And let's test.

  • Step 9. 'win_lose_judgment.rs'
    • Win.
    • Draw - Not win, not lose, can not play.
    • Lose. - Not win is lose.

Before creating a computer player, let's create a mechanism to measure performance.

  • Step 10. 'performance_measurement.rs'
    • Seconds. - Stopwatch.
    • Node per second.

Let's make a computer player.

  • Step 11. 'computer_player/search.rs'
    • Search.
  • 'main.py'
    • Create "go" command.

Let's larning a computer thinking.

  • Step 12. 'computer_player/evaluation.rs'
    • Evaluation - None.
      • ways_weight.
      • Save.
      • Load.
      • Get value.
      • Set value.
      • Print some feature.
  • Step 13. computer_player/learn.rs

Finally.

  • Remeve all 'TODO' tasks. Examples: '// TODO Write a code here.'

Dependencies

~4–5MB
~83K SLoC