2017-06-18 7 views
0

私はこのプログラムで、グリッドパズルのシミュレーションです。グリッドはRubikのキューブの2次元バージョンのようなものです。ここで、このパズルのための基準である:2D Rubikのパズルの回転を修正しました

  • 6×6グリッド
  • 2面
  • ユーザが行と列
  • 回転を回転させることができるが、180度
  • 2色
から構成されています

注:白を表すには「*」、黒を表すには「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; 
} 
+0

正しい回転の例では、2つではなく3つの上の行を実際に回転しないでください。そうでなければ、私はその例を理解していない –

答えて

0

最も簡単な方法は、配列を180度回転させることは、行方向に1回、列方向に1回反転することと同じです。だから、次の手順を実行します。

  1. 操作
  2. あなた反転ですか行方向
  3. サブフリップでサブアレイをフリップボード配列
  4. からユーザによって選択されたサブ配列を取得します。列方向
  5. コピーバックボードアレイへ

あなたは一時的な配列を使用することができますフリップ動作のためのサブ配列の配列。ところで、ここでは配列と呼んでいますが、データの扱いを簡単にするためにstd :: arrayを調べたいと思うかもしれません。イテレータとスワップ関数を使用することができます。

関連する問題