Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

file connect4.c #include \"connect4_functions.h\" int main() { int board[BOARD_S

ID: 3640160 • Letter: F

Question

file connect4.c

#include "connect4_functions.h"




int main()
{
int board[BOARD_SIZE_HORIZ][BOARD_SIZE_VERT] = { {0} };
int player_num, computer_num;
int last_move;


player_num = print_welcome();
if (player_num == 1) computer_num = 2;
else computer_num = 1;

if (player_num == 1)
{
display_board(board);
last_move = player_move(board,player_num);
display_board(board);
}



while (1)
{
last_move = random_move(board,computer_num);
printf("Computer moved in column: %d ", last_move);
display_board(board);

if (check_win_or_tie(board,last_move)) return 0;


last_move = player_move(board,player_num);
display_board(board);

if (check_win_or_tie(board,last_move)) return 0;


}

}





file connect4_functions.h

#ifndef CONNECT4_FUNCTIONS
#define CONNECT4_FUNCTIONS




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




#define BOARD_SIZE_HORIZ 7
#define BOARD_SIZE_VERT 6







int print_welcome(void);
void display_board(int board[][BOARD_SIZE_VERT]);



int random_move(int board[][BOARD_SIZE_VERT], int computer_num);



int player_move(int board[][BOARD_SIZE_VERT], int player_num);



int check_winner(int board[][BOARD_SIZE_VERT], int last_move);



int best_move(int board[][BOARD_SIZE_VERT], int computer_num);


#endif

file connect4_functions.c

#include "connect4_functions.h"
bool check_vertical(int board[][BOARD_SIZE_VERT], int player,int col,int row);
bool check_horizontal(int board[][BOARD_SIZE_VERT],int player,int col,int row);
bool check_diagonal(int board[][BOARD_SIZE_VERT], int player,int col,int row);

void print_row_separator(void);
void print_column_numbers(void);


int print_welcome(void)
{
char c = 'y';

printf("*** Welcome to the Connect-Four game!!! *** ");
printf("Would you like to make the first move [y/n]: ");

c = getchar();
printf(" ");


while (getchar() != ' ');

if (c == 'n' || c == 'N') return 2;
else return 1;
}




void display_board(int board[][BOARD_SIZE_VERT])
{
int i, j;




printf(" ");
print_row_separator();
printf(" ");




for (j = 0; j < BOARD_SIZE_VERT; j++)
{
for (i = 0; i < BOARD_SIZE_HORIZ; i++)
{
printf("|");
switch (board[i][j])
{
case 0:
printf(" ");
break;
case 1:
printf(" X ");
break;
case 2:
printf(" O ");
break;
}


}

printf("| ");
print_row_separator();
printf(" ");

}

print_column_numbers();
printf(" ");

}
int random_move(int board[][BOARD_SIZE_VERT], int computer_num)
{
int m;
static bool first = true;




if (first)
{
srand(time(0));
first = false;
}




do m = (rand()%BOARD_SIZE_HORIZ) + 1;
while (is_column_full(board, m));





update_board(board, m, computer_num);
return m;
}bool check_win_or_tie(int board[][BOARD_SIZE_VERT], int last_move)
{
int who_won;
int i, j;




who_won = check_winner(board, last_move);

if (who_won != 0)
{
printf("***************************** ");

if (who_won == 1)
printf("* Player X won!!! Game over * ");
else printf("* Player O won!!! Game over * ");

printf("***************************** ");

return true;
}





for (i = 0; i < BOARD_SIZE_HORIZ; i++)
for (j = 0; j < BOARD_SIZE_VERT; j++)
if (board[i][j] == 0) return false;

printf("***************************** ");
printf("* Game is a tie!! No winner * ");
printf("***************************** ");
return true;

}




bool is_column_full(int board[][BOARD_SIZE_VERT], int m)
{

if (board[m-1][0] == 0) return false;

return true;

}





void update_board(int board[][BOARD_SIZE_VERT], int m, int player_num)
{
int i;

for (i = BOARD_SIZE_VERT-1; i >= 0; i--)
if (board[m-1][i] == 0)
{
board[m-1][i] = player_num;
break;
}
}

int check_winner(int board[][BOARD_SIZE_VERT], int last_move)
{
int i, player, last_row;



last_move--;
for (i = 0; i < BOARD_SIZE_HORIZ; i++)
if (board[last_move][i] != 0)
{
player = board[last_move][i];
last_row = i;
break;
}




if (check_vertical(board, player, last_move, last_row)) return player;
if (check_horizontal(board, player, last_move, last_row)) return player;
if (check_diagonal(board, player, last_move, last_row)) return player;

return 0;

}





int best_move(int board[][BOARD_SIZE_VERT], int computer_num)
{



return 0;

}




