QuestionQuestion

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! In this lab, we will use pthreads and signals to simulate a game of Quidditch.

Here is some relevant information from the 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.
PS: In order to compile your code with gcc, you would need to include the flag -lpthread:
gcc hw5.c -o quidditch -lpthread

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>



#define SNITCH 0
#define QUAFFLE 1
#define BLUDGER_1 2
#define BLUDGER_2 3


#define TEAM_1_INDEX 0
#define TEAM_2_INDEX 1

#define SEEKER 0
#define KEEPER 1
#define BEATERS 2
#define CHASERS 4


#define BEATER_NUM 2
#define CHASER_NUM 3
#define TEAM_NUM 2
#define TEAM_MEMBER_NUM 7
#define GOAL_NUM 2
#define BALL_NUM 4

#define QUAFFLE_SCORE 10
#define SNITCH_SCORE 150


typedef int boolean;
#define TRUE 1
#define FALSE 0
#define SNITCH_MAX_RAND_TIME 30
#define BLUDGER_MAX_RAND_TIME 42
#define MAX_RAND_TIME 15
#define MIN_RAND_TIME 2
#define WAITING_TIME 2



// global variables

pthread_t * goals;
pthread_t * balls;
pthread_t ** players;
boolean Caught_Snitch;
boolean snitch_caught;
int player_num;
int score[2];
boolean on_broomstick[TEAM_NUM][TEAM_MEMBER_NUM];

int getTeamFromPthread();
int getPlayerFromPthread();

/**
* signal handlers
* @param signo
*/
static void int_handler(int signo),
usr1_handler(int signo),
usr2_handler(int signo),
quit_handler(int signo);

void exit_game();

void * snitch_thread(void * param);
void * seeker_thread(void * param);
void * goal_thread(void * param);
void * keeper_thread(void * param);

int getRandomOnBroomPlayerIndex(int team);
int getRandomOnBroomChaserIndexFromTeam(int team);
int getRandomOnBroomChaserIndex(int * team);

void * beater_thread(void * param);

void * chaser_thread(void * param);...

By purchasing this solution you'll be able to access the following files:
Solution.c.

$78.00
for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Operating Systems Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats