Question

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)

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
Z    A   B   G   G
Z    B   A   G   G
Z    Z   X   G   K
X    X   O   K   O
X    X   O   O   W
A    B   B   D   E

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

  • win(board,'A') returns 0
  • win(board,'B') returns 0
  • win(board,'D') returns 0
  • win(board,'G') returns 1
  • win(board,'O') returns 0
  • win(board,'X') returns 0
  • win(board,'Z') returns 1

Note: for testing, the board shown above can by copied and pasted into the input box when in Develop mode. Additionally in the input box, you will need to provide one additional character to be checked. See the test case descriptions for samples.

Reminder: You should not modify any of the code in main(), where the 7x5 board AND the player character to check is entered from user input. Then, a call is made to win(), and, based on the returned value (0 or 1), an appropriate message is printed to screen (e.g. either "Player A didn't win." OR "Player A won the game!").

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

Please find the code below::

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

int win(char board[7][5], char player){
   int i,j;
   for(i=0;i<7;i++){
       int countRow = 0;
       int countCol = 0;
       for(j=0;j<5;j++){
           if(board[i][j]==player){
               countRow++;
           }else{
               countRow = 0;
           }


           if(board[j][i]==player){
               countCol++;
           }else{
               countCol = 0;
           }
           if(countRow==3||countCol==3){
               return 1;
           }
       }
   }
   return 0;
}
int main(){
   char board[7][5] = {
           {'A', 'B','B','D','E'},
           {'Z', 'A','B','G','G'},
           {'Z', 'B','A','G','G'},
           {'Z', 'Z','X','G','K'},
           {'X', 'X','O','K','O'},
           {'X', 'X','O','O','W'},
           {'A', 'B','B','D','E'}};
   printf("%d\n",win(board,'A'));
   printf("%d\n",win(board,'B'));
   printf("%d\n",win(board,'D'));
   printf("%d\n",win(board,'G'));
   printf("%d\n",win(board,'O'));
   printf("%d\n",win(board,'X'));
   printf("%d\n",win(board,'Z'));
   return 0;
}

output:

Add a comment
Know the answer?
Add Answer to:
In basic C please with comments so I can redo It on my own, thank you!...
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
  • 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...

  • 11.9 Week 11 Lab: 2D Arrays For this lab, you only need to write a single...

    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 chars. You do NOT need to write any of the code in main0 that calls your function. Make sure to use the template and only add code to the user-defined function. Write a function win0 with the following definition (i.e. prototype): int win(char board17)15), char player) The function win0 should return a 1 if the character...

  • 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)<<"...

  • 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...

  • c++ help please! Create a 2D character array in your main function and use nested for...

    c++ help please! Create a 2D character array in your main function and use nested for loops to fill the array with the letter ‘e’ to represent empty spaces. Create a function to print the board on the screen using a nested for loop. The function header is: void printBoard (char board [][3]) Create a function that checks whether a particular space has already been filled. If the space is filled it returns a boolean value of true, otherwise false....

  • Write a C++ program, tictac.cpp, that repeatedly reads in tic-tac-toe boards and for each one indicates...

    Write a C++ program, tictac.cpp, that repeatedly reads in tic-tac-toe boards and for each one indicates if there is a winner and who won. The board will be read in as 3x3 array of characters (x, o or . for a blank spot). You are required to write the following functions to help solve the problem: // input prompts the user for a board and inputs it // into the given array void input (char board [3][3]); // print prints...

  • [Using Python] I need my code given below to loop the user input, so that you...

    [Using Python] I need my code given below to loop the user input, so that you are asked to input without it stopping after 4 inputs. Code: #A program that converts a hexadecimal number to its decimal value #Define function for hexadecimal to decimal def hexToDec(hexi): result = 0 #For loop to test input for correct values for char in hexi: if 'A' <= char <= 'F' or 'a' <= char <= 'f': if 'a' <= char <= 'f': result...

  • 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...

  • I need help solving this in c++ using visual Studio. For this assignment, you will be filling in missing pieces of code within a program, follow the comments in the code. These comments will describe...

    I need help solving this in c++ using visual Studio. For this assignment, you will be filling in missing pieces of code within a program, follow the comments in the code. These comments will describe the missing portion. As you write in your code, be sure to use appropriate comments to describe your work. After you have finished, test the code by compiling it and running the program, then turn in your finished source code. // TicTacToe.cpp: Follow along with...

  • C++ for this exercise, you will make a simple Dungeon Crawl game. For an enhanced version,...

    C++ for this exercise, you will make a simple Dungeon Crawl game. For an enhanced version, you can add monsters that randomly move around. Program Requirements While the program is an introduction to two-dimensional arrays, it is also a review of functions and input validation. check: Does the program compile and properly run? does all functions have prototypes? Are all functions commented? Is the program itself commented? Are constants used where appropriate? Are the required functions implemented? Is the dungeon...

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