Convert following C++ code to C: #include using namespace std; char board[3][3] = {{'1','2','3'},{'4','5','6'},{'7','8','9'}}; int choice,row,column; char symbol = 'X'; bool draw = false; void show_board(){ cout<<"User [X]\t Computer [O]\n\n"; cout<<"\t\t | | \n"; cout<<"\t\t "<> choice; switch(choice){ case 1: row=0; column=0; break; case 2: row=0; column=1; break; case 3: row=0; column=2; break; case 4: row=1; column=0; break; case 5: row=1; column=1; break; case 6: row=1; column=2; break; case 7: row=2; column=0; break; case 8: row=2; column=1; break; case 9: row=2; column=2; break; default: cout<<"Invalid Move"; } if(symbol == 'X' && board[row][column] != 'X' && board[row][column] != 'O'){ board[row][column] = 'X'; symbol = 'O'; }else if(symbol == 'O' && board[row][column] != 'X' && board[row][column] != 'O'){ board[row][column] = 'O'; symbol = 'X'; }else { user_turn(); } show_board(); } bool gameover(){ for(int i=0; i<3; i++) if(board[i][0] == board[i][1] && board[i][0] == board[i][2] || board[0][i] == board[1][i] && board[0][i] == board[2][i]) return false; if(board[0][0] == board[1][1] && board[0][0] == board[2][2] || board[0][2] == board[1][1] && board[0][2] == board[2][0]) return false; for(int i=0; i<3; i++) for(int j=0; j<3; j++) if(board[i][j] != 'X' && board[i][j] != 'O') return true; draw = true; return false; } int main() { while(gameover()){ show_board(); user_turn(); gameover(); } if(symbol == 'X' && !draw){ cout<<"Computer wins the game"; } else if(symbol == 'O' && !draw){ cout<<"User wins the game"; } else cout<<"The game ended in a draw"; }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Convert following C++ code to C:

#include <iostream>
using namespace std;
char board[3][3] = {{'1','2','3'},{'4','5','6'},{'7','8','9'}};
int choice,row,column;
char symbol = 'X';
bool draw = false;

void show_board(){

cout<<"User [X]\t Computer [O]\n\n";
cout<<"\t\t | | \n";
cout<<"\t\t "<<board[0][0]<<" | "<<board[0][1]<<" | "<<board[0][2]<<"\n";
cout<<"\t\t_____|_____|_____\n";
cout<<"\t\t | | \n";
cout<<"\t\t "<<board[1][0]<<" | "<<board[1][1]<<" | "<<board[1][2]<<"\n";
cout<<"\t\t_____|_____|_____\n";
cout<<"\t\t | | \n";
cout<<"\t\t "<<board[2][0]<<" | "<<board[2][1]<<" | "<<board[2][2]<<"\n";
cout<<"\t\t | | \n";
}
void user_turn(){
if(symbol == 'X'){
cout<<"User turn : ";
}
else if(symbol == 'O'){
cout<<"Computer turn : ";
}
cin>> choice;

switch(choice){
case 1: row=0; column=0; break;
case 2: row=0; column=1; break;
case 3: row=0; column=2; break;
case 4: row=1; column=0; break;
case 5: row=1; column=1; break;
case 6: row=1; column=2; break;
case 7: row=2; column=0; break;
case 8: row=2; column=1; break;
case 9: row=2; column=2; break;
default:
cout<<"Invalid Move";
}

if(symbol == 'X' && board[row][column] != 'X' && board[row][column] != 'O'){
board[row][column] = 'X';
symbol = 'O';
}else if(symbol == 'O' && board[row][column] != 'X' && board[row][column] != 'O'){
board[row][column] = 'O';
symbol = 'X';
}else {
user_turn();
}
show_board();
}


bool gameover(){
for(int i=0; i<3; i++)
if(board[i][0] == board[i][1] && board[i][0] == board[i][2] || board[0][i] == board[1][i] && board[0][i] == board[2][i])
return false;

if(board[0][0] == board[1][1] && board[0][0] == board[2][2] || board[0][2] == board[1][1] && board[0][2] == board[2][0])
return false;
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
if(board[i][j] != 'X' && board[i][j] != 'O')
return true;
draw = true;
return false;
}

int main()
{
while(gameover()){
show_board();
user_turn();
gameover();
}
if(symbol == 'X' && !draw){
cout<<"Computer wins the game";
}
else if(symbol == 'O' && !draw){
cout<<"User wins the game";
}
else
cout<<"The game ended in a draw";
}

Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education