Solved

Connect Four..just minor fixes

Posted on 2003-11-03
28
302 Views
Last Modified: 2010-04-15
Here is my code for a Connect Four game, but I'm getting errors everywhere and I don't know how to fix them.  This is probably not the most efficient way of doing this game but I have to do it this way since this is for beginner's.  I just need my main() function to work.  Can someone take a look below?

---------------------------------
#include <stdio.h>

#define TABLESIZE 8
#define PL1 '1'
#define PL2 '2'
#define EMPTY '0'

int drop(int player, int row, int table[][], int openSpot[]) {

  if(openSpot[row] < TABLESIZE) {
    Board[row][openSpot[row]] = player;
    openSpot[row]++;
    return 1;
  }
  else {
    printf("Row is full\n");
    return 0;
  }

}

int playerHasWon(int player, int table[][]) {

  int curRow;
  int curCol;

  /* This checks for horizontal victory */
  for(curRow = 0; curRow < TABLESIZE; curRow++) {
    for(curCol = 0; curCol < TABLESIZE - 3; curCol++) {
      if(table[curRow][curCol] == player && table[curRow][curCol+1] == player && table[curRow][curCol+2] == player && table[curRow][curCol+3] == player) {
      printf("Victory for %d\n",player);
      return player;
      }
    }
  }

  /* This checks for vertical victory */
  for(curCol = 0; curCol < TABLESIZE; curCol++) {
      for(curRow = 0; curRow < TABLESIZE - 3; curRow++) {
      if(table[curRow + 1][curCol] == player && table[curRow + 1][curCol] == player && table[curRow + 2][curCol] == player && table[curRow+3][curCol] == player) {
      printf("Victory for %d\n",player);
      return player;
      }

  /* This checks for diagonal victory:  First ascending, then descending */
  for(curRow = 0; curRow < TABLESIZE-3; curRow++) {
    for(curCol = 0; curCol < TABLESIZE-3; curCol++) {
      if(table[curRow][curCol] == player && table[curRow+1][curCol+1] == player && table[curRow+2][curCol+2] == player && table[curRow+3][curCol+3] == player) {
      //Same deal
      }
    }
  }

  for(curRow = 3; curRow < TABLESIZE; curRow++) {
    for(curCol = 0; curCol < TABLESIZE-3; curCol++) {
      if(table[curRow][curCol] == player && table[curRow-1][curCol+1] == player && table[curRow-2][curCol+2] == player && table[curRow-3][curCol+3] == player) {
      //Same deal
      }
    }
  }

  return 0;
}

void printTable(int table[][]) {

  int temp1, temp2;

  for(temp1 = TABLESIZE - 1; temp1 >= 0; temp1++) {
    for(temp2 = 0; temp2 < TABLESIZE; temp2++) {
      printf("| %c ",table[temp1][temp2]);
    }
    printf("|\n");
  }

}

void main() {
  /* Initialize stuff */
  int numMoves = 0;
  int game, temp1, temp2, choice, choice2, player1, player2;

  for(curRow = 0; curRow < TABLESIZE; curRow++) {
    for(curCol = 0; curCol < TABLESIZE; curCol++) {
      table[curRow][curCol] = EMPTY;
    }
  }

  while(game == 0 && numMoves < TABLESIZE * TABLESIZE) {

    //PlayGame

        printf("Welcome to Connect4!);
             
            printTable(8,8);
        
        printf("Player 1: Choose column (0-7):  ");

        choice = getInt();

        player1 = drop(1,choice,8);

        game = playerHasWon(1,8)

        printTable(8,8);       

        printf("Player 2: Choose column (0-7);  ");

        choice2 = getInt();

        player2 = drop(2, choice2,8);

        game = playerHasWon(1,8);

        printTable(8,8);
 


    numMoves++;
  }

 
}
0
Comment
Question by:cooldean
  • 12
  • 9
  • 6
  • +1
28 Comments
 

Expert Comment

by:Spyder596
ID: 9675990
First of all, you never define the two dimensional array table[][]. Also, you seem to be passing two integers (8) into the function printTable, when you should be passing in the array table. Another occurance of this problem is in the function drop(). According to your deffinition, it takes in the player, the row they want, the table, and an array openSpot. Im not really sure what that array is supposed to do, but it doesnt match up with the rest of your code.

You seem to have the right idea for making this program, but you have a hidious number of coding mistakes. I would suggest that you go through it and make sure that all your function parameters match up, and that they return the correct things and whatnot. If you still have problems, gimme an email ;)
0
 

Expert Comment

by:Spyder596
ID: 9676138
err, if you still have problems, reply. Dunno what i was thinking ... :\
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9676203
     printf("Welcome to Connect4!);
missing closing " ... it should be

      printf("Welcome to Connect4!");

in function drop, variable Board is undeclared... you need to specify its data type

0
 

Author Comment

by:cooldean
ID: 9676212
Yeah, I have no clue what you said..haha.

What I did is I put 8 on both [][] for int table [][].

Don't know how to go from there though.
0
 

Author Comment

by:cooldean
ID: 9676216
my last comment was for Spyder...

got it sunnycoder...I fixed that.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9676220
change all function declaration to
int playerHasWon(int player, int * table[])

in place of
>
int playerHasWon(int player, int table[][])
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9676233
in function playerHasWon, perhaps you would like to close all loop braces here... you are closing only one in the entire function

 /* This checks for vertical victory */
 for(curCol = 0; curCol < TABLESIZE; curCol++) {
    for(curRow = 0; curRow < TABLESIZE - 3; curRow++) {
     if(table[curRow + 1][curCol] == player && table[curRow + 1][curCol] == player && table[curRow + 2][curCol] == player && table[curRow+3][curCol] == player) {
    printf("Victory for %d\n",player);
    return player;
     }

0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9676260
void main() {
 /* Initialize stuff */
 int numMoves = 0;
 int game, temp1, temp2, choice, choice2, player1, player2;

 for(curRow = 0; curRow < TABLESIZE; curRow++) {
   for(curCol = 0; curCol < TABLESIZE; curCol++) {
     table[curRow][curCol] = EMPTY;
   }
 }

you have not declared table,curRow and curCol in main()

>         printTable(8,8);
you are passing two ints to printTable while you need to pass an array of int pointers
void printTable(int * table[])
maybe this is what you want
printTable(table);
make sure that you declare table as an array of int pointers and malloc for required space

sampe problem in these statements in the rest of the function
>     player1 = drop(1,choice,8);
>   game = playerHasWon(1,8)  //you are also missing a ; here
>      printTable(8,8);
>      player2 = drop(2, choice2,8);
>      game = playerHasWon(1,8);
>      printTable(8,8);
0
 

Author Comment

by:cooldean
ID: 9676368
ok..here are the changes...and I added few more functions...but I still don't know how to fix my main though....I know player 1 and player 2 should be in a while loop but how do you do that?

----------------
#include <stdio.h>

/* course-specific functions to avoid using scanf */
int getInt() {
  int tmp = 0;
  char buf[200];
  return sscanf(gets(buf), "%d", &tmp) * tmp;
}

double getDouble() {
  double tmp = 0.0;
  char buf[200];
  return sscanf(gets(buf), "%lf", &tmp) * tmp;
}


#define TABLESIZE 8
#define PL1 '1'
#define PL2 '2'
#define EMPTY '0'

int drop(int player, int col, int table[TABLESIZE][TABLESIZE], int openSpot[]) {

  if(openSpot[col] < TABLESIZE) {
    table[openSpot[col]][col] = player;
    openSpot[col]++;
    return 1;
  }

  else {
    printf("Row is full\n");
    return 0;
  }


}


int playerHasWon(int player, int table[TABLESIZE][TABLESIZE]) {
  int curRow;
  int curCol;


  /* This checks for horizontal victory */

  for(curRow = 0; curRow < TABLESIZE; curRow++) {
    for(curCol = 0; curCol < TABLESIZE - 3; curCol++) {
      if(table[curRow][curCol] == player && table[curRow][curCol+1] == player && table[curRow][curCol+2] == player && table[curRow][curCol+3] == player) {
      printf("Victory for %d\n",player);
      return player;
      }
    }
  }


  /* This checks for vertical victory */

  for(curCol = 0; curCol < TABLESIZE; curCol++) {
    for(curRow = 0; curRow < TABLESIZE - 3; curRow++) {
      if(table[curRow + 1][curCol] == player && table[curRow + 1][curCol] == player && table[curRow + 2][curCol] == player && table[curRow+3][curCol] == player) {
      printf("Victory for %d\n",player);
      return player;
      }
    }
  }



  /* This checks for diagonal victory:  First ascending, then descending */

  for(curRow = 0; curRow < TABLESIZE-3; curRow++) {
    for(curCol = 0; curCol < TABLESIZE-3; curCol++) {
      if(table[curRow][curCol] == player && table[curRow+1][curCol+1] == player && table[curRow+2][curCol+2] == player && table[curRow+3][curCol+3] == player) {

       }
    }
  }

  for(curRow = 3; curRow < TABLESIZE; curRow++) {
    for(curCol = 0; curCol < TABLESIZE-3; curCol++) {
      if(table[curRow][curCol] == player && table[curRow-1][curCol+1] == player && table[curRow-2][curCol+2] == player && table[curRow-3][curCol+3] == player) {

      }
    }
  }

  return 0;
}


void printTable(int table[TABLESIZE][TABLESIZE]) {

  int temp1, temp2;

  for(temp1 = TABLESIZE - 1; temp1 >= 0; temp1++) {
    for(temp2 = 0; temp2 < TABLESIZE; temp2++) {
      printf("| %c ",table[temp1][temp2]);
    }
    printf("|\n");
  }

}


void main() {

  int numMoves = 0;
  int game, choice, choice2;
  int curRow, curCol;
  int table[TABLESIZE][TABLESIZE];
  int openSpot[TABLESIZE];
  int goodDrop;


  for(curRow = 0; curRow < TABLESIZE; curRow++) {
    for(curCol = 0; curCol < TABLESIZE; curCol++) {
      table[curRow][curCol] = EMPTY;
    }
  }

  while(game == 0 && numMoves < TABLESIZE * TABLESIZE) {

           printf("Welcome to Connect4!");
        printTable(table);
        goodDrop = 0;



        while(1){  

        printf("Player 1: Choose column (0-7):  ");

        choice = getInt();

        goodDrop = drop(1,choice,table,openSpot);

        game = playerHasWon(1,table);  //FIX THIS

        }

        printTable(table);
        
        while(1){

        //SAME DEAL AS PLAYER ONE

        printf("Player 2: Choose column (0-7);  ");

        choice2 = getInt();

        goodDrop = drop(2,choice,table,openSpot);

        game = playerHasWon(2,table); //FIX THIS

        }

        printTable(table);

 

    numMoves++;

  }

  printf("All done\n");

 

}

0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9676388
>return sscanf(gets(buf), "%d", &tmp) * tmp;

do NOT use gets anywhere in your program or your results will always be unpredictable

>but I still don't know how to fix my main though....
>I know player 1 and player 2 should be in a while loop but how do you do that?
I am not familiar with the game ... if you can list the objectives may be I can help ...

>game = playerHasWon(2,table); //FIX THIS
what fix do you want ?

seems like you want alternate tirns for the players ... if yes then use this

while(1){  

      printf("Player 1: Choose column (0-7):  ");

      choice = getInt();

      goodDrop = drop(1,choice,table,openSpot);

      game = playerHasWon(1,table);  //FIX THIS

       printTable(table);
     
       //SAME DEAL AS PLAYER ONE

      printf("Player 2: Choose column (0-7);  ");

      choice2 = getInt();

      goodDrop = drop(2,choice,table,openSpot);

      game = playerHasWon(2,table); //FIX THIS

      }

0
 

Author Comment

by:cooldean
ID: 9676445
Ok...I added that, sunnycoder...but when I execute it, it just goes directly to printf("All done!")...how come it doesn't go into the loop like I want it to.  It's not even printing out the table.  The revised version is below.

-------------------------------

#include <stdio.h>

/* course-specific functions to avoid using scanf */
int getInt() {
  int tmp = 0;
  char buf[200];
  return sscanf(gets(buf), "%d", &tmp) * tmp;
}

double getDouble() {
  double tmp = 0.0;
  char buf[200];
  return sscanf(gets(buf), "%lf", &tmp) * tmp;
}


/* Here is my code for Connect4.  I seem to have everything for it but it just won't compile right.  I think I'm calling
the functions wrong in my main but I spent hours trying to play with it with no good results. */



#define TABLESIZE 8
#define PL1 '1'
#define PL2 '2'
#define EMPTY '0'

int drop(int player, int col, int table[TABLESIZE][TABLESIZE], int openSpot[]) {

  if(openSpot[col] < TABLESIZE) {
    table[openSpot[col]][col] = player;
    openSpot[col]++;
    return 1;
  }

  else {
    printf("Row is full\n");
    return 0;
  }


}


int playerHasWon(int player, int table[TABLESIZE][TABLESIZE]) {
  int curRow;
  int curCol;


  /* This checks for horizontal victory */

  for(curRow = 0; curRow < TABLESIZE; curRow++) {
    for(curCol = 0; curCol < TABLESIZE - 3; curCol++) {
      if(table[curRow][curCol] == player && table[curRow][curCol+1] == player && table[curRow][curCol+2] == player && table[curRow][curCol+3] == player) {
      printf("Victory for %d\n",player);
      return player;
      }
    }
  }


  /* This checks for vertical victory */

  for(curCol = 0; curCol < TABLESIZE; curCol++) {
    for(curRow = 0; curRow < TABLESIZE - 3; curRow++) {
      if(table[curRow + 1][curCol] == player && table[curRow + 1][curCol] == player && table[curRow + 2][curCol] == player && table[curRow+3][curCol] == player) {
      printf("Victory for %d\n",player);
      return player;
      }
    }
  }



  /* This checks for diagonal victory:  First ascending, then descending */

  for(curRow = 0; curRow < TABLESIZE-3; curRow++) {
    for(curCol = 0; curCol < TABLESIZE-3; curCol++) {
      if(table[curRow][curCol] == player && table[curRow+1][curCol+1] == player && table[curRow+2][curCol+2] == player && table[curRow+3][curCol+3] == player) {

       }
    }
  }

  for(curRow = 3; curRow < TABLESIZE; curRow++) {
    for(curCol = 0; curCol < TABLESIZE-3; curCol++) {
      if(table[curRow][curCol] == player && table[curRow-1][curCol+1] == player && table[curRow-2][curCol+2] == player && table[curRow-3][curCol+3] == player) {

      }
    }
  }

  return 0;
}

/* This prints out the table. */


void printTable(int table[TABLESIZE][TABLESIZE]) {

  int temp1, temp2;

  for(temp1 = TABLESIZE - 1; temp1 >= 0; temp1++) {
    for(temp2 = 0; temp2 < TABLESIZE; temp2++) {
      printf("| %c ",table[temp1][temp2]);
    }
    printf("|\n");
  }

}


void main() {

  int numMoves = 0;
  int game, choice, choice2;
  int curRow, curCol;
  int table[TABLESIZE][TABLESIZE];
  int openSpot[TABLESIZE];
  int goodDrop;


  for(curRow = 0; curRow < TABLESIZE; curRow++) {
    for(curCol = 0; curCol < TABLESIZE; curCol++) {
      table[curRow][curCol] = EMPTY;
    }
  }

  while(game == 0 && numMoves < TABLESIZE * TABLESIZE) {

           printf("Welcome to Connect4!");
        printTable(table);
        goodDrop = 0;



       while(1){  

      printf("Player 1: Choose column (0-7):  ");

      choice = getInt();

      goodDrop = drop(1,choice,table,openSpot);

      game = playerHasWon(1,table);  

       printTable(table);
         

      printf("Player 2: Choose column (0-7);  ");

      choice2 = getInt();

      goodDrop = drop(2,choice,table,openSpot);

      game = playerHasWon(2,table);

      }

        printTable(table);

 

    numMoves++;

  }

  printf("All done\n");

 

}

0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9676480
>while(game == 0 && numMoves < TABLESIZE * TABLESIZE) {
game is uninitialized ... it may have some junk value which may not be 0 ... thus it will never enter the loop
always intialize your variables
0
 
LVL 5

Expert Comment

by:dennis_george
ID: 9676500
Hi,

The main reason for your control not going in the loop is that your variable game is not initialized .....
int game, choice, choice2;  

game will have garbage value...... Not neccessarily 0....

therefore the condition

 while(game == 0 && numMoves < TABLESIZE * TABLESIZE) {
    ......
  }

will never(mostly) be satisfied .... So it won't go inisde the loop

you should always initialize your variables.... like

int game = 0 ;  

Then it should work....

Dennis
0
 

Author Comment

by:cooldean
ID: 9678163
I did that but its still won't print the table right and it crashes.  Are my functions wrong?

-------------void main() {

  int numMoves = 0;
  int game = 0, choice, choice2;
  int curRow, curCol;
  int table[TABLESIZE][TABLESIZE];
  int openSpot[TABLESIZE];
  int goodDrop;


  for(curRow = 0; curRow < TABLESIZE; curRow++) {
    for(curCol = 0; curCol < TABLESIZE; curCol++) {
      table[curRow][curCol] = EMPTY;
    }
  }

  while(game == 0 && numMoves < TABLESIZE * TABLESIZE) {

           printf("Welcome to Connect4!");
        printTable(table);
        goodDrop = 0;
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 45

Expert Comment

by:sunnycoder
ID: 9678217
run it through a debugger to see where it crashes ... or copy paste the entire code (corrected one) and we will try to help too
0
 

Author Comment

by:cooldean
ID: 9678263
Here is the entire corrected code:

----------------------------------
#include <stdio.h>

/* course-specific functions to avoid using scanf */
int getInt() {
  int tmp = 0;
  char buf[200];
  return sscanf(gets(buf), "%d", &tmp) * tmp;
}

double getDouble() {
  double tmp = 0.0;
  char buf[200];
  return sscanf(gets(buf), "%lf", &tmp) * tmp;
}



#define TABLESIZE 8
#define PL1 '1'
#define PL2 '2'
#define EMPTY '0'

int drop(int player, int col, int table[TABLESIZE][TABLESIZE], int openSpot[]) {

  if(openSpot[col] < TABLESIZE) {
    table[openSpot[col]][col] = player;
    openSpot[col]++;
    return 1;
  }

  else {
    printf("Row is full\n");
    return 0;
  }


}


int playerHasWon(int player, int table[TABLESIZE][TABLESIZE]) {
  int curRow;
  int curCol;


  /* This checks for horizontal victory */

  for(curRow = 0; curRow < TABLESIZE; curRow++) {
    for(curCol = 0; curCol < TABLESIZE - 3; curCol++) {
      if(table[curRow][curCol] == player && table[curRow][curCol+1] == player && table[curRow][curCol+2] == player && table[curRow][curCol+3] == player) {
      printf("Victory for %d\n",player);
      return player;
      }
    }
  }


  /* This checks for vertical victory */

  for(curCol = 0; curCol < TABLESIZE; curCol++) {
    for(curRow = 0; curRow < TABLESIZE - 3; curRow++) {
      if(table[curRow + 1][curCol] == player && table[curRow + 1][curCol] == player && table[curRow + 2][curCol] == player && table[curRow+3][curCol] == player) {
      printf("Victory for %d\n",player);
      return player;
      }
    }
  }



  /* This checks for diagonal victory:  First ascending, then descending */

  for(curRow = 0; curRow < TABLESIZE-3; curRow++) {
    for(curCol = 0; curCol < TABLESIZE-3; curCol++) {
      if(table[curRow][curCol] == player && table[curRow+1][curCol+1] == player && table[curRow+2][curCol+2] == player && table[curRow+3][curCol+3] == player) {

       }
    }
  }

  for(curRow = 3; curRow < TABLESIZE; curRow++) {
    for(curCol = 0; curCol < TABLESIZE-3; curCol++) {
      if(table[curRow][curCol] == player && table[curRow-1][curCol+1] == player && table[curRow-2][curCol+2] == player && table[curRow-3][curCol+3] == player) {

      }
    }
  }

  return 0;
}

/* This prints out the table. */


void printTable(int table[TABLESIZE][TABLESIZE]) {

  int temp1, temp2;

  for(temp1 = TABLESIZE - 1; temp1 >= 0; temp1++) {
    for(temp2 = 0; temp2 < TABLESIZE; temp2++) {
      printf("| %c ",table[temp1][temp2]);
    }
    printf("|\n");
  }

}


void main() {

  int numMoves = 0;
  int game = 0, choice, choice2;
  int curRow, curCol;
  int table[TABLESIZE][TABLESIZE];
  int openSpot[TABLESIZE];
  int goodDrop;


  for(curRow = 0; curRow < TABLESIZE; curRow++) {
    for(curCol = 0; curCol < TABLESIZE; curCol++) {
      table[curRow][curCol] = EMPTY;
    }
  }

  while(game == 0 && numMoves < TABLESIZE * TABLESIZE) {

           printf("Welcome to Connect4!");
        printTable(table);
        goodDrop = 0;



       while(1){  

      printf("Player 1: Choose column (0-7):  ");

      choice = getInt();

      goodDrop = drop(1,choice,table,openSpot);

      game = playerHasWon(1,table);  

      printTable(table);
         

      printf("Player 2: Choose column (0-7);  ");

      choice2 = getInt();

      goodDrop = drop(2,choice,table,openSpot);

      game = playerHasWon(2,table);

      }

        printTable(table);

 

    numMoves++;

  }

  printf("All done\n");

 

}

0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9678383
remove gets !!!!!!!!!!!!!!!!!!!!!

> for(temp1 = TABLESIZE - 1; temp1 >= 0; temp1++)
                                                                    temp1-- !!!!!
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 350 total points
ID: 9678399
your corrected code

#include <stdio.h>

/* course-specific functions to avoid using scanf */
int getInt() {
 int tmp = 0;
 char buf[200];
 return sscanf(fgets(buf,200,stdin), "%d", &tmp) * tmp;
}

double getDouble() {
 double tmp = 0.0;
 char buf[200];
 return sscanf(fgets(buf,200,stdin), "%lf", &tmp) * tmp;
}



#define TABLESIZE 8
#define PL1 '1'
#define PL2 '2'
#define EMPTY '0'

int drop(int player, int col, int * table[TABLESIZE], int openSpot[]) {

 if(openSpot[col] < TABLESIZE) {
   table[openSpot[col]][col] = player;
   openSpot[col]++;
   return 1;
 }

 else {
   printf("Row is full\n");
   return 0;
 }


}


int playerHasWon(int player, int * table[TABLESIZE]) {
 int curRow;
 int curCol;


 /* This checks for horizontal victory */

 for(curRow = 0; curRow < TABLESIZE; curRow++) {
   for(curCol = 0; curCol < TABLESIZE - 3; curCol++) {
     if(table[curRow][curCol] == player && table[curRow][curCol+1] == player && table[curRow][curCol+2] == player && table[curRow][curCol+3] == player) {
    printf("Victory for %d\n",player);
    return player;
     }
   }
 }


 /* This checks for vertical victory */

 for(curCol = 0; curCol < TABLESIZE; curCol++) {
   for(curRow = 0; curRow < TABLESIZE - 3; curRow++) {
     if(table[curRow + 1][curCol] == player && table[curRow + 1][curCol] == player && table[curRow + 2][curCol] == player && table[curRow+3][curCol] == player) {
    printf("Victory for %d\n",player);
    return player;
     }
   }
 }



 /* This checks for diagonal victory:  First ascending, then descending */

 for(curRow = 0; curRow < TABLESIZE-3; curRow++) {
   for(curCol = 0; curCol < TABLESIZE-3; curCol++) {
     if(table[curRow][curCol] == player && table[curRow+1][curCol+1] == player && table[curRow+2][curCol+2] == player && table[curRow+3][curCol+3] == player) {

     }
   }
 }

 for(curRow = 3; curRow < TABLESIZE; curRow++) {
   for(curCol = 0; curCol < TABLESIZE-3; curCol++) {
     if(table[curRow][curCol] == player && table[curRow-1][curCol+1] == player && table[curRow-2][curCol+2] == player && table[curRow-3][curCol+3] == player) {

    }
   }
 }

 return 0;
}

/* This prints out the table. */


void printTable(int * table[TABLESIZE]) {

 int temp1, temp2;

 for(temp1 = TABLESIZE - 1; temp1 >= 0; temp1--) {
   for(temp2 = 0; temp2 < TABLESIZE; temp2++) {
     printf("| %c ",table[temp1][temp2]);
   }
   printf("|\n");
 }

}


void main() {

 int numMoves = 0;
 int game = 0, choice, choice2;
 int curRow, curCol,i;
// int table[TABLESIZE][TABLESIZE];
 int * table[TABLESIZE];
 int openSpot[TABLESIZE];
 int goodDrop;

 for ( i = 0; i < TABLESIZE; i++ )
       table [i] = (int *) malloc ( sizeof(int) * TABLESIZE);

 for(curRow = 0; curRow < TABLESIZE; curRow++) {
   for(curCol = 0; curCol < TABLESIZE; curCol++) {
     table[curRow][curCol] = EMPTY;
   }
 }

 while(game == 0 && numMoves < TABLESIZE * TABLESIZE) {

         printf("Welcome to Connect4!");
      printTable(table);
      goodDrop = 0;



     while(1){  

     printf("Player 1: Choose column (0-7):  ");

     choice = getInt();

     goodDrop = drop(1,choice,table,openSpot);

     game = playerHasWon(1,table);  

     printTable(table);
       

      printf("Player 2: Choose column (0-7);  ");

     choice2 = getInt();

     goodDrop = drop(2,choice,table,openSpot);

     game = playerHasWon(2,table);

     }

      printTable(table);

 

   numMoves++;

 }

 printf("All done\n");

 

}
0
 

Author Comment

by:cooldean
ID: 9678455
Thanks sunnycoder...I really appreciate it.

But when I run it, it crashes.  It won't even go to asking Player 2 to choose...do u get that error?

0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9678468
nope works file here ... did you change gets() ... the code I posted has removed gets()
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9678501
as i said, i dont know the game ... here is a sample output

test]$ ./a.out
Welcome to Connect4!| 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 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
Player 1: Choose column (0-7):  4
| 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 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | ` | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
Player 2: Choose column (0-7);  3
Player 1: Choose column (0-7):  6
Row is full
| 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 | 0 | 0 | a | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | ` | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
Player 2: Choose column (0-7);  7
Row is full
Player 1: Choose column (0-7):  2
Row is full
| 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 | 0 | 0 | a | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | ` | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
Player 2: Choose column (0-7);  1
Row is full
Player 1: Choose column (0-7):  
0
 

Author Comment

by:cooldean
ID: 9678985
Hmm...I'll use another computer and try the code there.

But basically, the objective of the game is to connect four of your pieces either vertically, horizontally, or diagonally.  When four pieces are in a row that way, then that player wins.

Everytime a player chooses a column, a piece is placed to the next open spot.  Both players go back and forth.  "Row is full" should only appear after there are no more open spots available in that space and there is still no winner in the game.

I've spent too much time on trying to get this game to work...
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9678993
in that case you should be asking for row and column... you are asking only for a column !!!
0
 

Expert Comment

by:Spyder596
ID: 9681153
No, sunnycoder, as you posted before you dont understand how the game works. It was originally a board game, so the basic concept behind how it worked was gravity. There is a vertical board (8x8) with 8 columns. players alternate dropping chips into the columns, trying to get 4 in a row either horizontally, vertically, or diagonally. So basically you dont need to enter a row, as the program should automatically put the chip in the next open slot from the bottom.
0
 

Assisted Solution

by:Spyder596
Spyder596 earned 150 total points
ID: 9681442
Ok, heres my version of the fixed code. I fixed a all of the little logic errors and such, and also implemented a better way of keeping track of how full the rows are: you dont. I made a function to get the next open row position, so you dont have to rely on a separate array that could get messed up.

I also fixed the victory logic error, where if player one wins, it still plays player two and if player two didnt have 4 in a row, the variable "game" got reset and the game continued.

Also changed empty to a space character, so that the output is more readable and looks better ;)

I didnt mess with the victory functions and im presuming they work.

---------------------------

#include <stdio.h>

/* course-specific functions to avoid using scanf */



#define TABLESIZE 8
#define PL1 '1'
#define PL2 '2'
#define EMPTY ' '

int getInt();
double getDouble();
bool drop(int player, int col, int table[TABLESIZE][TABLESIZE]);
int getNextSlot(int col, int table[TABLESIZE][TABLESIZE]);
int playerHasWon(int player, int table[TABLESIZE][TABLESIZE]);
void printTable(int table[TABLESIZE][TABLESIZE]);

int getInt() {
 int tmp = 0;
 char buf[200];
 return sscanf(fgets(buf,200,stdin), "%d", &tmp) * tmp;
}

double getDouble() {
 double tmp = 0.0;
 char buf[200];
 return sscanf(fgets(buf,200,stdin), "%lf", &tmp) * tmp;
}

bool drop(int player, int col, int table[TABLESIZE][TABLESIZE])
{
      int open = getNextSlot(col,table);
      if(open != -1)
      {
            table[open][col] = player;
            return true;
      }else{
            printf("Row is full\n");
            return false;
      }
}

int getNextSlot(int col, int table[TABLESIZE][TABLESIZE])
{
      for(int x=0;x<TABLESIZE;x++)
            if(table[x][col] == EMPTY)
                  return x;
      return -1;
}

int playerHasWon(int player, int table[TABLESIZE][TABLESIZE])
{
      int curRow;
      int curCol;


 /* This checks for horizontal victory */

 for(curRow = 0; curRow < TABLESIZE; curRow++) {
   for(curCol = 0; curCol < TABLESIZE - 3; curCol++) {
     if(table[curRow][curCol] == player && table[curRow][curCol+1] == player && table[curRow][curCol+2] == player && table[curRow][curCol+3] == player) {
    printf("Victory for %d\n",player);
    return player;
     }
   }
 }


 /* This checks for vertical victory */

 for(curCol = 0; curCol < TABLESIZE; curCol++) {
   for(curRow = 0; curRow < TABLESIZE - 3; curRow++) {
     if(table[curRow + 1][curCol] == player && table[curRow + 1][curCol] == player && table[curRow + 2][curCol] == player && table[curRow+3][curCol] == player) {
    printf("Victory for %d\n",player);
    return player;
     }
   }
 }



 /* This checks for diagonal victory:  First ascending, then descending */

 for(curRow = 0; curRow < TABLESIZE-3; curRow++) {
   for(curCol = 0; curCol < TABLESIZE-3; curCol++) {
     if(table[curRow][curCol] == player && table[curRow+1][curCol+1] == player && table[curRow+2][curCol+2] == player && table[curRow+3][curCol+3] == player) {

     }
   }
 }

 for(curRow = 3; curRow < TABLESIZE; curRow++) {
   for(curCol = 0; curCol < TABLESIZE-3; curCol++) {
     if(table[curRow][curCol] == player && table[curRow-1][curCol+1] == player && table[curRow-2][curCol+2] == player && table[curRow-3][curCol+3] == player) {

    }
   }
 }

 return 0;
}

/* This prints out the table. */


void printTable(int table[TABLESIZE][TABLESIZE]) {

 int temp1, temp2;

 for(temp1 = TABLESIZE - 1; temp1 >= 0; temp1--) {
   for(temp2 = 0; temp2 < TABLESIZE; temp2++) {
     printf("| %c ",table[temp1][temp2]);
   }
   printf("|\n");
 }

}


void main() {

 int numMoves = 0;
 int game = 0, choice, choice2;
 int curRow, curCol;
 int table[TABLESIZE][TABLESIZE];
 bool goodDrop;

 for(curRow = 0; curRow < TABLESIZE; curRow++) {
   for(curCol = 0; curCol < TABLESIZE; curCol++) {
     table[curRow][curCol] = EMPTY;
   }
 }
 printf("Welcome to Connect 4!\n");

 while(game == 0 && numMoves < TABLESIZE * TABLESIZE) {

    goodDrop = false;
      while(!goodDrop)
      {
             printf("Player 1: Choose column (1-8):  ");

             choice = getInt()-1;

             goodDrop = drop(1,choice,table);

             game = playerHasWon(1,table);  

             printTable(table);
      }
   
      if(game != 0)
            continue;

      goodDrop = false;
      while(!goodDrop)
      {
             printf("Player 2: Choose column (1-8);  ");

             choice2 = getInt()-1;

             goodDrop = drop(2,choice2,table);

             game = playerHasWon(2,table);

             printTable(table);
      }

 

   numMoves++;

 }

 printf("All done\n");

 

}
0
 

Author Comment

by:cooldean
ID: 9681638
thanks Spyder, but running your revised code gave me alot of errors..mostly undeclared identifiers...and we didn't learn "bool" yet so I can't use that.  Thanks alot, though.  

0
 

Expert Comment

by:Spyder596
ID: 9683506
hmm, ran just fine for me. I dunno what to say about that. Where was it getting those errors?

As for the bool, its pretty much just like using an int as 1 or 0, but makes the code more readable. You can easly just change all the "true" to 1 and "false" to 0, and wherever it says bool make it an int.

0
 

Expert Comment

by:Spyder596
ID: 9683515
oh, and in main(), change

while(!goodDrop)

to

while(goodDrop != 1)
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now