bool check_vertical(int board[][BOARD_SIZE_VERT], int player, int col, int row)
{
int i;
int counter = 1;

for (i = row+1; i < BOARD_SIZE_VERT; i++)
{
if (board[col][i] != player) break;
counter++;
}

if (counter >= 4) return true;
return false;
}


bool check_horizontal(int board[][BOARD_SIZE_VERT], int player,int col,int row)
{
int i;
int counter = 1;

for (i = col+1; i < BOARD_SIZE_HORIZ; i++)
{
if (board[i][row] != player) break;
counter++;
}

for (i = col-1; i >= 0; i--)
{
if (board[i][row] != player) break;
counter++;
}

if (counter >= 4) return true;
return false;
}

bool check_diagonal(int board[][BOARD_SIZE_VERT], int player, int col, int row)
{
int i, j;
int counter = 1;



i = col+1; j = row+1;
for ( ; i < BOARD_SIZE_HORIZ && j < BOARD_SIZE_VERT ; i++, j++)
{
if (board[i][j] != player) break;
counter++;
}

for (i = col-1, j = row-1; i >= 0 && j >= 0 ; i--, j--)
{
if (board[i][j] != player) break;
counter++;
}

if (counter >= 4) return true;





counter = 1;
for (i = col+1, j = row-1; i < BOARD_SIZE_HORIZ && j >= 0 ; i++, j--)
{
if (board[i][j] != player) break;
counter++;
}

for (i = col-1, j = row+1; i >= 0 && j < BOARD_SIZE_VERT ; i--, j++)
{
if (board[i][j] != player) break;
counter++;
}

if (counter >= 4) return true;



return false;

}




void print_row_separator()
{
int i;

printf("+---+");
for (i = 1; i < BOARD_SIZE_HORIZ; i++) printf("---+");
}


void print_column_numbers()
{
int i;

printf(" ");
for (i = 1; i <= BOARD_SIZE_HORIZ; i++) printf(" %d ", i);

printf(" ");
}

hint: (gcc -Wall -std=c99 connect4.c connect4_functions.c -o connect4) this how you should compile all those 3 files together

Explanation / Answer

PS: Please rate the answer I've got it to compile. You had some function declarations missing and a function definition missing - player_move ---------------------------------------------------------------------------------------------- connect4.c ---------------------------------------------------------------------------------------------- #include "connect4_functions.h" int main() { int board[BOARD_SIZE_HORIZ][BOARD_SIZE_VERT] = { {0} }; int player_num, computer_num; int last_move; player_num = print_welcome(); if (player_num == 1) computer_num = 2; else computer_num = 1; if (player_num == 1) { display_board(board); last_move = player_move(board,player_num); display_board(board); } while (1) { last_move = random_move(board,computer_num); printf("Computer moved in column: %d ", last_move); display_board(board); if (check_win_or_tie(board,last_move)) return 0; last_move = player_move(board,player_num); display_board(board); if (check_win_or_tie(board,last_move)) return 0; } } ---------------------------------------------------------------------------------------------- connect4_functions.h ---------------------------------------------------------------------------------------------- #ifndef CONNECT4_FUNCTIONS #define CONNECT4_FUNCTIONS #include #include #include #include #define BOARD_SIZE_HORIZ 7 #define BOARD_SIZE_VERT 6 int print_welcome(void); void display_board(int board[][BOARD_SIZE_VERT]); int random_move(int board[][BOARD_SIZE_VERT], int computer_num); int player_move(int board[][BOARD_SIZE_VERT], int player_num); int check_winner(int board[][BOARD_SIZE_VERT], int last_move); int best_move(int board[][BOARD_SIZE_VERT], int computer_num); #endif ---------------------------------------------------------------------------------------------- connect4_functions.c ---------------------------------------------------------------------------------------------- #include "connect4_functions.h" bool check_vertical(int board[][BOARD_SIZE_VERT], int player,int col,int row); bool check_horizontal(int board[][BOARD_SIZE_VERT],int player,int col,int row); bool check_diagonal(int board[][BOARD_SIZE_VERT], int player,int col,int row); void print_row_separator(void); bool is_column_full(int board[][BOARD_SIZE_VERT], int m); void print_column_numbers(void); void update_board(int board[][BOARD_SIZE_VERT], int m, int player_num); int player_move(int board[][BOARD_SIZE_VERT], int player_num); int player_move(int board[][BOARD_SIZE_VERT], int player_num) { } int print_welcome(void) { char c = 'y'; printf("*** Welcome to the Connect-Four game!!! *** "); printf("Would you like to make the first move [y/n]: "); c = getchar(); printf(" "); while (getchar() != ' '); if (c == 'n' || c == 'N') return 2; else return 1; } void display_board(int board[][BOARD_SIZE_VERT]) { int i, j; printf(" "); print_row_separator(); printf(" "); for (j = 0; j