My Project
kanjiPrint.cc
Go to the documentation of this file.
5#include <iostream>
6#include <string>
7#include <algorithm>
8#include <cctype>
9
10bool caseInsentiveCharCompare(char a, char b)
11{
12 return toupper(a) == toupper(b);
13}
14
15bool caseInsentiveCompare(const std::string& s1, const std::string& s2)
16{
17 return s1.size() == s2.size() &&
18 equal(s1.begin(), s1.end(), s2.begin(), caseInsentiveCharCompare);
19}
20
21const osl::record::Color osl::record::Color::NONE ("", "NONE", false);
22const osl::record::Color osl::record::Color::Black ("0;30", "BLACK");
24const osl::record::Color osl::record::Color::Green ("0;32", "GREEN");
25const osl::record::Color osl::record::Color::Brown ("0;33", "BROWN");
26const osl::record::Color osl::record::Color::Blue ("0;34", "BLUE");
27const osl::record::Color osl::record::Color::Purple ("0;35", "PURPLE");
28const osl::record::Color osl::record::Color::Cyan ("0;36", "CYAN");
29const osl::record::Color osl::record::Color::LightGray ("0;37", "LIGHTGRAY");
30const osl::record::Color osl::record::Color::DarkGray ("1;30", "DARKGRAY");
31const osl::record::Color osl::record::Color::LightRed ("1;31", "LIGHTRED");
32const osl::record::Color osl::record::Color::LightGreen ("1;32", "LIGHTGREEN");
33const osl::record::Color osl::record::Color::Yellow ("1;33", "YELLOW");
34const osl::record::Color osl::record::Color::LightBlue ("1;34", "LIGHTBLUE");
35const osl::record::Color osl::record::Color::LightPurple ("1;35", "LIGHTPURPLE");
36const osl::record::Color osl::record::Color::LightCyan ("1;36", "LIGHTCYAN");
37const osl::record::Color osl::record::Color::White ("1;37", "WHITE");
38
59
60osl::record::Color::Color(const std::string& value, const std::string& name, const bool valid)
61 : value(value), name(name), valid(valid)
62{
63}
67
68std::string osl::record::
69kanjiNumber(const int n)
70{
71 assert((1 <= n) && (n <= 18));
72 switch(n) {
73 case 1: return K_K1;
74 case 2: return K_K2;
75 case 3: return K_K3;
76 case 4: return K_K4;
77 case 5: return K_K5;
78 case 6: return K_K6;
79 case 7: return K_K7;
80 case 8: return K_K8;
81 case 9: return K_K9;
82 case 10: return K_K10;
83 case 11: return K_K11;
84 case 12: return K_K12;
85 case 13: return K_K13;
86 case 14: return K_K14;
87 case 15: return K_K15;
88 case 16: return K_K16;
89 case 17: return K_K17;
90 case 18: return K_K18;
91 }
92 assert(false);
93 return "";
94}
95
99
101 {{// WHITE
102 K_NAKAGURO, "+E",
105 // BLACK
106 K_NAKAGURO, "+E",
109 }};
110
112 {{"", K_K1, K_K2, K_K3, K_K4, K_K5, K_K6, K_K7, K_K8, K_K9}};
114 {{"", K_R1, K_R2, K_R3, K_R4, K_R5, K_R6, K_R7, K_R8, K_R9}};
116 {{// WHITE
117 K_NAKAGURO, "+E",
120 // BLACK
121 K_NAKAGURO, "+E",
124 }};
125
129 {{// WHITE
130 K_NAKAGURO, "+E",
133 // BLACK
134 K_NAKAGURO, "+E",
137 }};
138
142 {{"", " " K_R1, " " K_R2, " " K_R3, " " K_R4, " " K_R5, " " K_R6, " " K_R7, " " K_R8, " " K_R9}};
144 {{// WHITE
145 " " K_NAKAGURO, "+E",
146 "v" K_PPAWN, "v" K_PLANCE, "v" K_PKNIGHT, "v" K_PSILVER, "v" K_PBISHOP, "v" K_PROOK, "v" K_KING,
147 "v" K_GOLD, "v" K_PAWN, "v" K_LANCE, "v" K_KNIGHT, "v" K_SILVER, "v" K_BISHOP, "v" K_ROOK,
148 // BLACK
149 " " K_NAKAGURO, "+E",
150 " " K_PPAWN, " " K_PLANCE, " " K_PKNIGHT, " " K_PSILVER, " " K_PBISHOP, " " K_PROOK, " " K_KING,
151 " " K_GOLD, " " K_PAWN, " " K_LANCE, " " K_KNIGHT, " " K_SILVER, " " K_BISHOP, " " K_ROOK
152 }};
153
154
155std::ostream& osl::record::
156operator<<(std::ostream& os, const Color& c)
157{
158 return os << c.value;
159}
160
162escColSet() const
163{
164 if (!color.isValid()) return;
165 os << "\033[" << color << "m"; //文字の属性をセットする
166}
167
169escColReSet() const
170{
171 if (!color.isValid()) return;
172 os << "\033[0m";
173}
174
176print(const SimpleState& state,
177 const Move *last_move) const
178{
179 os << misc::eucToLang(K_WHITE_STAND) << " ";
180 for (Ptype ptype: PieceStand::order) {
181 const int count = state.countPiecesOnStand(WHITE, ptype);
182 if (count)
183 os << misc::eucToLang(pieces->stand_kanji(newPtypeO(BLACK, ptype)))
184 << count << " ";
185 }
186 os << std::endl;
187
188 os << " ";
189 for(int x=9;x>0;x--)
190 {
191 os << misc::eucToLang(pieces->getSuji(x));
192 }
193 os << std::endl;
194
195 os << "+";
196 for(int x=9*pieces->getSuji(1).size();x>0;x--)
197 {
198 os << "-";
199 }
200 os << "+" << std::endl;
201
202 for(int y=1;y<=9;y++)
203 {
204 os << '|';
205 for(int x=9;x>0;x--)
206 {
207 const PtypeO ptypeo = state.pieceOnBoard(Square(x,y)).ptypeO();
208 const std::string piece = misc::eucToLang(pieces->kanji(ptypeo));
209 if (last_move &&
210 !last_move->isInvalid() &&
211 last_move->to() == Square(x,y))
212 {
213 ChangeShellColor csc(os, last_move_color);
214 os << piece;
215 } // csc destroyed
216 else if (isPiece(ptypeo)) {
217 Player owner = getOwner(ptypeo);
218 osl::record::Color color;
219 if (owner == BLACK)
220 color = black_color;
221 else
222 color = white_color;
223 ChangeShellColor csc(os, color);
224 os << piece;
225 } // csc destroyed
226 else
227 { // empty space
228 os << piece;
229 }
230 }
231 os << '|';
232 os << misc::eucToLang(pieces->getDan(y));
233
234 os << std::endl;
235 }
236
237 os << "+";
238 for(int x=9*pieces->getSuji(1).size();x>0;x--)
239 {
240 os << "-";
241 }
242 os << "+" << std::endl;
243
244 // 持ち駒の表示
245 os << misc::eucToLang(K_BLACK_STAND) << " ";
246 for (Ptype ptype: PieceStand::order) {
247 const int count = state.countPiecesOnStand(BLACK, ptype);
248 if (count)
249 os << misc::eucToLang(pieces->stand_kanji(newPtypeO(BLACK, ptype)))
250 << count << " ";
251 }
252 os << std::endl;
253 os << state.turn() << std::endl;
254}
255
256/* ------------------------------------------------------------------------- */
257// ;;; Local Variables:
258// ;;; mode:c++
259// ;;; c-basic-offset:2
260// ;;; End:
圧縮していない moveの表現 .
bool isInvalid() const
state に apply 可能でない場合にtrue
const Square to() const
static const CArray< Ptype, 7 > order
持駒の表示で良く使われる順番.
const Piece pieceOnBoard(Square sq) const
Player turn() const
int countPiecesOnStand(Player pl, Ptype ptype) const
持駒の枚数を数える
shellの文字出力にて、色を変える。
Definition kanjiPrint.h:78
static const CArray< std::string, 32 > stand
Definition kanjiPrint.h:100
カラーコードを保持するクラス。
Definition kanjiPrint.h:23
std::string value
Definition kanjiPrint.h:29
static const Color Red
Definition kanjiPrint.h:54
static const Color Cyan
Definition kanjiPrint.h:59
static const Color Yellow
Definition kanjiPrint.h:64
static const Color Brown
Definition kanjiPrint.h:56
static const Color Purple
Definition kanjiPrint.h:58
static const Color White
Definition kanjiPrint.h:68
static const Color colorFor(const std::string &str)
文字列に対応するColor objectを返す
Definition kanjiPrint.cc:40
static const Color LightGreen
Definition kanjiPrint.h:63
static const Color LightCyan
Definition kanjiPrint.h:67
static const Color Black
Definition kanjiPrint.h:53
static const Color LightGray
Definition kanjiPrint.h:60
static const Color Blue
Definition kanjiPrint.h:57
static const Color LightBlue
Definition kanjiPrint.h:65
static const Color DarkGray
Definition kanjiPrint.h:61
static const Color Green
Definition kanjiPrint.h:55
static const Color LightRed
Definition kanjiPrint.h:62
static const Color NONE
色指定しない(デフォルトのまま)ことを示す特別なオブジェクト
Definition kanjiPrint.h:52
static const Color LightPurple
Definition kanjiPrint.h:66
void print(const SimpleState &state, const Move *last_move=NULL) const
出力
#define K_PSILVER
Definition kanjiCode.h:74
#define K_KNIGHT
Definition kanjiCode.h:67
#define K_NAKAGURO
Definition kanjiCode.h:16
#define K_K18
Definition kanjiCode.h:44
#define K_K13
Definition kanjiCode.h:39
#define K_R4
Definition kanjiCode.h:21
#define K_BLACK_STAND
Definition kanjiCode.h:95
#define K_R2
Definition kanjiCode.h:19
#define K_PROOK_R
Definition kanjiCode.h:87
#define K_R5
Definition kanjiCode.h:22
#define K_PLANCE
Definition kanjiCode.h:64
#define K_PLANCE_R
Definition kanjiCode.h:65
#define K_GOLD
Definition kanjiCode.h:77
#define K_K3
Definition kanjiCode.h:29
#define K_K14
Definition kanjiCode.h:40
#define K_LANCE
Definition kanjiCode.h:62
#define K_K7
Definition kanjiCode.h:33
#define K_K15
Definition kanjiCode.h:41
#define K_ROOK
Definition kanjiCode.h:83
#define K_KING
Definition kanjiCode.h:88
#define K_PAWN
Definition kanjiCode.h:58
#define K_K11
Definition kanjiCode.h:37
#define K_R8
Definition kanjiCode.h:25
#define K_K8
Definition kanjiCode.h:34
#define K_KNIGHT_R
Definition kanjiCode.h:68
#define K_K9
Definition kanjiCode.h:35
#define K_R7
Definition kanjiCode.h:24
#define K_PPAWN_R
Definition kanjiCode.h:61
#define K_SILVER_R
Definition kanjiCode.h:73
#define K_PSILVER_R
Definition kanjiCode.h:75
#define K_PPAWN
Definition kanjiCode.h:60
#define K_K4
Definition kanjiCode.h:30
#define K_GOLD_R
Definition kanjiCode.h:78
#define K_WHITE_STAND
Definition kanjiCode.h:96
#define K_LANCE_R
Definition kanjiCode.h:63
#define K_PKNIGHT_R
Definition kanjiCode.h:70
#define K_BISHOP
Definition kanjiCode.h:79
#define K_PBISHOP
Definition kanjiCode.h:81
#define K_K5
Definition kanjiCode.h:31
#define K_K1
Definition kanjiCode.h:27
#define K_PKNIGHT
Definition kanjiCode.h:69
#define K_K10
Definition kanjiCode.h:36
#define K_R6
Definition kanjiCode.h:23
#define K_SILVER
Definition kanjiCode.h:72
#define K_K17
Definition kanjiCode.h:43
#define K_PROOK
Definition kanjiCode.h:85
#define K_KING_R
Definition kanjiCode.h:90
#define K_ROOK_R
Definition kanjiCode.h:84
#define K_R3
Definition kanjiCode.h:20
#define K_R9
Definition kanjiCode.h:26
#define K_PBISHOP_R
Definition kanjiCode.h:82
#define K_R1
Definition kanjiCode.h:18
#define K_K2
Definition kanjiCode.h:28
#define K_PAWN_R
Definition kanjiCode.h:59
#define K_K6
Definition kanjiCode.h:32
#define K_K16
Definition kanjiCode.h:42
#define K_BISHOP_R
Definition kanjiCode.h:80
#define K_K12
Definition kanjiCode.h:38
bool caseInsentiveCharCompare(char a, char b)
Definition kanjiPrint.cc:10
bool caseInsentiveCompare(const std::string &s1, const std::string &s2)
Definition kanjiPrint.cc:15
std::string eucToLang(const std::string &src)
Definition eucToLang.cc:14
std::string kanjiNumber(const int n)
漢数字を返す(持ち駒の数などで用いる)
Definition kanjiPrint.cc:69
std::ostream & operator<<(std::ostream &os, const Color &c)
Ptype
駒の種類を4ビットでコード化する
Definition basic_type.h:84
Player getOwner(PtypeO ptypeO)
Definition basic_type.h:256
Player
Definition basic_type.h:8
@ WHITE
Definition basic_type.h:10
@ BLACK
Definition basic_type.h:9
constexpr bool isPiece(Ptype ptype)
ptypeが空白やEDGEでないかのチェック
Definition basic_type.h:120
PtypeO
Player + Ptype [-15, 15] PtypeO の O は Owner の O.
Definition basic_type.h:199
PtypeO newPtypeO(Player player, Ptype ptype)
Definition basic_type.h:211
static const CArray< std::string, 10 > dan
Definition kanjiPrint.h:172
static const CArray< std::string, 32 > pieces
Definition kanjiPrint.h:143
static const CArray< std::string, 10 > suji
Definition kanjiPrint.h:141
static const CArray< std::string, 10 > suji
Definition kanjiPrint.h:161
static const CArray< std::string, 10 > dan
Definition kanjiPrint.h:160
static const CArray< std::string, 32 > pieces
Definition kanjiPrint.h:128
static const CArray< std::string, 32 > pieces
盤面上の駒の漢字文字
Definition kanjiPrint.h:115
static const CArray< std::string, 10 > dan
段数の文字
Definition kanjiPrint.h:111
static const CArray< std::string, 10 > suji
筋の文字。駒の文字幅に合わせる必要がある。
Definition kanjiPrint.h:113