00001 #ifndef _RECORD_H
00002 #define _RECORD_H
00003 #include "osl/record/searchInfo.h"
00004 #include "osl/move.h"
00005 #include "osl/state/numEffectState.h"
00006 #include "osl/misc/carray.h"
00007 #include "osl/misc/align16New.h"
00008 #include <boost/ptr_container/ptr_vector.hpp>
00009 #include <iosfwd>
00010
00011 namespace osl
00012 {
00013 namespace record
00014 {
00015 class Record;
00016 class IRecordStream{
00017 public:
00018 virtual void load(Record*)=0;
00019 virtual ~IRecordStream();
00020 private:
00021 };
00022 class ORecordStream{
00023 public:
00024 virtual void save(Record*)=0;
00025 virtual ~ORecordStream();
00026 private:
00027 };
00028
00029 enum NodeType{
00030 MOVE,
00031 TORYO,
00032 MATTA,
00033 CHUDAN,
00034 SENNICHITE,
00035 JISHOGI,
00036 TSUMI,
00037 FUZUMI,
00038 ND_ERROR,
00039 KACHI,
00040 HIKIWAKE,
00041 };
00042
00048 class MoveRecord{
00049 private:
00050 Move move;
00051 int nodeIndex;
00052 int time;
00053 std::string comment;
00054 public:
00055 SearchInfo info;
00056
00057 MoveRecord(const Move& mv, int ni);
00058 const Move getMove() const;
00059 int getNodeIndex() const;
00060 void setTime(int t);
00061 int getTime() const{ return time; }
00062 void setComment(const std::string& com){ comment=com; }
00063 void addComment(const std::string& com)
00064 {
00065 if (! comment.empty())
00066 comment += "\n";
00067 comment += com;
00068 }
00069 const std::string& getComment() const{ return comment; }
00070 };
00071
00072 class NodeRecord{
00073 private:
00074 NodeType type;
00075 vector<int> moves;
00076 std::string comment;
00077 public:
00078 NodeRecord():type(MOVE){}
00079 NodeType getType() const{ return type; }
00080 int size() const { return moves.size(); }
00081 int at(int index) const{ return moves.at(index); }
00082 void setComment(const std::string& com){ comment=com; }
00083 const std::string& getComment() const{ return comment; }
00084 void addMoveRecord(int moveIndex);
00085 };
00086
00087 class Record
00088 #if OSL_WORDSIZE == 32
00089 : public misc::Align16New
00090 #endif
00091 {
00092 public:
00093 enum ResultType {
00094 UNKNOWN=0,
00095 BLACK_WIN=1,
00096 WHITE_WIN=2,
00097 SENNNICHITE=3,
00098 JISHOGI=4,
00099 };
00100 private:
00101 SimpleState initialState;
00102 std::string version, initial_comment, tounament_name;
00103 CArray<std::string,2> playerNames;
00104 vector<NodeRecord> nrs;
00105 vector<MoveRecord> mrs;
00106 ResultType result;
00107 public:
00108 Record();
00109 void init();
00110 void setVersion(const std::string& str);
00111 const std::string getVersion() const { return version; }
00112 void addInitialComment(const std::string& comment)
00113 {
00114 if (! initial_comment.empty())
00115 initial_comment += "\n";
00116 initial_comment += comment;
00117 }
00118 const std::string getInitialComment() const
00119 {
00120 return initial_comment;
00121 }
00122 void setPlayer(Player player,const std::string& str);
00123 const std::string& getPlayer(Player player) const;
00124 void setInitialState(const SimpleState& state);
00125 const NumEffectState getInitialState() const;
00126 int addNodeRecord();
00127 int addMoveRecord(const MoveRecord& moveRecord);
00128 NodeRecord* nodeOf(int index);
00129 NodeRecord& operator[](int index);
00130 MoveRecord* moveOf(int index);
00131 void load(IRecordStream&);
00132 void save(ORecordStream&);
00133 const vector<Move> getMoves() const;
00134 void getMoves(vector<Move>&, vector<int>&) const;
00135 void getMoves(vector<Move>&, vector<int>&, vector<std::string>&,
00136 vector<SearchInfo>&) const;
00137 const NodeRecord* nodeOf(int index) const;
00138 const MoveRecord* moveOf(int index) const;
00139 void setResult(ResultType new_result) { result = new_result; }
00140 ResultType getResult() const { return result; }
00141 void setTounamentName(const std::string& name) { tounament_name = name; }
00142 const std::string& tounamentName() const { return tounament_name; }
00143 };
00144
00145 class RecordVisitor;
00146
00147 class RecordVisitorObserver {
00148 public:
00149 virtual ~RecordVisitorObserver() {}
00150 virtual void update(RecordVisitor* rv) = 0;
00151 };
00152
00153 class RecordVisitor{
00154 private:
00155 Record* rec;
00156 SimpleState* state;
00157 int lastMoveIndex;
00158 int nodeIndex;
00159 boost::ptr_vector<RecordVisitorObserver> observers;
00160 public:
00161 RecordVisitor():rec(NULL),state(NULL),lastMoveIndex(0),nodeIndex(0){}
00162 RecordVisitor(Record& r);
00163 ~RecordVisitor();
00164
00165 SimpleState *getState() const{ return state; }
00166 void setState(SimpleState *s){ state=s;}
00167 Record *getRecord() { return rec; }
00168 void setRecord(Record *r){ rec=r;}
00169 MoveRecord *getLastMove(){ return rec->moveOf(lastMoveIndex); }
00170 void addMoveAndAdvance(Move move);
00171 NodeRecord *getNode(){ return rec->nodeOf(nodeIndex); }
00172 void addObserver(RecordVisitorObserver *observer)
00173 { observers.push_back(observer); }
00174 };
00175
00176 std::ostream& operator<<(std::ostream&,const MoveRecord &);
00177 std::ostream& operator<<(std::ostream&,Record &);
00178
00179 int readInt(std::istream& is);
00180 void writeInt(std::ostream& os, int n);
00181 }
00182 using record::Record;
00183 }
00184 #endif
00185
00186
00187
00188