/// <summary> /// try to get the letter at the provided x and y pos /// returns the letter object if possible, null if not /// </summary> /// <param name="x">the x pos of the tile to get</param> /// <param name="y">the y pos of the tile to get</param> /// <returns></returns> private LetterGameLetter BoardTryGetTile(int x, int y) { LetterGameLetter ret = null; if (IsBoardTileValid(x, y)) { if (tileMap[x, y] != null) { ret = tileMap[x, y]; } } return(ret); }
/// <summary> /// Tries to get a specific letter from the game manager /// If a letter is available (not on the board) the letters object is returned /// if not null /// </summary> /// <param name="letter">the letter to check if is available</param> /// <returns>the letters object is it is available else null</returns> public LetterGameLetter TryGetLetter(string letter) { LetterGameLetter ret = null; foreach (LetterGameLetter l in playerLetters[letter]) { if (!l.IsOnBoard) { ret = l; break; } } this.RefreshLetterCountDisplay(); return(ret); }
/// <summary> /// Tries to remove the provided letter from the board /// </summary> /// <param name="letter">the letter object to remove from the board</param> /// <returns>true if successful false if not</returns> private bool BoardTryRemoveLetter(LetterGameLetter letter) { bool suc = false; var foundLetter = WUArrays.MultiDimFind(tileMap, letter); if (foundLetter != null) { tileMap[letter.XPos, letter.YPos] = null; letter.IsOnBoard = false; letter.XPos = -1; letter.YPos = -1; this.RefreshLetterCountDisplay(); suc = true; } return(suc); }
/// <summary> /// Fills in the players letters /// </summary> /// <param name="playerDataDict"></param> private void FillPlayerLetters(Dictionary <string, int> playerDataDict) { if (playerDataDict == null) { throw new NullReferenceException("Letter dictionary is null"); } foreach (string key in playerDataDict.Keys) { if (playerLetters.Keys.Contains(key)) { for (int i = 0; i < playerDataDict[key]; i++) { int letterValue = letters[key]; LetterGameLetter newLetter = new LetterGameLetter(-1, -1, key, letterValue); playerLetters[key].Add(newLetter); } } } }
/// <summary> /// Set a tile on the board. if the tile is occupied the occupant wil be removed /// </summary> /// <param name="x">the x pos to place the letter</param> /// <param name="y">the y pos to place the letter</param> /// <param name="tile">the letter object to place</param> private void BoardSetTile(int x, int y, LetterGameLetter tile) { LetterGameLetter oldTile = this.BoardTryGetTile(x, y); if (oldTile != null) { // if there is a tile at the position remove it this.BoardTryRemoveLetter(oldTile); } // remove the tiles old position from the log if (tile.IsOnBoard) { tileMap[tile.XPos, tile.YPos] = null; } tile.XPos = x; tile.YPos = y; tile.IsOnBoard = true; tileMap[x, y] = tile; }
/// <summary> /// Updates the provided letters position to the provided coordinates /// </summary> /// <param name="newX">the new x pos for the letter</param> /// <param name="newY">the new x pos for the letter</param> /// <param name="letter">the letter object to update the cords of</param> public void UpdateLetterPos(int newX, int newY, LetterGameLetter letter) { if (IsBoardTileValid(newX, newY)) { // Add the tile to the board this.BoardSetTile(newX, newY, letter); } else { // Remove the letter from the board BoardTryRemoveLetter(letter); } wordsPoints = 0; this.ResetAllTilesOnBoard(); this.FindWordsInDimension(X_DIMENSION); this.FindWordsInDimension(Y_DIMENSION); this.RefreshLetterCountDisplay(); OnWordScoreUpdateEvent.Invoke(this, new WordScoreUpdateArgs { Score = this.wordsPoints }); }