Question

In: Computer Science

Implement Hot Potato game A group of people, numbered 1 to N, are sitting in a...

Implement Hot Potato game

A group of people, numbered 1 to N, are sitting in a circle. Starting at person 1, a hot potato is passed. After X passes, the person holding the hot potato is eliminated, the circle closes ranks, and the game continues with the person who was sitting after the eliminated person picking up the hot potato. The last remaining person wins.

For example:

  • if X = 0 and N = 5, players are eliminated in order, and player 5 wins
  • If X = 1 and N = 5, the order of elimination is 2, 4, 1, 5.   

Write a program for general values of X and N.

  • Ask a user for number of people and number of passes
  • To speed up input, debugging, store names of the people in a file. Make sure no two names start the same letter ( Alex and Ana are not OK)
  • Output number and/or the name of a person being eliminated
  • Output number and the name of the winner
  • Allow user to play the game as many times as user wants
  • Node and List should be classes
  • use try/catch to check dynamic memory allocation
  • Make sure each function has a description, post and pre-conditions

Solutions

Expert Solution

The problem statement given is basically called as the josephus problem or the suicide circle problem and is an application of the queue.

The above diagram shows the execution of this problem.

The formula we use to resolve this problem is

josephus(n, k) = (josephus(n - 1, k) + k-1) % n + 1

  josephus(1, k) = 1

After the first person (kth from beginning) is killed, n-1 persons are left. So we call josephus(n – 1, k) to get the position with n-1 persons. But the position returned by josephus(n – 1, k) will consider the position starting from k%n + 1. So, we must make adjustments to the position returned by josephus(n – 1, k).

Program for this problem

// CPP program to find last man standing

#include<bits/stdc++.h>

using namespace std;

  

/* structure for a node in circular

   linked list */

struct Node

{

    int data;

    struct Node *next;

};

  

// To create a new node of circular

// linked list

Node *newNode(int data)

{

   Node *temp = new Node;

   temp->next = temp;

   temp->data = data;

}

  

/* Function to find the only person left

   after one in every m-th node is killed

   in a circle of n nodes */

void getJosephusPosition(int m, int n)

{

    // Create a circular linked list of

    // size N.

    Node *head = newNode(1);

    Node *prev = head;

    for (int i = 2; i <= n; i++)

    {

        prev->next = newNode(i);

        prev = prev->next;

    }

    prev->next = head; // Connect last

                       // node to first

  

    /* while only one node is left in the

    linked list*/

    Node *ptr1 = head, *ptr2 = head;

    while (ptr1->next != ptr1)

    {

        // Find m-th node

        int count = 1;

        while (count != m)

        {

            ptr2 = ptr1;

            ptr1 = ptr1->next;

            count++;

        }

  

        /* Remove the m-th node */

        ptr2->next = ptr1->next;

        free(ptr1);

        ptr1 = ptr2->next;

    }

  

    printf ("Last person left standing "

            "(Josephus Position) is %d\n ",

            ptr1->data);

}

  

/* Driver program to test above functions */

int main()

{

    int n = 14, m = 2;

    getJosephusPosition(m, n);

    return 0;

}

The last man standing (josephus position) = 13


Related Solutions

Objective: Write a program which simulates a hot potato game. In this version of a classic...
Objective: Write a program which simulates a hot potato game. In this version of a classic game, two or more players compete to see who can hold onto a potato the longest without getting caught. First the potato is assigned a random value greater than one second and less than three minutes both inclusive. This time is the total amount of time the potato may be held in each round. Next players are put into a circular list. Then each...
Consider a group of people sitting in a classroom acting very much alike: listening to the...
Consider a group of people sitting in a classroom acting very much alike: listening to the professor, taking notes, raising a hand to ask an occasional question. Now consider the same group of people at the beach on a sunny afternoon – some are swimming and riding the waves, some are reading quietly under an umbrella, some are sleeping in the sun, some are watching people, some are running and laughing and wrestling in the sea. Using ideas from chapter...
Use C language Level 2 Problem description n (n is odd) people sitting around a round...
Use C language Level 2 Problem description n (n is odd) people sitting around a round table playing a game. In this situation, everyone has a left neighbour and a right neighbour. At the beginning, each of them is holding a whiteboard with an integer number. Now, they are playing a game consisting of several rounds. In each round, everyone will first look at the numbers of his/her left neighbour and right neighbour, then calculate the average of the two...
Using C language Problem description n (n is odd) people sitting around a round table playing...
Using C language Problem description n (n is odd) people sitting around a round table playing a game. In this situation, everyone has a left neighbour and a right neighbour. At the beginning, each of them is holding a whiteboard with an integer number. Now, they are playing a game consisting of several rounds. In each round, everyone will first look at the numbers of his/her left neighbour and right neighbour, then calculate the average of the two numbers, replace...
Suppose that the birthdays of different people in a group of n people are independent, each...
Suppose that the birthdays of different people in a group of n people are independent, each equally likely to be on the 365 possible days. (Pretend there's no such thing as a leap day.) What's the smallest n so that it's more likely than not that someone among the n people has the same birthday as you? (You're not part of this group.)
PROBLEM 6. Suppose that the birthdays of different people in a group of n people are...
PROBLEM 6. Suppose that the birthdays of different people in a group of n people are independent, each equally likely to be on the 365 possible days. (Pretend there's no such thing as a leap day.)What's the smallest n so that it's more likely than not that someone among the n people has the same birthday as you? (You're not part of this group.)
BACKGROUND: Given a group of 'n' people, the odds that at least two people have the...
BACKGROUND: Given a group of 'n' people, the odds that at least two people have the same birthday are much higher than you would think. PLEASE WRITE CODE IN C++ The program takes no input. Assumptions: 1. There is an equal chance of birthday landing on any day of the year. 2. We are not considering a leap year (only 365 days) The simulation will be run in the following manner: 1. For a group size 2, assign a random...
In a certain game of? chance, a wheel consists of 42 slots numbered? 00, 0,? 1,...
In a certain game of? chance, a wheel consists of 42 slots numbered? 00, 0,? 1, 2,..., 40. To play the? game, a metal ball is spun around the wheel and is allowed to fall into one of the numbered slots. Complete parts? (a) through? (c) below. ?(a) Determine the sample space. Choose the correct answer below. A. The sample space is? {00}. B. The sample space is? {00, 0,? 1, 2,..., 40?}. C. The sample space is? {00, 0}....
In the game of​ roulette, a wheel consists of 38 slots numbered​ 0, 00,​ 1, 2,...,...
In the game of​ roulette, a wheel consists of 38 slots numbered​ 0, 00,​ 1, 2,..., 36. To play the​ game, a metal ball is spun around the wheel and is allowed to fall into one of the numbered slots. If the number of the slot the ball falls into matches the number you​ selected, you win​ $35; otherwise you lose​ $1. Complete parts ​(a) through ​(g) below (a) Construct a probability distribution for the random variable​ X, the winnings...
Suppose that there are n people in a group, each aware of a different secret no...
Suppose that there are n people in a group, each aware of a different secret no one else in the group knows about. These people communicate by phone; when two people in the group talk, they share information about all secretes each knows about. For example, on the first call, two people share information, so by the end of the call, each of them knows about two secretes. The gossip problem asks for the number of phone calls that are...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT