00001 #include "osl/record/kanjiPrint.h"
00002 #include "osl/record/kanjiCode.h"
00003 #include "osl/ptype.h"
00004 #include "osl/pieceStand.h"
00005 #include "osl/move.h"
00006 #include "osl/misc/carray.h"
00007 #include <boost/foreach.hpp>
00008 #include <iostream>
00009 #include <string>
00010 #include <algorithm>
00011 #include <cctype>
00012
00013 bool caseInsentiveCharCompare(char a, char b)
00014 {
00015 return toupper(a) == toupper(b);
00016 }
00017
00018 bool caseInsentiveCompare(const std::string& s1, const std::string& s2)
00019 {
00020 return s1.size() == s2.size() &&
00021 equal(s1.begin(), s1.end(), s2.begin(), caseInsentiveCharCompare);
00022 }
00023
00024 const osl::record::Color osl::record::Color::NONE ("", "NONE", false);
00025 const osl::record::Color osl::record::Color::Black ("0;30", "BLACK");
00026 const osl::record::Color osl::record::Color::Red ("0;31", "RED");
00027 const osl::record::Color osl::record::Color::Green ("0;32", "GREEN");
00028 const osl::record::Color osl::record::Color::Brown ("0;33", "BROWN");
00029 const osl::record::Color osl::record::Color::Blue ("0;34", "BLUE");
00030 const osl::record::Color osl::record::Color::Purple ("0;35", "PURPLE");
00031 const osl::record::Color osl::record::Color::Cyan ("0;36", "CYAN");
00032 const osl::record::Color osl::record::Color::LightGray ("0;37", "LIGHTGRAY");
00033 const osl::record::Color osl::record::Color::DarkGray ("1;30", "DARKGRAY");
00034 const osl::record::Color osl::record::Color::LightRed ("1;31", "LIGHTRED");
00035 const osl::record::Color osl::record::Color::LightGreen ("1;32", "LIGHTGREEN");
00036 const osl::record::Color osl::record::Color::Yellow ("1;33", "YELLOW");
00037 const osl::record::Color osl::record::Color::LightBlue ("1;34", "LIGHTBLUE");
00038 const osl::record::Color osl::record::Color::LightPurple ("1;35", "LIGHTPURPLE");
00039 const osl::record::Color osl::record::Color::LightCyan ("1;36", "LIGHTCYAN");
00040 const osl::record::Color osl::record::Color::White ("1;37", "WHITE");
00041
00042 const osl::record::Color osl::record::
00043 Color::colorFor(const std::string& str)
00044 {
00045 static const CArray<const osl::record::Color,17> colors = {{
00046 osl::record::Color::NONE,
00047 osl::record::Color::Black, osl::record::Color::Red,
00048 osl::record::Color::Green, osl::record::Color::Brown,
00049 osl::record::Color::Blue, osl::record::Color::Purple,
00050 osl::record::Color::Cyan, osl::record::Color::LightGray,
00051 osl::record::Color::DarkGray, osl::record::Color::LightRed,
00052 osl::record::Color::LightGreen, osl::record::Color::Yellow,
00053 osl::record::Color::LightBlue, osl::record::Color::LightPurple,
00054 osl::record::Color::LightCyan, osl::record::Color::White}};
00055
00056 BOOST_FOREACH(const record::Color& c, colors)
00057 {
00058 if (caseInsentiveCompare(str, c.getName()))
00059 return c;
00060 }
00061 return osl::record::Color::NONE;
00062 }
00063
00064 osl::record::Color::Color(const std::string& value, const std::string& name, const bool valid)
00065 : value(value), name(name), valid(valid)
00066 {
00067 }
00068 osl::record::Color::~Color()
00069 {
00070 }
00071
00072 osl::record::Characters::~Characters()
00073 {
00074 }
00075
00076 const osl::misc::CArray<std::string,32> osl::record::Characters::stand =
00077 {{
00078 K_NAKAGURO, "+E",
00079 K_PPAWN, K_PLANCE, K_PKNIGHT, K_PSILVER, K_PBISHOP, K_PROOK, K_KING,
00080 K_GOLD, K_PAWN, K_LANCE, K_KNIGHT, K_SILVER, K_BISHOP, K_ROOK,
00081
00082 K_NAKAGURO, "+E",
00083 K_PPAWN, K_PLANCE, K_PKNIGHT, K_PSILVER, K_PBISHOP, K_PROOK, K_KING,
00084 K_GOLD, K_PAWN, K_LANCE, K_KNIGHT, K_SILVER, K_BISHOP, K_ROOK
00085 }};
00086
00087 const osl::misc::CArray<std::string,10> osl::record::StandardCharacters::dan =
00088 {{"", K_K1, K_K2, K_K3, K_K4, K_K5, K_K6, K_K7, K_K8, K_K9}};
00089 const osl::misc::CArray<std::string,10> osl::record::StandardCharacters::suji =
00090 {{"", K_R1, K_R2, K_R3, K_R4, K_R5, K_R6, K_R7, K_R8, K_R9}};
00091 const osl::misc::CArray<std::string,32> osl::record::StandardCharacters::pieces =
00092 {{
00093 K_NAKAGURO, "+E",
00094 K_PPAWN, K_PLANCE, K_PKNIGHT, K_PSILVER, K_PBISHOP, K_PROOK, K_KING,
00095 K_GOLD, K_PAWN, K_LANCE, K_KNIGHT, K_SILVER, K_BISHOP, K_ROOK,
00096
00097 K_NAKAGURO, "+E",
00098 K_PPAWN, K_PLANCE, K_PKNIGHT, K_PSILVER, K_PBISHOP, K_PROOK, K_KING,
00099 K_GOLD, K_PAWN, K_LANCE, K_KNIGHT, K_SILVER, K_BISHOP, K_ROOK
00100 }};
00101
00102 const osl::misc::CArray<std::string,10> osl::record::RussianCharacters::dan = osl::record::StandardCharacters::dan;
00103 const osl::misc::CArray<std::string,10> osl::record::RussianCharacters::suji = osl::record::StandardCharacters::suji;
00104 const osl::misc::CArray<std::string,32> osl::record::RussianCharacters::pieces =
00105 {{
00106 K_NAKAGURO, "+E",
00107 K_PPAWN_R, K_PLANCE_R, K_PKNIGHT_R, K_PSILVER_R, K_PBISHOP_R, K_PROOK_R, K_KING_R,
00108 K_GOLD_R, K_PAWN_R, K_LANCE_R, K_KNIGHT_R, K_SILVER_R, K_BISHOP_R, K_ROOK_R,
00109
00110 K_NAKAGURO, "+E",
00111 K_PPAWN, K_PLANCE, K_PKNIGHT, K_PSILVER, K_PBISHOP, K_PROOK, K_KING,
00112 K_GOLD, K_PAWN, K_LANCE, K_KNIGHT, K_SILVER, K_BISHOP, K_ROOK
00113 }};
00114
00115 const osl::misc::CArray<std::string,10> osl::record::KIFCharacters::dan =
00116 osl::record::StandardCharacters::dan;
00117 const osl::misc::CArray<std::string,10> osl::record::KIFCharacters::suji =
00118 {{"", " "+K_R1, " "+K_R2, " "+K_R3, " "+K_R4, " "+K_R5, " "+K_R6, " "+K_R7, " "+K_R8, " "+K_R9}};
00119 const osl::misc::CArray<std::string,32> osl::record::KIFCharacters::pieces =
00120 {{
00121 " "+K_NAKAGURO, "+E",
00122 "v"+K_PPAWN, "v"+K_PLANCE, "v"+K_PKNIGHT, "v"+K_PSILVER, "v"+K_PBISHOP, "v"+K_PROOK, "v"+K_KING,
00123 "v"+K_GOLD, "v"+K_PAWN, "v"+K_LANCE, "v"+K_KNIGHT, "v"+K_SILVER, "v"+K_BISHOP, "v"+K_ROOK,
00124
00125 " "+K_NAKAGURO, "+E",
00126 " "+K_PPAWN, " "+K_PLANCE, " "+K_PKNIGHT, " "+K_PSILVER, " "+K_PBISHOP, " "+K_PROOK, " "+K_KING,
00127 " "+K_GOLD, " "+K_PAWN, " "+K_LANCE, " "+K_KNIGHT, " "+K_SILVER, " "+K_BISHOP, " "+K_ROOK
00128 }};
00129
00130
00131 std::ostream& osl::record::
00132 operator<<(std::ostream& os, const Color& c)
00133 {
00134 return os << c.value;
00135 }
00136
00137 void osl::record::ChangeShellColor::
00138 escColSet() const
00139 {
00140 if (!color.isValid()) return;
00141 os << "\033[" << color << "m";
00142 }
00143
00144 void osl::record::ChangeShellColor::
00145 escColReSet() const
00146 {
00147 if (!color.isValid()) return;
00148 os << "\033[0m";
00149 }
00150
00151 void osl::record::KanjiPrint::
00152 print(const osl::state::SimpleState& state,
00153 const osl::Move *last_move) const
00154 {
00155 os << K_WHITE_STAND << " ";
00156 BOOST_FOREACH(Ptype ptype, PieceStand::order)
00157 {
00158 const int count = state.countPiecesOnStand(WHITE, ptype);
00159 if (count)
00160 os << pieces->stand_kanji(newPtypeO(BLACK, ptype))
00161 << count << " ";
00162 }
00163 os << std::endl;
00164
00165 os << " ";
00166 for(int x=9;x>0;x--)
00167 {
00168 os << pieces->getSuji(x);
00169 }
00170 os << std::endl;
00171
00172 os << "+";
00173 for(int x=9*pieces->getSuji(1).size();x>0;x--)
00174 {
00175 os << "-";
00176 }
00177 os << "+" << std::endl;
00178
00179 for(int y=1;y<=9;y++)
00180 {
00181 os << '|';
00182 for(int x=9;x>0;x--)
00183 {
00184 const PtypeO ptypeo = state.getPieceOnBoard(Position(x,y)).ptypeO();
00185 if (last_move &&
00186 !last_move->isInvalid() &&
00187 last_move->to() == Position(x,y))
00188 {
00189 ChangeShellColor csc(os, last_move_color);
00190 os << pieces->kanji(ptypeo);
00191 }
00192 else if (isPiece(ptypeo)) {
00193 Player owner = getOwner(ptypeo);
00194 osl::record::Color color;
00195 if (owner == BLACK)
00196 color = black_color;
00197 else
00198 color = white_color;
00199 ChangeShellColor csc(os, color);
00200 os << pieces->kanji(ptypeo);
00201 }
00202 else
00203 {
00204 os << pieces->kanji(ptypeo);
00205 }
00206 }
00207 os << '|';
00208 os << pieces->getDan(y);
00209 os << std::endl;
00210 }
00211
00212 os << "+";
00213 for(int x=9*pieces->getSuji(1).size();x>0;x--)
00214 {
00215 os << "-";
00216 }
00217 os << "+" << std::endl;
00218
00219
00220 os << K_BLACK_STAND << " ";
00221 BOOST_FOREACH(Ptype ptype, PieceStand::order)
00222 {
00223 const int count = state.countPiecesOnStand(BLACK, ptype);
00224 if (count)
00225 os << pieces->stand_kanji(newPtypeO(BLACK, ptype))
00226 << count << " ";
00227 }
00228 os << std::endl;
00229 os << state.getTurn() << std::endl;
00230 }
00231
00232
00233
00234
00235
00236