×
Samples Blogs Make Payment About Us Reviews 4.9/5 Order Now

Creating the Game Black or White in C++ Homework Help

June 11, 2024
Emily Jason
Emily Jason
🇬🇧 United Kingdom
C++
Emily Jason, an accomplished Programming Specialist with over 10 years of expertise, holds a Master's degree from a reputable academic institution.
Key Topics
  • Enum Classes and Data Sets
Tip of the day
Start with basic image processing concepts like color spaces and transformations. Practice simple OpenCV examples to build confidence before tackling complex tasks.
News
In 2024, the release of the 'AsyncIO Toolkit' has made asynchronous programming in Python more efficient. Similarly, 'React Quantum' introduces quantum computing concepts to JavaScript, expanding learning opportunities for students.

The game of Black or White is a solo player game. You use a 5 x 5 grid and fill it with random tiles that can be either black or white. You select a tile and flip it and all the ones on the same vertical and horizontal line. The game ends when all the tiles are the same color. You should also allow the player to give up and for the computer to show the moves remaining that will win the game. Take a look at the solution below prepared by our C++ homework help doers.

Enum Classes and Data Sets

#include"State.h" #include<fstream> #include<map> #include<iostream> #include<random> #include<ctime> using namespace std; #define MAX ((1&lt;&lt;25)-1) map<int, state=""> states; map<unsigned long="" int,="" state=""> 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 &gt;&gt; id &gt;&gt; delim &gt;&gt; board &gt;&gt; delim &gt;&gt; parentId &gt;&gt; delim &gt;&gt; parentMove; states[id] = State(id, bitset&lt;25&gt;(board), parentId, parentMove); states_board_key[board] = State(id, bitset&lt;25&gt;(board), parentId, parentMove); } } void solveStatringFrom(int id) { State current = states[id]; cout &lt;&lt; endl &lt;&lt; "Solving the game:"&lt;<endl; while(current.getid()="" !="0" &&="" current.getid()="" current.printboard();="" cout="" <<="" endl="" "action="" to="" do:="" "="" current.getparentmove()="" +="" 1="" endl;="" current="states[current.getParentId()];" }="" "solved!"<<endl;="" return;="" int="" main()="" {="" srand(time(null));="" initiate="" the="" seed="" of="" random="" variable="" "reading="" database!"<<endl;="" readdatabase();="" initialstateid;="" do="" initialstateid="states.lower_bound(rand()" %="" max)-="">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 &lt;&lt; "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 &gt;&gt; action; } while (action != 0 &amp;&amp; (action &lt;1 || action&gt;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<queue> #include<map> #include<set> #include<vector> #include<fstream> /* 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&lt;25&gt;((1 &lt;&lt; 25) - 1),-1,-1); // the state filled with ones queue <state>q; vector<state> vec; set<unsigned long="" int=""> visited; vector<state> res; q.push(bs); res.push_back(bs); visited.insert(bs.getBoard().to_ullong()); bs = State(bitset&lt;25&gt;(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 &lt; 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 &lt; res.size(); i++) { out &lt;&lt; res[i].getId() &lt;&lt;","&lt;&lt; res[i].getBoard().to_ullong()&lt;&lt;"," &lt;&lt; res[i].getParentId()&lt;&lt;"," &lt;&lt; res[i].getParentMove()&lt;<endl; }="" system("pause");="" return="" 0;="" state.cpp="" #include"state.h"="" int="" state::globalid="0;" bitset<25=""> State::prepareMask(int move) { // generates the mask suited to the action bitset&lt;5&gt;notRow(0); notRow.set(move % 5); bitset&lt;5&gt;row((1&lt;&lt;6)-1); int rowNumber = move / 5; bitset&lt;25&gt; res; for (int i = 0; i &lt; 5; i++) { if (i == rowNumber) { res |= (bitset&lt;25&gt;(row.to_ullong())&lt;&lt; (5 * i)); } else { res |= bitset&lt;25&gt;(notRow.to_ullong()) &lt;&lt; (5 * i); } } return res; } State::State(bitset&lt;25&gt; board, int parentId, int parentMove) { this-&gt;id = globalId++; this-&gt;board = board; this-&gt;parentId = parentId; this-&gt;parentMove = parentMove; } State::State(int id, bitset&lt;25&gt; board, int parentId, int parentMove) { this-&gt;id = id; this-&gt;board = board; this-&gt;parentId = parentId; this-&gt;parentMove = parentMove; } void State::generatePossibleMoves(vector<state>&amp;v) { /*Generates all the possible moves by flipping each token on the board once*/ for (int i = 0; i &lt; 25; i++) { v.push_back(State(this-&gt;board^prepareMask(i),id,i)); } } void State::printBoard() { for (int i = 0; i &lt; 25;) { cout &lt;&lt; this-&gt;board[i]; if ((++i) % 5 == 0) cout &lt;&lt; endl; } } void State::flip(int x) { /*Xor the board with the mask suit to the action*/ this-&gt;board ^= prepareMask(x); } State.h #pragma once #include<iostream> #include<bitset> #include<vector> using namespace std; class State { private: static int globalId; bitset&lt;25&gt; prepareMask(int move); int id; bitset&lt;25&gt;board; int parentId; int parentMove; public: State(bitset&lt;25&gt;board, int parentId, int parentMove); State(int id,bitset&lt;25&gt;board, int parentId, int parentMove); State() {}; void generatePossibleMoves(vector<state>&amp;); void printBoard(); bitset&lt;25&gt; getBoard() { return this-&gt;board; } void flip(int x); int getId() { return id; } int getParentId() { return parentId; } int getParentMove() { return parentMove; } };

Similar Samples

Welcome to our Samples section! Here, you can explore a variety of programming assignments and projects that showcase our expertise. From basic coding exercises to complex algorithm implementations, these samples highlight our commitment to delivering high-quality solutions. Take a look to see the standard of work you can expect from our dedicated team.