public void addNewScore(PlayerScores ps) { if (ps != null) { Scores.Add(ps); } }
// play the game step by step... public void playBTB(Cell c) { try { //revealBlankCell(colInt, rowInt); // reveal the cell, and any relevant neighbors bool lost = checkForLose(); // check if the cell is a bomb bool won = checkForWin(); // check if that's the last inactive cell if (lost == true) // if it was a bomb... { s.Stop(); revealGrid(); MessageBox.Show("We're sorry. You failed.\nTime elapsed: " + s.ElapsedMilliseconds / 1000 + " seconds."); HighScoreForm hsf = new HighScoreForm(GridSize, null); hsf.Show(); } else if (won == true) // if all inactives are visited... { s.Stop(); revealGrid(); MessageBox.Show("Congratulations! You won!\nTime elapsed: " + s.ElapsedMilliseconds / 1000 + " seconds."); PlayerScores ps = new PlayerScores(); ps.setPlayerName(playerName); if (GridSize == 5) { ps.setDifficultyPlayed("Easy"); } else if (GridSize == 10) { ps.setDifficultyPlayed("Medium"); } else { ps.setDifficultyPlayed("Hard"); } ps.setTimeElapsed((int)s.ElapsedMilliseconds / 1000); MessageBox.Show(ps.getPlayerName() + "\n" + ps.getDifficultyPlayed() + "\n" + ps.getTimeElapsed()); HighScoreForm hsf = new HighScoreForm(GridSize, ps); hsf.Show(); } else // if the game hasn't ended yet, update the grid and ask again. { showPlayerGrid(); } } catch (Exception e) { MessageBox.Show(e.ToString()); } }
// load data when called -- path PlayerScores.txt public void loadData() { if (File.Exists("PlayerScores.txt")) { StreamReader sr = new StreamReader("PlayerScores.txt"); PlayerScores ps = new PlayerScores(); string str; while (!sr.EndOfStream) { try { str = sr.ReadLine(); ps.setPlayerName(str); str = sr.ReadLine(); ps.setDifficultyPlayed(str); str = sr.ReadLine(); int time = int.Parse(str); ps.setTimeElapsed(time); Scores.Add(ps); ps = new PlayerScores(); } catch { MessageBox.Show("We have a problem with loadData."); } } sr.Close(); } else { //just create the file using (StreamWriter sw = new StreamWriter(@"PlayerScores.txt", true)) { } } }
public HighScoreForm(int size, PlayerScores ps) { loadData(); addNewScore(ps); if (Scores.Count > 0) { DataTable dt_easy = new DataTable(); DataTable dt_Medium = new DataTable(); DataTable dt_Hard = new DataTable(); List <DataTable> dt_list = new List <DataTable>(); dt_list.Add(dt_easy); dt_list.Add(dt_Medium); dt_list.Add(dt_Hard); foreach (DataTable dt in dt_list) { dt.Columns.Add("Place", typeof(int)); dt.Columns.Add("Player Name", typeof(string)); dt.Columns.Add("Time Elapsed", typeof(int)); } List <PlayerScores> easyList = new List <PlayerScores>(); List <PlayerScores> mediumList = new List <PlayerScores>(); List <PlayerScores> hardList = new List <PlayerScores>(); foreach (PlayerScores s in Scores) { if (s.getDifficultyPlayed() == "Easy") { easyList.Add(s); } else if (s.getDifficultyPlayed() == "Medium") { mediumList.Add(s); } else // if == "Hard" { hardList.Add(s); } } List <PlayerScores> sortedEasy = easyList.OrderBy(s => s.getTimeElapsed()).ToList(); List <PlayerScores> sortedMedium = mediumList.OrderBy(s => s.getTimeElapsed()).ToList(); List <PlayerScores> sortedHard = hardList.OrderBy(s => s.getTimeElapsed()).ToList(); PlayerScores[] easyArr = sortedEasy.ToArray(); PlayerScores[] mediumArr = sortedMedium.ToArray(); PlayerScores[] hardArr = sortedHard.ToArray(); for (int i = 0; i < 5; i++) { if (easyArr.Length > 0 && easyArr.Length - 1 >= i) { dt_easy.Rows.Add(i + 1, easyArr[i].getPlayerName(), easyArr[i].getTimeElapsed()); } if (mediumArr.Length > 0 && mediumArr.Length - 1 >= i) { dt_Medium.Rows.Add(i + 1, mediumArr[i].getPlayerName(), mediumArr[i].getTimeElapsed()); } if (hardArr.Length > 0 && hardArr.Length - 1 >= i) { dt_Hard.Rows.Add(i + 1, hardArr[i].getPlayerName(), hardArr[i].getTimeElapsed()); } } DataGridView dgv = new DataGridView(); Label lbl_difficulty = new Label(); if (size == 5) { dgv.DataSource = dt_easy; lbl_difficulty.Text = "Easy Leaderboard"; } else if (size == 10) { dgv.DataSource = dt_Medium; lbl_difficulty.Text = "Medium Leaderboard"; } else { dgv.DataSource = dt_Hard; lbl_difficulty.Text = "Hard Leaderboard"; } dgv.Location = new Point(20, lbl_difficulty.Location.Y + lbl_difficulty.Height); dgv.Size = new Size(345, 160); Controls.Add(lbl_difficulty); Controls.Add(dgv); Size = new Size(dgv.Width + 100, dgv.Height + 100); } FormClosing += saveData; }