private void btnStart_Click(object sender, EventArgs e) { int players = 0; bool emptyName = false; var list = new List <Player>(); var dict = new Dictionary <ColorType, string>(); AudioPlayer.PlayClickSound(); if (plrOneCheck.Checked) { players++; if (string.IsNullOrEmpty(plrOneText.Text)) { emptyName = true; } } if (plrTwoCheck.Checked) { players++; if (string.IsNullOrEmpty(plrTwoText.Text)) { emptyName = true; } } if (plrThreeCheck.Checked) { players++; if (string.IsNullOrEmpty(plrThreeText.Text)) { emptyName = true; } } if (plrFourCheck.Checked) { players++; if (string.IsNullOrEmpty(plrFourText.Text)) { emptyName = true; } } if (players < 2) { try { throw new InvalidPlayerCountException("At least two players have to be checked."); } catch (InvalidPlayerCountException ex) { lblWarning.Text = ex.Message; lblWarning.ForeColor = Color.Red; } return; } if (emptyName) { try { throw new InvalidNameException("Please, don't leave empty name spaces."); } catch (InvalidNameException ex) { lblWarning.Text = ex.Message; } return; } if (plrOneCheck.Checked) { dict.Add(ColorType.Red, plrOneText.Text); } if (plrTwoCheck.Checked) { dict.Add(ColorType.Green, plrTwoText.Text); } if (plrThreeCheck.Checked) { dict.Add(ColorType.Yellow, plrThreeText.Text); } if (plrFourCheck.Checked) { dict.Add(ColorType.Blue, plrFourText.Text); } if (plrOneCheck.Checked) { list.Add(new Player(plrOneText.Text, ColorType.Red)); } if (plrTwoCheck.Checked) { list.Add(new Player(plrTwoText.Text, ColorType.Green)); } if (plrThreeCheck.Checked) { list.Add(new Player(plrThreeText.Text, ColorType.Yellow)); } if (plrFourCheck.Checked) { list.Add(new Player(plrFourText.Text, ColorType.Blue)); } var game = new Game(dict); game.FormBorderStyle = FormBorderStyle.FixedSingle; game.Show(); }
// Starting the Game private void btnStart_Click(object sender, EventArgs e) { // Init player count int players = 0; // Empty name exception bool emptyName = false; // List of players pawn var list = new List <Player>(); // A Dictionary of players color and name // Which at the highest count is equal to 4 (4 players) var dict = new Dictionary <ColorType, string>(); // Updating the count of the players // Adding their color and name to Dictionary // And at the end adding theme to the list if (plrOneCheck.Checked) { players++; dict.Add(ColorType.Red, plrOneText.Text); list.Add(new Player(plrOneText.Text, ColorType.Red)); // Empty name exception occured if (string.IsNullOrEmpty(plrOneText.Text)) { emptyName = true; } } if (plrTwoCheck.Checked) { players++; dict.Add(ColorType.Green, plrTwoText.Text); list.Add(new Player(plrTwoText.Text, ColorType.Green)); // Empty name exception occured if (string.IsNullOrEmpty(plrTwoText.Text)) { emptyName = true; } } if (plrThreeCheck.Checked) { players++; dict.Add(ColorType.Yellow, plrThreeText.Text); list.Add(new Player(plrThreeText.Text, ColorType.Yellow)); // Empty name exception occured if (string.IsNullOrEmpty(plrThreeText.Text)) { emptyName = true; } } if (plrFourCheck.Checked) { players++; dict.Add(ColorType.Blue, plrFourText.Text); list.Add(new Player(plrFourText.Text, ColorType.Blue)); // Empty name exception occured if (string.IsNullOrEmpty(plrFourText.Text)) { emptyName = true; } } // No player choosen or there is only one player if (players < 2) { try { throw new InvalidPlayerCountException("At least two players have to be checked."); } catch (InvalidPlayerCountException ex) { lblWarning.Text = ex.Message; lblWarning.ForeColor = Color.Red; } return; } // Handling the empty name exception if (emptyName) { try { throw new InvalidNameException("Please, don't leave empty name spaces."); } catch (InvalidNameException ex) { lblWarning.Text = ex.Message; } return; } // An object of the Game class which has a dictionary of the players var game = new Game(dict); game.FormBorderStyle = FormBorderStyle.FixedSingle; game.Show(); }