Resume

Pictures

Connect 4 Source

#include 
#include 
#define BOARDROW 6
#define BOARDCOLUMN 7
#define pos_infinity 32766
#define neg_infinity -32767
#define MAX 1
#define MIN 0


/*----------------------------------------------------------------------------*/

int
nazizi_changeboard(int oldboard[BOARDROW][BOARDCOLUMN],
                   int newboard[BOARDROW][BOARDCOLUMN], int position, int player)
 {
  /* This function will take a board, a player, and a position where the player
   * wants to play and create the newboard with that position.
   * Pre: oldboard, place, player must be defined.
   * Post: if the move is legal a 1 will be returned and the newboard will be
   *       constructed otherwise a 0 will be returned. */

  int i, j; /*Loop counter variables */

  /* Initialize the newboard */
  for(i = 0; i < BOARDROW; i++)
    for(j = 0; j < BOARDCOLUMN; j++)
      newboard[i][j] = oldboard[i][j];

  i = 1;
  /* Check if the column is full */
  if (newboard[BOARDROW - i][position] != 0)
    return(0);
  else
   {
    /* Drop the piece down the column */
    while ((newboard[BOARDROW - i][position] == 0) && (i >= BOARDROW))
      i++;
    newboard[BOARDROW -i + 1][position] = player;
   }
  return(1);
 }

/*----------------------------------------------------------------------------*/

int
nazizi_key(int number, int number2, int of, int obf, int ob, 
           int obb, int m, int mb, int player, int MINE, int direction)
 {
  /* This function will return the score of one sequence of pieces.
   * Pre: number, number2, of,obf,ob,obb,m,mb,player,MINE, and direction must be defined.
   * Post: A score will be returned. */

  /* Score is very subjective, changing a few values can lead to loss or win */

  if (direction == 2)
   {
    if (number >= 4)
      return(pos_infinity);
    else if ((number == 3) && (obf >= 1))
     {
      return(600);
   /*   if (player == MINE)
        return(490);
      else
        return(680);*/
     }
    else
      return(0);
   }

  else if (number2 > 0)
   {
    if ((number2 >=4) || (number >= 4))
      return(pos_infinity);
    if ((number2 + number) >= 3)
     {
      /* Note: when number2 > 0, m and of are actually reversed and the same for
      mb and obf. */

      if (obf == 1)
       {
        return(700);
       }
      else if (of == 1)
        return(600);
      else if (obf == 2)
        return(100);
      else if ((ob == 2) && (mb > 0) && (obb > 0))
        return(20);
      else
        return(10);
     }
    else if ((number2 + number) == 2)
     {
      if ((obf == 1) && (mb > 0) && (obb > 0))
        return(100);
      else if ((obf == 1) && ((mb > 0) || (obb > 0)))
        return(80);
      else if ((obf == 1) && ((m > 0) || (ob > 0)))
        return(20);
      else if ((of == 1) && (mb > 0) && (obb > 0))
        return(20);
      else if ((of == 1) && ((mb > 0) || (obb > 0)))
        return(10);
      else if ((of == 1) && ((m > 0) || (ob > 0)))
        return(5);
      else if (of == 1)
        return(0);
      else if (obf == 2)
        return(25);
      else if ((ob == 2) && (mb > 0) && (obb > 0))
        return(5);
      else
        return(0);
     }
    else
      return(0);
   }
  if (number >= 4)
    return(pos_infinity);
  else if ((of == 0) && (ob == 0))
    return(0);
  else if (number == 3)
   {
    if ((obb > 0) && (obf > 0))
      return(3000);
    else
      return(600);

/*    if ((obb > 0) && (obf > 0))
      return(3000);
    else if ((obb > 1) || (obf > 1))
     {
      if (player == MINE)
        return(500);
      else
        return(690);
     }
    else if (((obb > 0) || (obf > 0)) && (((ob > 1) && (obb > 0)) || ((of > 1) && (obf > 0))))
     {
      if (player == MINE)
        return(490);
      else
        return(680);
     }
    else if ((obb > 0) || (obf > 0)) && ((ob > 0) || (of > 0)))
     {
      if (player == MINE)
        return(480);
      else
        return(600);
     }
    else if ((of > 1) && (ob > 1))
     {
      if (player == MINE)
        return(470);
      else
        return(550);
     }
    else if ((of > 1) || (ob > 1))
     {
      if (player == MINE)
        return(460);
      else
        return(500);
     }
    else if ((of > 0) || (ob > 0))
      return (90);
    else
      return(50);
                       */


   }
  else if (number == 2)
   {
    if ((ob + of) < 2)
      return(0);
    else if ((obb > 1) && (obf > 1))
      return(100);
    else if (((obb > 1) || (obf > 1)) && (((ob > 1) && (obb > 1)) || ((of > 1) && (obf > 1))))
      return(50);
    else if (((obb > 0) || (obf > 0)) && (((ob > 1) && (obb > 0)) || ((of > 1) && (obf > 0))))
      return(40);
    else if ((obb > 0) || (obf > 0))/* && ((ob > 0) || (of > 0)))*/
      return(20);
    else if ((of > 1) && (ob > 1))
      return(10);
    else if ((of > 1) || (ob > 1))
      return(5);
    else if ((of > 0) || (ob > 0))
      return (3);
    else
      return(2);
   }
  else if (number == 1)
   {
    if ((ob + of) < 3)
      return(0);
    else if ((obb > 1) && (obf > 1))
      return(10);
    else
      return(0);
   }
  player = player + 1;
  MINE = MINE + 1;
  return(0);
 }

