• C

# Connect Four..just minor fixes

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++;
}

}
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
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 ;)
Commented:
err, if you still have problems, reply. Dunno what i was thinking ... :\
Commented:
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

Author Commented:
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.
Author Commented:
my last comment was for Spyder...

got it sunnycoder...I fixed that.
Commented:
change all function declaration to
int playerHasWon(int player, int * table[])

in place of
>
int playerHasWon(int player, int table[][])
Commented:
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;
}

Commented:
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);
Author Commented:
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");

}

Commented:
>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

}

Author Commented:
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");

}

Commented:
>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
Commented:
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
Author Commented:
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;
Commented:
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
Author Commented:
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");

}

Commented:
remove gets !!!!!!!!!!!!!!!!!!!!!

> for(temp1 = TABLESIZE - 1; temp1 >= 0; temp1++)
temp1-- !!!!!
Commented:

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

}

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
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?

Commented:
nope works file here ... did you change gets() ... the code I posted has removed gets()
Commented:
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):
Author Commented:
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...
Commented:
in that case you should be asking for row and column... you are asking only for a column !!!
Commented:
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.
Commented:
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");

}
Author Commented:
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.

Commented:
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.

Commented:
oh, and in main(), change

while(!goodDrop)

to

while(goodDrop != 1)
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.