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<<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 >> 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:"<<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 << "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<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<25>((1 << 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<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()<<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<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<state>&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<iostream>
#include<bitset>
#include<vector>
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<state>&);
void printBoard();
bitset<25> getBoard() { return this->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.
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++