/// <summary> /// Handles playing a turn. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnPlay_Click(object sender, EventArgs e) { if (!GamePlaying) { return; } var tilesPlayed = PlayerManager.CurrentPlayer.Tiles.Where(r => string.IsNullOrEmpty(r.Text)).Count(); if (tilesPlayed == 0) { MessageBox.Show("You must place some tiles on the board and make a word to play. You can pass or swap your letters if cannot make a word. You can use the hint to see available words in your rack."); return; } var checkTilePositions = TileManager.ValidateTilePositions(); //var moveResult = WordValidator.ValidateWordsInPlay(); if (!checkTilePositions) { MessageBox.Show("The placement of your tiles is invalid."); } //else if (!moveResult.Valid) // moveResult.Words.Where(w => !w.IsAllowed).ToList().ForEach(w => MessageBox.Show($"{w.Text} is not a valid word")); else { foreach (var t in this.TileManager.TilesInPlay) { Game.Grid[t.Ligne, t.Col].Letter = Game.Alphabet.Find(a => a.Char == t.Text[0]); } TileManager.ResetTilesInPlay(); RackManager.FillRack(PlayerManager.CurrentPlayer.Tiles); StatManager.Moves += 1; StatManager.ConsecutivePasses = 0; //moveResult.Words.ForEach(w => Logger.LogMessage($"{PlayerManager.CurrentPlayer.Name} played {w.Text} for {w.Points} points")); //Logger.LogMessage($"Turn ended - total score: {moveResult.TotalScore}"); btnLetters.Text = $"{TileManager.TileBag.LetterCountRemaining()} Letters Remaining"; // A player has finished all their tiles so the game is over if (PlayerManager.PlayerOne.Tiles.Count == 0 || PlayerManager.PlayerTwo.Tiles.Count == 0) { GameOver(); } //PlayerManager.CurrentPlayer.Score += moveResult.TotalScore; PlayerManager.SwapCurrentPlayer(); } }
/// <summary> /// Handles allowing the user to swap their tiles. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSwap_Click(object sender, EventArgs e) { if (!GamePlaying) { return; } TileManager.ResetTilesOnBoardFromTurn(); var verification = MessageBox.Show("Do you really want to swap the tiles you have selected?", "Swap your letters", MessageBoxButtons.YesNo); if (verification == DialogResult.Yes) { var tiles = PlayerManager.CurrentPlayer.Tiles.Where(c => c.LetterSelected).ToList(); // Trying to swap no letters at all. if (tiles.Count == 0) { MessageBox.Show("You must select at least one letter from your rack to swap."); return; } // Trying to swap more letters than are left in the bag if (tiles.Count > TileManager.TileBag.LetterCountRemaining()) { MessageBox.Show($"You can only swap {TileManager.TileBag.LetterCountRemaining()} letter(s) or less."); return; } tiles.ForEach(t => { TileManager.TileBag.GiveLetter(t.Text[0]); t.ClearDisplay(); }); RackManager.FillRack(PlayerManager.CurrentPlayer.Tiles); StatManager.Swaps += 1; StatManager.ConsecutivePasses = 0; Logger.LogMessage($"Turn ended - {PlayerManager.CurrentPlayer.Name} swapped {tiles.Count} tile(s)."); PlayerManager.SwapCurrentPlayer(); } }