0

I'm working on this program that is a simulation for the Grid puzzle. Grid is like a 2- Dimensional version of the Rubik's Cube. Here is the criteria for this puzzle:

  • 6 x 6 grid
  • 2 faces
  • user can rotate columns and rows
  • rotations are 180 degrees
  • consist of two colors

Note: I have used the '*' star character to represent black and 'o' to represent white.

The user enters the side (top, bottom, left or right) and the number of columns or rows they want to rotate, and the simulation will simulate the movement. Please take a look at the grid:enter image description here

When the user rotates, the color changes to the opposite color and CHANGES POSITION according to the rotation.

I have mostly everything done, except the rotation. When the user enters the side and row/ column number, only the color reverses, the placement of the color is not correct.

Here is an example of the rotation:enter image description here

Can you please help me so that the rotations work properly. Thanks a lot in advance.

Here is my class code.

// 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|
+---+---+---+---+---+---+
*/

And here is my c++ file that runs the class.

// 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;
}
braX
  • 11,506
  • 5
  • 20
  • 33
  • In the correct rotation example don't you actually rotate the 3 top rows instead of two? Otherwise I don't understand the example – D.J. Klomp Jun 18 '17 at 15:26

1 Answers1

0

The easiest way is to realize that rotating an array 180 degrees is the same as flipping the array once in the row direction and once in the column direction. So do the following steps:

  1. Get the sub array that is selected by the user from the board array
  2. Do your invert operation
  3. Flip the sub array in the row direction
  4. Flip the sub array in the column direction
  5. Copy the sub array back to the board array

For the flip operation you can use temporary arrays. By the way I call it arrays here but you might want to look into std::array for easier handling of data. There you can use iterators and swap functions.

D.J. Klomp
  • 2,429
  • 1
  • 15
  • 30