#include<stdio.h> #include<stdlib.h> int win(char board[6][6], char player) { //
ID: 3729822 • Letter: #
Question
#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;
} #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;
} Write a function win0 with the following prototype int win(char board[61(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 B B D E G The following values should be returned by win0 if called as: win(board'A) would return win(board:8) would return O win(board D) would return O win(board G) would return 1 win(board O) would return 0 win(bo win(board, Z) would return 1 s points will be for the autograded tests, the remaining S points will be for a header with your nome () point), approprinte sityle (2 point d approp iate comiments (2 paints)
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
int win(char board[6][6], char player) // the function
{
//code
int i,j;
int count = 0;
int flag = 0;
for(i = 0; i < 6; i++)
{
for(j = 0 ; j < 6; j++)
{
if(flag!=1 && board[i][j] == player)
{
flag =1;
count ++;
}
else if(flag == 1 && board[i][j] == player)
{
count++;
if(count == 3)
{
return 1;
}
}
else if(flag ==1 && board[i][j]!= player)
{
count =0;
flag = 0;
}
}
for(j = 0 ; j < 6; j++)
{
if(flag!=1 && board[j][i] == player)
{
flag =1;
count ++;
}
else if(flag == 1 && board[j][i] == player)
{
count++;
if(count == 3)
{
return 1;
}
}
else if(flag ==1 && board[j][i]!= player)
{
count =0;
flag = 0;
}
}
}
return 0;
}
int main()
{
char board[6][6];
int i,j;
char user;
int gameresult;
char dummy;
printf("Enter 36 value for the 6x6 game board ");
for(i = 0; i < 6; i ++){
for(j =0 ; j <6; j++){ // while giving input dont use any space between the charecters
scanf("%c",&(board[i][j])); // or scanf will take space as input
}
}
scanf("%c",&dummy); //after giving the input when you press enter
//the enter must be consumed so we used dummy for that.
//print the board
for(i=0;i<6;i++){
for(j=0;j<6;j++){
printf("%c ",(board[i][j]));
}
printf(" ");
}
printf("Enter the user'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 the game!",user);
else
printf("invalid response from win() ");
return 0;
}
Output:
$ ./a.out
Enter 36 value for the 6x6 game board
ABBDEGZABGGEZBAGGGZZXGKKXXOKOWXXOOWW
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
Enter the user's token to see if they won the game!Z
Player Z won the game!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.