fork download
  1. // 2012/9/23 By s3748679
  2. // xAxBGame_1 : xAxB Game Level 1 (xAxB遊戲第一層架構)
  3. // * 特性
  4. // 1. 將4位數字組的設定解釋成 xAxBGame_1<int>(4, {0,1,2,3,4,5,6,7,8,9}),
  5. // 換句話說就是從{0,1,2,3,4,5,6,7,8,9}挑4個的意思。
  6. // 2. {0,1,2,3,4,5,6,7,8,9} 被解釋成SampleSpace(樣本空間),
  7. // 4 被解釋成sampleLength(樣本長度),
  8. // 而遊戲所使用的數字組被解釋成Sample(樣本),
  9. // PS: SampleSpace與Sample都由std::vector<TSampleElement>定義而成。
  10. // 3. 在決定遊戲相關參數後提供相對應的工具: sampleChecker、sampleComparer
  11. // sampleChecker負責(由已知的sampleLength與sampleSpace)檢查Sample是否合法,
  12. // 而sampleComparer則是負責比對二Sample並產生xAxB的結果。
  13. // 4. 提供xAxB的型別用以放置sampleComparer的比對結果。
  14. // * 備註
  15. // 適用發展於 AI 與 一般化xAxBGame遊戲模型(例如提供 guest, isWin, guestTimes, reset..) // 應該吧.. (汗
  16.  
  17. //==================================================================
  18. // xAxBGame_1.h
  19. #include <vector>
  20.  
  21. template <typename TSampleElement>
  22. class xAxBGame_1 { // level 1
  23. public: // typedefs
  24. typedef std::vector<TSampleElement> SampleSpace;
  25. typedef std::vector<TSampleElement> Sample;
  26. public: // classes
  27. class xAxB;
  28. class _SampleChecker; // result's type: bool
  29. class _SampleComparer; // result's type: xAxB
  30. public: // functions
  31. // ctor
  32. xAxBGame_1(int sampleLength, const SampleSpace&);
  33.  
  34. // info
  35. const SampleSpace& sampleSpace() const {return _sampleSpace;}
  36. int sampleLength() const {return _sampleLength;}
  37.  
  38. // tools
  39. const _SampleChecker& sampleChecker() const {return __sampleChecker;}
  40. const _SampleComparer& sampleComparer() const {return __sampleComparer;}
  41. private: // data
  42. SampleSpace _sampleSpace;
  43. int _sampleLength;
  44.  
  45. _SampleChecker __sampleChecker;
  46. _SampleComparer __sampleComparer;
  47. };
  48.  
  49. template <typename TSampleElement>
  50. class xAxBGame_1<TSampleElement>::xAxB{
  51. public: // functions
  52. // ctor
  53. xAxB(int a, int b) : _a(a), _b(b) {}
  54.  
  55. // operator
  56. bool operator!=(const xAxB& xAB) const {
  57. if (_a != xAB.a()) return true;
  58. if (_b != xAB.b()) return true;
  59. return false;
  60. }
  61. bool operator==(const xAxB& xAB) const {return !(*this != xAB);}
  62.  
  63. // info
  64. int a() const {return _a;}
  65. int b() const {return _b;}
  66. private: // data
  67. int _a, _b;
  68. };
  69.  
  70. template <typename TSampleElement>
  71. class xAxBGame_1<TSampleElement>::_SampleChecker{
  72. friend class xAxBGame_1<TSampleElement>;
  73. public: // typedefs
  74. typedef xAxBGame_1<TSampleElement> ParentType;
  75. typedef typename ParentType::Sample _Sample;
  76. typedef typename ParentType::SampleSpace _SampleSpace;
  77. public: // functions
  78. // method
  79. bool check(const _Sample& s) const {
  80. if (s.size() != _parent->sampleLength()) return false;
  81. if (!_isInSampleSpace(s, _parent->sampleSpace())) return false;
  82. if (_isRepeat(s)) return false;
  83. return true;
  84. }
  85.  
  86. // info
  87. const ParentType* parent() const {return _parent;}
  88. private: // functions
  89. static bool _isInSampleSpace(const _Sample& s, const _SampleSpace& space){
  90. for (auto it_s=s.begin(); it_s!=s.end(); it_s++){
  91. bool isFind = false;
  92. for (auto it_space=space.begin(); it_space!=space.end(); it_space++){
  93. if (*it_s != *it_space)
  94. continue;
  95. isFind = true;
  96. break;
  97. }
  98. if (!isFind)
  99. return false;
  100. }
  101. return true;
  102. }
  103. static bool _isRepeat(const _Sample& s){
  104. for (auto it_s=s.begin(); it_s!=s.end(); it_s++){
  105. for (auto it2_s=(it_s+1); it2_s!=s.end(); it2_s++){
  106. if (*it_s == *it2_s)
  107. return true;
  108. }
  109. }
  110. return false;
  111. }
  112. private: // data
  113. _SampleChecker(){}
  114. ParentType *_parent;
  115. };
  116.  
  117. template <typename TSampleElement>
  118. class xAxBGame_1<TSampleElement>::_SampleComparer{
  119. friend class xAxBGame_1<TSampleElement>;
  120. public: // typedefs
  121. typedef xAxBGame_1<TSampleElement> ParentType;
  122. typedef typename ParentType::xAxB _xAxB;
  123. typedef typename ParentType::Sample _Sample;
  124. public: // functions
  125. // method
  126. const _xAxB compare(const _Sample& s1, const _Sample& s2) const{
  127. int a=0, b=0;
  128. for (std::size_t i_s1=0; i_s1<s1.size(); i_s1++){
  129. for (std::size_t i_s2=0; i_s2<s2.size(); i_s2++){
  130. if (s1[i_s1] == s2[i_s2]){
  131. if (i_s1 != i_s2)
  132. b++;
  133. else
  134. a++;
  135. }
  136. }
  137. }
  138. return _xAxB(a, b);
  139. }
  140.  
  141. // info
  142. const ParentType* parent() const {return _parent;}
  143. private: // data
  144. // ctor
  145. _SampleComparer(){}
  146.  
  147. ParentType *_parent;
  148. };
  149. //==================================================================
  150.  
  151. //==================================================================
  152. // xAxBGame.tcc
  153. template <typename TSampleElement>
  154. xAxBGame_1<TSampleElement>::
  155. xAxBGame_1(int sampleLength, const SampleSpace& sampleSpace)
  156. : _sampleLength(sampleLength), _sampleSpace(sampleSpace)
  157. {
  158. __sampleChecker._parent = this;
  159. __sampleComparer._parent = this;
  160. }
  161.  
  162. //==================================================================
  163.  
  164. //==================================================================
  165. // main.cpp
  166. #include <iostream>
  167. #include <sstream>
  168. #include <string>
  169.  
  170. // ---------------------------------------------------------------
  171. // -declaration---------------------------------------------------
  172.  
  173. template<typename TxAxB>
  174. const std::string toString_xAxB(const TxAxB&);
  175.  
  176. template<typename TxAxBGameSample>
  177. const std::string toString_xAxBGameSample(const TxAxBGameSample&);
  178.  
  179. int main();
  180.  
  181. // ---------------------------------------------------------------
  182. // -definition----------------------------------------------------
  183.  
  184. template<typename TxAxB>
  185. const std::string toString_xAxB(const TxAxB& xAB){
  186. std::stringstream ss;
  187. ss << xAB.a() << "A" << xAB.b() << "B";
  188.  
  189. std::string s;
  190. ss >> s;
  191.  
  192. return s;
  193. }
  194.  
  195. template<typename TxAxBGameSample>
  196. const std::string toString_xAxBGameSample(const TxAxBGameSample& sample){
  197. std::stringstream ss;
  198. for (auto it=sample.begin(); it!=sample.end(); it++)
  199. ss << *it;
  200.  
  201. std::string s;
  202. ss >> s;
  203.  
  204. return s;
  205. }
  206.  
  207. int main(){
  208. using namespace std;
  209.  
  210. auto const game = (
  211. []() -> xAxBGame_1<int> {
  212. int sampleSpace[] = {0,1,2,3,4,5,6,7,8,9};
  213. int sampleLength = 4;
  214. return xAxBGame_1<int>(
  215. sampleLength,
  216. xAxBGame_1<int>::SampleSpace(&sampleSpace[0], &sampleSpace[10])
  217. );
  218. }
  219. )();
  220.  
  221. auto const createGameSample = (
  222. [](int e1, int e2, int e3, int e4) -> xAxBGame_1<int>::Sample{
  223. int sampleElements[4];
  224. sampleElements[0] = e1; sampleElements[1] = e2;
  225. sampleElements[2] = e3; sampleElements[3] = e4;
  226. return xAxBGame_1<int>::Sample(&sampleElements[0], &sampleElements[4]);
  227. }
  228. );
  229.  
  230. auto sample1_error = createGameSample(0,1,2,-3);
  231. cout << "check(" << toString_xAxBGameSample(sample1_error) << "): "
  232. << game.sampleChecker().check(sample1_error) << endl;
  233.  
  234. auto sample2_error = createGameSample(0,2,2,3);
  235. cout << "check(" << toString_xAxBGameSample(sample2_error) << "): "
  236. << game.sampleChecker().check(sample2_error) << endl;
  237.  
  238. cout << endl;
  239.  
  240. auto sample1 = createGameSample(0,1,2,3);
  241. cout << "check(" << toString_xAxBGameSample(sample1) << "): "
  242. << game.sampleChecker().check(sample1) << endl;
  243.  
  244. auto sample2 = createGameSample(0,2,4,5);
  245. cout << "check(" << toString_xAxBGameSample(sample2) << "): "
  246. << game.sampleChecker().check(sample2) << endl;
  247.  
  248. cout << "compare(" << toString_xAxBGameSample(sample1) << "," << toString_xAxBGameSample(sample2) << "): "
  249. << toString_xAxB(game.sampleComparer().compare(sample1, sample2)) << endl;
  250.  
  251.  
  252.  
  253. #ifdef _DEBUG
  254. system("pause");
  255. #endif
  256. return 0;
  257. }
  258. //==================================================================
  259.  
Success #stdin #stdout 0s 3024KB
stdin
Standard input is empty
stdout
check(012-3): 0
check(0223): 0

check(0123): 1
check(0245): 1
compare(0123,0245): 1A1B