00001 #include <iostream> 00002 #include <algorithm> 00003 #include "osl/record/record.h" 00004 #include "osl/record/compactBoard.h" 00005 00006 namespace osl 00007 { 00008 namespace record 00009 { 00010 00011 struct opiece_sort 00012 { 00013 bool operator()(const OPiece& l, const OPiece& r) 00014 { 00015 // need to special case pieces on stand 00016 if (l.getPosition() == Position::STAND() || r.getPosition() == Position::STAND()) 00017 { 00018 if (l.getPosition() == Position::STAND() && r.getPosition() != Position::STAND()) 00019 return true; 00020 else if (l.getPosition() != Position::STAND() && r.getPosition() == Position::STAND()) 00021 return false; 00022 else 00023 { 00024 if (l.getOwner() != r.getOwner()) 00025 return l.getOwner() == WHITE; 00026 return l.getPtype() < r.getPtype(); 00027 } 00028 } 00029 else 00030 { 00031 if (l.getPosition().x() < r.getPosition().x()) 00032 return true; 00033 else if (l.getPosition().x() > r.getPosition().x()) 00034 return false; 00035 else 00036 { 00037 if (l.getPosition().y() <= r.getPosition().y()) 00038 return true; 00039 else 00040 return false; 00041 } 00042 } 00043 } 00044 }; 00045 00046 CompactBoard::CompactBoard(const SimpleState& state) 00047 { 00048 pieces.reserve(40); 00049 for (int i = 0; i < 40; i++) 00050 { 00051 pieces.push_back(OPiece(state.getPieceOf(i))); 00052 } 00053 std::sort(pieces.begin(), pieces.end(), opiece_sort()); 00054 turn = state.getTurn(); 00055 } 00056 00057 SimpleState 00058 CompactBoard::getState() 00059 { 00060 assert(pieces.size() == 40); 00061 00062 SimpleState state; 00063 state.init(); 00064 00065 for (unsigned int i = 0; i < pieces.size(); i++) 00066 { 00067 const OPiece& p = pieces[i]; 00068 state.setPiece(p.getOwner(), p.getPosition(), p.getPtype()); 00069 } 00070 state.setTurn(turn); 00071 00072 return state; 00073 } 00074 00075 std::ostream& operator<<(std::ostream& os, const CompactBoard& c) 00076 { 00077 assert(c.pieces.size() == 40); 00078 00079 for (unsigned int i = 0; i < c.pieces.size(); i++) 00080 { 00081 writeInt(os, static_cast<int>(c.pieces[i])); 00082 } 00083 writeInt(os, static_cast<int>(c.turn)); 00084 return os; 00085 } 00086 00087 std::istream& operator>>(std::istream& is, CompactBoard& c) 00088 { 00089 assert(c.pieces.size() == 0); 00090 00091 for (unsigned int i = 0; i < 40; i++) 00092 { 00093 c.pieces.push_back(OPiece(readInt(is))); 00094 } 00095 c.turn = static_cast<Player>(readInt(is)); 00096 return is; 00097 } 00098 } 00099 } 00100 00101 /* ------------------------------------------------------------------------- */ 00102 // ;;; Local Variables: 00103 // ;;; mode:c++ 00104 // ;;; c-basic-offset:2 00105 // ;;; End: