Question

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 spaces in a 3×3
grid. The player who succeeds in placing three of their marks in a horizontal, vertical, or
diagonal row wins the game.
Questions
Q1) Write a C function that allows the user to initialize a Tic-Tac-Toe board. The board is
a 2D array of size 3x3. The function will set an id for each cell of the board starting from
1 and stop at 9.
The function prototype is void InitializeBoard(int m, int n , char board[][n])[0 Points]
Here is sample output showing the board after initializing it using the function

void InitializeBoard(int m, int n , char board[][n]){
int c =1;
for(int i =0; i<m; i++){
for(int j=0; j< n; j++){
board[i][j] = c+'0';
c++;
}
}
}
NOTE: The answer for Q1 is given to you as a help to get you started

Q2) Write a C function that allows the user to print a Tic-Tac-Toe board.
The function prototype is void PrintBoard(int m, int n, char board[][n]). [10 points]

Q3) Write a C function that allows the user to create a Tic-Tac-Toe board. In this case, the
board is a 2D array of size 3x3, The function allows the user to create a board and set some
X or O on the board at any cell.
The function prototype is void CreateBoard(int m, int n, char board[][n]). [10 points]

If the user typed any invalid input like cell number 21 or rather than entering X or O the
user typed C the function should ignore his input and ask him to enter a valid input. Here
is an example of this behaviour.

Q4) Write a C function that allows the user to check if a given Tic-Tac-Toe board is a valid
board or an invalid board. The board is valid if it is an empty board or if the difference
between the total number of X and the total number of O symbols on the board is 0 or 1.
The function prototype is int IsValidBoard(int m, int n, char board[][n]). [10 points]

Q5) Write a C function that allows the user to find if any next move or next play for any
player X or O who has the turn to play is winning move. The function will print the cell
number or ID that if the player places an X or O in it he/she will win the game. If there is
more than one cell where the player could place his symbol in and win the game the function
should print all the winning cells for that player.
The function prototype is void ListWinningCells(int m, int n, char board[][n])[30 points]

Q6) Write a C function that finds which player (player X or O) has won the game (if any) use
recursion to implement this function. If there is no winner the function should return 'D'
otherwise return the winner symbol, either 'X' or 'O'
The function prototype is char WhoIsTheWinner(int m, int n, char board[][n])[30 points]
Q7) Write a main function that displays a menu for the user and asks him to select which
function he/she wants to

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

Solution:

#include<stdlib.h>
#include <stdio.h>
#include<conio.h>
void InitializeBoard(int m,int n,char board[][n]);//function declarations
void PrintBoard(int m,int n,char board[][n]);
void CreateBoard(int m,int n,char board[][n]);
int IsValidBoard(int m,int n,char board[][n]);
char board[3][3];

void InitializeBoard(int m,int n,char board[][n]){//function definition
int i,j;
int count=1;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
board[i][j]=count+'0'; //place 1 to 9 elements at each cell
count++;
}
}
}
void PrintBoard(int m,int n,char board[][n]){//function definition
int i,j;
for(i=0;i<m;i++){
printf(" | | \n");
printf(" %c | %c | %c \n",board[i][0],board[i][1],board[i][2]);//print board
printf("___|___|___\n");
  
}
  
}
void CreateBoard(int m,int n,char board[][n]){//function definition
InitializeBoard(m,n,board);
int temp,i,j,count;
char c;
do{
printf("Enter value");
scanf("%d",&temp);
scanf("%c",&c);
if(temp>9 || temp<1){
printf("Enter valid input");
continue;
}
else{
i=(int)temp/3;//place X or O at each cell of board
j=temp%3;
printf("Enter X or O");
scanf("%c",&c);
board[i][j-1]=c;
count++;
}
}while(count!=9);
}

