1 /* ColorCode, a free MasterMind clone with built in solver
2  * Copyright (C) 2009  Dirk Laebisch
3  * http://www.laebisch.com/
4  *
5  * ColorCode is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * ColorCode is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with ColorCode. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #ifndef CCSOLVER_H
20 #define CCSOLVER_H
21 
22 #include <QtWidgets>
23 #include <iostream>
24 #include "colorcode.h"
25 
26 using namespace std;
27 
28 class CCSolver : public QThread
29 {
30     Q_OBJECT
31 
32 public:
33     static const int STRENGTH_LOW;
34     static const int STRENGTH_MEDIUM;
35     static const int STRENGTH_HIGH;
36     static const int STRENGTH_DYNAMIC;
37 
38     CCSolver(QObject* parent = 0);
39     ~CCSolver();
40 
41     vector<int> GetSolutionByNo(const uint no) const;
42     int GetAllCnt() const;
43     void NewGame(const int ccnt, const int pcnt = 4, const int doubles = 1, const int level = 2, const int gcnt = 10);
44     void RestartGame();
45     string FirstGuess();
46     void GuessIn(string str);
47     void GuessIn(const vector<int>* rowsol);
48     int ResIn(const vector<int>* res);
49     int* GuessOut();
50     void StartGuess();
51     void Interrupt();
52 
53     int GetRandGameNo(const int ccnt, const int pcnt, const int doubles) const;
54     const int* GetGameByNo(const int uint) const;
55     int CalcAllCnt(const int ccnt, const int pcnt) const;
56     int CalcPosCnt(const int ccnt, const int pcnt, const int doubles) const;
57 
58     void run();
59 
60 signals:
61     void GuessDoneSignal();
62 
63 private:
64     static const int mFGCnts[4][9][4];
65     static const int mGameNos[66][4];
66 
67     static const int GAME_NOS_CNT;
68 
69     volatile bool mInterrupt;
70 
71     int mMaxGuessCnt;
72     int mColorCnt;
73     int mPegCnt;
74     int mDoubles;
75     int mLevel;
76 
77     volatile int mLastGuess;
78     int mLastPosCnt;
79 
80     int mAllCnt;
81     int mPosCnt;
82     int mMaxPosCnt;
83     int mHistCnt;
84     int mGuessCnt;
85     int mMaxRes;
86     int mResCnt;
87 
88     int mRestartCnt;
89 
90     int* mResTable;
91     int* mCascade;
92     int* mPossible;
93     int* mPos2AllIx;
94     int** mAllTable;
95     int** mHist;
96 
97     int* mS;
98     int* mP;
99 
100     void FreeTables();
101     void InitTables();
102     void FillTable(const int ix);
103     int RowCmp(const int* sol, const int* pat);
104     void UpdPos(const int ix, const int res);
105     int Guess();
106 
107     string RowOut(const int* r);
108     int Row2Ix(const string str);
109     int Row2Ix(const vector<int>* v);
110 
111     int IntPow(const int b, const int e) const;
112     int IntFac(const int n) const;
113 
114     int GetRealGameNo(const uint no) const;
115     int GetGameIxByNo(const int no) const;
116 };
117 
118 #endif // CCSOLVER_H
119