/*----------------------------------------------------------------------------*/
int
nazizi_analyze(int i, int j, int MINE, int YOURS, int EMPTY, int *of, int *ob,
               int *whos, int *obf, int *obb1, int *obb2, int *lastpiece, int *number,
               int *number2, int *myscore, int *yourscore, int *m, int *mb,
               int board[BOARDROW][BOARDCOLUMN], int direction)
 {
  /* This function will find sequences of pieces in one line of the board.
   * Pre: i, j, MINE, YOURS, EMPTY, *of, *ob, *whos, *obf, *obb1, *obb2,
   *      *lastpiece, *number, *number2, *myscore, *intscore, *m, *mb
   *      board and direction must be defined.
   * Post: If MINE leads to a win return 1, if YOURS leads to a win return -1,
   *       else all the parameters will be updated accordingly. */

  int newscore;  /* Newscore of a sequence */

  /* If the position is empty */
  if (board[i][j] == EMPTY)
   {
    /* Check if the position underneath is occupied and increment paramters*/
    if (((i == 0) || (board[i-1][j] != EMPTY)) && (*obb1 == *ob))
      (*obb1)++;
    else if ((i == 0) || (board[i-1][j] != EMPTY))
      (*obb2)++;
    else
      *obb2 = 0;
    *ob = *ob + 1;
    /* Mark the new last piece */
    *lastpiece = EMPTY;
   }


  /* If the position is occupied by my piece */
  else if (board[i][j] == MINE)
   {
    /* If the last piece was mine increment number in sequence */
    if (*lastpiece == MINE)
      (*number)++;

    /* If the last piece was yours find score for your sequence and reinitalize parameters */
    else if (*lastpiece == YOURS)
     {
      newscore =nazizi_key(*number,*number2,*of,*obf, 0,0, *m, *mb, YOURS, MINE, direction);
      if (newscore == pos_infinity)
	return(neg_infinity);
      else
       {
	*yourscore = *yourscore + newscore;
        *number = 1;
        *of = 0;
        *obf = 0;
        *ob = 0;
        *obb1 = 0;
        *obb2 = 0;
        *whos = MINE;
        *lastpiece = MINE;
        *m = 0;
        *mb = 0;
        *number2 = 0;
       }
     }

    /* If the last piece was empty if the last sequence was YOURS then find the
       score for YOURS sequence and initialize parameters. If the last sequence
       was MINE then there are middle positions empty bewteen the sequence thus
       update parameters for these cases.  If there was no last sequence then
       initialize parameters */
    else if (*lastpiece == EMPTY)
     {
      if (*whos == MINE)
       {
        if (*number2 > 0)
         {
          newscore =nazizi_key(*number,*number2, *of,*obf, *ob, *obb1, *m, *mb, MINE,MINE, direction);
          if (newscore == pos_infinity)
            return(pos_infinity);
          else
            *myscore = *myscore + newscore;
          *number2= 0;
          *m = 0;
          *mb = 0;
         }
        else
         {
          *number2 = *number;
          *m = *of;
          *mb = *obf;
         }
       }

      else if (*whos == YOURS)
       {
        newscore =nazizi_key(*number,*number2, *of,*obf, *ob, *obb1, *m, *mb,YOURS,MINE, direction);
        if (newscore == pos_infinity)
	  return(neg_infinity);
        else
          *yourscore = *yourscore + newscore;
        *m = 0;
        *mb = 0;
        *number2 = 0;
       }

      *of = *ob;
      if (*ob == *obb1)
        *obf = *obb1;
      else
        *obf = *obb2;
      *obb1 = 0;
      *obb2 = 0;
      *ob = 0;
      *lastpiece = MINE;
      *number = 1;
      *whos = MINE;
     }
   }

  /* If the position is occupied by your piece */
  else if (board[i][j] == YOURS)
   {
    /* If the last piece was YOURS then update the number in the sequence */
    if (*lastpiece == YOURS)
      (*number)++;

    /* If the last piece was MINE find score for MINE sequence and reinitalize parameters */
    else if (*lastpiece == MINE)
     {
      newscore =nazizi_key(*number,*number2, *of,*obf, 0,0, *m, *mb, MINE,MINE, direction);
      if (newscore == pos_infinity)
        return(pos_infinity);
      else
       {
	*myscore = *myscore + newscore;
        *number = 1;
        *of = 0;
        *obf = 0;
        *ob = 0;
        *obb1 = 0;
        *obb2 = 0;
        *whos = YOURS;
        *lastpiece = YOURS;
        *mb = 0;
        *m = 0;
        *number2 = 0;
       }
     }

    /* If the last piece was empty if the last sequence was MINE then find the
       score for MINE sequence and initialize parameters. If the last sequence
       was YOURS then there are middle positions empty bewteen the sequence thus
       update parameters for these cases.  If there was no last sequence then
       initialize parameters */
    else if (*lastpiece == EMPTY)
     {
      if (*whos == YOURS)
       {
        if (*number2 > 0)
         {
          newscore =nazizi_key(*number,*number2,*of,*obf, *ob, *obb1, *m, *mb,YOURS,MINE, direction);
          if (newscore == pos_infinity)
            return(neg_infinity);
          else
            *yourscore = *yourscore + newscore;
          *number2 = 0;
          *m = 0;
          *mb = 0;
         }
        else
         {
          *number2 = *number;
          *m = *of;
          *mb = *obf;
         }
       }
      else if (*whos == MINE)
       {
        newscore =nazizi_key(*number,*number2, *of,*obf, *ob, *obb1, *m, *mb,MINE,MINE, direction);
        if (newscore == pos_infinity)
	  return(pos_infinity);
        else
          *myscore = *myscore + newscore;
        *number2 = 0;
        *m = 0;
        *mb = 0;
       }

      *of = *ob;
      if (*ob == *obb1)
        *obf = *obb1;
      else
        *obf = *obb2;
      *obb1 = 0;
      *obb2 = 0;
      *ob = 0;
      *lastpiece = YOURS;
      *whos = YOURS;
      *number = 1;
     }
   }
  return(0);
 }

