public void SetCharTileTest_SettingCharOnInvalidCoordinates_BoardTileIsNotPlaced_ReturnedObjectIsNull() { const int x = 1; const int y = 10; Board board = new(x, y); CharTile charTileToPlace = new('A'); CharTile placedCharTile = board.PlaceCharTile(99, 99, charTileToPlace); Assert.IsTrue(placedCharTile == null); }
public void SetCharTileTest_SettingCharOnValidCoordinates_BoardTileIsPopulatedWithChar_ReturnedObjectIsThePlacedCharTile() { const int x = 1; const int y = 10; Board board = new(x, y); CharTile charTileToPlace = new('A'); CharTile placedCharTile = board.PlaceCharTile(x, y, charTileToPlace); Assert.IsTrue(board.Tiles[x - 1][y - 1].CharTile.Letter == 'A'); Assert.IsTrue(placedCharTile == charTileToPlace); }
internal void UpdateCharTile(CharTile charTile) { int index = (charTile.Index & 0x7F) * 8; for (int i = 0; i < 8; i++) { fnt[i + index] = charTile.Glyph[i]; } CreateBitmapFromFnt(); pictureBox1.Invalidate();//refreshes the picturebox Dirty = true; }
public void NewCharTile_InitialisationWithLowerCaseChar_ThrowExceptionThatCharMustBeUppercase() { CharTile c; UnitTestGlobals.InvokeActionAndAssertThatCorrectExceptionMessageIsThrown(() => c = new CharTile('a'), ExceptionMessages.LetterCanOnlyBeBetweenAAndZ); }
public void NewCharTile_InitialisationWith0Score_ThrowExceptionThatScoreMustBeMoreThan0() { CharTile c; UnitTestGlobals.InvokeActionAndAssertThatCorrectExceptionMessageIsThrown(() => c = new CharTile('A', 0), ExceptionMessages.ScoreMustBeGreaterThan0); }
/// <summary> /// Builds all possible right parts for given anchor /// </summary> /// <param name="partialWord">Word preceding right part</param> /// <param name="anchor">Tile from which to build up right part</param> /// <param name="boardArray">Board to work with</param> /// <param name="validCrossChecks">List of valid cross checks for all empty tiles</param> /// <param name="validMovesList">List of valid moves that gets updated</param> /// <param name="boardIsHorizontal">Board is untransposed or transposed</param> /// <param name="boardBeforeMove">Board before any moves were generated</param> /// <param name="tileExtendedWith">Tile that was used to extend the right part</param> public void ExtendRight(string partialWord, int[] anchor, BoardTile[,] boardArray, Dictionary <BoardTile, List <CharTile> > validCrossChecks, HashSet <GeneratedMove> validMovesList, bool boardIsHorizontal, BoardTile[,] boardBeforeMove, CharTile tileExtendedWith = null) { //If no tile is present.. if (boardArray[anchor[0], anchor[1]].CharTile == null) { //If word up until current tile is valid.. if (Helper.CheckWordValidity(Dawg, partialWord)) { //If the move (word, row and column indexes) has not been added already.. if (!Moves.Any(m => m.Word.Equals(partialWord))) { //Adds generated move to list of valid moves Dictionary <BoardTile, CharTile> tilesUsed = new Dictionary <BoardTile, CharTile>(); //Adds tiles that were used in move for (int i = 0; i < partialWord.Length; i++) { var letter = partialWord[i]; var boardTile = boardArray[anchor[0], anchor[1] - partialWord.Length + i]; if (boardTile.CharTile != null) { tilesUsed.Add(boardTile, boardTile.CharTile); } else { tilesUsed.Add(boardTile, tileExtendedWith); } } validMovesList.Add(new GeneratedMove(boardIsHorizontal, anchor[1] - partialWord.Length, anchor[1] - 1, anchor, tilesUsed, boardBeforeMove, RackOfCurrentPlayer)); } } //GEts all letters that can follow current right part word HashSet <string> labelsOfDawgEdges = new HashSet <string>(new DawgEdgeEqualityComparer()); var wordsWithCommonPreffix = Dawg.MatchPrefix(partialWord); foreach (var word in wordsWithCommonPreffix) { labelsOfDawgEdges.Add(word.Key.Substring(partialWord.Length)); } foreach (var label in labelsOfDawgEdges) { //If the valid letter is in our rack and can be played on the board tile, places it and extends right again //with the newly filled board tile used as anchor if board limit is not reached if (label == "") { continue; } if (RackOfCurrentPlayer.CheckIfTileIsInRack(label[0], true) && (!validCrossChecks.Any(c => c.Key == boardArray[anchor[0], anchor[1]]) || validCrossChecks.Any(c => c.Key == boardArray[anchor[0], anchor[1]] && c.Value.Any(x => x.Letter == label[0])))) { CharTile tileToWorkWith = null; if (RackOfCurrentPlayer.CheckIfTileIsInRack(label[0], false)) { tileToWorkWith = Dictionary.CharTiles.Where(c => c.Letter == label[0] && c.Score != 0).FirstOrDefault(); } else { tileToWorkWith = Dictionary.CharTiles.Where(c => c.Letter == '*').FirstOrDefault(); } RackOfCurrentPlayer.SubstractFromRack(tileToWorkWith); boardArray[anchor[0], anchor[1]].CharTile = Dictionary.CharTiles.Where(c => c.Letter == label[0] && c.Score == tileToWorkWith.Score).FirstOrDefault(); if (anchor[1] < boardArray.GetLength(1) - 1) { ExtendRight(partialWord + label[0], new int[] { anchor[0], anchor[1] + 1 }, boardArray, validCrossChecks, validMovesList, boardIsHorizontal, boardBeforeMove, tileToWorkWith); } //Otherwise places the tile on the last empty square of the board, checks if its valid and doesn't attempt to extend anymore else { var finalWord = partialWord + boardArray[anchor[0], anchor[1]].CharTile.Letter; if (Helper.CheckWordValidity(Dawg, finalWord)) { if (!Moves.Any(m => m.Word.Equals(finalWord))) { Dictionary <BoardTile, CharTile> tilesUsed = new Dictionary <BoardTile, CharTile>(); for (int i = 0; i < finalWord.Length; i++) { var letter = finalWord[i]; var boardTile = boardArray[anchor[0], anchor[1] - finalWord.Length + 1 + i]; if (boardTile.CharTile != null) { tilesUsed.Add(boardTile, boardTile.CharTile); } else { tilesUsed.Add(boardTile, tileToWorkWith); } } validMovesList.Add(new GeneratedMove(boardIsHorizontal, anchor[1] - finalWord.Length + 1, anchor[1], anchor, tilesUsed, boardBeforeMove, RackOfCurrentPlayer)); } } } RackOfCurrentPlayer.AddToRack(tileToWorkWith); boardArray[anchor[0], anchor[1]].CharTile = null; } } } //Otherwise if the current tile is already taken and not empty, used letter from board tile to build to the right again else { var tile = boardArray[anchor[0], anchor[1]].CharTile; HashSet <string> labelsOfDawgEdges = new HashSet <string>(new DawgEdgeEqualityComparer()); var wordsWithCommonPreffix = Dawg.MatchPrefix(partialWord + tile.Letter); foreach (var word in wordsWithCommonPreffix) { labelsOfDawgEdges.Add(word.Key.Substring((partialWord + tile.Letter).Length)); } //Extends right if any letters can follow the current right part if (labelsOfDawgEdges.Any() && anchor[1] < boardArray.GetLength(1) - 1) { ExtendRight(partialWord + tile.Letter, new int[] { anchor[0], anchor[1] + 1 }, boardArray, validCrossChecks, validMovesList, boardIsHorizontal, boardBeforeMove, tile); } } }
/// <summary> /// Builds left part of word before anchor /// </summary> /// <param name="partialWord">Word preceding anchor. Might already be on the board or placed from previous LeftPart calls</param> /// <param name="limit">Limit of tiles we can place before anchor</param> /// <param name="anchor">Anchor from which to build left and right part</param> /// <param name="boardArray">Board array to work with (untransposed or transposed)</param> /// <param name="validCrossChecks">A list of crosschecks which say what tiles can be played in each board tile</param> /// <param name="validMovesList">A list of valid moves which is continuously updated</param> /// <param name="boardIsHorizontal">Board is untransposed or transposed</param> /// <param name="boardBeforeMove">Board before any moves were generated</param> public void LeftPart(string partialWord, int limit, int[] anchor, BoardTile[,] boardArray, Dictionary <BoardTile, List <CharTile> > validCrossChecks, HashSet <GeneratedMove> validMovesList, bool boardIsHorizontal, BoardTile[,] boardBeforeMove) { //Tries to build all right parts for given left part ExtendRight(partialWord, anchor, boardArray, validCrossChecks, validMovesList, boardIsHorizontal, boardBeforeMove); if (limit > 0) { //Gets a list of letters that can follow the current left part and can be placed to the left of the anchor HashSet <string> labelsOfDawgEdges = new HashSet <string>(new DawgEdgeEqualityComparer()); var wordsWithCommonPreffix = Dawg.MatchPrefix(partialWord); foreach (var word in wordsWithCommonPreffix) { labelsOfDawgEdges.Add(word.Key.Substring(partialWord.Length)); } foreach (var label in labelsOfDawgEdges) { if (label == "") { continue; } if (OriginalRackOfPlayer.CheckIfWordIsPlayable(partialWord + label[0]) && ((!validCrossChecks.Any(c => c.Key == boardArray[anchor[0], anchor[1] - 1]) || validCrossChecks.Any(c => c.Key == boardArray[anchor[0], anchor[1] - 1] && c.Value.Any(x => x.Letter == label[0]))))) { CharTile tileToWorkWith = null; if (RackOfCurrentPlayer.CheckIfTileIsInRack(label[0], false)) { tileToWorkWith = Dictionary.CharTiles.Where(c => c.Letter == label[0] && c.Score != 0).FirstOrDefault(); } else { tileToWorkWith = Dictionary.CharTiles.Where(c => c.Letter == '*').FirstOrDefault(); } //Rebuilds left part with new tiles for (int i = 0; i < partialWord.Length; i++) { boardArray[anchor[0], anchor[1] - 1 - i].CharTile = null; } for (int i = 0; i < partialWord.Length; i++) { var tileToAdd = OriginalRackOfPlayer.GetTile(partialWord[i]); boardArray[anchor[0], anchor[1] - partialWord.Length - 1 + i].CharTile = tileToAdd; } RackOfCurrentPlayer.SubstractFromRack(tileToWorkWith); boardArray[anchor[0], anchor[1] - 1].CharTile = tileToWorkWith; bool validPrefix = true; //Checks if the newly formed left part's components are valid for the board for (int i = 0; i < partialWord.Length; i++) { var boardTile = boardArray[anchor[0], anchor[1] - 1 - partialWord.Length + i]; if (!(!validCrossChecks.Any(c => c.Key == boardTile) || validCrossChecks.Any(c => c.Key == boardTile && c.Value.Any(x => x.Letter == partialWord[i])))) { validPrefix = false; break; } } //If they are valid, call LeftPart again, which will call ExtendRight for all possible right moves and eventually try a new left part if (validPrefix) { LeftPart(partialWord + label[0], limit - 1, anchor, boardArray, validCrossChecks, validMovesList, boardIsHorizontal, boardBeforeMove); } RackOfCurrentPlayer.AddToRack(tileToWorkWith); boardArray[anchor[0], anchor[1] - 1].CharTile = null; //Resets the left part once done for (int i = 0; i < partialWord.Length; i++) { boardArray[anchor[0], anchor[1] - partialWord.Length - 1 + i].CharTile = null; } } } } }
public TileSelectedEventArgs(byte cchar, List <byte> glyph) : base() { this.CharTile = new CharTile(cchar, new List <byte>(glyph).ToArray()); }