In: Computer Science
C program. librarys that are in use; stdio.h, stdlib.h, time.h, stddef.h, ctype.h, math.h, string.h.
Need to rewrite the functions linked in the code: "return add_move_lib ( board, col, colour ) ;" "return board_full_lib ( board ) ;" "return display_board_lib ( board ) ;
// TASKS
// board_full() display_board() add_move()
// adds a token of the given value (1 or 2) to the board at the
// given column (col between 1 and COLS inclusive)
// Returns 0 if successful, -1 otherwise
int add_move ( int board[COLS][ROWS], int col, int colour )
{
// check that the column isn't full - if it is, return -1 and abort
// start at the bottom of the board, and move upwards in the specified column until an empty spot is found
// put a token of the specified colour at that location
return add_move_lib ( board, col, colour ) ;
}
// determines if the board is completely full or not.
// Return TRUE if full, false otherwise
int board_full ( int board[COLS][ROWS] ){
// loop through each column
// if a column ISN'T full, return 0 (board can't be full if a single column isn't
// if all columns are checked and full, return 1
return board_full_lib ( board ) ;
}
// displays the board to the screen
int display_board ( int board[COLS][ROWS] ){
// loop through each row and column of the board and display in appropriate format
// use | and - characters to draw boundaries of the board, and put numbers at the bottom to indicate the column numbers
return display_board_lib ( board ) ;
}
// adds a token of the given value (1 or 2) to the board at the
// given column (col between 1 and COLS inclusive)
// Returns 0 if successful, -1 otherwise
int add_move ( int board[COLS][ROWS], int col, int colour )
{
// check that the column isn't full - if it is, return -1 and abort
// start at the bottom of the board, and move upwards in the specified column until an empty spot is found
// put a token of the specified colour at that location
return add_move_lib ( board, col, colour ) ;
}
// helper function to add a token of the given value (1 or 2) to the board at the given column
int add_move_lib ( int board[COLS][ROWS], int col, int colour )
{
// check if valid column
if(col < 1 || col > COLS)
{
return -1;
}else
{
int i;
// get the first empty spot from bottom
for(i=ROWS;i>0;i--)
{
if((board[col][i] != 1) && (board[col][i] != 2))
{
board[col][i] = colour;
return 0;
}
}
return -1; // non-empty column
}
}
// determines if the board is completely full or not.
// Return TRUE if full, false otherwise
int board_full ( int board[COLS][ROWS] ){
// loop through each column
// if a column ISN'T full, return 0 (board can't be full if a single column isn't
// if all columns are checked and full, return 1
return board_full_lib ( board ) ;
}
// helper function to check if board is empty or not
int board_full_lib ( int board[COLS][ROWS] )
{
int i,j;
for(i=1;i<=COLS;i++)
{
for(j=1;j<=ROWS;j++)
if((board[i][j] != 1) && (board[i][j] != 2))
return 0;
}
return 1;
}
// displays the board to the screen
int display_board ( int board[COLS][ROWS] ){
// loop through each row and column of the board and display in appropriate format
// use | and - characters to draw boundaries of the board, and put numbers at the bottom to indicate the column numbers
return display_board_lib ( board ) ;
}
// helper function to display the board on the console
int display_board_lib ( int board[COLS][ROWS] )
{
int i,j;
for(i=1;i<=COLS;i++)
{
printf("\n|");
for(j=1;j<=ROWS;j++)
{
if(board[i][j] == 1 || board[i][j] == 2)
printf("%d|",board[i][j]);
else
printf(" |");
}
printf("\n");
if(i < COLS)
{
for(j=0;j<2*ROWS;j++)
printf("-");
}
}
return 0;
}