/*----------------------------------------------------------------------------*/
int
nazizi_score_direction(int board[BOARDROW][BOARDCOLUMN], int MINE, int YOURS, int EMPTY, int direction)
 {
  /* This function will analyze the board in one direction giving it a score.
   * Pre: board, MINE, YOURS, EMPTY, and direction must be defined.
   * Post: A score for the board in the specified direction will be returned.*/

  int k,i,j;        /* Loop counter variables */
  int myscore=0;   /* Score of my positions on board */
  int yourscore=0; /* Score of your positions on board */
  int number;       /* Number of consecutive pieces of board */
  int number2;      /* If spaces seperate two sequences of one kind of pieces this will contain the first number of pieces */
  int lastpiece;    /* The last piece visited */
  int whos;         /* Was the last sequence of pieces mine or yours? */
  int of;           /* Number of open spaces in front of a sequence of pieces */
  int ob;           /* Number of open spaces in behind of a sequence of pieces */
  int obf;          /* Number of open spaces in front of a sequence that have pieces underneath them */
  int obb1;         /* Number of open spaces in behind of a sequence that have pieces underneath them */
  int obb2;         /* Same as above, but to be able to keep data on two sequencs */
  int m;            /* Number of open spaces bewteen two sequences of the same player */
  int mb;           /* Number of open spaces bewteen two sequences of the same player with pieces underneath them */
  int check;        /* Check for return of pos_infinity */
  int newscore;    /* The score of a sequence of pieces on the board */

  /* Go through the lines of the board */
  for (k = 0; k < 7; k++)
   {
    /* If checking horizontal each line starts at (k,0) */
    if (direction == 1)
     {
      i = k;
      j = 0;
     }
    /* If checking horizontal each line starts at (0,k) */
    else if (direction == 2)
     {
      j = k;
      i = 0;
     }
    /* If checking diagonally we have to go through the cases */
    else if (direction == 3)
     {
      switch(k)
       {
        case 0: i = 0;
                j = 3;
                break;
        case 1: i = 0;
                j = 2;
                break;
        case 2: i = 0;
                j = 1;
                break;
        case 3: i = 0;
                j = 0;
                break;
        case 4: i = 1;
                j = 0;
                break;
        case 5: i = 2;
                j = 0;
                break;
        case 6: i = -1;
                j = -1;
                break;
       }
     }
    /* If checking diagonally we have to go through the cases */
    else if (direction == 4)
     {
      switch(k)
       {
        case 0: i = 0;
                j = 3;
                break;
        case 1: i = 0;
                j = 4;
                break;
        case 2: i = 0;
                j = 5;
                break;
        case 3: i = 0;
                j = 6;
                break;
        case 4: i = 1;
                j = 6;
                break;
        case 5: i = 2;
                j = 6;
                break;
        case 6: i = -1;
                j = -1;
                break;
       }
     }

    /* Initialize varibales each time through the loop */
    m = 0;
    mb = 0;
    ob = 0;
    of = 0;
    obf = 0;
    obb1 = 0;
    obb2 = 0;
    number = 0;
    number2 = 0;
    whos = EMPTY;
    lastpiece = EMPTY;

    /* Go through the line until end up outside board */
    while ((j < BOARDCOLUMN) && (i < BOARDROW) && (i >= 0) && (j >= 0))
     {
      /* Analyze the next piece */
      check = nazizi_analyze(i,j,MINE,YOURS,EMPTY,&of,&ob,&whos,&obf,
                             &obb1,&obb2,&lastpiece,&number,&number2,
                             &myscore,&yourscore,&m,&mb,board, direction);

      /* Return if win or lose */
      if ((check == pos_infinity) || (check == neg_infinity))
        return(check);
      else
       {
        /* Go to the next piece */
        if (direction == 1)
          j++;
        else if (direction == 2)
          i++;
        else if (direction == 3)
         {
          i++;
          j++;
         }
        else if (direction == 4)
         {
          i++;
          j--;
         }
       }
     }

    /* Once finished one line get the score for the last sequence of pieces */

    /* Add the score to the appropriate player */
    if (whos == MINE)
     {
      newscore =nazizi_key(number,number2,of,obf,ob,obb1,m,mb,MINE,MINE, direction);
      if (newscore == pos_infinity)
        return(pos_infinity);
      myscore = myscore + newscore;
     }
    else
     {
      newscore =nazizi_key(number,number2,of,obf,ob,obb1,m,mb,YOURS,MINE, direction);
      if (newscore == pos_infinity)
        return(neg_infinity);
      yourscore = yourscore + newscore;
     }
   }

  /* Total score is the score of my positions minus the score of your positions */
  return(myscore - yourscore);
 }

