Search Unity

Something weird's happening here..

Discussion in 'Scripting' started by AndrAla, May 14, 2017.

  1. AndrAla

    AndrAla

    Joined:
    Mar 11, 2017
    Posts:
    18
    I've made a 24 puzzle, and it's working okay apart from its initialization.
    Code (csharp):
    1.  
    2.     [HideInInspector] public int empty;
    3.     [HideInInspector] public TileScript[] TS = new TileScript[26];
    4.     [HideInInspector] public int wrong;
    5.     [HideInInspector] public int pending;
    6.     public Text movesTakenText;
    7.     public Text solvableText;
    8.     int moves = 0;
    9.     int movedTile;
    10.     private bool _b = true;
    11.     public bool canMove {/*get/set*/}
    12.  
    13.     public void ShuffleTiles (){
    14.         int[] order = new int[26];
    15.         Sprite[] sprt = new Sprite[26];
    16.         bool isSolvable = false;
    17.         for (int i = 0; i <= 25; i++) {
    18.             order [i] = TS[i].tile;
    19.             sprt [order[i]] = TS [i].spr;
    20.         }
    21.         pending = 0;
    22.         wrong = 0;
    23.         int inversions = 0;
    24.         int a;
    25.         int b;
    26.         while (!isSolvable || order[0] == 0) {
    27.             Shuffle (order);
    28.             for (int i = 1; i < order.Length; i++) {
    29.                 a = order [i];
    30.                 if (a != 0) {
    31.                     for (int j = 1; j < i; j++) {
    32.                         b = order [j];
    33.                         if (b != 0) {
    34.                             if (b > a) inversions++;
    35.                         }
    36.                     }
    37.                 } else {
    38.                     empty = i;
    39.                 }
    40.             }
    41.             isSolvable = inversions % 2 == 0 && (Row (empty) - Row (order [0])) % 2 == 0;
    42.         }
    43.         for (int i = 0; i <= 25; i++) {
    44.             TS [i].tile = order [i];
    45.             TS [i].spr = sprt [order [i]];
    46.             if (!TS [i].correct && i > 0 && order[i] != 0) wrong++;
    47.         }
    48.         moves = 0;
    49.         movesTakenText.text = "Moves: 0";
    50.         solvableText.text = "Unchecked";
    51.         canMove = true;
    52.     }
    53.  
    54.     public void CheckIfSolvable(){
    55.         int[] order = new int[26];
    56.         int a;
    57.         int b;
    58.         int inversions = 0;
    59.         for (int i = 0; i <= 25; i++) {
    60.             order [i] = TS[i].tile;
    61.         }
    62.         for (int i = 2; i < order.Length; i++) {
    63.             a = order [i];
    64.             if (a != 0) {
    65.                 for (int j = 1; j < i; j++) {
    66.                     b = order [j];
    67.                     if (b != 0) {
    68.                         if (b > a) inversions++;
    69.                     }
    70.                 }
    71.             }
    72.         }
    73.         if ((inversions % 2 == 0 && (Row (empty) - Row (order [0])) % 2 == 0)) {
    74.             solvableText.text = "Solvable";
    75.         } else {
    76.             solvableText.text = "Unsolvable";
    77.         }
    78.     }
    79.  
    80.     public static void Shuffle<T>(T[] array)
    81.     {
    82.         System.Random rand = new System.Random();
    83.         int m = array.Length;
    84.         for (int i = 0; i < m; i++)
    85.         {
    86.             int idx = rand.Next(i, m);
    87.             T tmp = array[i];
    88.             array[i] = array[idx];
    89.             array[idx] = tmp;
    90.         }
    91.     }
    92.  
    93.     public static int Row(int i){
    94.         return (i-1) / 5;
    95.     }
    96.  
    97.     public static int Col(int i){
    98.         return (i-1) % 5;
    99.     }
    100.  
    The [0] tile is the missing one, and I initiate the TS (TileScript)s in another script.
    And I can call the "ShuffleTiles" and "CheckIfSolvable" via UI buttons;

    So the problem is:
    I have the "CheckIfSolvable" method integrated into the "ShuffleTiles" method, and I'm shuffling the order until the puzzle is solvable (with the while), however the puzzles don't always start solvable. When I use the "CheckIfSolvable" after a shuffle - they aren't always solvable.
    Any idea why is that happening?