public void MoveButtons(GameButton button, SLIDE_DIRECTION dir) { int row = (buttonCount - (buttonCount - button.index)) / fieldSize; int[] indexes = new int[(int)Mathf.Sqrt(buttonCount)]; int currentIndex = 0; for (int i = 0; i < indexes.Length; i++) { indexes[i] = -1; } switch (dir) { case SLIDE_DIRECTION.TOP: for (int i = button.index; i >= 0; i -= fieldSize) { if (i - fieldSize < 0) //out of bounds { if (buttons[i].isEmptyCell) { GameButton temp = buttons[emptyButtonIndex]; for (int j = indexes.Length - 1; j >= 0; j--) { if (indexes[j] >= 0) { buttons[indexes[j] - fieldSize] = buttons[indexes[j]]; buttons[indexes[j] - fieldSize].SetIndex(indexes[j] - fieldSize); buttons[indexes[j]].Move(dir); } } if (indexes[0] >= 0) { buttons[indexes[0]] = temp; emptyButtonIndex = indexes[0]; temp.SetIndex(emptyButtonIndex); } else { buttons[i].Move(dir); } break; } else { break; } } else if (!buttons[i].isEmptyCell) { indexes[currentIndex++] = buttons[i].index; } else if (buttons[i].isEmptyCell) { GameButton temp = buttons[emptyButtonIndex]; for (int j = indexes.Length - 1; j >= 0; j--) { if (indexes[j] >= 0) { buttons[indexes[j] - fieldSize] = buttons[indexes[j]]; buttons[indexes[j] - fieldSize].SetIndex(indexes[j] - fieldSize); buttons[indexes[j]].Move(dir); } } if (indexes[0] >= 0) { buttons[indexes[0]] = temp; emptyButtonIndex = indexes[0]; temp.SetIndex(emptyButtonIndex); } else { buttons[i].Move(dir); } break; } } break; case SLIDE_DIRECTION.DOWN: for (int i = button.index; i < buttonCount; i += fieldSize) { if (i + fieldSize >= buttonCount) { if (buttons[i].isEmptyCell) { GameButton temp = buttons[emptyButtonIndex]; for (int j = indexes.Length - 1; j >= 0; j--) { if (indexes[j] >= 0) { buttons[indexes[j] + fieldSize] = buttons[indexes[j]]; buttons[indexes[j] + fieldSize].SetIndex(indexes[j] + fieldSize); buttons[indexes[j]].Move(dir); } } if (indexes[0] >= 0) { buttons[indexes[0]] = temp; emptyButtonIndex = indexes[0]; temp.SetIndex(emptyButtonIndex); } else { buttons[i].Move(dir); } break; } else { break; } } else if (!buttons[i].isEmptyCell) { indexes[currentIndex++] = buttons[i].index; } if (buttons[i].isEmptyCell) { GameButton temp = buttons[emptyButtonIndex]; for (int j = indexes.Length - 1; j >= 0; j--) { if (indexes[j] >= 0) { buttons[indexes[j] + fieldSize] = buttons[indexes[j]]; buttons[indexes[j] + fieldSize].SetIndex(indexes[j] + fieldSize); buttons[indexes[j]].Move(dir); } } if (indexes[0] >= 0) { buttons[indexes[0]] = temp; emptyButtonIndex = indexes[0]; temp.SetIndex(emptyButtonIndex); } else { buttons[i].Move(dir); } break; } } break; case SLIDE_DIRECTION.LEFT: for (int i = button.index; i >= row * fieldSize; i--) { if ((i - 1 < 0 || i % fieldSize == 0)) { if (buttons[i].isEmptyCell) { GameButton temp = buttons[emptyButtonIndex]; for (int j = indexes.Length - 1; j >= 0; j--) { if (indexes[j] >= 0) { buttons[indexes[j] - 1] = buttons[indexes[j]]; buttons[indexes[j] - 1].SetIndex(indexes[j] - 1); buttons[indexes[j]].Move(dir); } } if (indexes[0] >= 0) { buttons[indexes[0]] = temp; emptyButtonIndex = indexes[0]; temp.SetIndex(emptyButtonIndex); } else { buttons[i].Move(dir); } break; } else { break; } } else if (!buttons[i].isEmptyCell) { indexes[currentIndex++] = buttons[i].index; } if (buttons[i].isEmptyCell) { GameButton temp = buttons[emptyButtonIndex]; for (int j = indexes.Length - 1; j >= 0; j--) { if (indexes[j] >= 0) { buttons[indexes[j] - 1] = buttons[indexes[j]]; buttons[indexes[j] - 1].SetIndex(indexes[j] - 1); buttons[indexes[j]].Move(dir); } } if (indexes[0] >= 0) { buttons[indexes[0]] = temp; emptyButtonIndex = indexes[0]; temp.SetIndex(emptyButtonIndex); } else { buttons[i].Move(dir); } break; } } break; case SLIDE_DIRECTION.RIGHT: for (int i = button.index; i <= ((row + 1) * fieldSize) - 1; i++) { if ((i + 1 > buttonCount || (i + 1) % fieldSize == 0)) { if (buttons[i].isEmptyCell) { GameButton temp = buttons[emptyButtonIndex]; for (int j = indexes.Length - 1; j >= 0; j--) { if (indexes[j] >= 0) { buttons[indexes[j] + 1] = buttons[indexes[j]]; buttons[indexes[j] + 1].SetIndex(indexes[j] + 1); buttons[indexes[j]].Move(dir); } } if (indexes[0] >= 0) { buttons[indexes[0]] = temp; emptyButtonIndex = indexes[0]; temp.SetIndex(emptyButtonIndex); } else { buttons[i].Move(dir); } break; } else { break; } } else if (!buttons[i].isEmptyCell) { indexes[currentIndex++] = buttons[i].index; } if (buttons[i].isEmptyCell) { GameButton temp = buttons[emptyButtonIndex]; for (int j = indexes.Length - 1; j >= 0; j--) { if (indexes[j] >= 0) { buttons[indexes[j] + 1] = buttons[indexes[j]]; buttons[indexes[j] + 1].SetIndex(indexes[j] + 1); buttons[indexes[j]].Move(dir); } } if (indexes[0] >= 0) { buttons[indexes[0]] = temp; emptyButtonIndex = indexes[0]; temp.SetIndex(emptyButtonIndex); break; } else { buttons[i].Move(dir); } break; } } break; } Check(); }