#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/wait.h>

void print_choice(char *who, int choice);

int main(int argc, char *argv[]) {
int pipe1[2], pipe2[2];
pid_t opponent_pid;
int my_choice, other_choice, score = 0, tries = 0;
struct timeval tv;


if ((pipe(pipe1) == -1) || (pipe(pipe2) == -1)) {
	perror("Creating pipe");
	exit(1);
}

opponent_pid = fork();
if (opponent_pid == -1) {
	perror("Creating opponent");
	exit(1);
}

gettimeofday(&tv, NULL);
srand(tv.tv_usec);

if (opponent_pid == 0) { /* child */
	close(pipe1[0]);
	close(pipe2[1]);
	for (tries = 0; tries < 10; tries++) {
		my_choice = rand() % 3;
		print_choice("child", my_choice);
		/* Read opponent's choice */
		/* Send choice to opponent */
		print_choice("child->opponent's choice", other_choice);
		/* determine win, loss or tie, adjust score */
	}
	/* print score */
	close(pipe1[1]);
	close(pipe2[0]);
	return 0;
}
else { /* parent */
	close(pipe1[1]);
	close(pipe2[0]);
	for (tries = 0; tries < 10; tries++) {
		my_choice = rand() % 3;
		print_choice("parent", my_choice);
		/* Read opponent's choice */
		/* Send choice to opponent */
		print_choice("parent->opponent's choice", other_choice);
		/* determine win, loss or tie, adjust score */
	}
	/* print score */
	close(pipe1[0]);
	close(pipe2[1]);
	wait(NULL);
}
return 0;
}

void print_choice(char *who, int choice) {
static char *descr[] = {"petra", "psalidi", "xarti", "AKYROS KWDIKOS"};
char *theDesc;

if ((choice >= 0) && (choice <= 2))
	theDesc = descr[choice];
else
	theDesc = descr[3];
printf("%s: %s\n", who, theDesc);
}
