Untitled
unknown
c_cpp
4 years ago
1.9 kB
11
Indexable
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
void makeYourPlay(int number){
char input;
printf("Es el turno del jugador %d \n",number);
printf("Escriba el numero de su opción:\n1-Piedra\n2-Papel\n3-Tijera\n");
}
int main(){
int pipePlayer1[2],pipePlayer2[2],pipeWaitP1[2],pipeWaitP2[2];
char inputPlayer1[1] = {'x'}, inputPlayer2[1] = {'x'}, waitP1[1] = {'1'},waitP2[1] = {'1'};
pid_t pidPlayer1, pidPlayer2;
pipe(pipePlayer1);
pipe(pipePlayer2);
pipe(pipeWaitP1);
pipe(pipeWaitP2);
// continueGame == 0 stop te game
char continueGame = '1';
while (continueGame != '0'){
if (inputPlayer1[0] == 'x'){
pidPlayer1 = fork();
if (pidPlayer1 == 0){
makeYourPlay(1);
close(pipePlayer1[0]);
close(pipeWaitP1[1]);
scanf("%c",&inputPlayer1[0]);
write(pipePlayer1[1],inputPlayer1,sizeof(inputPlayer1));
while(waitP1[0] != '1'){
read(pipeWaitP1[0],waitP1,sizeof(waitP1));
}
}
else {
close(pipePlayer1[1]);
close(pipeWaitP1[0]);
read(pipePlayer1[0],inputPlayer1, sizeof(inputPlayer1));
}
}
if (inputPlayer2[0] == 'x'){
pidPlayer2 = fork();
if (pidPlayer2 = 0){
makeYourPlay(2);
close(pipePlayer2[0]);
close(pipeWaitP2[1]);
scanf("%c",&inputPlayer2[0]);
write(pipePlayer2[1],inputPlayer2, sizeof(inputPlayer2));
while(waitP2[0] != '1'){
read(pipeWaitP2[0], waitP2,sizeof(waitP2));
}
}
else {
close(pipePlayer2[1]);
close(pipeWaitP2[0]);
read(pipePlayer2[0],inputPlayer2, sizeof(inputPlayer2));
}
}
if (pidPlayer1 != 0 && pidPlayer2 != 0){
if (inputPlayer1[0] == inputPlayer2[0]){
printf("Ambos jugadores escogieron la misma opción, ambos mueren");
kill(pidPlayer1, SIGKILL);
kill(pidPlayer2, SIGKILL);
}
}
}
}Editor is loading...