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:
- main thread
Initialize statistics: # of wins for player 1 and 2
Loop 100 rounds
create two player threads
wait for result back (join)
judge and report result
update statistics
End loop
Report game statistics and game result
- player thread
pick one option: random number generation
Terminate and send result out
- Requires: pthread methods (pthread.h)
Paper-rock-scissors game started .. . Round 1: player 2 won (paper)-paper covers rocks Round 2: tie - both players played rock Round 3: player 2 won (scissors)scissors cut paper Round 4: player 2 won (paper) -paper covers rocks Round 5: player 2 won (scissors)scissors cut paper Round 6: player 1 won (rock) - rocks crush scissors Round 7 tie both players played scissors Round 8: player 1 won (rock) - rocks crush scissors Round 9: player 1 won (rock) - rocks crush scissors Round 10: player 1 won (paper)-paper covers rocksSolution
Executable Code
//Include the needed headers
#include <stdio.h>
#include <pthread.h>
#include <time.h>
#include <stdlib.h>
#include <semaphore.h>
//Declare constant variables.
const int RUNS = 100;
const char* const MYROLLS[] = {\"rock\", \"paper\", \"scissors\"};
//Declare semaphore variables.
//Mutex.
sem_t mutex[2];
//Empty.
sem_t empty[2];
//Full.
sem_t full[2];
//Buffer.
int bufferArray[2];
//Method playGenerator().
void* playGenerator(void* threadID)
{
//The thread generates rock/paper/scissor
//And put it in a buffer.
if ((int) threadID > 2)
{
printf(\"\ ThreadID cannot be more than 2.\ \");
printf(\"Ending...\ \");
exit(1);
}
//For loop()
for (int k=0; k<RUNS; k++)
{
//Request a empty space.
sem_wait(&empty[(int) threadID]);
//Request a critical region.
sem_wait(&mutex[(int) threadID]);
//Critical region
bufferArray[(int) (int) threadID] = rand() % 3;
//Release the critical region.
sem_post(&mutex[(int) threadID]);
//Release one full slot.
sem_post(&full[(int) threadID]);
}
pthread_exit(NULL);
}
//Main().
int main()
{
//Declare the needed variables.
//Player threads.
pthread_t pTh1, pTh2;
int rollOne, rollTwo;
int scoreOne = 0;
int scoreTwo = 0;
int numberOfTies = 0;
//For randomly generating number.
srand(time(NULL));
//Begin game.
printf(\"Game begins...\");
//Create thread.
int ptOne = pthread_create(&pTh1, NULL, playGenerator, 0);
int ptTwo = pthread_create(&pTh2, NULL, playGenerator, 1);
//Condition check.
if (ptOne || ptTwo)
{
printf(\"\ Failure in creating the player thread. ending. \");
exit(1);
}
//For loop() to iterate.
for(int k=1; k<=RUNS; k++)
{
//Player1.
//Ask a full space.
sem_wait(&full[0]);
//Ask a critical region.
sem_wait(&mutex[0]);
//Get player1 roll.
rollOne = bufferArray[0];
//Give critical region.
sem_post(&mutex[0]);
//Give empty space.
sem_post(&empty[0]);
//Player 2.
//Ask a full space.
sem_wait(&full[1]);
//Ask a critical region.
sem_wait(&mutex[1]);
//Get player2 roll.
rollOne = bufferArray[1];
//Give critical region.
sem_post(&mutex[1]);
//Give an empty space.
sem_post(&empty[1]);
//Comparison of Scores.
//Draw condition.
if (rollOne == rollTwo)
{
numberOfTies++;
printf(\"\ Round %d: tie - both players played %s\", k, MYROLLS[rollOne]);
}
//Player one played rock.
else if(rollOne == 0)
{
//Player1 played paper -> wins.
if (rollTwo == 2)
{
scoreTwo++;
printf(\"\ Round %d: player2 won (%s) - paper covers rocks\", k, MYROLLS[rollTwo]);
}
//Player2 played scissors -> loses.
else
{
scoreOne++;
printf(\"\ Round %d: player1 won (%s) - rocks crushes scissors\", k, MYROLLS[rollOne]);
}
}
//Player1 played paper.
else if (rollOne == 1)
{
//Player2 played rock -> loses.
if (rollTwo == 0)
{
scoreOne++;
printf(\"\ Round %d: player1 won (%s) - paper covers rocks\", k, MYROLLS[rollOne]);
}
//Player2 played scissors -> wins.
else
{
scoreTwo++;
printf(\"\ Round %d: player2 won (%s) - scissors cuts paper\", k, MYROLLS[rollTwo]);
}
}
//Player one played scissors.
else
{
//Player2 played rock -> wins.
if(rollTwo == 0)
{
scoreTwo++;
printf(\"\ Round %d: player 2 won (%s) - rocks crushes scissors\", k, MYROLLS[rollTwo]);
}
// player2 played paper -> loses.
else
{
scoreTwo++;
printf(\"\ Round %d: player 1 won (%s) - scissors cuts paper\", k, MYROLLS[rollOne]);
}
}
}
//Results.
printf(\"\ Game result: \");
//Tie.
if (scoreOne == scoreTwo)
{
printf(\"Game tied - \");
}
//Player one wins.
else if (scoreOne > scoreTwo)
{
printf(\"player1 won - \");
}
//Player two wins.
else
{
printf(\"player2 won - \");
}
printf(\"player 1 won %d rounds, player2 won %d rounds, tie %d rounds.\ \", scoreOne, scoreTwo, numberOfTies);
pthread_exit(NULL);
}





