Question

11.9 Week 11 Lab: 2D Arrays For this lab, you only need to write a single user-defined function that analyzes a 2D array of cthe following values should be returned by win0 if called as: win(board A) returns 0 win(board,B) returns 0 win(board,D) retumain.c Load default template... 1 2 #include<stdio.h> #include<stdlib.h> 4 int win(char board[ 7][5], char player) i 5 I/ ins32 printf(n); 34 35 36 printf( Enter the uesrs token to see if they won the game!); 37 scanf&user) 38 printf(Inln); 39

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C code

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

int win(char board[7][5], char player){

//looping variables
int i,j;
//checking consecutive positions in a row

//for looping through the rows
for(i=0;i<7;i++){
//for looping through the columns
//in this loop 3 elements are bring compared at once
//so the loop starts from 1 and ends at 3
//because j-1,j and jth elements are being considered
for(j=1;j<4;j++){
//if the given character is present in these 3 consecutive positions
//then 1 will be returned by the function
//in the if condition the row variable 'i' is same
//and the column variable 'j' is changed for checking the values in a single row
if( (board[i][j-1]==player) && (board[i][j]==player) && (board[i][j+1]==player)){
return 1;
}
}
}

//now we check for consecutive positions in a column

//for looping through the column
for(i=0;i<5;i++){
//for looping through the columns
//as 3 elements are being checked at once
//so the loop will run from 1 to 5
for(j=1;j<6;j++){
//here the column variable 'i' is kept constant
//and the row variable 'j' is changed for checking values
//in a single column
//if three consecutive values are found then 1 is returned
if( (board[j-1][i]==player) && (board[j][i]==player) && (board[j+1][i]==player)){
return 1;
}
}
}
//if no value is returned from the execution of above loops
//then the given character is not present consecutively
//so 0 will be returned
return 0;
}

int main() {
//initialize the array
char board[7][5];
int i,j;
char user;
int gameresult;

//read the board
printf("Enter 35 values for the 7X5 game board\n");
for(i=0;i<7;i++){
for(j=0;j<5;j++){
scanf(" %c",&(board[i][j]));
}
}

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

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

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()\n");

return 0;

}

***Important Note***

One very important that needs to be taken care of here is how you take character input.

The %c conversion specifier won't automatically skip any leading white space, so if there's a stray newline in the input stream (from a previous entry, for example) the scanf call will consume it immediately.

This problem can be solved by putting a white space before the conversion specifier in format string.

scanf(" %c", &c);

The blank in the format string tells scanf to skip leading whitespace, and the first non-whitespace character will be read with the %c conversion specifier.

SnapShots of the code

# include<stdio.h> # include<stdlib . h> int win (char board[7] [5], char player) t hooping variables int i,j //checking cons//now we check for consecutive positions in a colum //for looping through the colum for (i=0;i< 5 ; i++) { //for looping throint main) initialize the array char boardt7] 151: int i,j: char user; int gameresult; //read the board printf (Enter 35 valuif (gane result=1) printf (Player %c won the game!, user); printf( Player %c didnt win., user); printf (Invalid respons

Output

Enter 35 values for the 7X5 game board A B B DE ZABGG ZBAGG ZZX GK A B B DE A B B DE Z A BGG Z BAGG A B B DE Enter the usersEnter 35 values for the 7X5 game board A B B DE ZABGG ZBAGG ZZX GK A B B DE A B B DE Z A BGG Z BAGG A B B DE Enter the users

