/// <summary> /// Initialize the tiles. /// </summary> public void SetupTiles() { Tiles = new ScrabbleTile[ScrabbleForm.BOARD_WIDTH, ScrabbleForm.BOARD_HEIGHT]; TileBag = new TileBag(); //var specialTilePositions = ScrabbleForm.WordScorer.GetTileTypes(); foreach (var virtuTile in ScrabbleForm.Game.Grid) { var tile = new ScrabbleTile(ScrabbleForm) { Ligne = virtuTile.Ligne, Col = virtuTile.Col, }; tile.BackColor = SystemColors.ButtonFace; tile.Location = new Point(virtuTile.Ligne * (ScrabbleForm.TILE_SIZE + 2), virtuTile.Col * (ScrabbleForm.TILE_SIZE + 2)); tile.Size = new Size(ScrabbleForm.TILE_SIZE, ScrabbleForm.TILE_SIZE); //tile.UseVisualStyleBackColor = false; tile.Font = new Font("Verdana", 25.75F, FontStyle.Regular); tile.Click += Tile_Click; tile.DragDrop += Tile_DragDrop; tile.SetRegularBackgroundColour(); ScrabbleForm.Controls.Add(tile); Tiles[virtuTile.Ligne, virtuTile.Col] = tile; } }
/// <summary> /// Initialize the tiles. /// </summary> public void SetupTiles() { Tiles = new ScrabbleTile[ScrabbleForm.BOARD_WIDTH, ScrabbleForm.BOARD_HEIGHT]; TileBag = new TileBag(); var specialTilePositions = WordScorer.GetTileTypes(); for (int x = 1; x <= ScrabbleForm.BOARD_WIDTH; x++) { for (int y = 1; y <= ScrabbleForm.BOARD_HEIGHT; y++) { var tile = new ScrabbleTile { XLoc = x - 1, YLoc = y - 1 }; tile.BackColor = SystemColors.ButtonFace; tile.Location = new Point(x * (ScrabbleForm.TILE_SIZE + 2), y * (ScrabbleForm.TILE_SIZE + 2)); tile.Size = new Size(ScrabbleForm.TILE_SIZE, ScrabbleForm.TILE_SIZE); tile.UseVisualStyleBackColor = false; tile.Font = new Font("Verdana", 15.75F, FontStyle.Regular); tile.Click += Tile_Click; tile.TileType = specialTilePositions[x - 1, y - 1]; tile.SetRegularBackgroundColour(); ScrabbleForm.Controls.Add(tile); Tiles[x - 1, y - 1] = tile; } } }
/// <summary> /// Given a set of rack tiles, find all the possible anagrams of those tiles. /// </summary> /// <param name="tiles"></param> /// <returns></returns> public List <Word> Anagrams(List <RackTile> tiles, ScrabbleTile adjacentTile = null) { string letters = string.Empty; tiles.ForEach(t => letters += t.Text); return(Anagrams(letters, adjacentTile)); }
/// <summary> /// Generate a Word to (potentially) be played on the board based on the anagrams available and /// the adjacent tiles. /// </summary> /// <param name="word"></param> /// <param name="adjacentTile"></param> /// <param name="direction"></param> /// <returns></returns> private Word GenerateWordInDirection(string word, ScrabbleTile adjacentTile, MovementDirection direction) { var adjacent = adjacentTile.Text; int adjacentIndex = word.IndexOf(adjacent); int sx, ex, sy, ey = 0; switch (direction) { case MovementDirection.Down: // Vertically placing the word sx = adjacentTile.XLoc; ex = adjacentTile.XLoc; sy = adjacentTile.YLoc - adjacentIndex; ey = adjacentTile.YLoc + (word.Length - 1 - adjacentIndex); break; case MovementDirection.Across: // Horizontally placing the word sx = adjacentTile.XLoc - adjacentIndex; ex = adjacentTile.XLoc + (word.Length - 1 - adjacentIndex); sy = adjacentTile.YLoc; ey = adjacentTile.YLoc; break; default: throw new Exception($"Unsupported movement direction to generate words for {direction}"); } var w = new Word { Text = word, StartX = sx, EndX = ex, StartY = sy, EndY = ey, Tiles = new List <ScrabbleTile>() }; for (int x = sx; x <= ex; x++) { for (int y = sy; y <= ey; y++) { if (x < 0 || x >= ScrabbleForm.BOARD_WIDTH - 1 || y < 0 || y >= ScrabbleForm.BOARD_HEIGHT - 1) { return(null); } w.Tiles.Add(ScrabbleForm.TileManager.Tiles[x, y]); } } //w.Score = WordScorer.ScoreWord(w); w.Score = WordScorer.RawWordScore(word); return(w); }
private void TileClick(ScrabbleTile tile) { ClearTileHighlights(); Console.WriteLine("Clicking a tile!"); // Clicked on a tile that they have just put down so move it back to the rack. if (tile.TileInPlay) { if (tile.Text == "") { tile.TileInPlay = false; } foreach (var t in ScrabbleForm.PlayerManager.CurrentPlayer.Tiles) { if (t.Letter.ToString() == tile.Text && string.IsNullOrEmpty(t.Text)) { // Put the tile back in the rack t.Text = tile.Text; // Reset the scrabble tile tile.OnLetterRemoved(); // Highlight on the remaining tiles if there are any valid words. //var mr = ScrabbleForm.WordValidator.ValidateWordsInPlay(); //ScrabbleForm.btnPlay.Text = $"Play {mr.TotalScore} pts"; return; } } } // Tile already in use, can't move there if (!string.IsNullOrEmpty(tile.Text)) { return; } // All is good - handle placing the tile on the board from the rack. foreach (var t in ScrabbleForm.PlayerManager.CurrentPlayer.Tiles) { if (t.LetterSelected) { tile.OnLetterPlaced(t.Letter.ToString()); t.ClearDisplay(); break; } } //var moveResult = ScrabbleForm.WordValidator.ValidateWordsInPlay(); //ScrabbleForm.btnPlay.Text = $"Play {moveResult.TotalScore} pts"; }
/// <summary> /// For all the available words in the scrabble dictionary, /// check which ones can be made from the provided string of letters. /// </summary> /// <param name="letters"></param> /// <returns></returns> public List <Word> Anagrams(string letters, ScrabbleTile adjacentTile = null) { // If we have been provided an adjacent tile, then we can also use that to make words. if (adjacentTile != null) { letters += adjacentTile.Text; } var words = new List <Word>(); foreach (var w in ScrabbleForm.WordValidator.ValidWords) { // If a certain letter must be in the output then ensure the word we are making contains it if (adjacentTile != null && !w.Contains(adjacentTile.Text)) { continue; } if (CanMakeWord(w, letters)) { if (adjacentTile != null) { var vertical = GenerateWordInDirection(w, adjacentTile, MovementDirection.Down); if (vertical != null && !words.Contains(vertical)) { words.Add(vertical); } var horizontal = GenerateWordInDirection(w, adjacentTile, MovementDirection.Across); if (horizontal != null && !words.Contains(horizontal)) { words.Add(horizontal); } } else { var word = new Word { Text = w, Score = WordScorer.RawWordScore(w) }; if (!words.Contains(word)) { words.Add(word); } } } } return(words.OrderByDescending(w => w.Score).ThenByDescending(w => w.Text.Length).ToList()); }