Project Five Solution

Below is a possible solution for the project :
/* MCS 260 Project 5 : simulate the game connect-4 (4-in-line).

The purpose of the project is to simulate the game "connect-4",
also known as "4-in-line".
In this game two players take turn in dropping a token in any of
the slots of the vertical columns of an 8-by-8 board.  After each
drop the token ends up at the lowest empty square in that column
on the board.  The game is won by the player who first gets four
tokens of his kind aligned, either horizontally, vertically,
diagonally, or along the antidiagonal.  The game ends in a tie when
the board is filled with tokens and no player succeeded in getting
his tokens aligned.

In this program the user plays against the computer.  The strategy
of the computer is very simple: it just picks a random number for
the column number to make the next move.

We represent the board as a two dimensional array of characters,
the token of the user is 'X', that of the computer is 'O'.       */

#include<stdio.h>
#include<ctype.h>
#include<time.h>
#include<stdlib.h>

/* N is size of the board, F equals four */

#define N 7
#define F 4

void init ( char b[N][N] );
/* initializes the board to spaces */

void print ( char b[N][N] );
/* prints the board to screen */

int allowed ( char b[N][N], int move );
/* returns 1 if the board allows the move,
   returns 0 otherwise */

void make_move ( char b[N][N], int move, char c );
/* puts the character c in the slot indicated by move */

void ask_move ( char b[N][N], int *move );
/* asks the user for a move */

void generate_move ( char b[N][N], int *move );
/* generates a random move */

int full ( char b[N][N] );
/* returns 1 if the board is full, returns 0 otherwise */

int found_winner ( char b[N][N], char c );
/* returns 1 if there are F entries of c in same row
   column, diagonal or antidiagonal on the board b */

int main ( void )
{
   char board[N][N];
   int move;

   srand(time(NULL));
   init(board);

   printf("\nWelcome to connect %d.\n\n", F);

   do
   {
      ask_move(board,&move);
      make_move(board,move,'X');
      if (found_winner(board,'X'))
      {
         printf("You win!\n"); break;
      }
      if (!full(board))
      {
         generate_move(board,&move);
         make_move(board,move,'O');
         if (found_winner(board,'O'))
         {
            printf("I win!\n"); break;
         }
         print(board);
      }

   } while (!full(board));

   printf("The final board :\n"); print(board);
   if (full(board))
      printf("A tie!\n");

   return 0;
}

void init ( char b[N][N] )
{
   int i,j;

   for (i = 0; i < N; i++)
      for (j = 0; j < N; j++)
        b[i][j] = ' ';
}   

void print ( char b[N][N] )
{
   int i,j;

   for (i = 0; i < N; i++, putchar('\n'))
      for (j = 0; j < N; j++)
         printf(" %c", b[i][j]);

   for (j = 0; j < N; j++)
      printf(" %d", j);
   printf("\n");
}

int allowed ( char b[N][N], int move )
{
   if ((move < 0) || (move >= N))
      return 0;
   else
      return isspace(b[0][move]);
}

void make_move ( char b[N][N], int move, char c )
{
   int i;

   for (i = N-1; !isspace(b[i][move]); i--);

   b[i][move] = c; 
}

void ask_move ( char b[N][N], int *move )
{
   for (;;)
   {
      printf("Type a number between 0 and %d to make a move : ", N-1);
      scanf("%d", move);
      if (allowed(b,*move)) break;
      printf("Move %d not allowed.  Please try again\n", *move);
   }
}

void generate_move ( char b[N][N], int *move )
{
   do
   {
      *move = rand()%N;

   } while (!allowed(b,*move));
}

int full ( char b[N][N] )
{
   int i;

   for (i = 0; i < N; i++)
     if (isspace(b[0][i])) return 0;

   return 1;
}

int found_winner ( char b[N][N], char c )
{
   int cnt = 0;
   int i,j;

   for (i=0; i<N; i++)      /* check i-th row */
   {
      cnt = 1;
      for (j=1; j<N; j++)
	 if ((b[i][j-1] != c) || (b[i][j] != c))
	    cnt = 1;
         else
            if (++cnt >= F) return 1;
   }

   for (j=0; j<N; j++)     /* check j-th column */
   {
      cnt = 1;
      for (i=1; i<N; i++)
	 if ((b[i-1][j] != c) || (b[i][j] != c))
	    cnt = 1;
         else
	    if (++cnt >= F) return 1;
   }

   for (i=1; i<N; i++)     /* diagonals starting at i-th row */
   {
      cnt = 1;
      for (j=1; i+j-1<N; j++)
	 if ((b[i+j-2][j-1] != c) || (b[i+j-1][j] != c))
	    cnt = 1;
         else
	    if (++cnt >= F) return 1;
   }

   for (j=2; j<N; j++)     /* diagonals starting at j-th column */
   {
      cnt = 1;
      for (i=1; i+j-1<N; i++)
         if ((b[i-1][i+j-2] != c) || (b[i][i+j-1] != c))
	    cnt = 1;
         else
	    if (++cnt >= F) return 1;
   }

   for (i=N-2; i>=0; i--)  /* antidiagonals starting at row i */
   {
      cnt = 1;
      for (j=1; i-j+1>=0; j++)
	 if ((b[i-j+2][j-1] != c) || (b[i-j+1][j] != c))
	    cnt = 1;
         else
	    if (++cnt >= F) return 1;
   }

   for (j=2; j<N; j++)     /* antidiagonals starting at column j */
   {
      cnt = 1;
      for (i=N-2; N-i+j-1<N; i--)
         if ((b[i+1][N-i+j-2] != c) || (b[i][N-i+j-1] != c))
	    cnt = 1;
         else
	    if (++cnt >= F) return 1;
   }

   return 0;
}