c++ program Problem 1: a. Write a simple function Display that receives the elem
ID: 3903726 • Letter: C
Question
c++ program
Problem 1: a. Write a simple function Display that receives the elements of tic-tac-toe" matrix and then displays the matrix to the user for input validation. b. Write a function Validate to make sure that an input matrix is valid. c. Write another function to determine the winnerloser neither win nor lose. Examples of feedbacks are as follows: OlOX XXO OlOX Valid"F"Neither X nor"O"wins oolO XiXX OlXIX Invalid"ticFtacFtoe"game because "both players win OlX XOX OIX valid"F X wins" ololoExplanation / Answer
void Display(char mat[][3]){
for (int i = 0; i<3; i++){
for (int j = 0; j<3; j++){
cout << mat[i][j] << " ";
}
cout << endl;
}
}
bool Validate(char mat[][3]){
int countx = 0;
int counto = 0;
for (int i = 0; i<3; i++){
for (int j = 0; j<3; j++){
if (mat[i][j] == 'X')
countx++;
if (mat[i][j] == 'O')
counto++;
}
}
if (countx == counto && checkWinner(mat) != 3)
return true;
return false;
}
int checkWinner(char mat[][3]){ //0-X win, 1- O win, 2- Tie, 3-Error
int xwin = 0;
int owin = 0;
for (int i = 0; i<3; i++){
if (mat[i][0] == mat[i][1] && mat[i][1] == mat[0][2]){
if (mat[i][j] == 'X')
xwin = 1;
if (mat[i][j] == 'O')
owin = 1;
}
if (mat[0][i] == mat[1][i] && mat[1]i] == mat[2][i]){
if (mat[i][j] == 'X')
xwin = 1;
if (mat[i][j] == 'O')
owin = 1;
}
}
for (mat[0][0] == mat[1][1] && mat[1][1] == mat[2][2]){
if (mat[i][j] == 'X')
xwin = 1;
if (mat[i][j] == 'O')
owin = 1;
}
for (mat[0][2] == mat[1][1] && mat[1][1] == mat[2][0]){
if (mat[i][j] == 'X')
xwin = 1;
if (mat[i][j] == 'O')
owin = 1;
}
if (xwin == 1 && owin == 1)
return 3
if (xwin == 1 && owin == 0)
return 0
if (xwin == 0 && owin == 1)
return 1
if (xwin == 0 && owin == 0)
return 2
return;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.