/// <summary> /// Update the current position of Mario, the current number of mushrooms and reset the timer. /// </summary> /// <param name="nextCellToGo"> The next cell where Mario will go. </param> private void RoundAndAroundAndAround(LevelCell nextCellToGo) { if (nextCellToGo != null) { if (nextCellToGo.HasChamp == true) { m_Score++; EditTextDetails(); } nextCellToGo.AddMario(); m_CellWithMario.ClearCell(); m_CellWithMario = nextCellToGo; if (nextCellToGo.HasPeach == true) { m_DisplayTimer.Stop(); m_DispatcherTimer.Stop(); DisplayPath(); MessageBox.Show("OKIDOKI ! LET'S GO MAMAMIA !\nYou got " + m_Score + " mushrooms out of the " + Pathfinding.GridMaximumMushroomsNumber + " possible"); initWPF(); } else { ResetTimers(); } } }
/// <summary> /// Creates a level matrix with the size "m_GridSize" with a LevelCell in each cell. /// </summary> private void CreateLevel() { m_LevelCellMatrix = new LevelCell[m_GridSize, m_GridSize]; // For every cell we have, we create a new LevelCell // and we address it to the LevelCell matrix. for (int i = 0; i < m_GridSize; i++) { for (int j = 0; j < m_GridSize; j++) { // Creates a new LevelCell and gives it the correct attributes for row and column. LevelCellMatrix[i, j] = new LevelCell(); LevelCellMatrix[i, j].LineIndex = i; LevelCellMatrix[i, j].ColumnIndex = j; } } }
/// <summary> /// Here, we compute the path to get to Peach to get the maximum number of mushrooms. /// </summary> /// <param name="p_LevelCell"> The cell we are currently located in. </param> private static void FindPathToPeach(LevelCell p_LevelCell) { // Dictionnary used to store the result from the FindNextMostWorthyMushroom function. Dictionary <string, object> HashMap = new Dictionary <string, object>(); // For each line, we run the FindNextMostWorthyMushroom function which figure out if the // next most worthy mushroom is there, i.e. if we have a mushroom with a worthy equals to // WorthinessIndex - 1. for (int i = p_LevelCell.LineIndex; i < m_LevelGridSize; i++) { HashMap = FindNextMostWorthyMushroom(i, p_LevelCell); // We break if we found the next mushroom to avoid unecessary searches. if (!(bool)HashMap["bool"]) { break; } } // We extract the next LevelCell from the Dictionnary. LevelCell NextCase = (LevelCell)HashMap["nextCase"]; // We add the horizontal cells to get to the NextCell in the path List. for (int i = p_LevelCell.ColumnIndex + 1; i <= NextCase.ColumnIndex; i++) { m_Path.Add(m_LevelGrid[p_LevelCell.LineIndex, i]); } // We add the vertical cells to get to the NextCell in the path List. for (int i = p_LevelCell.LineIndex + 1; i <= NextCase.LineIndex; i++) { m_Path.Add(m_LevelGrid[i, NextCase.ColumnIndex]); } // If the next cell contains Peach, we stop the recurrence. if (NextCase.HasPeach) { return; } // Otherwise we recursively execute this algorithm of the remaining cells. else { FindPathToPeach(NextCase); } return; }
/// <summary> /// We go through the reachable cells from the current cell p_LevelCell and we find the one /// which has the highest worthiness. /// </summary> /// <param name="p_LevelCell"> The current cell. </param> /// <returns> Le score maximal trouvé </returns> private static int FindMaxWorthinessFromCell(LevelCell p_LevelCell) { int MaxWorthiness = 0; for (int i = p_LevelCell.LineIndex; i < m_LevelGridSize; i++) { for (int j = p_LevelCell.ColumnIndex; j < m_LevelGridSize; j++) { if (m_LevelGrid[i, j].HasChamp) { if (m_LevelGrid[i, j].Worthiness > MaxWorthiness) { MaxWorthiness = m_LevelGrid[i, j].Worthiness; } } } } return(MaxWorthiness); }
/// <summary> /// Here, We try to figure out if the next most worthy cell is in the line number p_LineIndex, /// i.e. if we have a mushroom with a worthy equals to WorthinessIndex - 1. /// </summary> /// <param name="p_LineIndex"> The line we are considering. </param> /// <param name="p_LevelCell"> The strating cell. </param> /// <returns> A Dictionnary containing a boolean indicating if we found the next most worthy cell /// and if so a LevelCell which is the next cell to consider. </returns> private static Dictionary <string, object> FindNextMostWorthyMushroom(int p_LineIndex, LevelCell p_LevelCell) { // Dictionnary used to store the result of the search. Dictionary <string, object> HashMap = new Dictionary <string, object>(); for (int j = p_LevelCell.ColumnIndex; j < m_LevelGridSize; j++) { // The next worthy cell will be the one which has a worthiness equals to WorthinessIndex - 1. if (m_LevelGrid[p_LineIndex, j].Worthiness == WorthinessIndex) { LevelCell NextCase = null; // If we found it and the current worthinessIndex equals 1, it is the last mushroom before // finding Peach so the next cell is the one of the bottom right with Peach. if (WorthinessIndex == 0) { NextCase = m_LevelGrid[m_LevelGridSize - 1, m_LevelGridSize - 1]; } // otherwise, the next case is the one with the worthiness equal to WorthinessIndex - 1. else { NextCase = m_LevelGrid[p_LineIndex, j]; // We update the WorthinessIndex to its new value (WorthinessIndex - 1). WorthinessIndex = m_LevelGrid[p_LineIndex, j].Worthiness - 1; } // We add the needed information to the Dictionnary. HashMap.Add("bool", false); HashMap.Add("nextCase", NextCase); return(HashMap); } } // We didin't find the next cell, we try with another line. HashMap.Add("bool", true); return(HashMap); }
/// <summary> /// Populates the matrix of cell that represents a level, and will later be displayed in the GUI. /// </summary> private void PopulateLevel() { for (int i = 0; i < m_GridSize; i++) { for (int j = 0; j < m_GridSize; j++) { if (i == 0 && j == 0) { LevelCellMatrix[i, j].AddMario(); m_CellWithMario = LevelCellMatrix[i, j]; } else if ((i == m_GridSize - 1) && (j == m_GridSize - 1)) { LevelCellMatrix[i, j].AddPeach(); } } } Random random = new Random(); // We randomly generate a number of coordinate couples equals to // (m_GridSize^2) / sqrt(m_GridSize). This allows us to have a reasonnable number // of mushrooms. for (int k = 0; k < (int)(Math.Pow(m_GridSize, 2) / Math.Sqrt(m_GridSize)); k++) { line.Add(random.Next(0, m_GridSize - 1)); column.Add(random.Next(0, m_GridSize - 1)); } for (int k = 0; k < (int)(Math.Pow(m_GridSize, 2) / Math.Sqrt(m_GridSize)); k++) { if (!(line[k] == 0 && column[k] == 0) && !(line[k] == m_GridSize - 1 && column[k] == m_GridSize - 1)) { LevelCellMatrix[line[k], column[k]].AddChamp(); } } }
/// <summary> /// Event called when the button for going down is clicked. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void On_GoDownButtonClick(object sender, RoutedEventArgs e) { LevelCell nextCellToGo = m_CellWithMario.getBottomCell(this); RoundAndAroundAndAround(nextCellToGo); }