/
kifuwarabe.go
131 lines (110 loc) · 3.49 KB
/
kifuwarabe.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package take6
import (
"bufio"
"flag"
"fmt"
"os"
"path/filepath"
"strings"
l "github.com/muzudho/go-logger"
)
// MainLoop - 開始。
func MainLoop() {
// Working directory
dwd, err := os.Getwd()
if err != nil {
// ここでは、ログはまだ設定できてない
panic(fmt.Sprintf("...Engine DefaultWorkingDirectory=%s", dwd))
}
// コマンドライン引数登録
workdir := flag.String("workdir", dwd, "Working directory path.")
// コマンドライン引数解析
flag.Parse()
engineConfPath := filepath.Join(*workdir, "input/take1/engine.conf.toml")
// グローバル変数の作成
G = *new(Variables)
tracePath := filepath.Join(*workdir, "output/trace.log")
debugPath := filepath.Join(*workdir, "output/debug.log")
infoPath := filepath.Join(*workdir, "output/info.log")
noticePath := filepath.Join(*workdir, "output/notice.log")
warnPath := filepath.Join(*workdir, "output/warn.log")
errorPath := filepath.Join(*workdir, "output/error.log")
fatalPath := filepath.Join(*workdir, "output/fatal.log")
printPath := filepath.Join(*workdir, "output/print.log")
// ロガーの作成。
// TODO ディレクトリが存在しなければ、強制終了します。
G.Log = *l.NewLogger(
tracePath,
debugPath,
infoPath,
noticePath,
warnPath,
errorPath,
fatalPath,
printPath)
// 既存のログ・ファイルを削除
G.Log.RemoveAllOldLogs()
// ログ・ファイルの開閉
err = G.Log.OpenAllLogs()
if err != nil {
// ログ・ファイルを開くのに失敗したのだから、ログ・ファイルへは書き込めません
panic(err)
}
defer G.Log.CloseAllLogs()
G.Log.Trace("Start Take1\n")
G.Log.Trace("engineConfPath=%s\n", engineConfPath)
// チャッターの作成。 標準出力とロガーを一緒にしただけです。
G.Chat = *l.NewChatter(G.Log)
G.StderrChat = *l.NewStderrChatter(G.Log)
// 設定ファイル読込。ファイルが存在しなければ強制終了してしまうので注意!
config, err := LoadEngineConf(engineConfPath)
if err != nil {
panic(G.Log.Fatal(fmt.Sprintf("engineConfPath=[%s] err=[%s]", engineConfPath, err)))
}
// 何か標準入力しろだぜ☆(^~^)
scanner := bufio.NewScanner(os.Stdin)
G.Log.FlushAllLogs()
var pPos = NewPosition()
MainLoop:
for scanner.Scan() {
command := scanner.Text()
G.Log.Trace("command=%s\n", command)
tokens := strings.Split(command, " ")
switch tokens[0] {
case "usi":
G.Chat.Print("id name %s\n", config.Profile.Name)
G.Chat.Print("id author %s\n", config.Profile.Author)
G.Chat.Print("usiok\n")
case "isready":
G.Chat.Print("readyok\n")
case "usinewgame":
case "position":
// TODO position うわっ、大変だ(^~^)
pPos.ReadPosition(command)
case "go":
bestmove := Search(pPos)
G.Chat.Print("bestmove %s\n", bestmove.ToCode())
case "quit":
break MainLoop
case "pos":
// 局面表示しないと、データが合ってんのか分からないからな(^~^)
G.Chat.Debug(pPos.Sprint())
case "do":
// 1手指すぜ(^~^)
// 前の空白を読み飛ばしたところから、指し手文字列の終わりまで読み進めるぜ(^~^)
i := 3
var move, err = ParseMove(command, &i, pPos.Phase)
if err != nil {
fmt.Println(pPos.Sprint())
panic(err)
}
pPos.DoMove(move)
case "undo":
// 棋譜を頼りに1手戻すぜ(^~^)
pPos.UndoMove()
}
G.Log.FlushAllLogs()
}
G.Log.Trace("Finished\n")
G.Log.FlushAllLogs()
}