kanjiPrint.cc
Go to the documentation of this file.
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 "osl/misc/iconvConvert.h"
00008 #include <boost/foreach.hpp>
00009 #include <iostream>
00010 #include <string>
00011 #include <algorithm>
00012 #include <cctype>
00013 
00014 bool caseInsentiveCharCompare(char a, char b)
00015 {
00016   return toupper(a) == toupper(b);
00017 }
00018 
00019 bool caseInsentiveCompare(const std::string& s1, const std::string& s2)
00020 {
00021   return s1.size() == s2.size() &&
00022          equal(s1.begin(), s1.end(), s2.begin(), caseInsentiveCharCompare);
00023 }
00024 
00025 const osl::record::Color osl::record::Color::NONE        ("", "NONE", false);
00026 const osl::record::Color osl::record::Color::Black       ("0;30", "BLACK");
00027 const osl::record::Color osl::record::Color::Red         ("0;31", "RED");
00028 const osl::record::Color osl::record::Color::Green       ("0;32", "GREEN");
00029 const osl::record::Color osl::record::Color::Brown       ("0;33", "BROWN");
00030 const osl::record::Color osl::record::Color::Blue        ("0;34", "BLUE");
00031 const osl::record::Color osl::record::Color::Purple      ("0;35", "PURPLE");
00032 const osl::record::Color osl::record::Color::Cyan        ("0;36", "CYAN");
00033 const osl::record::Color osl::record::Color::LightGray   ("0;37", "LIGHTGRAY");
00034 const osl::record::Color osl::record::Color::DarkGray    ("1;30", "DARKGRAY");
00035 const osl::record::Color osl::record::Color::LightRed    ("1;31", "LIGHTRED");
00036 const osl::record::Color osl::record::Color::LightGreen  ("1;32", "LIGHTGREEN");
00037 const osl::record::Color osl::record::Color::Yellow      ("1;33", "YELLOW");
00038 const osl::record::Color osl::record::Color::LightBlue   ("1;34", "LIGHTBLUE");
00039 const osl::record::Color osl::record::Color::LightPurple ("1;35", "LIGHTPURPLE");
00040 const osl::record::Color osl::record::Color::LightCyan   ("1;36", "LIGHTCYAN");
00041 const osl::record::Color osl::record::Color::White       ("1;37", "WHITE");
00042 
00043 const osl::record::Color osl::record::
00044 Color::colorFor(const std::string& str)
00045 {
00046   static const CArray<const osl::record::Color,17> colors = {{
00047     osl::record::Color::NONE,
00048     osl::record::Color::Black, osl::record::Color::Red, 
00049     osl::record::Color::Green, osl::record::Color::Brown, 
00050     osl::record::Color::Blue, osl::record::Color::Purple, 
00051     osl::record::Color::Cyan, osl::record::Color::LightGray, 
00052     osl::record::Color::DarkGray, osl::record::Color::LightRed,
00053     osl::record::Color::LightGreen, osl::record::Color::Yellow, 
00054     osl::record::Color::LightBlue, osl::record::Color::LightPurple, 
00055     osl::record::Color::LightCyan, osl::record::Color::White}};
00056 
00057   BOOST_FOREACH(const record::Color& c, colors)
00058   {
00059     if (caseInsentiveCompare(str, c.getName()))
00060       return c;
00061   }
00062   return osl::record::Color::NONE;
00063 }
00064 
00065 osl::record::Color::Color(const std::string& value, const std::string& name, const bool valid)
00066   : value(value), name(name), valid(valid) 
00067 {
00068 }
00069 osl::record::Color::~Color()
00070 {
00071 }
00072 
00073 std::string osl::record::
00074 kanjiNumber(const int n)
00075 {
00076   assert((1 <= n) && (n <= 18));
00077   switch(n) {
00078     case 1: return K_K1;
00079     case 2: return K_K2;
00080     case 3: return K_K3;
00081     case 4: return K_K4;
00082     case 5: return K_K5;
00083     case 6: return K_K6;
00084     case 7: return K_K7;
00085     case 8: return K_K8;
00086     case 9: return K_K9;
00087     case 10: return K_K10;
00088     case 11: return K_K11;
00089     case 12: return K_K12;
00090     case 13: return K_K13;
00091     case 14: return K_K14;
00092     case 15: return K_K15;
00093     case 16: return K_K16;
00094     case 17: return K_K17;
00095     case 18: return K_K18;
00096   }
00097   assert(false);
00098   return "";
00099 }
00100 
00101 osl::record::Characters::~Characters()
00102 {
00103 }
00104 
00105 const osl::misc::CArray<std::string,32> osl::record::Characters::stand = 
00106   {{// WHITE
00107     K_NAKAGURO, "+E",
00108     K_PPAWN, K_PLANCE, K_PKNIGHT, K_PSILVER, K_PBISHOP, K_PROOK,  K_KING, 
00109     K_GOLD,  K_PAWN,   K_LANCE,   K_KNIGHT,  K_SILVER,  K_BISHOP, K_ROOK, 
00110     // BLACK
00111     K_NAKAGURO, "+E",
00112     K_PPAWN, K_PLANCE, K_PKNIGHT, K_PSILVER, K_PBISHOP, K_PROOK,  K_KING, 
00113     K_GOLD,  K_PAWN,   K_LANCE,   K_KNIGHT,  K_SILVER,  K_BISHOP, K_ROOK
00114   }};
00115 
00116 const osl::misc::CArray<std::string,10> osl::record::StandardCharacters::dan = 
00117   {{"", K_K1, K_K2, K_K3, K_K4, K_K5, K_K6, K_K7, K_K8, K_K9}};
00118 const osl::misc::CArray<std::string,10> osl::record::StandardCharacters::suji = 
00119   {{"", K_R1, K_R2, K_R3, K_R4, K_R5, K_R6, K_R7, K_R8, K_R9}};
00120 const osl::misc::CArray<std::string,32> osl::record::StandardCharacters::pieces = 
00121   {{// WHITE
00122     K_NAKAGURO, "+E",
00123     K_PPAWN, K_PLANCE, K_PKNIGHT, K_PSILVER, K_PBISHOP, K_PROOK,  K_KING, 
00124     K_GOLD,  K_PAWN,   K_LANCE,   K_KNIGHT,  K_SILVER,  K_BISHOP, K_ROOK, 
00125     // BLACK
00126     K_NAKAGURO, "+E",
00127     K_PPAWN, K_PLANCE, K_PKNIGHT, K_PSILVER, K_PBISHOP, K_PROOK,  K_KING, 
00128     K_GOLD,  K_PAWN,   K_LANCE,   K_KNIGHT,  K_SILVER,  K_BISHOP, K_ROOK
00129   }};
00130 
00131 const osl::misc::CArray<std::string,10> osl::record::RussianCharacters::dan = osl::record::StandardCharacters::dan;
00132 const osl::misc::CArray<std::string,10> osl::record::RussianCharacters::suji = osl::record::StandardCharacters::suji;
00133 const osl::misc::CArray<std::string,32> osl::record::RussianCharacters::pieces = 
00134   {{// WHITE
00135     K_NAKAGURO, "+E",
00136     K_PPAWN_R, K_PLANCE_R, K_PKNIGHT_R, K_PSILVER_R, K_PBISHOP_R, K_PROOK_R,  K_KING_R,
00137     K_GOLD_R,  K_PAWN_R,   K_LANCE_R,   K_KNIGHT_R,  K_SILVER_R,  K_BISHOP_R, K_ROOK_R, 
00138     // BLACK
00139     K_NAKAGURO, "+E",
00140     K_PPAWN, K_PLANCE, K_PKNIGHT, K_PSILVER, K_PBISHOP, K_PROOK,  K_KING, 
00141     K_GOLD,  K_PAWN,   K_LANCE,   K_KNIGHT,  K_SILVER,  K_BISHOP, K_ROOK
00142   }};
00143 
00144 const osl::misc::CArray<std::string,10> osl::record::KIFCharacters::dan = 
00145   osl::record::StandardCharacters::dan;
00146 const osl::misc::CArray<std::string,10> osl::record::KIFCharacters::suji = 
00147   {{"", " "+K_R1, " "+K_R2, " "+K_R3, " "+K_R4, " "+K_R5, " "+K_R6, " "+K_R7, " "+K_R8, " "+K_R9}};
00148 const osl::misc::CArray<std::string,32> osl::record::KIFCharacters::pieces = 
00149   {{// WHITE
00150     " "+K_NAKAGURO, "+E",
00151     "v"+K_PPAWN, "v"+K_PLANCE, "v"+K_PKNIGHT, "v"+K_PSILVER, "v"+K_PBISHOP, "v"+K_PROOK,  "v"+K_KING, 
00152     "v"+K_GOLD,  "v"+K_PAWN,   "v"+K_LANCE,   "v"+K_KNIGHT,  "v"+K_SILVER,  "v"+K_BISHOP, "v"+K_ROOK,
00153     // BLACK
00154     " "+K_NAKAGURO, "+E",
00155     " "+K_PPAWN, " "+K_PLANCE, " "+K_PKNIGHT, " "+K_PSILVER, " "+K_PBISHOP, " "+K_PROOK,  " "+K_KING, 
00156     " "+K_GOLD,  " "+K_PAWN,   " "+K_LANCE,   " "+K_KNIGHT,  " "+K_SILVER,  " "+K_BISHOP, " "+K_ROOK
00157   }};
00158 
00159 
00160 std::ostream& osl::record::
00161 operator<<(std::ostream& os, const Color& c)
00162 {
00163   return os << c.value;
00164 }
00165 
00166 void osl::record::ChangeShellColor::
00167 escColSet() const
00168 {
00169    if (!color.isValid()) return;
00170    os << "\033[" << color << "m"; //文字の属性をセットする
00171 }
00172 
00173 void osl::record::ChangeShellColor::
00174 escColReSet() const
00175 {
00176    if (!color.isValid()) return;
00177    os << "\033[0m";
00178 }
00179 
00180 void osl::record::KanjiPrint::
00181 print(const osl::state::SimpleState& state, 
00182       const osl::Move *last_move) const
00183 {
00184   os << IconvConvert::eucToLang(K_WHITE_STAND) << " ";
00185   BOOST_FOREACH(Ptype ptype, PieceStand::order)
00186   {
00187     const int count = state.countPiecesOnStand(WHITE, ptype);
00188     if (count)
00189       os << IconvConvert::eucToLang(pieces->stand_kanji(newPtypeO(BLACK, ptype)))
00190          << count << " ";
00191   }
00192   os << std::endl;  
00193 
00194   os << " ";
00195   for(int x=9;x>0;x--)
00196   {
00197     os << IconvConvert::eucToLang(pieces->getSuji(x));
00198   }
00199   os << std::endl;  
00200 
00201   os << "+";
00202   for(int x=9*pieces->getSuji(1).size();x>0;x--)
00203   {
00204     os << "-";
00205   }
00206   os << "+" << std::endl;
00207 
00208   for(int y=1;y<=9;y++)
00209   {
00210     os << '|';  
00211     for(int x=9;x>0;x--)
00212     {
00213       const PtypeO ptypeo = state.pieceOnBoard(Square(x,y)).ptypeO();
00214       const std::string piece =
00215         IconvConvert::eucToLang(pieces->kanji(ptypeo));
00216       if (last_move && 
00217           !last_move->isInvalid() && 
00218           last_move->to() == Square(x,y))
00219       {
00220         ChangeShellColor csc(os, last_move_color);
00221         os << piece;
00222       } // csc destroyed
00223       else if (isPiece(ptypeo)) {
00224         Player owner = getOwner(ptypeo);
00225         osl::record::Color color; 
00226         if (owner == BLACK)
00227           color = black_color;
00228         else
00229           color = white_color;
00230         ChangeShellColor csc(os, color);
00231         os << piece;
00232       } // csc destroyed
00233       else
00234       { // empty space
00235         os << piece;
00236       }
00237     }
00238     os << '|';  
00239     os << IconvConvert::eucToLang(pieces->getDan(y));
00240     os << std::endl;
00241   }
00242 
00243   os << "+";
00244   for(int x=9*pieces->getSuji(1).size();x>0;x--)
00245   {
00246     os << "-";
00247   }
00248   os << "+" << std::endl;
00249 
00250   // 持ち駒の表示
00251   os << IconvConvert::eucToLang(K_BLACK_STAND) << " ";
00252   BOOST_FOREACH(Ptype ptype, PieceStand::order)
00253   {
00254     const int count = state.countPiecesOnStand(BLACK, ptype);
00255     if (count)
00256       os << IconvConvert::eucToLang(pieces->stand_kanji(newPtypeO(BLACK, ptype)))
00257          << count << " ";
00258   }
00259   os << std::endl;  
00260   os << state.turn() << std::endl;
00261 }
00262 
00263 /* ------------------------------------------------------------------------- */
00264 // ;;; Local Variables:
00265 // ;;; mode:c++
00266 // ;;; c-basic-offset:2
00267 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines