修复2D魔方谜题的旋转

时间:2017-06-18 07:01:56

标签: c++ puzzle rubiks-cube

我正在开发这个程序,它是 Grid 拼图的模拟。网格就像魔方的二维版本。以下是此难题的标准:

  • 6 x 6网格
  • 2张脸
  • 用户可以旋转列和行
  • 旋转180度
  • 由两种颜色组成

注意:我使用'*'星号代表黑色,'o'代表白色。

用户输入侧面(顶部,底部,左侧或右侧)以及要旋转的列数或行数,模拟将模拟运动。 请看一下网格:enter image description here

当用户旋转时,颜色会变为相反的颜色,并根据旋转更改位置。

除旋转之外,我完成了大部分工作。当用户输入边和行/列编号时,只有颜色反转,颜色的位置不正确。

以下是轮换的示例:enter image description here

请你帮我一下,让旋转工作正常。 非常感谢。

这是我的班级代码。

// Library Files
using namespace std;
# include <iostream>    // input and output

#ifndef G_R_I_D
#define G_R_I_D

class grid
{
public:
    int input();                                        // side and num
    int flipTop();
    int flipBottom();
    int flipRight();
    int flipLeft();
    void initBoard();                                   // Create blank board
    void printBoard();                                  // print board

private:
    char board [6][6];                                  // board
    int num;                                            // number of rows/ columns from side
};

/******************
*Member Definition*
******************/
int grid::input()
{
    cout << "# of Rows /Columns: ";                     // Indicates num of rows or columns
    cin >> num;
}

int grid::flipRight()
{
    num = 6 - num;                                     // Match input to correct array ex. if right 2, actual array is 4
    for(num; num < 6; num++)                           // Because bottom contains the y axis arrays 3, 4, 5, count up in sequence
    {
        for(int i = 0; i < 6; i++)
        {
            if (board[i][num] == 'o')
            {
                board[i][num] = '*';
            }


            else if (board[i][num] == '*')
            {
                board[i][num] = 'o';
            }
        }
    }
}

int grid::flipLeft()
{
    num = num - 1;                                      // If between arrays 0-2, subtract one from input because first array is 1
    for(num; num >= 0; num--)                           // Because bottom contains the y axis arrays 0, 1, 2, count down in sequence
    {
        for(int i = 0; i < 6; i++)
        {
            if (board[i][num] == 'o')
                board[i][num] = '*';

            else if (board[i][num] == '*')
                board[i][num] = 'o';
        }
    }
}

int grid::flipTop()
{
    num = num - 1;                                      // If between arrays 0-2, subtract one from input because first array is 1
    for(num; num >= 0; num--)                           // Because bottom contains the y axis arrays 0, 1, 2, count down in sequence
    {
        for(int i = 0; i < 6; i++)
            if (board[num][i] == 'o')
                board[num][i] = '*';

            else if (board[num][i] == '*')
                board[num][i] = 'o';
    }
}

int grid::flipBottom()
{
    num = 6 - num;                                       // Match input to correct array ex. if right 2, actual array is 4
    for(num; num < 6; num++)                             // Because bottom contains the y axis arrays 3, 4, 5, count up in sequence
    {
        for(int i = 0; i < 6; i++)
            if (board[num][i] == 'o')
                board[num][i] = '*';

            else if (board[num][i] == '*')
                board[num][i] = 'o';
    }
}

void grid::initBoard()                                // Goes through each 36 array starting with 0,0
{
    for(int y = 0; y < 6; y++)                          // columns
    {
        for (int x = 0; x < 6; x++)                     // rows
        {
            board[y][x] = 'o';                          // assign each array to the char 'o'
        }
    }
}

void grid::printBoard()
{
    for (int y = 0; y < 6; y++)                         // Defining y axis (wait until x axis arrays have printed to continue to next column)
    {
        for (int x = 0; x < 6; x++)                     // Defining x axis (once row is finished, proceed to next column)
        {
            cout << ' ' << board[y][x] << ' ';          // Place space between each character
        }
        cout << endl << endl;                           // New line, next row
    }
}

#endif // G_R_I_D


/*
+---+---+---+---+---+---+
|0,0|0,1|0,2|0,3|0,4|0,5|
+---+---+---+---+---+---+
|1,0|1,1|1,2|1,3|1,4|1,5|
+---+---+---+---+---+---+
|2,0|2,1|2,2|2,3|2,4|2,5|
+---+---+---+---+---+---+
|3,0|3,1|3,2|3,3|3,4|3,5|
+---+---+---+---+---+---+
|4,0|4,1|4,2|4,3|4,4|4,5|
+---+---+---+---+---+---+
|5,0|5,1|5,2|5,3|5,4|5,5|
+---+---+---+---+---+---+
*/

这是我运行该类的c ++文件。

// Library Files
using namespace std;
# include <iostream>
# include "grid.h"

// Start of main function
int main()
{

    // Variables
    char side;
    grid gridBoard;
    // Introduction
    cout << "Grid\n Instructions: This program is a simulation of the puzzle Grid. The Grid grid has four sides and each \n"
         << "side has three rows or columns. To twist a side, enter the side you want to turn and the number of rows or columns. \n"
         << "Sides:\n- (T)op \n- (B)ottom \n- (R)ight \n- (L)eft\n- (E)xit \nColumns/Rows: 1-3\n\n";

    // Initialize
    gridBoard.initBoard();

    //Rotations
    do
    {
        gridBoard.printBoard();
        cout << "Side: ";
        cin >> side;
        gridBoard.input();
        switch (toupper(side))
        {
        case 'T':
            gridBoard.flipTop();
            break;
        case 'B':
            gridBoard.flipBottom();
            break;
        case 'R':
            waffleBoard.flipRight();
            break;
        case 'L':
            gridBoard.flipLeft();
            break;
        case 'E':
            cout << "Simulation will exit.";
            break;
        default:
            cout << "Invalid input, please try again.\n\n";
            break;
        }
    }
    while (side != 'E');

    return 0;
}

1 个答案:

答案 0 :(得分:0)

最简单的方法是实现将数组旋转180度与在行方向上翻转一次数组和在列方向上翻转一次相同。请执行以下步骤:

  1. 从电路板阵列中获取用户选择的子阵列
  2. 进行反转操作
  3. 沿行方向翻转子阵列
  4. 在列方向上翻转子数组
  5. 将子阵列复制回电路板阵列
  6. 对于翻转操作,您可以使用临时数组。顺便说一句,我在这里称它为数组,但您可能希望查看std :: array以便于处理数据。在那里你可以使用迭代器和交换函数。