Skip to content
Snippets Groups Projects
Move.cpp 2.75 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"
5PR05's avatar
5PR05 committed
                 "For example, \"g!\" will rotate the green side counterclockwise.\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 < 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()) {
5PR05's avatar
5PR05 committed
                    rotateCubeFace(color, direction, 1);
                    printTemplate(cube);
                } else {
                    std::cout << "Invalid move\n";
                }
            }
        } else {
            std::cout << "Congratulations!\nYou solved the rubik's cube";
            break;
        }
    }
}