//// read write functions void WriteNewBestplayer(Player p) { // Write sample data to CSV file using (CsvFileWriter writer = new CsvFileWriter("Scores.csv", true)) { CsvRow row = new CsvRow(); row.Add(p.Name); row.Add(p.Score.ToString()); row.Add(p.bestWord); writer.WriteRow(row); } }
/*Check if the letter you've written is valid * if you finished the game, we add you as a new player * */ public void done() { currentScore = 0; word = ""; if (buttons.Count != 0) { foreach (Button b in buttons) { word += b.Text; } if (words.IsValid(word)) { if (word.Length > bestWord.Length) bestWord = word; currentScore = word.Length; totalScore += currentScore; wordTxt.Text = bestWord; valid.Text = "valid"; lblValidStars.Visible = true; timer3.Start(); Invalidate(); } else { valid.Text = "invalid"; bunnyPanel.Visible = true; timer3.Start(); } } else { bunnyPanel.Visible = true; timer3.Start(); valid.Text = "invalid"; } timer1.Stop(); watch = 30; if (round <= 4) { nextRound.Enabled = true; } else { Player player = new Player(name, totalScore, bestWord); // MessageBox.Show(player.ToString()); bool f = false; int i = 0; if (players.Count != 0) foreach (Player p in players) { if (p.Name == name) { f = true; break; } i++; } if (!f) { players.Add(player); WriteNewBestplayer(player); } else { if (players.ElementAt(i).Score < totalScore) players.ElementAt(i).Score = totalScore; if (players.ElementAt(i).bestWord.Length < bestWord.Length) players.ElementAt(i).bestWord = bestWord; WriteNewBestplayer(players.ElementAt(i)); } bestWord = ""; } scoreTxt.Text = totalScore.ToString(); }
List<Player> ReadAllPlayers() { // Read sample data from CSV file using (CsvFileReader reader = new CsvFileReader("Scores.csv", true)) { CsvRow row = new CsvRow(); List<Player> lstPlayers = new List<Player>(); while (reader.ReadRow(row)) { int br = 0; String name = ""; String word = ""; int score = 0; foreach (string s in row) { if (br == 0) name = s; else if (br == 1) score = int.Parse(s); else word = s; br++; } Player p = new Player(name, score, word); lstPlayers.Add(p); } return lstPlayers; } }