8Puzzle game
-
I have been trying to create a puzzle that can solve itself using BFS algorithm. Currently, the search algorithm and the main but I don't know how to integrate both of them together. my BFS:
#ifndef BFS_h
#define BFS_h
// Program to print BFS traversal from a given source vertex. BFS(int s)
// traverses vertices reachable from s.
#include<iostream>
#include <list>using namespace std;
// This class represents a directed graph using adjacency list representation
class Graph
{
int V; // No. of vertices
list<int> *adj; // Pointer to an array containing adjacency lists
public:
Graph(int V); // Constructor
void addEdge(int v, int w); // function to add an edge to graph
void BFS(int s); // prints BFS traversal from a given source s
};Graph::Graph(int V)
{
this->V = V;
adj = new list<int>[V];
}void Graph::addEdge(int v, int w)
{
adj[v].push_back(w); // Add w to v’s list.
}void Graph::BFS(int s)
{
// Mark all the vertices as not visited
bool *visited = new bool[V];
for(int i = 0; i < V; i++)
visited[i] = false;// Create a queue for BFS list<int> queue; // Mark the current node as visited and enqueue it visited\[s\] = true; queue.push\_back(s); // 'i' will be used to get all adjacent vertices of a vertex list<int>::iterator i; while(!queue.empty()) { // Dequeue a vertex from queue and print it s = queue.front(); cout << s << " "; queue.pop\_front(); // Get all adjacent vertices of the dequeued vertex s // If a adjacent has not been visited, then mark it visited // and enqueue it for(i = adj\[s\].begin(); i != adj\[s\].end(); ++i) { if(!visited\[\*i\]) { visited\[\*i\] = true; queue.push\_back(\*i); } } }
}
and my main:
#include "queue"
#include <iostream>
#include <iomanip>
#include "BFS.h"
#include "A*search.h"using namespace std;
int main()
{
int s;
int box1=3,box2=6,box3=9,box4=1,box5=s,box6=7,box7=5,box8=8,box9=2;cout << " -----------" << endl; cout << " | " << box1 << " | " << box2 << " | " << box3 <<
-
I have been trying to create a puzzle that can solve itself using BFS algorithm. Currently, the search algorithm and the main but I don't know how to integrate both of them together. my BFS:
#ifndef BFS_h
#define BFS_h
// Program to print BFS traversal from a given source vertex. BFS(int s)
// traverses vertices reachable from s.
#include<iostream>
#include <list>using namespace std;
// This class represents a directed graph using adjacency list representation
class Graph
{
int V; // No. of vertices
list<int> *adj; // Pointer to an array containing adjacency lists
public:
Graph(int V); // Constructor
void addEdge(int v, int w); // function to add an edge to graph
void BFS(int s); // prints BFS traversal from a given source s
};Graph::Graph(int V)
{
this->V = V;
adj = new list<int>[V];
}void Graph::addEdge(int v, int w)
{
adj[v].push_back(w); // Add w to v’s list.
}void Graph::BFS(int s)
{
// Mark all the vertices as not visited
bool *visited = new bool[V];
for(int i = 0; i < V; i++)
visited[i] = false;// Create a queue for BFS list<int> queue; // Mark the current node as visited and enqueue it visited\[s\] = true; queue.push\_back(s); // 'i' will be used to get all adjacent vertices of a vertex list<int>::iterator i; while(!queue.empty()) { // Dequeue a vertex from queue and print it s = queue.front(); cout << s << " "; queue.pop\_front(); // Get all adjacent vertices of the dequeued vertex s // If a adjacent has not been visited, then mark it visited // and enqueue it for(i = adj\[s\].begin(); i != adj\[s\].end(); ++i) { if(!visited\[\*i\]) { visited\[\*i\] = true; queue.push\_back(\*i); } } }
}
and my main:
#include "queue"
#include <iostream>
#include <iomanip>
#include "BFS.h"
#include "A*search.h"using namespace std;
int main()
{
int s;
int box1=3,box2=6,box3=9,box4=1,box5=s,box6=7,box7=5,box8=8,box9=2;cout << " -----------" << endl; cout << " | " << box1 << " | " << box2 << " | " << box3 <<
You need to create a
Graph
object, with one of its constructors, and then call the various methods to construct and solve the puzzle:int main()
{
int s;
int box1=3,box2=6,box3=9,box4=1,box5=s,box6=7,box7=5,box8=8,box9=2;cout << " -----------" << endl; cout << " | " << box1 << " | " << box2 << " | " << box3 << " | " << endl; cout << " -----------" << endl; cout << " | " << box4 << " | " << box5 << " | " << box6 << " | " << endl; cout << " -----------" << endl; cout << " | " << box7 << " | " << box8 << " | " << box9 << " | " << endl; cout << " -----------" << endl;
// void BFS(int s);
Graph graph = new Graph(V); // where does the value of V come from?
graph.addEdge(v, w); // what are these values?
graph.BFS(int s); // where does the value of s come from?return 0;
}
-
I have been trying to create a puzzle that can solve itself using BFS algorithm. Currently, the search algorithm and the main but I don't know how to integrate both of them together. my BFS:
#ifndef BFS_h
#define BFS_h
// Program to print BFS traversal from a given source vertex. BFS(int s)
// traverses vertices reachable from s.
#include<iostream>
#include <list>using namespace std;
// This class represents a directed graph using adjacency list representation
class Graph
{
int V; // No. of vertices
list<int> *adj; // Pointer to an array containing adjacency lists
public:
Graph(int V); // Constructor
void addEdge(int v, int w); // function to add an edge to graph
void BFS(int s); // prints BFS traversal from a given source s
};Graph::Graph(int V)
{
this->V = V;
adj = new list<int>[V];
}void Graph::addEdge(int v, int w)
{
adj[v].push_back(w); // Add w to v’s list.
}void Graph::BFS(int s)
{
// Mark all the vertices as not visited
bool *visited = new bool[V];
for(int i = 0; i < V; i++)
visited[i] = false;// Create a queue for BFS list<int> queue; // Mark the current node as visited and enqueue it visited\[s\] = true; queue.push\_back(s); // 'i' will be used to get all adjacent vertices of a vertex list<int>::iterator i; while(!queue.empty()) { // Dequeue a vertex from queue and print it s = queue.front(); cout << s << " "; queue.pop\_front(); // Get all adjacent vertices of the dequeued vertex s // If a adjacent has not been visited, then mark it visited // and enqueue it for(i = adj\[s\].begin(); i != adj\[s\].end(); ++i) { if(!visited\[\*i\]) { visited\[\*i\] = true; queue.push\_back(\*i); } } }
}
and my main:
#include "queue"
#include <iostream>
#include <iomanip>
#include "BFS.h"
#include "A*search.h"using namespace std;
int main()
{
int s;
int box1=3,box2=6,box3=9,box4=1,box5=s,box6=7,box7=5,box8=8,box9=2;cout << " -----------" << endl; cout << " | " << box1 << " | " << box2 << " | " << box3 <<
So did someone give you these two pieces of code?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles