00001
00002
00003 #include "osl/ntesuki/ntesukiTable.h"
00004 #include "osl/ntesuki/ntesukiMoveGenerator.h"
00005 #include "osl/apply_move/applyMoveWithPath.h"
00006 #include <iterator>
00007
00008 using namespace osl;
00009 using namespace osl::ntesuki;
00010
00011 template<class Search, class F> class
00012 DoUndoMoveHelper
00013 {
00014 Search* searcher;
00015 F& func;
00016 NumEffectState& state;
00017 NtesukiRecord *child;
00018 public:
00019 DoUndoMoveHelper(Search* searcher,
00020 F& func,
00021 NumEffectState& state,
00022 NtesukiRecord *child)
00023 : searcher(searcher), state(state), child(child)
00024 {
00025 }
00026
00027 void operator()(Position last_to)
00028 {
00029 (*searcher).template forEachRecordFrom<F>(func, state, child);
00030 }
00031 };
00032
00033
00034 template <class F>
00035 void
00036 osl::ntesuki::NtesukiTable::Table::
00037 forEachRecord(F& func)
00038 {
00039 for (iterator it = begin(); it != end(); ++it)
00040 {
00041 for (NtesukiRecord::RecordList::iterator p = it->second.begin();
00042 p != it->second.end(); ++p)
00043 {
00044 NtesukiRecord *r = &(*p);
00045 func(r);
00046 }
00047 }
00048 }
00049
00050 template <class F>
00051 void
00052 osl::ntesuki::NtesukiTable::Table::
00053 forEachRecordFrom(F& func,
00054 NumEffectState& state,
00055 NtesukiRecord *record)
00056 {
00057 NtesukiMoveGenerator mg;
00058 NtesukiMoveList all_moves;
00059 mg.generateSlow(state.getTurn(), state, all_moves);
00060
00061 func.enter(record);
00062
00063 std::vector<NtesukiMove> moves;
00064 std::copy(all_moves.begin(), all_moves.end(),
00065 std::back_insert_iterator<std::vector<NtesukiMove> >(moves));
00066 typename F::Compare c;
00067 std::sort(moves.begin(), moves.end(), c);
00068 for (std::vector<NtesukiMove>::const_iterator it = moves.begin();
00069 it != moves.end(); ++it)
00070 {
00071 const NtesukiMove& m = *it;
00072 NtesukiRecord *child = find(record->key.newHashWithMove(m.getMove()));
00073 if (child)
00074 {
00075 if (func.withChildMove(m, child))
00076 {
00077 DoUndoMoveHelper<Table, F> helper(func, state, child);
00078 ApplyMoveOfTurn::doUndoMove(state, m.getMove(), helper);
00079 }
00080 }
00081 else
00082 {
00083 func.noChildMove(m);
00084 }
00085 }
00086 func.exit();
00087 }
00088
00089 template <class F>
00090 void
00091 osl::ntesuki::NtesukiTable::Table::
00092 forEachRecordFromRoot(F& func)
00093 {
00094 if (rootState.get() == NULL)
00095 {
00096 throw RootStateNotSet();
00097 }
00098
00099 NumEffectState state(*rootState);
00100 forEachRecordFrom<F>(func, state, root);
00101 }
00102
00103
00104
00105