Question: I wrote the following code for a Tic Tac Toe game from my c++ class. I need to create a flow chart for it. Can

I wrote the following code for a Tic Tac Toe game from my c++ class. I need to create a flow chart for it. Can anyone help?

//header files #include #include #include #include #include using namespace std;

class tictac { public: char square[10] = { 'o','1','2','3','4','5','6','7','8','9' }; int player = 1, i, choice, p1w = 0, p2w = 0, p1l = 0, p2l = 0, draw = 0; char mark, mark1; string name1, name2; public:

void init_(char c, string n1, string n2) { name1 = n1; name2 = n2; do {

player = (player % 2) ? 1 : 2;

mark = c;

if (c == 'X') mark1 = (player) ? 'O' : c; else mark1 = (player) ? 'X' : c;

//cout<<"hi"<> choice;

if (choice == 1 && square[1] == '1') square[1] = mark; else if (choice == 2 && square[2] == '2') square[2] = mark; else if (choice == 3 && square[3] == '3') square[3] = mark; else if (choice == 4 && square[4] == '4') square[4] = mark; else if (choice == 5 && square[5] == '5') square[5] = mark; else if (choice == 6 && square[6] == '6') square[6] = mark; else if (choice == 7 && square[7] == '7') square[7] = mark; else if (choice == 8 && square[8] == '8') square[8] = mark; else if (choice == 9 && square[9] == '9') square[9] = mark; else { cout << "Invalid move "; player--; cin.ignore(); cin.get(); } i = checkwin(); player++; } while (i == -1); board(); if (i == 1) { --player; if (player = 1) cout << "==>\aPlayer " << name1 << " win "; else

cout << "==>\aPlayer " << name2 << " win "; if (player == 1) { p1w++; } else { p2w++; } }

else { cout << "==>\aGame draw"; draw++; } player = 1; square[0] = 'o'; square[1] = '1'; square[2] = '2'; square[3] = '3'; square[4] = '4'; square[5] = '5'; square[6] = '6'; square[7] = '7'; square[8] = '8'; square[9] = '9'; cin.ignore(); cin.get();

} /********************************************* FUNCTION TO RETURN GAME STATUS 1 FOR GAME IS OVER WITH RESULT -1 FOR GAME IS IN PROGRESS O GAME IS OVER AND NO RESULT **********************************************/ int checkwin() { if (square[1] == square[2] && square[2] == square[3]) return 1; else if (square[4] == square[5] && square[5] == square[6]) return 1; else if (square[7] == square[8] && square[8] == square[9]) return 1; else if (square[1] == square[4] && square[4] == square[7]) return 1; else if (square[2] == square[5] && square[5] == square[8]) return 1; else if (square[3] == square[6] && square[6] == square[9]) return 1; else if (square[1] == square[5] && square[5] == square[9]) return 1; else if (square[3] == square[5] && square[5] == square[7]) return 1; else if (square[1] != '1' && square[2] != '2' && square[3] != '3' && square[4] != '4' && square[5] != '5' && square[6] != '6' && square[7] != '7' && square[8] != '8' && square[9] != '9') return 0; else return -1; }

/******************************************************************* FUNCTION TO DRAW BOARD OF TIC TAC TOE WITH PLAYERS MARK ********************************************************************/

void board() { system("cls"); cout << " \tTic Tac Toe "; cout << "Player " << name1 << " " << mark << " - Player " << name2 << " " << mark1 << endl << endl; cout << endl; cout << " | | " << endl; cout << " " << square[1] << " | " << square[2] << " | " << square[3] << endl; cout << "_____|_____|_____" << endl; cout << " | | " << endl; cout << " " << square[4] << " | " << square[5] << " | " << square[6] << endl; cout << "_____|_____|_____" << endl; cout << " | | " << endl; cout << " " << square[7] << " | " << square[8] << " | " << square[9] << endl; cout << " | | " << endl << endl; } void print()

{ cout << " player " << name1 << " wins = " << p1w; cout << " player " << name2 << " wins = " << p2w; cout << " Number of draws between players = " << draw; }

}; int main() { //declare variables int age; string name1, name2; char XorO, ch; tictac obj;

//request for input

cout << " Player 1 Please Enter your Name : "; cin >> name1; cout << " Please Enter your age : "; cin >> age; cout << " Please Enter X or O : "; cin >> XorO;

//display output cout << " ====================================" << endl; cout << " INPUT ENTERED : " << endl; cout << " Name : " << name1 << endl; cout << " Age : " << age << endl; cout << " X or O : " << XorO << endl;

//request for input cout << " Player 2 Please Enter your Name : "; cin >> name2; cout << " Please Enter your age : "; cin >> age;

//display output cout << " ====================================" << endl; cout << " INPUT ENTERED : " << endl; cout << " Name : " << name2 << endl; cout << " Age : " << age << endl; do { obj.init_(XorO, name1, name2); cout << " want to play again ..press Y or N : "; cin >> ch; } while (ch == 'y' || ch == 'Y'); obj.print(); system("PAUSE");//Pause the output for display return 0;//succesfully exit to main }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!