void start() { RegularCandy[,] candyGrid = new RegularCandy[9, 9]; if (File.Exists(saveFilePath)) { try { candyGrid = readPlayingField(saveFilePath); } catch { Console.WriteLine("Het bestand kon niet worden ingelezen, waarschijnlijk omdat hij is aangepast."); Console.WriteLine("Er word nu een nieuw speelveld gegenereerd en opgeslagen"); initAndSaveCandies(candyGrid, saveFilePath); } } else { initAndSaveCandies(candyGrid, saveFilePath); } printCandies(candyGrid); bool scoreRowPresent = CandyCrusher.checkScoreRowPresent(candyGrid); bool scoreColumnPresent = CandyCrusher.checkScoreColumnPresent(candyGrid); Console.WriteLine("{0}, er is {1}een score rij aanwezig", scoreRowPresent ? "Ja" : "Nee", scoreRowPresent ? "" : "GEEN "); Console.WriteLine("{0}, er is {1}een score kolom aanwezig", scoreColumnPresent ? "Ja" : "Nee", scoreColumnPresent ? "" : "GEEN "); Console.ReadKey(); }
ConsoleColor getColorForCandy(RegularCandy candy) { int candyIndex = (int)candy; switch (candyIndex) { case 1: return(ConsoleColor.Red); case 2: return(ConsoleColor.Cyan); case 3: return(ConsoleColor.Yellow); case 4: return(ConsoleColor.Green); case 5: return(ConsoleColor.Blue); case 6: return(ConsoleColor.Magenta); default: return(ConsoleColor.White); } }
void applyGravity() { for (int c = 0; c < grid.GetLength(1); c++) { for (int i = 0; i < grid.GetLength(0); i++) { for (int r = 0; r < grid.GetLength(0); r++) { RegularCandy currentCandy = grid[r, c]; if (currentCandy != RegularCandy.Empty) { if (r + 1 < grid.GetLength(0)) { RegularCandy candyBelow = grid[r + 1, c]; if (candyBelow == RegularCandy.Empty) { switchCandyPositions(new Position(r, c), new Position(r + 1, c)); } } } } } } }
void start() { RegularCandy[,] candyGrid = new RegularCandy[9, 9]; initCandies(candyGrid); printCandies(candyGrid); bool scoreRowPresent = checkScoreRowPresent(candyGrid); bool scoreColumnPresent = checkScoreColumnPresent(candyGrid); Console.WriteLine("{0}, er is {1}een score rij aanwezig", scoreRowPresent ? "Ja" : "Nee", scoreRowPresent ? "" : "GEEN "); Console.WriteLine("{0}, er is {1}een score kolom aanwezig", scoreColumnPresent ? "Ja" : "Nee", scoreColumnPresent ? "" : "GEEN "); Console.ReadKey(); }
public static bool checkScoreColumnPresent(RegularCandy[,] grid) { for (int c = 0; c < grid.GetLength(0); c++) { for (int r = 0; r < grid.GetLength(1) - 2; r++) { RegularCandy comparisonCandy = grid[r, c]; if (grid[r + 1, c] == comparisonCandy && grid[r + 1, c] == comparisonCandy) { return(true); } } } return(false); }
bool checkScoreRowPresent(RegularCandy[,] grid) { for (int r = 0; r < grid.GetLength(0); r++) { for (int c = 0; c < grid.GetLength(1) - 2; c++) { RegularCandy comparisonCandy = grid[r, c]; if (grid[r, c + 1] == comparisonCandy && grid[r, c + 2] == comparisonCandy) { return(true); } } } return(false); }
void fillEmptySlots() { for (int r = 0; r < grid.GetLength(0); r++) { for (int c = 0; c < grid.GetLength(1); c++) { RegularCandy candyAtCurrentPosition = grid[r, c]; if (candyAtCurrentPosition == RegularCandy.Empty) { int candyCountPossible = Enum.GetNames(typeof(RegularCandy)).Length; grid[r, c] = (RegularCandy)random.Next(1, candyCountPossible); } } } }
void switchCandyPositions(Position pos1, Position pos2) { if (pos2.row > 0 && pos2.row < grid.GetLength(0) && pos2.column > 0 && pos2.column < grid.GetLength(1)) { RegularCandy candyAtPos1 = grid[pos1.row, pos1.column]; RegularCandy candyAtPos2 = grid[pos2.row, pos2.column]; grid[pos1.row, pos1.column] = candyAtPos2; grid[pos2.row, pos2.column] = candyAtPos1; return; } else { //showMoveInput(); } }
RegularCandy[,] readPlayingField(string fileName) { StreamReader streamReader = null; try { streamReader = new StreamReader(fileName); int rows = int.Parse(streamReader.ReadLine()); int columns = int.Parse(streamReader.ReadLine()); RegularCandy[,] grid = new RegularCandy[rows, columns]; int row = 0; while (!streamReader.EndOfStream) { string line = streamReader.ReadLine(); string[] candies = line.Split(' '); for (int i = 0; i < candies.Length; i++) { int candyId = int.Parse(candies[i]); if (Enum.IsDefined(typeof(RegularCandy), candyId)) { RegularCandy candy = (RegularCandy)candyId; grid[row, i] = candy; } else { throw new Exception("Candy id was not present in Enum"); } } row++; } return(grid); } catch (IOException exception) { Console.WriteLine(exception.Message); } catch (Exception exception) { throw exception; } finally { streamReader.Close(); } }
void printCandy(RegularCandy candy) { Console.ForegroundColor = getColorForCandy(candy); Console.Write("#"); Console.ResetColor(); }
void checkScoreFromPosition(int r, int c, string checkDirection) { RegularCandy currentCandy = grid[r, c]; bool foundScoreRow = false; bool foundScoreColumn = false; if (currentCandy != RegularCandy.Empty) { if (checkDirection == "row" || checkDirection == "both") { if (c < grid.GetLength(0) - 2 && currentCandy == grid[r, c + 1] && currentCandy == grid[r, c + 2]) { score += 150; checkScoreFromPosition(r, c + 1, "column"); checkScoreFromPosition(r, c + 2, "column"); grid[r, c + 1] = RegularCandy.Empty; grid[r, c + 2] = RegularCandy.Empty; foundScoreRow = true; } else if (c > 0 && c < grid.GetLength(0) - 1 && currentCandy == grid[r, c - 1] && currentCandy == grid[r, c + 1]) { score += 150; checkScoreFromPosition(r, c + 1, "column"); checkScoreFromPosition(r, c - 1, "column"); grid[r, c + 1] = RegularCandy.Empty; grid[r, c - 1] = RegularCandy.Empty; foundScoreRow = true; } else if (c > 2 && currentCandy == grid[r, c - 2] && currentCandy == grid[r, c - 1]) { score += 150; checkScoreFromPosition(r, c - 1, "column"); checkScoreFromPosition(r, c - 2, "column"); grid[r, c - 1] = RegularCandy.Empty; grid[r, c - 2] = RegularCandy.Empty; foundScoreRow = true; } } if (checkDirection == "column" || checkDirection == "both") { if (r < grid.GetLength(1) - 2 && currentCandy == grid[r + 1, c] && currentCandy == grid[r + 2, c]) { score += 150; checkScoreFromPosition(r + 1, c, "row"); checkScoreFromPosition(r + 2, c, "row"); grid[r + 1, c] = RegularCandy.Empty; grid[r + 2, c] = RegularCandy.Empty; foundScoreColumn = true; } else if (r > 0 && r < grid.GetLength(1) - 1 && currentCandy == grid[r + 1, c] && currentCandy == grid[r - 1, c]) { score += 150; checkScoreFromPosition(r + 1, c, "row"); checkScoreFromPosition(r - 1, c, "row"); grid[r + 1, c] = RegularCandy.Empty; grid[r - 1, c] = RegularCandy.Empty; foundScoreColumn = true; } else if (r > 2 && currentCandy == grid[r - 1, c] && currentCandy == grid[r - 2, c]) { score += 150; checkScoreFromPosition(r - 1, c, "row"); checkScoreFromPosition(r - 2, c, "row"); grid[r - 1, c] = RegularCandy.Empty; grid[r - 2, c] = RegularCandy.Empty; foundScoreColumn = true; } } if (foundScoreRow || foundScoreColumn) { grid[r, c] = RegularCandy.Empty; } } }