Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

All fans of Harry Potter and the books bearing his name are familiar with Quidditch, a popular sports in the wizarding world played on flying

All fans of Harry Potter and the books bearing his name are familiar with Quidditch, a popular sports in the wizarding world played on flying broomsticks! A concise description of the sports can be seen at: https://en.wikipedia.org/wiki/Quidditch. In this lab, we will use pthreads and signals to simulate a game of Quidditch.

Here is some relevant information from the above mentioned wikipedia page:

"The objective of Quidditch, as with most sports, is to be the team that has gained the most points by the end of the match. Matches are played between two opposing teams of seven players riding flying broomsticks; using four balls: a Quaffle, two Bludgers, and a Golden Snitch. Centred around the use of each ball, there are four positions: the Chasers and Keeper (who play with the Quaffle), the Beaters (who play with the Bludgers), and the Seekers (who play with the Golden Snitch)."

"Quidditch matches are played over an oval-shaped pitch, with a scoring area at each end consisting of three hooped goal posts, each at a different height. Each team is made up of seven players, consisting of three Chasers, two Beaters, one Keeper and one Seeker. The job of the Chasers is to keep possession of the scarlet Quaffle, a leather ball passed between players. They must attempt to score goals (worth 10 points) by throwing it through one of the opponents' three hoops. These hoops are defended by the opposing team's Keeper, who ideally tries to block their goals. Meanwhile, players of both teams are attacked indiscriminately by the two Bludgers. These are round, jet-black balls made of iron that fly around violently trying to knock players off their brooms. It is the Beaters' job to defend their teammates from the Bludgers; they carry short wooden clubs, which they use to knock the Bludgers away from their teammates and/or toward the opposing team. Finally, the role of the Seeker is to catch the Golden Snitch. This is a small golden ball the approximate size of a walnut. The winged Snitch is enchanted to hover, dart, and fly around the pitch, avoiding capture while remaining within the boundaries of the playing area. Catching the Snitch ends the game and scores the successful Seeker's team 150 points. "

All fourteen players of two teams, four balls and two goal posts will be represented by pthreads and will communicate with each other via signals. Pointers to pthread_t objects representing these pthreads will be stored in appropriately named global variables.

Two pthreads representing two Bludgers will spend most of their time sleeping. Once in a while, a Bludger will wake up, randomly pick one of 14 players and send it a SIGINT signal. Receipt of a SIGINT by a player pthread is assumed to be a hit from a Bludger. When a player pthread receives a hit from a Bludger (i.e. it receives a SIGINT signal), she will fall off her broomstick and hence exit the game UNLESS it receives a SIGUSR1 signal within 2 seconds (or some other suitable time duration) of receiving a SIGINT. Receipt of a SIGUSR1 signal by a player pthread is considered as a Beater intercepting the Bludger. A Beater pthread essentially performs an infinite loop where in each iteration it randomly selects a teammate still around (possibly herself), sends this teammate a SIGUSR1 (guessing that the teammate is hit by a Bludger) and goes to sleep for some random short time duration.

The pthread representing the Quaffle also performs an infinite loop where in each iteration it randomly picks a Chaser and sends it a SIGUSR2 signal. The receipt of a SIGUSR2 signal by a Chaser means that this Chaser is in possession of the Quaffle. The Chaser then sends a SIGINT signal to the pthread representing the opponent's goal post, which indicates an attempt to score a goal. The Keeper pthread can prevent this goal by sending its goal post a SIGUSR1 signal within 2 seconds of the goal post receiving the SIGINT signal. For this reason, each Keeper pthread executes an infinite loop where in each iteration it sends a SIGUSR1 signal to her goal post (to prevent any goal attempt) and then goes to sleep for a small random time duration. If a goal does happen (i.e. no SIGUSR1 signal is received by the goal post pthread within 2 seconds of receiving a SIGINT goal attempt), the goal post pthread increases by 10 points the global variable indicating the the opponent team's score.

The pthread representing the Golden Snitch periodically wakes up, toggles the value of a global boolean flag "Caught_Snitch" and then goes back to sleep. Each Seeker pthread periodically wakes up and checks the value of "Caught_Snitch". If the value is true, the Seeker adds 150 points to her team score, prints the final scores of both teams and calls exit() to terminate the simulation. If the value is false, the Seeker goes back to sleep. The simulation should also terminate if the last player falls off the broom. All the pthreads should print informative messages (also containing the current team scores) whenever any event occurs (e.g. a Bludger targeting a player, the player falling off the broom, a goal scored/prevented, catching of the Snitch etc.). You can decide any reasonable values for different sleep durations.

Step by Step Solution

3.33 Rating (159 Votes )

There are 3 Steps involved in it

Step: 1

1 The objective of Quidditch is to score more points than the opposing team by the end of the match ... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Probability and Stochastic Processes A Friendly Introduction for Electrical and Computer Engineers

Authors: Roy D. Yates, David J. Goodman

3rd edition

1118324560, 978-1118324561

More Books

Students also viewed these Computer Network questions