Professional Documents
Culture Documents
Assignment II (35 PTS) : Burak Ekici
Assignment II (35 PTS) : Burak Ekici
Burak Ekici
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 ;
};
2 Tasks
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