Question

In: Computer Science

Can you write a program for snakes and ladder board game using linked lists in c++

Can you write a program for snakes and ladder board game using linked lists in c++

Solutions

Expert Solution

/ C++ program to find minimum number of dice throws required to

// reach last cell from first cell of a given snake and ladder

// board

#include<iostream>

#include <queue>

using namespace std;

// An entry in queue used in BFS

struct queueEntry

{

    int v;     // Vertex number

    int dist; // Distance of this vertex from source

};

// This function returns minimum number of dice throws required to

// Reach last cell from 0'th cell in a snake and ladder game.

// move[] is an array of size N where N is no. of cells on board

// If there is no snake or ladder from cell i, then move[i] is -1

// Otherwise move[i] contains cell to which snake or ladder at i

// takes to.

int getMinDiceThrows(int move[], int N)

{

    // The graph has N vertices. Mark all the vertices as

    // not visited

    bool *visited = new bool[N];

    for (int i = 0; i < N; i++)

        visited[i] = false;

    // Create a queue for BFS

    queue<queueEntry> q;

    // Mark the node 0 as visited and enqueue it.

    visited[0] = true;

    queueEntry s = {0, 0}; // distance of 0't vertex is also 0

    q.push(s); // Enqueue 0'th vertex

    // Do a BFS starting from vertex at index 0

    queueEntry qe; // A queue entry (qe)

    while (!q.empty())

    {

        qe = q.front();

        int v = qe.v; // vertex no. of queue entry

        // If front vertex is the destination vertex,

        // we are done

        if (v == N-1)

            break;

        // Otherwise dequeue the front vertex and enqueue

        // its adjacent vertices (or cell numbers reachable

        // through a dice throw)

        q.pop();

        for (int j=v+1; j<=(v+6) && j<N; ++j)

        {

            // If this cell is already visited, then ignore

            if (!visited[j])

            {

                // Otherwise calculate its distance and mark it

                // as visited

                queueEntry a;

                a.dist = (qe.dist + 1);

                visited[j] = true;

                // Check if there a snake or ladder at 'j'

                // then tail of snake or top of ladder

                // become the adjacent of 'i'

                if (move[j] != -1)

                    a.v = move[j];

                else

                    a.v = j;

                q.push(a);

            }

        }

    }

    // We reach here when 'qe' has last vertex

    // return the distance of vertex in 'qe'

    return qe.dist;

}

// Driver program to test methods of graph class

int main()

{

    // Let us construct the board given in above diagram

    int N = 30;

    int moves[N];

    for (int i = 0; i<N; i++)

        moves[i] = -1;

    // Ladders

    moves[2] = 21;

    moves[4] = 7;

    moves[10] = 25;

    moves[19] = 28;

    // Snakes

    moves[26] = 0;

    moves[20] = 8;

    moves[16] = 3;

    moves[18] = 6;

    cout << "Min Dice throws required is " << getMinDiceThrows(moves, N);

    return 0;

}


Related Solutions

Can you program Exploding kittens card game in c++ using linked lists and classes! The game...
Can you program Exploding kittens card game in c++ using linked lists and classes! The game is played with between 2 and 4 players. You'll have a deck of cards containing some Exploding Kittens. You play the game by putting the deck face down and taking turns drawing cards until someone draws an Exploding Kitten. When that happens, that person explodes. They are now dead and out of the game. This process continues until there's only one player left, who...
I'm having trouble programming connect four board game using linked lists, sets and maps in c++....
I'm having trouble programming connect four board game using linked lists, sets and maps in c++. Can you code connect four game using these concepts.
Using C++, you will create a program, where you will create two doubly linked lists. These...
Using C++, you will create a program, where you will create two doubly linked lists. These doubly linked lists will contain integers within them. Using the numbers in both of these linked lists, you add the numbers together, and insert the addition of the two numbers into a singly linked list. the input can be from the user or you just write the input. for example, if one number in the doubly linked list is 817 and in the other...
Please include comments on what you are doing.   Using linked lists, write a Python program that...
Please include comments on what you are doing.   Using linked lists, write a Python program that performs the following tasks: store the records for each college found in the input file - colleges.csv - into a linked list. (File includes name and state data fields) allow the user to search the linked list for a college’s name; display a message indicating whether or not the college’s name was in the database allow the user to enter a state's name and...
C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of...
C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of songs/artist pairs. Allow your user to add song / artist pairs to the list, remove songs (and associated artist) from the list and be sure to also write a function to print the list! Have fun! Make sure you show your implementation of the use of vectors in this lab (You can use them too ) You MUST modularize your code ( meaning, there...
In C++ Use vectors instead of linked lists Create a Hash table program using H(key) =...
In C++ Use vectors instead of linked lists Create a Hash table program using H(key) = key%tablesize with Chaining and Linear probing for a text file that has a list of 50 numbers Ask the user to enter the file name, what the table size is, and which of the two options they want to use between chaining and linear probing
C++ Write a C++ program that implements a tree using a linked representation Each node will...
C++ Write a C++ program that implements a tree using a linked representation Each node will contain a single integer data element. Initialize the tree to contain 10 nodes. The program should allow for the insertion and deletion of data. The program should allow the user to output data in Preorder, Inorder and Postorder.
Can you solve this C program by using Function? Q1. Write a C program to ring...
Can you solve this C program by using Function? Q1. Write a C program to ring the computer bell at any number of times you specify. Use the system clock as a delay, you need to include the time header file.
Write a program that plays snakes and ladders.
 C++ program Write a program that plays snakes and ladders.  How to play:   Each player puts their counter on the tile that says 'start here'.   Take it in turns to roll the dice. Move your counter forward the number of spaces shown on the dice.   If your counter lands at the bottom of a ladder, you can move up to the top of the ladder.   If your counter lands on the head of a snake, you must slide down to the bottom...
Write a code for simple racing game (using dots) on c program.
Write a code for simple racing game (using dots) on c program.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT