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

USING C LANGUAGE PLEASE, Write a function win() with the following prototype: in

ID: 3729636 • Letter: U

Question

USING C LANGUAGE PLEASE,

Write a function win() with the following prototype:

int win(char board[6][6], char player)

The function win should return a 1 if the character player is found in three consecutive positions in the board.

Consecutive for this lab means in the same row, or in the same column, NOT on a diagonal.

For the board shown below,

A B B D E G Z A B G G E Z B A G G G Z Z X G K K X X O K O W X X O O W W

The following values should be returned by win() if called as:

win(board,'A') would return 0

win(board,'B') would return 0

win(board,'D') would return 0

win(board,'G') would return 1

win(board,'O') would return 0

win(board,'X') would return 0

win(board,'Z') would return 1

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

int win(char board[6][6], char player) {
// insert your code for the function here
return -1;
}


int main() {

//initialize the array

char board[6][6];
int i,j;
char user;
int gameresult;

//read in the board
printf("Enter 36 values for the 6x6 game board ");
for(i=0;i<6;i++){
for(j=0;j<6;j++) {
scanf("%c ",&(board[i][j]));
}
}

// print the board
for(i=0;i<6;i++){
for(j=0;j<6;j++) {
printf("%c ",(board[i][j]));
}
printf(" ");
}


printf("Enter the uesr's token to see if they won the game!");
scanf("%c",&user);
printf(" ");

gameresult = win(board,user);

if (gameresult == 1)
printf("Player %c won the game!",user);
else if (gameresult == 0)
printf("Player %c didn't win.",user);
else
printf("invalid response from win() ");


return 0;

}

Explanation / Answer

#include<stdio.h>

#include<stdlib.h>

#include <stdbool.h>

int win(char board[6][6], char player) {

// insert your code for the function here

bool result = false;

for(int i = 0; i < 4; i++)

{

for(int j = 0; j < 4; j++)

{

if(board[i][j] == player)

{

if(board[i][j] == board[i+1][j] && board[i][j] == board[i+2][j] || board[i][j]== board[i][j+1] && board[i][j] == board[i][j+2])

{

result = true;

break;

}

}

}

  

if(result)

{

break;

}

}

if(result)

{

return 1;

}

else

{

return 0;

}

}

int main() {

//initialize the array

char board[6][6];

int i,j;

char user;

int gameresult;

//read in the board

printf("Enter 36 values for the 6x6 game board ");

for(i=0;i<6;i++){

for(j=0;j<6;j++) {

scanf("%c ",&(board[i][j]));

}

}

// print the board

for(i=0;i<6;i++){

for(j=0;j<6;j++) {

printf("%c ",(board[i][j]));

}

printf(" ");

}

printf("Enter the uesr's token to see if they won the game!");

scanf("%c",&user);

printf(" ");

gameresult = win(board,user);

if (gameresult == 1)

printf("Player %c won the game!",user);

else if (gameresult == 0)

printf("Player %c didn't win.",user);

else

printf("invalid response from win() ");

return 0;

}

// PLEASE PROVIDE FEEDBACK