Enum Classes and Data Sets

#include"State.h" #include #include #include #include #include using namespace std; #define MAX ((1<<25)-1) map states; map states_board_key; void readDatabase() { ifstream f("database"); int id; unsigned long long int board; int parentId; int parentMove; char delim; while (!f.eof()) { f >> id >> delim >> board >> delim >> parentId >> delim >> parentMove; states[id] = State(id, bitset<25>(board), parentId, parentMove); states_board_key[board] = State(id, bitset<25>(board), parentId, parentMove); } } void solveStatringFrom(int id) { State current = states[id]; cout << endl << "Solving the game:"<first; // get the board according to the generated random number } while (states[initialStateId].getBoard().to_ullong() == MAX || states[initialStateId].getBoard().to_ullong() == 0); State current_state = states[initialStateId]; int action; do { current_state.printBoard(); do { cout << "please choose an action to do (1-25) to flip the exact point in the board or (0) to solve it step by step: "; cin >> action; } while (action != 0 && (action <1 || action>25)); if (action != 0) { // flip and prompt again current_state.flip(action-1); continue; } else { // solve and close solveStatringFrom(current_state.getId()); break; } } while (true); system("pause"); return 0; } Generator.cpp #include"State.h" #include #include #include #include #include /* The algorithm used here: 1- Start with a queue that is filled with the two-goal state (zeros and ones) 2- while this queue is not empty: A- get the front of the queue B- get all the possible states that can be reached from that state C- add them to the queue if they are not visited before */ int main() { //1 - Start with a queue that is filled with the two-goal state(zeros and ones) State bs(bitset<25>((1 << 25) - 1),-1,-1); // the state filled with ones queue q; vector vec; set visited; vector res; q.push(bs); res.push_back(bs); visited.insert(bs.getBoard().to_ullong()); bs = State(bitset<25>(0), -1, -1); // the state filled with zeros q.push(bs); res.push_back(bs); visited.insert(bs.getBoard().to_ullong()); while (!q.empty()) { vec.clear(); bs = q.front(); //A- get the front of the queue bs.generatePossibleMoves(vec); //B- get all the possible states can be reached from that state for (unsigned int i = 0; i < vec.size(); i++) { if (visited.find(vec[i].getBoard().to_ullong()) == visited.end()) // if not visited child { //C- add them to the queue if they are not visited before q.push(vec[i]); res.push_back(vec[i]); visited.insert(vec[i].getBoard().to_ullong()); } } q.pop(); } /*Saving the output*/ ofstream out ("database"); for (int i = 0; i < res.size(); i++) { out << res[i].getId() <<","<< res[i].getBoard().to_ullong()<<"," << res[i].getParentId()<<"," << res[i].getParentMove()< State::prepareMask(int move) { // generates the mask suited to the action bitset<5>notRow(0); notRow.set(move % 5); bitset<5>row((1<<6)-1); int rowNumber = move / 5; bitset<25> res; for (int i = 0; i < 5; i++) { if (i == rowNumber) { res |= (bitset<25>(row.to_ullong())<< (5 * i)); } else { res |= bitset<25>(notRow.to_ullong()) << (5 * i); } } return res; } State::State(bitset<25> board, int parentId, int parentMove) { this->id = globalId++; this->board = board; this->parentId = parentId; this->parentMove = parentMove; } State::State(int id, bitset<25> board, int parentId, int parentMove) { this->id = id; this->board = board; this->parentId = parentId; this->parentMove = parentMove; } void State::generatePossibleMoves(vector&v) { /*Generates all the possible moves by flipping each token on the board once*/ for (int i = 0; i < 25; i++) { v.push_back(State(this->board^prepareMask(i),id,i)); } } void State::printBoard() { for (int i = 0; i < 25;) { cout << this->board[i]; if ((++i) % 5 == 0) cout << endl; } } void State::flip(int x) { /*Xor the board with the mask suit to the action*/ this->board ^= prepareMask(x); } State.h #pragma once #include #include #include using namespace std; class State { private: static int globalId; bitset<25> prepareMask(int move); int id; bitset<25>board; int parentId; int parentMove; public: State(bitset<25>board, int parentId, int parentMove); State(int id,bitset<25>board, int parentId, int parentMove); State() {}; void generatePossibleMoves(vector&); void printBoard(); bitset<25> getBoard() { return this->board; } void flip(int x); int getId() { return id; } int getParentId() { return parentId; } int getParentMove() { return parentMove; } };