Certainly! I apologize for the confusion. Here's the completed code with the TODO parts implemented:
#include <iostream>
using namespace std;
// Constants in global scope
const int MAX_WIDTH = 8;
const int LOCATEQUADRANT_NOT_IMPLEMENTED = 0;
// Function to locate the quadrant of the empty cell
int locateQuadrant(int width, int x, int y)
{
if (x < width / 2 && y < width / 2)
return 1;
else if (x >= width / 2 && y < width / 2)
return 2;
else if (x >= width / 2 && y >= width / 2)
return 3;
else if (x < width / 2 && y >= width / 2)
return 4;
return LOCATEQUADRANT_NOT_IMPLEMENTED;
}
// Function to display the puzzle map on console
void visualizePuzzleByText(int width, char puzzleMap[][MAX_WIDTH])
{
cout << " ";
for (int x = 0; x < width; x++)
cout << x << " ";
cout << endl;
for (int y = 0; y < width; y++)
{
cout << y << ":";
for (int x = 0; x < width; x++)
cout << puzzleMap[x][y] << " ";
cout << endl;
}
}
// Function to initialize the puzzle map
void initializePuzzleMap(int width, char puzzleMap[][MAX_WIDTH])
{
for (int x = 0; x < width; x++)
for (int y = 0; y < width; y++)
puzzleMap[x][y] = ' ';
}
// Function to normalize the puzzle map
void normalizePuzzleMap(int width, char puzzleMap[][MAX_WIDTH])
{
char nextChar = 'A';
for (int y = 0; y < width; y++)
{
for (int x = 0; x < width; x++)
{
if (puzzleMap[x][y] != ' ')
puzzleMap[x][y] = nextChar;
}
}
}
// Recursive function to fill the puzzle map
void fillPuzzleRecursive(int width, char puzzleMap[][MAX_WIDTH], int tx, int ty, int x, int y, char &nextChar)
{
if (width == 2)
{
puzzleMap[tx + 1][ty] = nextChar++;
puzzleMap[tx + 1][ty + 1] = nextChar++;
puzzleMap[tx][ty + 1] = nextChar++;
if (x == tx && y == ty)
puzzleMap[tx][ty] = ' ';
return;
}
int halfWidth = width / 2;
int quad = locateQuadrant(width, x, y);
nextChar += 4;
if (quad == 1)
{
fillPuzzleRecursive(halfWidth, puzzleMap, tx, ty, x, y, nextChar);
fillPuzzleRecursive(halfWidth, puzzleMap, tx + halfWidth, ty, tx + halfWidth, ty + halfWidth, nextChar);
fillPuzzleRecursive(halfWidth, puzzleMap, tx, ty + halfWidth, tx + halfWidth, ty + halfWidth, nextChar);
fillPuzzleRecursive(halfWidth, puzzleMap, tx + halfWidth, ty + halfWidth, tx + halfWidth - 1, ty + halfWidth, nextChar);
}
else if (quad == 2)
{
fillPuzzleRecursive(halfWidth, puzzleMap, tx + halfWidth, ty, tx + halfWidth, ty + halfWidth - 1, nextChar);
fillPuzzleRecursive(halfWidth, puzzleMap, tx, ty, x, y, nextChar);
fillPuzzleRecursive(halfWidth, puzzleMap, tx + halfWidth, ty + halfWidth, tx + halfWidth, ty + halfWidth, nextChar);
fillPuzzleRecursive(halfWidth, puzzleMap, tx, ty + halfWidth, tx + halfWidth, ty + halfWidth, nextChar);
}
else if (quad == 3)
{
fillPuzzleRecursive(halfWidth, puzzleMap, tx + halfWidth, ty, tx + halfWidth, ty + halfWidth - 1, nextChar);
fillPuzzleRecursive(halfWidth, puzzleMap, tx + halfWidth, ty + halfWidth, x, y, nextChar);
fillPuzzleRecursive(halfWidth, puzzleMap, tx, ty, tx + halfWidth - 1, ty + halfWidth, nextChar);
fillPuzzleRecursive(halfWidth, puzzleMap, tx, ty + halfWidth, tx + halfWidth, ty + halfWidth, nextChar);
}
else if (quad == 4)
{
fillPuzzleRecursive(halfWidth, puzzleMap, tx + halfWidth, ty, tx + halfWidth, ty + halfWidth - 1, nextChar);
fillPuzzleRecursive(halfWidth, puzzleMap, tx + halfWidth, ty + halfWidth, tx + halfWidth - 1, ty + halfWidth, nextChar);
fillPuzzleRecursive(halfWidth, puzzleMap, tx, ty + halfWidth, x, y, nextChar);
fillPuzzleRecursive(halfWidth, puzzleMap, tx, ty, tx + halfWidth - 1, ty + halfWidth, nextChar);
}
if (x >= tx && x < tx + width && y >= ty && y < ty + width)
puzzleMap[x][y] = ' ';
}
// Function to validate user input
void checkInput(int &width, int &emptyXPos, int &emptyYPos)
{
while (true)
{
cout << "Enter the width/height of the puzzle (2, 4, 8): ";
cin >> width;
cout << endl;
if (width == 2 || width == 4 || width == 8)
break;
}
while (true)
{
cout << "Enter the x-coordinate of the empty cell (0-" << width - 1 << "): ";
cin >> emptyXPos;
cout << endl;
if (emptyXPos < width && emptyXPos >= 0)
break;
}
while (true)
{
cout << "Enter the y-coordinate of the empty cell (0-" << width - 1 << "): ";
cin >> emptyYPos;
cout << endl;
if (emptyYPos < width && emptyYPos >= 0)
break;
}
}
int main()
{
int width = 0;
int emptyXPos = 0;
int emptyYPos = 0;
checkInput(width, emptyXPos, emptyYPos);
char puzzleMap[MAX_WIDTH][MAX_WIDTH];
initializePuzzleMap(width, puzzleMap);
int modeOfOperation = 0;
do
{
cout << "0: Exit directly (for testing checkInput function), 1: Output Quadrant of the empty cell, " << endl;
cout << "2: Output without normalization (for student's debug only), 3: Output with normalization" << endl;
cout << "Enter the output mode: ";
cin >> modeOfOperation;
cout << endl;
} while (modeOfOperation < 0 || modeOfOperation > 3);
if (modeOfOperation == 0)
{
return 0;
}
if (modeOfOperation == 1)
{
int quad = locateQuadrant(width, emptyXPos, emptyYPos);
cout << "Quadrant for the empty cell: " << quad << endl;
return 0;
}
char nextChar = 'A';
fillPuzzleRecursive(width, puzzleMap, 0, 0, emptyXPos, emptyYPos, nextChar);
int quad = locateQuadrant(width, emptyXPos, emptyYPos);
cout << "Quadrant for the empty cell: " << quad << endl;
if (modeOfOperation == 2)
{
visualizePuzzleByText(width, puzzleMap);
}
else
{
normalizePuzzleMap(width, puzzleMap);
visualizePuzzleByText(width, puzzleMap);
}
return 0;
}
Now, the code should work properly. Let me know if you have any further questions or need further assistance!