I am making a connect four game and I would like to have Multiple processes and pipes, but I'm not sure where to start. I know you have to use fork and pipe, but when I fork just before the start of the game I get the same result for each game. I'm just confused where to go from here. Any suggestion would be greatly appreciated. Below is some of my code, I remover the parts for checking for wins, because I is not necessary to see.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
int *moves;
//int **Board;
int w;
void display(int ** Board,int rows, int columns);
int** build_board(int N);
int makeMove(int** Board, int player);
int checkVictory(int** Board);
int checkHorr(int** Board);
void AI_move(int** Board,int player, int player2);
void play(int **Board);
int main(){
srand((int) time(NULL));
int width= 8;
w=8-1;
int** Board=build_board(width);
int **Board2=build_board(width);
//display(width, length);
int i, check;
if(fork()==0){
play(Board);
}else{
puts("In Else");
play(Board2);
}
return 0;
}
void play(int **Board){
int i, check;
for (i=0; i<((w+1)*(w+1)/2); i++) {
AI_move(Board,1,2);
// makeMove(Board, 1);
// display(width, width);
check=checkVictory(Board);
if (check==1 || check==2) {
puts("Winning Board");
display(Board,w+1, w+1);
break;
}
// AI_move(Board,2,1);
makeMove(Board,2);
check=checkVictory(Board);
if (check==1 || check==2) {
puts("Winning Board");
display(Board, w+1, w+1);
break;
}
}
}
int** build_board(int N){
int i,j;
int **Board = (int**) malloc(N*sizeof(int*));
for (i = 0; i < N; i++)
Board[i] = (int*) malloc(N*sizeof(int));
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
Board[i][j] = 0;
}
}
return Board;
}
void AI_move(int**Board,int player, int player2){
int i,j;
for (j=0; j<=w; j++) {
for (i=w; i>=0; i--) {
// printf("I: %d\n", i);
if ( j < w && Board[j][i]==0 && Board[j+1][i]!=0) {
Board[j][i]=player;
if(checkVictory(Board)==1){
puts("Found Winning Move");
display(Board,w+1, w+1);
return;
}
else
Board[j][i]=0;
}
}
}
makeMove(Board,player);
}
int makeMove(int** Board,int player){
int a;
start:a= rand()%(w+1);
int i;
for (i=w; i>=0; i--) {
if ((Board[i][a])==0) {
Board[i][a]=player;
return 1;
}
}
goto start;
}
void display(int** Board,int rows, int columns){
int i,j;
for (i=0; i <= w;i++){
for (j=0;j <=w;j++){
if (Board[i][j]==1) {
printf(" R ");
}
else if(Board[i][j]==2)
printf(" B ");
//printf(" %d ",Board[i][j]);
else
printf(" - ");
}
printf("\n");
}
}