00001 #include "osl/hash/hashKey.h"
00002 #include <iomanip>
00003 #include <cstdlib>
00004 #include <iostream>
00005 #include <sstream>
00006
00007 namespace osl
00008 {
00009 BOOST_STATIC_ASSERT(sizeof(HashKey) >= sizeof(int)*5);
00010 }
00011
00012 #ifndef MINIMAL
00013 std::ostream& osl::hash::operator<<(std::ostream& os,const osl::hash::HashKey& h)
00014 {
00015 os << h.pieceStand();
00016 const BoardKey& board_key = h.boardKey();
00017 for (size_t i=0; i<board_key.size(); ++i)
00018 {
00019 os << ':'
00020 << std::setfill('0') << std::setbase(16) << std::setw(8)
00021 << board_key[i];
00022 }
00023 return os << ':' << std::setbase(10);
00024 }
00025
00026 void osl::hash::HashKey::dumpContents(std::ostream& os) const
00027 {
00028 os << pieceStand().getFlags();
00029 for (size_t i=0; i<size(); ++i) {
00030 os << ' ' << operator[](i);
00031 }
00032 }
00033
00034 void osl::hash::HashKey::dumpContentsCerr() const
00035 {
00036 dumpContents(std::cerr);
00037 }
00038
00039 const osl::hash::HashKey osl::hash::HashKey::readFromDump(const std::string& str)
00040 {
00041 std::istringstream is(str);
00042 return readFromDump(is);
00043 }
00044
00045 const osl::hash::HashKey osl::hash::HashKey::readFromDump(std::istream& is)
00046 {
00047 HashKey key;
00048 int stand;
00049 is >> stand;
00050 key.piece_stand = PieceStand(stand);
00051 for (size_t i=0; i<key.size(); ++i) {
00052 is >> key[i];
00053 }
00054 return key;
00055 }
00056 #endif
00057
00058 osl::hash::HashKey::HashKey(const SimpleState& state)
00059 {
00060 for(int num=0;num<40;num++){
00061 Piece p=state.getPieceOf(num);
00062 Hash_Gen_Table.addHashKey(*this, p.position(),p.ptypeO());
00063 }
00064 setPlayer(state.getTurn());
00065 }
00066
00067 const osl::hash::HashKey osl::hash::HashKey::
00068 newHashWithMove(Move move) const
00069 {
00070 return newMakeMove(move);
00071 }
00072
00073 const osl::hash::HashKey osl::hash::HashKey::
00074 newMakeMove(Move move) const
00075 {
00076 HashKey ret(*this);
00077 if (! move.isPass())
00078 {
00079 assert(move.isValid());
00080 Position from=move.from();
00081 Position to=move.to();
00082 Ptype capturePtype=move.capturePtype();
00083 PtypeO ptypeO=move.ptypeO();
00084 PtypeO oldPtypeO=move.oldPtypeO();
00085 if (capturePtype!=PTYPE_EMPTY)
00086 {
00087 PtypeO capturePtypeO=newPtypeO(alt(move.player()),capturePtype);
00088 PtypeO capturedPtypeO=captured(capturePtypeO);
00089
00090 Hash_Gen_Table.subHashKey(ret,to,capturePtypeO);
00091 Hash_Gen_Table.addHashKey(ret,Position::STAND(),capturedPtypeO);
00092 }
00093 Hash_Gen_Table.subHashKey(ret,from,oldPtypeO);
00094 Hash_Gen_Table.addHashKey(ret,to,ptypeO);
00095 }
00096 ret.changeTurn();
00097 return ret;
00098 }
00099
00100 const osl::hash::HashKey osl::hash::HashKey::
00101 newUnmakeMove(Move move) const
00102 {
00103 HashKey ret(*this);
00104 if (! move.isPass())
00105 {
00106 assert(move.isValid());
00107 Position from=move.from();
00108 Position to=move.to();
00109 Ptype capturePtype=move.capturePtype();
00110 PtypeO ptypeO=move.ptypeO();
00111 PtypeO oldPtypeO=move.oldPtypeO();
00112 if (capturePtype!=PTYPE_EMPTY)
00113 {
00114 PtypeO capturePtypeO=newPtypeO(alt(move.player()),capturePtype);
00115 PtypeO capturedPtypeO=captured(capturePtypeO);
00116
00117 Hash_Gen_Table.addHashKey(ret,to,capturePtypeO);
00118 Hash_Gen_Table.subHashKey(ret,Position::STAND(),capturedPtypeO);
00119 }
00120 Hash_Gen_Table.addHashKey(ret,from,oldPtypeO);
00121 Hash_Gen_Table.subHashKey(ret,to,ptypeO);
00122 }
00123 ret.changeTurn();
00124 return ret;
00125 }
00126
00127
00128 osl::hash::HashGenTable::HashGenTable()
00129 {
00130 for(int j=0;j<PTYPEO_SIZE;j++)
00131 {
00132 const PtypeO pjo = (PtypeO)(j+PTYPEO_MIN);
00133 for(int i=0;i<Position::SIZE;i++)
00134 {
00135 if (Position::nth(i) == Position::STAND())
00136 {
00137 const Ptype pj = getPtype(pjo);
00138 if (isBasic(pj) && (getOwner(pjo) == BLACK))
00139 {
00140 PieceStand stand;
00141 stand.add(pj);
00142 hashKey[i][j].setPieceStand(stand);
00143 }
00144 }
00145 else
00146 {
00147 hashKey[i][j].setRandom();
00148 }
00149 }
00150 }
00151 }
00152
00153
00154
00155
00156
00157