/*----------------------------------------------------------------------------*/
int
nazizi_score(int board[BOARDROW][BOARDCOLUMN], int MINE, int YOURS, int EMPTY)
 {
  /* This function will return the score of the total board.
   * Pre: board, MINE, YOURS, and EMPTY must be defined.
   * Post: The score of all four directions will be returned. */

  int horiz;  /* Score in horizontal direction */
  int vert;   /* Score in vertical direction */
  int diag1;  /* Score in one diagonal direction */
  int diag2;  /* Score in the other diagonal direction */

  /* Check score of each direction individually and if win or lose return score */
  horiz = nazizi_score_direction(board, MINE, YOURS, EMPTY,1);
  if ((horiz == pos_infinity) || (horiz == neg_infinity))
    return(horiz);
  vert = nazizi_score_direction(board, MINE, YOURS, EMPTY, 2);
  if ((vert == pos_infinity) || (vert == neg_infinity))
    return(vert);
  diag1 = nazizi_score_direction(board, MINE, YOURS, EMPTY,3);
  if ((diag1 == pos_infinity) || (diag1 == neg_infinity))
    return(diag1);
  diag2 = nazizi_score_direction(board, MINE, YOURS, EMPTY,4);
  if ((diag2 == pos_infinity) || (diag2 == neg_infinity))
    return(diag2);

  /* Return total score */
  return(horiz + vert + diag1 + diag2);
 }