Add a comment
Know the answer?
Add Answer to:
11.9 Week 11 Lab: 2D Arrays For this lab, you only need to write a single...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In basic C please with comments so I can redo It on my own, thank you!...

    In basic C please with comments so I can redo It on my own, thank you! For this lab, you only need to write a single user-defined function that analyzes a 2D array of chars. You do NOT need to write any of the code in main() that calls your function. Make sure to use the template and only add code to the user-defined function. Write a function win() with the following definition (i.e. prototype): int win(char board[7][5], char player)...

  • home / study / engineering / computer science / computer science questions and answers / in...

    home / study / engineering / computer science / computer science questions and answers / in basic c please with comments so i can redo it on my own, thank you! for this lab, you only ... Question: In basic C please with comments so I can redo It on my own, thank you! For this lab, you only nee... In basic C please with comments so I can redo It on my own, thank you! For this lab, you...

  • C programming language Purpose The purpose of this assignment is to help you to practice working...

    C programming language Purpose The purpose of this assignment is to help you to practice working with functions, arrays, and design simple algorithms Learning Outcomes ● Develop skills with multidimensional arrays ● Learn how to traverse multidimensional arrays ● Passing arrays to functions ● Develop algorithm design skills (e.g. recursion) Problem Overview Problem Overview Tic-Tac-Toe (also known as noughts and crosses or Xs and Os) is a paper-and-pencil game for two players, X and O, who take turns marking the...

  • For a C program hangman game: Create the function int play_game [play_game ( Game *g )]...

    For a C program hangman game: Create the function int play_game [play_game ( Game *g )] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) (Also the link to program files (hangman.h and library file) is below the existing code section. You can use that to check if the code works) What int play_game needs to do mostly involves calling other functions you've already...

  • The Code is C++ // tic tac toe game #include <iostream> using namespace std; const int SIZE = 9; int check(char *); void displayBoard(char *); void initBoard(char *); int main() {    char board[...

    The Code is C++ // tic tac toe game #include <iostream> using namespace std; const int SIZE = 9; int check(char *); void displayBoard(char *); void initBoard(char *); int main() {    char board[SIZE];    int player, choice, win, count;    char mark;    count = 0; // number of boxes marked till now    initBoard(board);       // start the game    player = 1; // default player    mark = 'X'; // default mark    do {        displayBoard(board);        cout << "Player " << player << "(" << mark...

  • Missing multiple labeled functions. Card matching game in C. Shouldn't need any more functions. I am...

    Missing multiple labeled functions. Card matching game in C. Shouldn't need any more functions. I am lost on how to complete the main function (play_card_match) without the sub functions complete. The program runs fine as is, but does not actually have a working turn system. It should display question marks on unflipped cards when the player is taking a turn and should also clear the screen so the player can't scroll up to cheat. Thank you #include "cardMatch.h" //main function /*...

  • Need help with this C program? I cannot get it to compile. I have to use...

    Need help with this C program? I cannot get it to compile. I have to use Microsoft Studio compiler for my course. It's a word game style program and I can't figure out where the issue is. \* Program *\ // Michael Paul Laessig, 07 / 17 / 2019. /*COP2220 Second Large Program (LargeProg2.c).*/ #define _CRT_SECURE_NO_DEPRECATE //Include the following libraries in the preprocessor directives: stdio.h, string.h, ctype.h #include <stdio.h> /*printf, scanf definitions*/ #include <string.h> /*stings definitions*/ #include <ctype.h> /*toupper, tolower...

  • Using C Programming: (use printf, scanf) 18. Tic-Tac-Toc Game Write a program that allows two players...

    Using C Programming: (use printf, scanf) 18. Tic-Tac-Toc Game Write a program that allows two players to play a game of tic-tac-toc. Use a two- dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that Displays the contents of the board array Allows player 1 to select a location on the board for an X. The program should...

  • Can somebody help me with this coding the program allow 2 players play tic Tac Toe....

    Can somebody help me with this coding the program allow 2 players play tic Tac Toe. however, mine does not take a turn. after player 1 input the tow and column the program eliminated. I want this program run until find a winner. also can somebody add function 1 player vs computer mode as well? Thanks! >>>>>>>>>>>>>Main program >>>>>>>>>>>>>>>>>>>>>>> #include "myheader.h" int main() { const int NUM_ROWS = 3; const int NUM_COLS = 3; // Variables bool again; bool won;...

  • I just need a help in replacing player 2 and to make it unbeatable here is my code: #include<i...

    I just need a help in replacing player 2 and to make it unbeatable here is my code: #include<iostream> using namespace std; const int ROWS=3; const int COLS=3; void fillBoard(char [][3]); void showBoard(char [][3]); void getChoice(char [][3],bool); bool gameOver(char [][3]); int main() { char board[ROWS][COLS]; bool playerToggle=false; fillBoard(board); showBoard(board); while(!gameOver(board)) { getChoice(board,playerToggle); showBoard(board); playerToggle=!playerToggle; } return 1; } void fillBoard(char board[][3]) { for(int i=0;i<ROWS;i++) for(int j=0;j<COLS;j++) board[i][j]='*'; } void showBoard(char board[][3]) { cout<<" 1 2 3"<<endl; for(int i=0;i<ROWS;i++) { cout<<(i+1)<<"...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT