I am trying to make a connect four game that plays its self multiple times. I want to do this using Processes and Pipes because I want to learn how to use them. I know the you need to use for to create the child processes, but I can't figure out what to do next.
Here is my code:
Ive tried to just for in my main, but each produce the same board and moves, even though their random. I am just lost on what I have to do next. Any suggestion would be very helpful.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <pthread.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);
printf("parent_pid = %d\n", getpid());
// fork();
play(Board);
return 0;
}
void play(int **Board){
int i, check;
for (i=0; i<((w+1)*(w+1)/2); i++) {
AI_move(Board,1,2);
check=checkVictory(Board);
if (check==1 || check==2) {
if (check==1)
puts("Winning Board Player1");
else
puts("Winning Board Player2");
display(Board,w+1, w+1);
break;
}
makeMove(Board,2);
check=checkVictory(Board);
if (check==1 || check==2) {
if (check==1)
puts("Winning Board Player1");
else
puts("Winning Board Player2");
display(Board, w+1, w+1);
break;
}
}
}
int checkVictory(int** Board){
int i,j;
//check horizontally
for (i=0; i<=w; i++) {
for (j=w; j>=0; j--) {
if ( j > 2 && Board[i][j]==1 && Board[i][j-1]==1 && Board[i][j-2]==1 && Board[i][j-3]==1) {
//puts("Horr Winner, Player1");
return 1;
}
if ( j > 2 && Board[i][j]==2 && Board[i][j-1]==2 && Board[i][j-2]==2 && Board[i][j-3]==2) {
//puts("Horr Winner, Player2");
return 2;
}
}
}
//check vertically
for (i=0; i<=w; i++) {
for (j=w; j>=0; j--) {
if ( i < (w-2) && Board[i][j]==1 && Board[i+1][j]==1 && Board[i+2][j]==1 && Board[i+3][j]==1) {
// puts("Vertical Winner, Player1");
return 1;
}
if ( i < (w-2) && Board[i][j]==2 && Board[i+1][j]==2 && Board[i+2][j]==2 && Board[i+3][j]==2) {
//puts("Vertical Winner, Player2");
return 2;
}
}
}
// //check diagonally
for (i=0; i<=w; i++) {
for (j=w; j>=0; j--) {
if (i < (w-2) && j > 2 && Board[i][j]==1 && Board[i+1][j-1]==1 && Board[i+2][j-2]==1 && Board[i+3][j-3]==1) {
//puts("DIAG L-R Winner, Player1");
return 1;
}
if (i > 2 && j < 5 && Board[i][j]==1 && Board[i-1][j+1]==1 && Board[i-2][j+2]==1 && Board[i-3][j+3]==1) {
// puts("DIAG L-R Winner3, Player1");
return 1;
}
}
}
for (i=0; i<=w; i++) {
for (j=w; j>=0; j--) {
if (i < (w-2) && j > 2 && Board[i][j]==2 && Board[i+1][j-1]==2 && Board[i+2][j-2]==2 && Board[i+3][j-3]==2) {
//puts("DIAG L-R Winner, Player2");
return 2;
}
if (i > 2 && j < 5 && Board[i][j]==2 && Board[i-1][j+1]==2 && Board[i-2][j+2]==2 && Board[i-3][j+3]==2) {
// puts("DIAG L-R Winner3, Player2");
return 2;
}
}
}
return 0;
}
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);
printf("Parent placed peice at: %d,%d\n", j,i);
return;
}
else
Board[j][i]=0;
}
}
}
// 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]=player2;
// if(checkVictory(Board)==2){
// puts("Found Blocking Move");
// Board[j][i]=player;
// display(Board,w+1, w+1);
// return;
// }
// else
// Board[j][i]=0;
// }
// }
// }
//makeMove(Board,player);
int a;
start:a= rand()%(w+1);
for (i=w; i>=0; i--) {
if ((Board[i][a])==0) {
Board[i][a]=player;
printf("Parent placed peice at: %d,%d\n", i,a);
return;
}
}
goto start;
}
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;
printf("Child laced peice at: %d,%d\n", i,a);
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");
}
}
Ive tried to just for in my main, but each produce the same board and moves, even though their random. I am just lost on what I have to do next. Any suggestion would be very helpful.