/*-----------------------------------------------------------------------------*/
int
nazizi_quickcheck(int board[BOARDROW][BOARDCOLUMN], int MINE, int YOURS, int EMPTY)
 {
  /* This function will only check if a player has won.
   * Pre: board, MINE, YOURS, and EMPTY must be defined.
   * Post: If MINE has won return pos_infinity, if YOURS has won return
   *       neg_infinity, if noone has won return 0. */

  int score; /* The score of the board */

  /* Get the score of the board */
  score = nazizi_score(board, MINE, YOURS, EMPTY);

  /* If someone has won return the win else return 0 */
  if ((score == pos_infinity) || (score == neg_infinity))
    return(score);
  else
    return(0);
 }

/*----------------------------------------------------------------------------*/
int
nazizi_minimaxAB(int MINE, int YOURS, int EMPTY, int depth, int max_depth,
                 int min_or_max, int board[BOARDROW][BOARDCOLUMN], int A, 
                 int B, int *position, int player)
 {
  /* This function will produce the minimax algorithm with alpla-beta pruning.
   * Pre: MINE, YOURS, EMPTY, depth, max_depth, min_or_max , A, B, *position
   *      and board must be defined.
   * Post: The score of the node will be returned and the position that led
   *       to that score will be returned in *position. */

  int alpha;                          /* The alpha value of the node */
  int newalpha;                       /* An alpha value of child node */
  int beta;                           /* The beta value of the node */
  int newbeta;                        /* A beta value of a child node */
  int newboard[BOARDROW][BOARDCOLUMN]; /* Recreation of board with one extra piece */
  int i;                               /* Loop counter variable */
  int legal;                           /* If a move is legal? */
  int finished;                       /* If have reached a leaf via win or lose */
  int newposition;                     /* Position to play to get best score for children */

  /* If reached the max_depth get the score of the board */
  if (depth == max_depth)
    return(nazizi_score(board, MINE, YOURS, EMPTY));
  else
   {
    /* Check for win or lose */
    finished = nazizi_quickcheck(board, MINE, YOURS, EMPTY);
    if ((finished == pos_infinity) || (finished == neg_infinity))
      return(finished);

    /* Initialize alpha and beta */
    alpha = A;
    beta = B;

    /* Determine if node is a max-node or min-node */
    if (min_or_max == MIN)
     {
      /* For each child of N do (ie 7 columns, 7 children) */
      for (i = 0; i < BOARDCOLUMN; i++)
       {
        /* Check if move is leagal */
        legal = nazizi_changeboard(board, newboard, i, player);
        if (legal)
         {
          /* Find beta of children */
          newbeta = nazizi_minimaxAB(MINE, YOURS, EMPTY, depth + 1, max_depth, MAX, newboard, alpha, beta,
                                     &newposition, -player);
          /* If beta of child is less that beta of node, change beta of node and record the position played */
          if (newbeta < beta)
           {
            beta = newbeta;
            *position = i;
           }
          /* If alpha is more than beta perform the alpha-beta pruning and return alpha */
          if (alpha >= beta)
            return(alpha);
         }
       }
      /* Return the min of all the child's betas */
      return(beta);
     }
    else
     {
      /* For each child of N do (ie 7 columns, 7 children) */
      for(i = 0; i < BOARDCOLUMN; i++)
       {
        /* Check if move is legal */
        legal = nazizi_changeboard(board, newboard, i, player);
        if (legal)
         {
          /* Find alpha of children */
          newalpha =nazizi_minimaxAB(MINE, YOURS, EMPTY, depth + 1, max_depth, MIN, newboard, alpha, beta, 
                                     &newposition, -player);
          /* If alpha of child is more that alpha of node, change alpha of node and record the position played */
          if (newalpha > alpha)
           {
            alpha = newalpha;
            *position = i;
           }
          /* If alpha is more than beta perform the alpha-beta pruning and return beta */
          if (alpha >= beta)
            return(beta);
         }
       }
      /* Return the max of all the child's alphas */
      return(alpha);
     }
   }
 }

