Please write in C language, Write a function win with the following prototype in
ID: 3729694 • Letter: P
Question
Please write in C language,
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 G G E B A G G G 7 The following values should be returned by win0 if called as win(board A) would return O 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 5 points will be for the autograded tests, the remaining 5 points will be for a header with your name (1 point), appropriate style (2 points) and appropriate comments (2 points)Explanation / Answer
Hello, I have a solution for you. Implemented everything as per the requirements. Defined following things in this answer.
//main.c
#include<stdio.h>
/* method to check if there is a 3 consecutive 'player' character
in the given 6*6 board*/
int win(char board[6][6],char player){
//a variable to keep the track of consecutive 'player' char found
int consecutiveCount=0;
/* searching horizontally (row wise)*/
for(int i=0;i<6;i++){
consecutiveCount=0;
for(int j=0;j<6;j++){
if(board[i][j]==player){
/*if the character at this position is player's character,
incrementing the consecutiveCount value */
consecutiveCount++;
if(consecutiveCount==3){
//three consecutive occurrence found, returning 1
return 1;
}
}else{
/*if the character is not player's character, resetting the
consecutiveCount variable to 0*/
consecutiveCount=0;
}
}
}
//similar to the above check, this time vertically (column wise)
for(int i=0;i<6;i++){
consecutiveCount=0;
for(int j=0;j<6;j++){
if(board[j][i]==player){
consecutiveCount++;
if(consecutiveCount==3){
return 1;
}
}else{
consecutiveCount=0;
}
}
}
return 0;
}
int main(){
/* defining a 6*6 board*/
char board[6][6]={
{'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'},
{'A','B','B','Z','O','K'}};
/*displaying the win status for each players*/
printf("A - %d ",win(board,'A'));
printf("B - %d ",win(board,'B'));
printf("D - %d ",win(board,'D'));
printf("G - %d ",win(board,'G'));
printf("O - %d ",win(board,'O'));
printf("X - %d ",win(board,'X'));
printf("Z - %d ",win(board,'Z'));
return 0;
}
/*OUTPUT*/
A - 0
B - 0
D - 0
G - 1
O - 0
X - 0
Z - 1