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