/*----------------------------------------------------------------------------*/
int
nazizi_play(int MINE, int YOURS, int EMPTY, int previousColumn, int board[BOARDROW][BOARDCOLUMN])
 {
  /* This function will determine the best position to play in a connect4 game.
   * Pre: MINE, YOURS, EMPTY, previousColumn, and board must be defined.
   * Post: the column that will result in the best move will be returned. */

  int position=-1; /* Position to be played */
  int score;       /* Check for immediate win */
  int k;

  /* If first move of game return middle of board */
  if (previousColumn == -1)
	 position =3;
  else
   {
    /* Quick check for win */
    score = nazizi_minimaxAB(MINE, YOURS, EMPTY, 0, 1, MAX, board, neg_infinity, pos_infinity, &position, MINE);
    if (score == pos_infinity)
      return(position);
    else
      position = -1;


    /* Perform the minmax algorithm with five move look-ahead */
    nazizi_minimaxAB(MINE, YOURS, EMPTY, 0, 5, MAX, board, neg_infinity, pos_infinity, &position, MINE);

    /* If all move are equally bad try to find the move that will avoid quicker loses */
    if (position == -1)
     {
      nazizi_minimaxAB(MINE, YOURS, EMPTY, 0, 4, MAX, board, neg_infinity, pos_infinity, &position, MINE);
     }
    if (position == -1)
     {
      nazizi_minimaxAB(MINE, YOURS, EMPTY, 0, 3, MAX, board, neg_infinity, pos_infinity, &position, MINE);
     }
    if (position == -1)
      {
       nazizi_minimaxAB(MINE, YOURS, EMPTY, 0, 2, MAX, board, neg_infinity, pos_infinity, &position, MINE);
      }
    if (position == -1)
     {
      nazizi_minimaxAB(MINE, YOURS, EMPTY, 0, 1, MAX, board, neg_infinity, pos_infinity, &position, MINE);
     }
    if (position == -1)
     {
      for (k=0; k< BOARDCOLUMN, position == -1; k++)
       {
        /* valid column will have top row (and maybe others) vacant */
        if (board[BOARDROW-1][k] == EMPTY)
          position = k;  /* as soon as column found with free space, choose it */
       }
        /* if all else fails, give any number */
     }

   }
  return(position);
 }

/*-------------------------------*/

int
changeboard(int board[BOARDROW][BOARDCOLUMN], int position, int player)
 {
  /* This function will take a board, a player, and a position where the player
   * wants to play and create the newboard with that position.
   * Pre: board, place, player must be defined.
   * Post: if the move is legal a 1 will be returned and the newboard will be
   *       constructed otherwise a 0 will be returned. */

  int i, j; /*Loop counter variables */

  i = 1;
  /* Check if the column is full */
  if (board[BOARDROW - i][position] != 0)
   return(0);
  else
   {
    /* Drop the piece down the column */
    while ((board[BOARDROW - i][position] == 0) && (i >= BOARDROW))
      i++;
    board[BOARDROW -i + 1][position] = player;
   }
  return(1);
 }
/*-----------------------------------------------------------------------*/
void
drawboard(int board[BOARDROW][BOARDCOLUMN])
 {
  int i;
  int j;

  clrscr();
  printf("  1   2   3   4   5   6   7\n");

  for (i = BOARDROW - 1; i >= 0; i--)
   {
    printf("| ");
    for (j = 0; j < BOARDCOLUMN; j++)
     {
      if (board[i][j] == -1)
        printf("O");
      else if (board[i][j] == 1)
        printf("X");
      else
        printf(" ");
      printf(" | ");
     }
    printf("\n");
   }
 }


