I cant get my code to say the winner after 4 spaces have been occupied?
Code:
#include "ConnectFour.h"
void initialize_game_board (Cell game_board [6][7])
{
int row_index = 0, col_index = 0;
for (row_index = 0; row_index < 6; row_index++)
{
for (col_index = 0; col_index < 7; col_index++)
{
game_board[row_index][col_index].color = NONE;
game_board[row_index][col_index].occupied = FALSE;
game_board[row_index][col_index].location.row = row_index;
game_board[row_index][col_index].location.column = col_index;
}
}
}
void print_game_board (Cell game_board [6][7])
{
int row_index = 0, col_index = 0;
printf ("0 1 2 3 4 5 6\n");
for (row_index = 0; row_index < 6; row_index++)
{
for (col_index = 0; col_index < 7; col_index++)
{
switch (game_board[row_index][col_index].color)
{
case NONE: printf ("- ");
break;
case RED: printf ("r ");
break;
case BLACK: printf ("b ");
break;
}
}
putchar ('\n');
}
}
void place_token (Cell game_board [6][7], Token_color new_color, int column)
{
int row_index = 0;
for (row_index = 0; (row_index <= 5) && (game_board[row_index][column].occupied == FALSE); row_index++)
{
// No need for a body here
}
switch (new_color)
{
case RED: game_board[row_index - 1][column].color = RED;
game_board[row_index - 1][column].occupied = TRUE;
break;
case BLACK: game_board[row_index - 1][column].color = BLACK;
game_board[row_index - 1][column].occupied = TRUE;
break;
}
}
int isWin(int player, Cell game_board[6][7], int size)
{
char symbol;
int i, j, number, win = 0;
if (player == 0)
symbol = 'b';
else if (player == 1)
symbol = 'r';
//horizontally
for (i = 0; i < size && win == 0; i++)
{
number = 0;
for (j = 0; j < size; j++)
if (game_board[i][j].color == symbol)
number++;
if (number == size)
win = 1;
}
if(win == 0)
{
//vertically
for (i = 0; i < size && win == 0; i++)
{
number = 0;
for (j = 0; j < size; j++)
if (game_board[j][i].color == symbol)
number++;
if (number == size)
win = 1;
}
}
if (win == 0)
{
//diagonally
number = 0;
for (i = 0; i < size; i++)
if (game_board[i][i].color == symbol)
number++;
if (number == size)
win = 1;
if(win == 0)
{
number = 0;
for (i = 0; i < size; i++)
if (game_board[i][size - 1 - i].color == symbol)
number++;
if (number == size)
win = 1;
}
}
return win;
}
Code:
#include "ConnectFour.h"
int main (void)
{
int col_slot = 0, flag = 0, player1, player2, column = 0;
Cell game_board[50][50];
Token_color new_color = RED;
srand (time(NULL));
initialize_game_board (game_board);
do
{
player2 = rand() % 6 + 1;
print_game_board (game_board);
new_color = RED;
printf ("PLAYER1:What column would you want to place your token in?");
scanf (" %d", &column);
place_token (game_board, new_color, column);
system("pause");
system("cls");
new_color = BLACK;
place_token (game_board, new_color, player2);
if(isWin(0, game_board, 4) == 1)
{
printf("You win!");
break;
}
else if(isWin(1, game_board, 4) == 1)
{
printf("You lose!");
break;
}
} while (flag < 13);
return 0;
}
Code:
#ifndef CONNECT_FOUR_H
#define CONNECT_FOUR_H
#include <stdio.h>
typedef enum boolean
{
FALSE, TRUE
} Boolean;
typedef enum token_color
{
NONE, RED, BLACK
} Token_color;
typedef struct position
{
int row;
int column;
} Position;
typedef struct cell
{
Boolean occupied;
Token_color color;
Position location;
} Cell;
void initialize_game_board (Cell game_board [6][7]);
void print_game_board (Cell game_board [6][7]);
void place_token (Cell game_board [6][7], Token_color new_color, int column);
int isWin(int player, Cell game_board[6][7], int size);
#endif