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)*6);
00010 }
00011
00012 std::ostream& osl::hash::operator<<(std::ostream& os,const osl::HashKey& h)
00013 {
00014 os << h.getPieceStand();
00015 os << ':'
00016 << std::setfill('0') << std::setbase(16) << std::setw(8)
00017 << h.getSignature();
00018 const BoardKey& board_key = h.getSignatureKey().getBoardKey();
00019 for (size_t i=0; i<board_key.size(); ++i)
00020 {
00021 os << ':'
00022 << std::setfill('0') << std::setbase(16) << std::setw(8)
00023 << board_key[i];
00024 }
00025 return os << ':' << std::setbase(10);
00026 }
00027
00028 void osl::hash::HashKey::dumpContents(std::ostream& os) const
00029 {
00030 os << getPieceStand().getFlags() << ' ' << getSignature();
00031 for (size_t i=0; i<size(); ++i) {
00032 os << ' ' << operator[](i);
00033 }
00034 }
00035
00036 const osl::hash::HashKey osl::hash::HashKey::readFromDump(const std::string& str)
00037 {
00038 std::istringstream is(str);
00039 return readFromDump(is);
00040 }
00041
00042 const osl::hash::HashKey osl::hash::HashKey::readFromDump(std::istream& is)
00043 {
00044 HashKey key;
00045 int stand;
00046 is >> stand;
00047 key.pieceStand = PieceStand(stand);
00048 unsigned int signature;
00049 is >> signature;
00050 key.setSignature(signature);
00051 for (size_t i=0; i<key.size(); ++i) {
00052 is >> key[i];
00053 }
00054 return key;
00055 }
00056
00057 osl::HashKey osl::HashKey::
00058 calcHash(const SimpleState& state)
00059 {
00060 HashKey v;
00061 for(int num=0;num<40;num++){
00062 Piece p=state.getPieceOf(num);
00063 Hash_Gen_Table.addHashKey(v,p.position(),p.ptypeO());
00064 }
00065 v.setPlayer(state.getTurn());
00066 return v;
00067 }
00068
00069 const osl::HashKey osl::HashKey::
00070 newHashWithMove(Move move) const
00071 {
00072 HashKey ret(*this);
00073 if (! move.isPass())
00074 {
00075 assert(move.isValid());
00076 Position from=move.from();
00077 Position to=move.to();
00078 Ptype capturePtype=move.capturePtype();
00079 PtypeO ptypeO=move.ptypeO();
00080 PtypeO oldPtypeO=move.oldPtypeO();
00081 if (capturePtype!=PTYPE_EMPTY)
00082 {
00083 PtypeO capturePtypeO=newPtypeO(alt(move.player()),capturePtype);
00084 PtypeO capturedPtypeO=captured(capturePtypeO);
00085
00086 Hash_Gen_Table.subHashKey(ret,to,capturePtypeO);
00087 Hash_Gen_Table.addHashKey(ret,Position::STAND(),capturedPtypeO);
00088
00089 Hash_Gen_Table.subHashKey(ret,from,oldPtypeO);
00090 Hash_Gen_Table.addHashKey(ret,to,ptypeO);
00091 }
00092 else{
00093 Hash_Gen_Table.subHashKey(ret,from,oldPtypeO);
00094 Hash_Gen_Table.addHashKey(ret,to,ptypeO);
00095 }
00096 }
00097 ret.changeTurn();
00098 return ret;
00099 }
00100
00101
00102 osl::hash::HashGenTable::HashGenTable()
00103 {
00104 for(int j=0;j<PTYPEO_SIZE;j++)
00105 {
00106 const PtypeO pjo = (PtypeO)(j+PTYPEO_MIN);
00107 for(int i=0;i<Position::SIZE;i++)
00108 {
00109 if (Position::nth(i) == Position::STAND())
00110 {
00111 const Ptype pj = getPtype(pjo);
00112 if (isBasic(pj) && (getOwner(pjo) == BLACK))
00113 {
00114 PieceStand stand;
00115 stand.add(pj);
00116 hashKey[i][j].setPieceStand(stand);
00117 }
00118 }
00119 else
00120 {
00121 hashKey[i][j].setRandom();
00122 }
00123 }
00124 }
00125 }
00126
00127
00128
00129
00130
00131