Skip to content
Snippets Groups Projects
Move.cpp 2.93 KiB
Newer Older
#include "Move.h"
#include <iostream>
#include "Controller.h"
#include "Printer.h"
#include "BasicFunctions.h"

bool isCubeSolved() {
    for (const auto& face : cube) {
        std::string color = face[0][0];
        for (const auto& row : face) {
            for (const auto& cell : row) {
                if (cell != color) return false;
            }
        }
    }
    return true;
}

void manipulation() {
    std::cout << "\nStart rotating the sides of the Rubik's cube, here are the commands you will need:\n"
                 "\"g\" for green clockwise\n"
                 "\"w\" for white clockwise\n"
                 "\"o\" for orange clockwise\n"
                 "\"y\" for yellow clockwise\n"
                 "\"r\" for red clockwise\n"
                 "\"b\" for blue clockwise\n"
                 "Add \"!\" to rotate counterclockwise\n"
                 "Add a number to set the amount of steps\n\n"
                 "For example, \"2g!\" will rotate the green side counterclockwise two times.\n"
                 "Type \"hint\" to get moves to solve.\n"
                 "Type \"x\" to cancel.\n\n";

    std::string move;
    while (true) {
        if (!isCubeSolved()) {
            std::cout << "Your move: ";
            std::cin >> move;

            if (move == "X" || move == "x") {
                break;
            } else if (move == "hint") {
                printHint();
            } else {
                int steps = 1;
                std::string direction = "clockwise";
                std::string color;

                size_t index = 0;
                while (index < move.size() && isdigit(move[index])) {
                    index++;
                }
                if (index > 0) {
                    steps = std::stoi(move.substr(0, index));
                }

                if (index < move.size()) {
                    char colorChar = move[index];
                    switch (colorChar) {
                        case 'g': color = "green"; break;
                        case 'w': color = "white"; break;
                        case 'o': color = "orange"; break;
                        case 'y': color = "yellow"; break;
                        case 'r': color = "red"; break;
                        case 'b': color = "blue"; break;
                        default:
                            std::cout << "Invalid move\n";
                            continue;
                    }
                    index++;
                }

                if (index < move.size() && move[index] == '!') {
                    direction = "counterclockwise";
                }

                if (!color.empty()) {
                    rotateCubeFace(color, direction, steps);
                    printTemplate(cube);
                } else {
                    std::cout << "Invalid move\n";
                }
            }
        } else {
            std::cout << "Congratulations!\nYou solved the rubik's cube";
            break;
        }
    }
}