/*------------------------------------------------------------------------------*/

int
ask(int board[BOARDROW][BOARDCOLUMN], int YOURS)
 {
  int answer;
  int i;
  int correct = 1;
  int legal;
  char buffer[8];

  do
   {
    printf("In what column do you want to play: ");
    i = scanf("%i", &answer);
    if (i != 1)
     {
      printf("Invalid input:  You must enter an integer\n");
      correct = 0;
      scanf("%s", buffer);
     }
    else if ((answer > BOARDCOLUMN) || (answer <= 0))
     {
      printf("Invalid input: You must enter an integer bewteen 1 and 7\n");
      correct = 0;
     }
    else
     {
      correct = 1;
      answer = answer - 1;

      legal = changeboard(board,answer,YOURS);
      if (legal == 0)
       {
        printf("Invalid input: That column is full. Please try again\n");
        correct = 0;
       }
     }
   }while (correct == 0);
  return(answer);
 }

/*--------------------------------------------------------------------------------*/

int
isfull(int board[BOARDROW][BOARDCOLUMN], int EMPTY)
 {
  int j;
  for (j = 0; j < BOARDCOLUMN; j++)
   if (board[BOARDROW - 1][j] == EMPTY)
     return(0);
  return(1);
 }

/*---------------------------------------------------------------------------------*/


int
main()
 {
  int board[BOARDROW][BOARDCOLUMN] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  int start;
  int ok;
  int correct = 1;
  int MINE;
  int YOURS;
  int EMPTY;
  int qcheck;
  int legal;
  int comploss=0;
  char buffer[8];
  int position;

  clrscr();


  do
   {
    printf("If I should start, enter -1; if you want to start, enter 1: ");
    ok = scanf("%i", &start);
    if (ok != 1)
     {
      printf("Invalid input:  You must enter an integer\n");
      correct = 0;
      ok = scanf("%s", buffer);
     }
    else if ((start != 1) && (start != -1))
     {
      printf("Invalid input: You must enter 1 or -1\n");
      correct = 0;
     }
    else
      correct = 1;
   }while (correct == 0);

  if (start == 1)
   {
    YOURS = 1;
    MINE = -1;
    EMPTY = 0;
   }
  else
   {
    YOURS = -1;
    MINE = 1;
    EMPTY = 0;
    position = nazizi_play(MINE, YOURS, EMPTY, -1, board);
    changeboard(board, position, MINE);
   }



  do
   {
    drawboard(board);
    ask(board, YOURS);
    position = nazizi_play(MINE, YOURS, EMPTY, 3, board);
    legal = changeboard(board, position, MINE);
    if (!legal)
     {
      comploss = 1;
      break;
     }
    qcheck = nazizi_quickcheck(board, MINE, YOURS, EMPTY);
   }while((!isfull(board,EMPTY)) && (qcheck != neg_infinity) && (qcheck != pos_infinity));

  drawboard(board);
  if (comploss == 1)
    printf("\nI lost the game because I played an illegal move\n");
  else if (isfull(board, EMPTY))
    printf("\nThe game is a tie\n");
  else if (qcheck == neg_infinity)
    printf("\nYou won the game\n");
  else
    printf("\nI won the game\n");
  return(0);
 }



/*
int
main2()
 {
  int board[BOARDROW][BOARDCOLUMN] = {0,0,0,0,0,0,0,
                                      0,0,0,0,0,0,0,
                                      0,0,0,0,0,0,0,
                                      0,0,0,0,0,0,0,
                                      0,0,0,0,0,0,0,
                                      1,1,1,1,0,0,0};
  int start;
  int ok;
  int correct = 1;
  int MINE=1;
  int YOURS=-1;
  int EMPTY=0;
  int qcheck;
  int legal;



  start = isfull(board, EMPTY);
  qcheck = nazizi_quickcheck(board, MINE, YOURS, EMPTY);
  printf("Hello World");
  return(start);
  }


  */

  


Quick Links
Contact Info
Publications
Research
Classes
Teaching
Computer Group
Electrical and
Computer Engineering

University of Toronto
Extracuricular
Activities
Pictures
Get Mozilla Firebird and see
how this webpage should
really appear!