/// <summary> /// Generates the grid of tiles given a number of rows, columns and a string of numbers /// </summary> /// <param name="rows">The number of rows in the grid</param> /// <param name="columns">The number of columns in the grid</param> /// <param name="numbers">The string of numbers in order</param> private void generateGrid(int rows, int columns, string numbers) { foreach (Tile tile in tiles) { Controls.Remove(tile); } tiles = new Tile[rows, columns]; int x; int y = TOP; int height = GAME_HEIGHT / num_rows; int width = GAME_WIDTH / num_columns; string[] allNumbers = numbers.Split(new char[] { '_' }); int counter = 0; for (int i = 0; i < num_rows; i++) { x = LEFT; for (int j = 0; j < num_columns; j++) { int num = int.Parse(allNumbers[counter]); if (num > 0) //Square should be blank { if (usingPicture) //generate a grid using a picture { tiles[i, j] = new Tile(height, width, y, x, num.ToString(), i, j, true, this); } else //generate a grid using numbers { tiles[i, j] = new Tile(height, width, y, x, num.ToString(), i, j, this); } } counter++; x += width; } y += height; } foreach (Tile tile in tiles) { Controls.Add(tile); } allMoves = new List<char>(); winString = ""; for (int i = 1; i < num_rows * num_columns; i++) { winString += i + "_"; } }
/// <summary> /// Moves the selected tile in the specified direction /// </summary> /// <param name="tile">The selected tile</param> /// <param name="direction">The specified direction</param> private void moveTile(Tile tile, char direction) { switch (direction) { case 'l': tiles[tile.Row, tile.Col - 1] = tile; tiles[tile.Row, tile.Col] = null; tile.Col--; tile.Left -= tile.Width; break; case 'r': tiles[tile.Row, tile.Col + 1] = tile; tiles[tile.Row, tile.Col] = null; tile.Col++; tile.Left += tile.Width; break; case 'u': tiles[tile.Row - 1, tile.Col] = tile; tiles[tile.Row, tile.Col] = null; tile.Row--; tile.Top -= tile.Height; break; case 'd': tiles[tile.Row + 1, tile.Col] = tile; tiles[tile.Row, tile.Col] = null; tile.Row++; tile.Top += tile.Height; break; default: break; } }
/// <summary> /// Selects the direction to move the clicked tile /// </summary> /// <param name="tile">the tile that has been clicked</param> public void selectDirection(Tile tile) { char direction; direction = ' '; Tile temp; temp = tile; if ((tile.Col != num_columns - 1) && (tiles[tile.Row, tile.Col + 1] == null)) { direction = 'r'; allMoves.Add('A'); } else if ((tile.Col != 0) && (tiles[tile.Row, tile.Col - 1] == null)) { direction = 'l'; allMoves.Add('D'); } else if ((tile.Row != num_rows - 1) && (tiles[tile.Row + 1, tile.Col] == null)) { direction = 'd'; allMoves.Add('W'); } else if ((tile.Row != 0) && (tiles[tile.Row - 1, tile.Col] == null)) { direction = 'u'; allMoves.Add('S'); } moveTile(tile, direction); }