Skip to content
Snippets Groups Projects
BasicFunctions.cpp 4.39 KiB
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <cstdlib>
#include <ctime>
#include "Controller.h"
#include "BasicFunctions.h"

std::vector<std::string> hints;


void rotateFace(std::vector<std::vector<std::string>>& face, const std::string& direction, int steps) {
    for (int s = 0; s < steps; ++s) {
        std::vector<std::vector<std::string>> temp = face;
        if (direction == "clockwise") {
            for (int i = 0; i < 3; ++i) {
                for (int j = 0; j < 3; ++j) {
                    face[j][2 - i] = temp[i][j];
                }
            }
        } else if (direction == "counterclockwise") {
            for (int i = 0; i < 3; ++i) {
                for (int j = 0; j < 3; ++j) {
                    face[2 - j][i] = temp[i][j];
                }
            }
        }
    }
}

void rotateAdjacentEdges(int faceIndex, const std::string& direction, int steps) {
    std::unordered_map<int, std::vector<std::pair<int, std::vector<int>>>> edgeMapping = {
        {0, {{3, {0, 1, 2}}, {2, {0, 1, 2}}, {1, {0, 1, 2}}, {4, {0, 1, 2}}}}, // Green
        {1, {{0, {6, 7, 8}}, {2, {0, 3, 6}}, {5, {0, 1, 2}}, {4, {2, 5, 8}}}}, // White
        {2, {{0, {2, 5, 8}}, {3, {0, 3, 6}}, {5, {2, 5, 8}}, {1, {2, 5, 8}}}}, // Orange
        {3, {{4, {0, 3, 6}}, {5, {6, 7, 8}}, {2, {2, 5, 8}}, {0, {0, 1, 2}}}}, // Yellow
        {4, {{5, {0, 3, 6}}, {3, {2, 5, 8}}, {0, {0, 3, 6}}, {1, {0, 3, 6}}}}, // Red
        {5, {{2, {6, 7, 8}}, {3, {6, 7, 8}}, {4, {6, 7, 8}}, {1, {6, 7, 8}}}}  // Blue
    };

    const auto& edges = edgeMapping[faceIndex];
    std::vector<std::vector<std::string>> tempEdges(4, std::vector<std::string>(3));

    for (int s = 0; s < steps; ++s) {
        for (size_t e = 0; e < edges.size(); ++e) {
            for (int i = 0; i < 3; ++i) {
                tempEdges[e][i] = cube[edges[e].first][edges[e].second[i] / 3][edges[e].second[i] % 3];
            }
        }

        if (direction == "clockwise") {
            for (size_t e = 0; e < edges.size(); ++e) {
                for (int i = 0; i < 3; ++i) {
                    cube[edges[e].first][edges[e].second[i] / 3][edges[e].second[i] % 3] =
                        tempEdges[(e + 3) % edges.size()][i];
                }
            }
        } else {
            for (size_t e = 0; e < edges.size(); ++e) {
                for (int i = 0; i < 3; ++i) {
                    cube[edges[e].first][edges[e].second[i] / 3][edges[e].second[i] % 3] =
                        tempEdges[(e + 1) % edges.size()][i];
                }
            }
        }
    }
}

void rotateCubeFace(const std::string& color, const std::string& direction, int steps) {
    std::unordered_map<std::string, int> colorToFace = {
        {"green", 0}, {"white", 1}, {"orange", 2}, {"yellow", 3}, {"red", 4}, {"blue", 5}
    };

    std::unordered_map<std::string, std::string> colorToMove = {
        {"green", "g"}, {"white", "w"}, {"orange", "o"}, {"yellow", "y"}, {"red", "r"}, {"blue", "b"}
    };

    auto it = colorToFace.find(color);
    if (it == colorToFace.end()) {
        std::cerr << "Invalid color: " << color << std::endl;
        return;
    }

    int faceIndex = it->second;

    std::string currMove = colorToMove[color];
    std::string curr = colorToMove[color];
    if (direction == "counterclockwise") curr += "!";
    if (!hints.empty()) {
        if (hints.back() == curr) {
        hints.pop_back();
    } else if (hints.back() != curr) {
        if (direction == "clockwise") currMove += "!";
        hints.push_back(currMove);
    }
    } else {
        if (direction == "clockwise") currMove += "!";
        hints.push_back(currMove);
    }


    for (int i = 0; i < steps; ++i) {
        rotateFace(cube[faceIndex], direction, 1);
        rotateAdjacentEdges(faceIndex, direction, 1);
    }
}

void mixCubeFace() {
    std::vector<std::string> colors = {"green", "white", "orange", "yellow", "red", "blue"};
    std::vector<std::string> directions = {"clockwise", "counterclockwise"};

    std::srand(std::time(0));

    int numberOfMoves = 15;
    for (int i = 0; i < numberOfMoves; ++i) {
        std::string color = colors[std::rand() % 6];
        std::string direction = directions[std::rand() % 2];
        int steps = 1;
        rotateCubeFace(color, direction, steps);
    }
}

void printHint() {
    for (auto it = hints.rbegin(); it != hints.rend(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;
}