Paper Rock Scissors game using threads C Language Paper R

Paper - Rock - Scissors game using threads (C Language)

Paper - Rock - Scissors game (Paper covers rocks, rocks crush scissors and scissors cut paper). Main thread is the referee. Two additional threads as the two players.

The players will each select its option (use random number generator), and communicate back to main thread

The main thread declares a winner of each round

Assume 100 rounds. The player with more rounds of winnings win

game simulation may look like this:

Use the following approch to solve this problem:

Pseudocode: producer-consumer pattern

(recommended) set-up: each player-main use their own set of: one-slot buffer, mutex-empty-full

sem_t mutex[2];

sem_t empty[2];

sem_t full[2];

int buf[2];

main thread (consumer)

Initialize statistics

Initialize semaphores

create two player threads

Loop 100 rounds

     // consumer behavior

     consume full slot from player1

     consume full slot from player2

     // use result

     judge and report result

     update statistics

End loop

Report game statistics and game result

- player thread (producer)

Loop 100 rounds

    // produce

    pick one option // produce

    put in slot of this player

End loop

Terminate

- Requires: pthread methods (pthread.h) and semaphore methods (semaphore.h)

Solution

#include <stdio.h>
#include <conio.h>

main ()
{
char p1, p2;
clrscr ();
printf (\"\ Enter Player 1: \");
p1=getche ();
printf (\"\ Enter Player 2: \");
p2=getche ();

if (((p1==\'p\')||(p1==\'P\')) && ((p2==\'R\') || (p2==\'r\')))

{
printf (\"\ Player 1 Wins !\");
printf (\"\ Paper Covers Rock\");
}

else if (((p2==\'p\') || (p2==\'P\')) && ((p1==\'r\') || (p1==\'R\')))

{
printf (\"\ Player 2 Wins !\");
printf (\"\ Paper Covers Rock\");
}

else if (((p1==\'r\') || (p1==\'R\')) && ((p2==\'s\') || (p2==\'S\')))

{
printf (\"\ Player 1 Wins !\");
printf (\"\ Rock Breaks Scissors\");
}

else if (((p2==\'r\') || (p2==\'R\')) && ((p1==\'s\') || (p1==\'S\')))

{
printf (\"\ Player 2 Wins !\");
printf (\"\ Rock Breaks Scissors\");
}

else if (((p1==\'s\') || (p1==\'S\')) && ((p2==\'p\') || (p2==\'P\')))

{
printf (\"\ Player 1 Wins !\");
printf (\"\ Scissors Cut Paper\");
}

else if (((p2==\'s\') || (p2==\'S\')) && ((p1==\'p\') || (p1==\'P\')))
{
printf (\"Player 2 Wins !\");
printf (\"Scissors Cut Paper\");
}

else if (p1==p2)
{
printf (\"\ Nobody Wins !\");
printf (\"\ Both Of You Entered The Same Letters\");
}

else {

printf (\"\ Unknown Input Data!\");
printf (\"\ Enter Only Letters : P,S or R\");
}

getch ();

}

Paper - Rock - Scissors game using threads (C Language) Paper - Rock - Scissors game (Paper covers rocks, rocks crush scissors and scissors cut paper). Main thr
Paper - Rock - Scissors game using threads (C Language) Paper - Rock - Scissors game (Paper covers rocks, rocks crush scissors and scissors cut paper). Main thr
Paper - Rock - Scissors game using threads (C Language) Paper - Rock - Scissors game (Paper covers rocks, rocks crush scissors and scissors cut paper). Main thr

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site