Write in C Language please. Write a function win() with the following prototype:
ID: 3599831 • Letter: W
Question
Write in 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,
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
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)
#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
int i, j, rowLenght = 6, colLenght = 6, board[rowLenght][colLenght];
for(i = 0; i < rowLenght; ++i)
{
for(j = 0; j < colLenght; ++j)
if(board[i][j] == player) && (board[i][j+1] == player) && (board[i][j+2] == player) || (board[i][j] == player) && (board[i+1][j] == player) && (board[i+2][j] == player)
return 1 ;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.