Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

MSKÜ - CENG 2034 - Operating Systems - Spring 2022-2023

Assignment II (35 pts)

Burak Ekici

Assigned : April the 14th , 23h55


Due : April the 27th , 23h55

1 Basics

The main purpose of this assignment is to write a C program that realizes the “rock-paper-scissors” game.
The game is supposed to be played by two players, player1 and player2 , respectively represented by a pair
of concurrently running pthreads sharing a board detailed below.
s t r u ct board
{
char ∗ buffer [ 2 ] ;
sem_t empty;
sem_t f u l l ;
};

typedef st ruc t board BRD;

Constraints of the board interaction are itemized as follows:


• player1 and player2 individually generate random numbers in the rage of [1, 3] such that
1 ⇐⇒ rock
2 ⇐⇒ paper
3 ⇐⇒ scissors.
• player1 and player2 update a shared string array “char ∗buffer[2]” such that
“buffer[0]” stores player1 ’s, and
“buffer[1]” stores player2 ’s
choice. E.g.,
“buffer[0]” contains rock
“buffer[1]” stores paper
if the player1 randomly generates 1 while this number is 2 for the player2 .
• the shared array “buffer[2]” must be updated sequentially. This could be achieved by employing the
semaphores “empty” and “full”.

2 Tasks

Implement in C below detailed functions, leaving the signatures unchanged:


a) (8 pts) void player1(BRD ∗) ;
plays the role of the player1 – takes the shared board, randomly generates a number in the range [1, 3],
and calls the function that updates the shared buffer with necessary arguments.

page 1 of 3
Note: recall that the buffer must be updated sequentially.
b) (8 pts) void player2(BRD ∗) ;
plays the role of the player2 – performs similarly with the function player1 given in above item “a)”.
c) (5 pts) int decideHandWinner(BRD ∗) ;
takes the shared board, decides the winner of the hand, and returns

0, if the hand ends in a draw
1, if player1 wins

2, if player2 wins.
The hand winner-decider table:
player1 ’s choice player2 ’s choice hand result function return value
rock paper player2 wins the hand 2
rock scissors player1 wins the hand 1
paper rock player1 wins the hand 1
paper scissors player2 wins the hand 2
scissors rock player2 wins the hand 2
scissors paper player1 wins the hand 1
otherwise draw hand; no winner 0
d) (5 pts) void printBoard(BRD ∗) ;
takes the shared board, and pretty-prints it in the following shape:
[p1: scissors | p2: paper ] --- player 1 wins the hand!
[p1: paper | p2: scissors] --- player 2 wins the hand!
[p1: rock | p2: rock ] --- draw hand; no winner!
e) (9 pts) int main(void) ;
makes sure that
• the game is being played by a pair of pthreads concurrently at every distinct game hand
• the game is on unless either of the players wins more than n/ 2 games for some n > 0; notice that
for even choices of n, there are chances to end up with a draw game
• an example to the program output with n = 7:
[p1: rock | p2: rock ] --- draw hand; no winner!
[p1: paper | p2: rock ] --- player 1 wins the hand!
[p1: paper | p2: paper ] --- draw hand; no winner!
[p1: rock | p2: rock ] --- draw hand; no winner!
[p1: paper | p2: paper ] --- draw hand; no winner!
[p1: rock | p2: scissors ] --- player 1 wins the hand!
[p1: paper | p2: paper ] --- draw hand; no winner!
[p1: scissors | p2: paper ] --- player 1 wins the hand!
[p1: paper | p2: scissors ] --- player 2 wins the hand!
[p1: scissors | p2: rock ] --- player 2 wins the hand!
[p1: rock | p2: scissors ] --- player 1 wins the hand!

*** player 1 achieves the best of 7 with the score: (4-2); congrats player 1 ***

page 2 of 3
Nota Bene (in general).
1. receive support from helper functions if needed;
2. the attached file (rps.c) must be the starting point;
3. do not remove or modify the types or functions contained in the file rps.c, and implement your functions
obeying the signatures stated above.

Important Notice:
• Collaboration is strictly and positively prohibited; lowers your score to 0 if detected.
• Any submission after 23h55 on April the 27th will NOT be accepted. Please beware and respect the
deadline!
• Implement your code within a file named yourname_surname.c, and submit it either in the raw form as it is
or in the ZIP compressed form. Do not RAR files.

page 3 of 3

You might also like