int IsValidBoard(int m,int n,char board[][n]){//function definition
int flag=0,error=0;
int cx,co,i,j,temp,count=0;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
temp=board[i][j]-'0';//Check each element if it is between 0 and 9
if(temp>=1 || temp<=9){
count++;
continue;
}
else{
error=1;
break;
}
}
}
if(error==1){
for(i=0;i<m;i++){
for(j=0;j<n;j++){
if(board[i][j]=='X') //count number of X and O
cx++;
else
co++;
}
}
if(abs(cx-co)==0 || abs(cx-co)==1){
flag=1;
}
}
else{
flag=1;
}
if(flag==1){
return 1;//return 1 if valid board, else return 0
}
else
return 0;
}
int main()//main method definition
{
int ch;
while(1){
printf("MENU\n");//print menu
printf("1.Initialize Board\n");
printf("2.Print board\n");
printf("3.Create board\n");
printf("4.Is valid board\n");
printf("5.Quit program\n");
printf("Enter choice\n");
scanf("%d",&ch);//read choice and call functions
if(ch==1)
InitializeBoard(3,3,board);
else if(ch==2)
PrintBoard(3,3,board);
else if(ch==3)
CreateBoard(3,3,board);
else if(ch==4){
int result=IsValidBoard(3,3,board);
if(result==1){
printf("Valid board\n");
}
else{
printf("Invalid board\n");
}
}
else if(ch==5)
break;
}
return 0;
}

Note:

As per the guidelines we are allowed to answer only 4 parts if the question is long.

Please let me know in case of any help.

I answered four questions(parts) and the last 7th question that displays menu.

Screenshots:

The screenshots are attached below for reference.

Please follow them.

Add a comment
Know the answer?
Add Answer to:
C programming language Purpose The purpose of this assignment is to help you to practice working...
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
  • Q1) Write a C function that allows the user to initialize a Tic-Tac-Toe board. The board...

    Q1) Write a C function that allows the user to initialize a Tic-Tac-Toe board. The board is a 2D array of size 3x3. The function will set an id for each cell of the board starting from 1 and stop at 9. The function prototype is ​void​​ ​​InitializeBoard​​(​​int​​ m, ​​int​​ n , ​​char​​ board[][n]) void​​ ​​InitializeBoard​​(​​int​​ m, ​​int​​ n , ​​char​​ board[][n])​​{ ​​int​​ c =​​1​​; ​ for​​(​​int​​ i =​​0​​; i<m; i++){ ​​ for​​(​​int​​ j=​​0​​; j< n; j++){ board[i][j] = c+​​'0'​​;...

  • Implement a tic-tac-toe game. At the bottom of these specifications you will see a template you...

    Implement a tic-tac-toe game. At the bottom of these specifications you will see a template you must copy and paste to cloud9. Do not change the provided complete functions, or the function stub headers / return values. Currently, if the variables provided in main are commented out, the program will compile. Complete the specifications for each function. As you develop the program, implement one function at a time, and test that function. The provided comments provide hints as to what...

  • JAVA TIC TAC TOE - please help me correct my code Write a program that will...

    JAVA TIC TAC TOE - please help me correct my code Write a program that will allow two players to play a game of TIC TAC TOE. When the program starts, it will display a tic tac toe board as below |    1       |   2        |   3 |    4       |   5        |   6                 |    7      |   8        |   9 The program will assign X to Player 1, and O to Player    The program will ask Player 1, to...

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

  • In traditional Tic Tac Toe game, two players take turns to mark grids with noughts and...

    In traditional Tic Tac Toe game, two players take turns to mark grids with noughts and crosses on the 3 x 3 game board. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row in the game board wins the game. Super Tic Tac Toe game also has a 3 x 3 game board with super grid. Each super grid itself is a traditional Tic Tac Toe board. Winning a game of Tic...

  • Write a program to Simulate a game of tic tac toe in c#

    Write a program to Simulate a game of tic tac toe. A game of tic tac toe has two players. A Player class is required to store /represent information about each player. The UML diagram is given below.Player-name: string-symbol :charPlayer (name:string,symbol:char)getName():stringgetSymbol():chargetInfo():string The tic tac toe board will be represented by a two dimensional array of size 3 by 3 characters. At the start of the game each cell is empty (must be set to the underscore character ‘_’). Program flow:1)    ...

  • 18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use di...

    18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use 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 does the following: Write . Displays the contents of the board array. . Allows player 1 to select a location on the board for an X. The program should ask the...

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

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

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