Thread: Multiple Processes and Threads

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    1

    Multiple Processes and Threads

    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.

    Code:
    #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;
            }
            
        }
    }
    
    
    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");
        }
        
    }

  2. #2
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    Why are you planning on using processes and/or threading?

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Justin Grover View Post
    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.

    You get the same result in both processes because you initialize the pseudo-random number generator before you fork, thus the child inherits the seed from its parent. You have to call srand() after the forking in each process to get different sequences of random numbers.
    But you also need to add a constant value to one of the return values of time(NULL) because it returns the time in seconds and your computer is probably fast enough that both processes call time() within the same second.

    Example:
    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <time.h>
    #include <stdlib.h>
    #include <sys/wait.h>
    
    int main(void)
    {
    //    srand((int) time(NULL));
    
    
        if (fork() == 0)
        {
            srand(time(NULL));
            int i;
            for (i = 0; i < 10; i++)
                printf("child #%d: %d\n", i, rand() % 100);
        }
        else
        {
            // adding a constant to time(NULL) to get different seed value
            srand(time(NULL) + 99);
            int i;
            for (i = 0; i < 10; i++)
                printf("parent #%d: %d\n", i, rand() % 100);
        }
    
        int status;
        wait(&status);
        return 0;
    }
    Bye, Andreas

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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.
    A clear description of what each pipe and process is for would be a start.

    Just throwing things in without a clear plan is a sure-fire way of making a big broken mess of a program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. threads and processes
    By lczarne in forum C Programming
    Replies: 4
    Last Post: 12-29-2010, 06:07 PM
  2. Mutiple processes and threads: Why?
    By Yarin in forum Tech Board
    Replies: 3
    Last Post: 02-26-2010, 11:49 AM
  3. threads and Real-time processes
    By inhahe in forum Windows Programming
    Replies: 5
    Last Post: 05-14-2008, 04:17 PM
  4. a point of threads to create multiple threads
    By v3dant in forum C Programming
    Replies: 3
    Last Post: 10-06-2004, 09:48 AM
  5. Threads and Processes
    By Draco in forum Tech Board
    Replies: 3
    Last Post: 11-09-2003, 03:48 AM

Tags for this Thread