Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. 8Puzzle game

8Puzzle game

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresgame-devalgorithmstutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Abdulrahman Mostafa
    wrote on last edited by
    #1

    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 <<
    
    L D 2 Replies Last reply
    0
    • A Abdulrahman Mostafa

      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 <<
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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;
      

      }

      1 Reply Last reply
      0
      • A Abdulrahman Mostafa

        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 <<
        
        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups