Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Problem C: Ways to play Tic-Tac-Toe (5 extra credit points) Use recursion to

ID: 3684561 • Letter: C

Question

C++ Problem C: Ways to play Tic-Tac-Toe (5 extra credit points)

Use recursion to find out how many ways there are to play 3x3 tic-tac-toe. If two games end in the same final position, but reached there in a different manner, then they are counted as different ways. Simply cout how many ways there are as the only output of the program.

For example, 2x2 tic-tac-toe has 24 possible ways to play ("1X" means this is the first move and it is an 1x120 1x120 1x13x 1x1 1x13x 1x1 3x1 2011x 2011x 3X11x 1X 3X11X 1X 3xl 2013X 20 3X20 20 3X 1x1 1x13x 1x1 1x13x 1x120 1x120 2013X 20 3X20 20 3X 3x 2 20 2013x 120 3x120 |3X 20 2013X 20 3X|20 13x 13x A fourth move is never played as 'X' always wins after 3 steps. Hint: This is similar to the sudoku problem. When you are done, name the source code file _6C.cpp. Here you replace with your U of M email address; for example, if your email address is smithx1234@umn.edu your file should be named smithx1234_6C.cpp. Remember to follow this naming convention diligently. Then submit your file using the HW6C link on Moodle

Explanation / Answer

Hi consider the below sample example function for your reference to play 3x3 tic-tac-toe in C++,

#include <iostream>
using namespace std;

char square[10] = {'o','1','2','3','4','5','6','7','8','9'};
int checkwin();
void board();

int main()
{
int player = 1,i,choice;
char mark;

do
{
board();
player=(player%2)?1:2;
cout << "Player " << player << ", enter a number: ";
cin >> choice;
mark=(player == 1) ? 'X' : 'O';
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--;
}

i=checkwin();
player++;
}while(i==-1);
board();
if(i==1)
cout<<"==>Player "<<--player<<" win ";
else
cout<<"==>Game draw";

return 0;
}


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;
}


void board()
{
cout << " Tic Tac Toe ";
cout << "Player 1 (X) - Player 2 (O)" << endl << endl;
cout << endl;
cout << " " << square[1] << " | " << square[2] << " | " << square[3] << endl;
cout << "_____|_____|" << endl;
cout << " " << square[4] << " | " << square[5] << " | " << square[6] << endl;
cout << "_____|_____|" << endl;
cout << " " << square[7] << " | " << square[8] << " | " << square[9] << endl;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote