title: 5x5 Sliding Puzzle
body: <p>I am taking a beginner level c++ class and come across a assignment. Write a program that will implement a 5x5 Sliding Puzzle. </p>
  
  <p>Here are my codes.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;ctime&gt;
  #include &lt;cstdlib&gt;
  #include &lt;iomanip&gt;
  #include &lt;string&gt;
  using namespace std;
  
  const int SIZE = 5;
  int moves = 0;
  
  void start_of_game(){
      cout &lt;&lt; "*** Welcome to Sliding Puzzle Game ***"&lt;&lt;endl;
      cout &lt;&lt; "--------------------------------------"&lt;&lt;endl&lt;&lt;endl;
  }
  
  void initialize_Board(int Board[SIZE][SIZE]){
      int a = 0;
      int temp = 0;
      int random_roll = 0;
      int random_column = 0;
      for(int i = 0; i &lt; SIZE; i++){
          for (int j = 0; j &lt; SIZE; j++){
              Board[i][j] = a;
              a++;
          }
      }
  
      for(int i = 0; i &lt; SIZE; i++){
          for (int j = 0; j &lt; SIZE; j++){
              random_roll = rand() % 5;
              random_column = rand() % 5;
              temp = Board[i][j];
              Board[i][j] = Board[random_roll][random_column];
              Board[random_roll][random_column] = temp;
          }
      }
  }
  
  void display_the_board(int Board[SIZE][SIZE]){
      cout &lt;&lt; "*** Current Board ***"&lt;&lt;endl;
      cout &lt;&lt; "zero(0) is the open spot"&lt;&lt;endl;
      cout &lt;&lt;endl;
  
      cout &lt;&lt; "      1    2    3    4    5|"&lt;&lt;endl;
      cout &lt;&lt; "-------------------------------------"&lt;&lt;endl;
      for(int i = 0; i &lt; SIZE; i++){
          cout &lt;&lt; i + 1 &lt;&lt; " |";
          for (int j = 0; j &lt; SIZE; j++){
              cout &lt;&lt; std::setw(4)&lt;&lt; Board[i][j] &lt;&lt; "|";
          }
          cout &lt;&lt;endl;
          cout &lt;&lt; "-------------------------------------" &lt;&lt;endl;
      }
  }
  
  void get_user_input(string&amp; input, int&amp; column, int&amp; roll){
      cout &lt;&lt; "*** Enter piece to move (use %% to give up): "&lt;&lt;endl;
      cin &gt;&gt; input;
  
      roll = (int)input[0] - 48;
      column = (int)input[1] - 48;
  }
  
  bool valid_input(string input, int column, int roll){
  
      if(input.length() == 2){
          if(input == "%%" || input == "$$"){
              return true;
          }
  
          else if(column &gt; 0 &amp;&amp; column &lt; 6 &amp;&amp; roll &gt; 0 &amp;&amp; roll &lt; 6){
              return true;
          }
      }
      return false;
  }
  
  bool have_valid_move(int column, int roll, int Board[SIZE][SIZE]){
  
      if(Board[roll][column - 1] == 0 || Board[roll - 2][column - 1] == 0 || Board[roll - 1][column] == 0 || Board[roll - 1][column - 2] == 0){
          return true;
      }
      else{
          return false;
      }
  }
  void swap(int&amp; a, int&amp; b){
      int temp = b;
      b = a;
      a = temp;
  }
  void move_number(int column, int roll, int Board[SIZE][SIZE]){
      if(Board[roll][column - 1] == 0){
          swap(Board[roll][column - 1], Board[roll - 1][column - 1]);
          moves++;
      }
      else if(Board[roll - 2][column - 1] == 0){
          swap(Board[roll - 2][column - 1], Board[roll - 1][column - 1]);
          moves++;
      }
      else if(Board[roll - 1][column] == 0){
          swap(Board[roll - 1][column], Board[roll - 1][column - 1]);
          moves++;
      }
      else{
          swap(Board[roll - 1][column - 2], Board[roll - 1][column - 1]);
          moves++;
      }
  }
  
  void one_step_before_complete(int Board[SIZE][SIZE]){
      int temp = 1;
      for(int i = 0; i &lt; SIZE; i++){
          for (int j = 0; j &lt; SIZE; j++){
              Board[i][j] = temp;
              temp++;
          }
      }
      Board[4][4] = Board[4][3];
      Board[4][3] = 0;
  }
  
  bool complete(int Board[SIZE][SIZE]){
  
      int temp = 1;
      for(int i = 0; i &lt; SIZE; i++){
          for (int j = 0; j &lt; SIZE; j++){
              if(temp == 25){
                  temp = 0;
              }
              if(Board[i][j] != temp){
                  return false;
              }
              temp++;
          }
      }
      return true;
  }
  
  void congraulations(){
      cout &lt;&lt; "*** Congratulations - you complete the puzzle successfully with " &lt;&lt; moves &lt;&lt; " move(s) ***"&lt;&lt;endl;
  }
  
  void end_of_game(){
      cout &lt;&lt; "*** End of Game - Thanks for playing!!!"&lt;&lt;endl;
  }
  
  int main(){
      srand(time(nullptr));
  
      start_of_game();
  
      int Board[SIZE][SIZE];
      string input;
      int column = 0;
      int roll = 0;
  
      initialize_Board(Board);
  
      while(!complete(Board)){
  
          display_the_board(Board);
          get_user_input(input, column, roll);
  
          if(valid_input(input, column, roll)){
              if(roll &gt; 0 &amp;&amp; roll &lt; 6 &amp;&amp; column &gt; 0 &amp;&amp; column &lt; 6){
                  if(have_valid_move(column, roll, Board)){
                      move_number(column, roll, Board);
                  }
                  else{
                      cout &lt;&lt; "*** Invalid Move - cannot make this move!"&lt;&lt;endl;
                  };
              }
  
              else if(input == "%%"){
                  break;
              }
  
              else {
                  one_step_before_complete(Board);
                  display_the_board(Board);
              }
          }
  
          else{
              cout &lt;&lt; "** Your move is invalid.  Retry! **"&lt;&lt;endl&lt;&lt;endl;
          }
      }
      if(complete(Board)){
          display_the_board(Board);
          congraulations();
      }
      end_of_game();
      return 0;
  
  }
  </code></pre>
  
  <p>The program works mostly fine. However it will change the last number to 0 whenever the input is 55, doesn't matter if its a valid input or not.</p>
  
  <pre><code>*** Enter piece to move (use %% to give up):
  55
  *** Current Board ***
  zero(0) is the open spot
  
        1    2    3    4    5|
  -------------------------------------
  1 |  23|  24|   5|   0|   2|
  -------------------------------------
  2 |  15|  16|  14|   9|  22|
  -------------------------------------
  3 |   3|  13|  20|  19|  18|
  -------------------------------------
  4 |   8|  17|  21|   6|   7|
  -------------------------------------
  5 |   4|  10|  12|   1|   0|
  -------------------------------------
  </code></pre>
  
  <p>I couldn't quite figure it out where is the problem. Hoping someone can help me with this assignment.</p>