private bool IsValidMove(GameTile nextMove, List<GameTile> gameTiles, List<GameTile> currentTiles, out string errorMessage) { errorMessage = string.Empty; if (gameTiles.Count == 0 && nextMove.Row == 8 && nextMove.Col == 8) { return true; } var foundTile = gameTiles.Find(gt => gt.Row == nextMove.Row && gt.Col == nextMove.Col); if (foundTile != null) { errorMessage = "Invalid move! Tile cant be placed on some other tile."; } var verTiles = GetRowTiles(true, nextMove.Row, nextMove.Col, gameTiles); var horTiles = GetRowTiles(false, nextMove.Row, nextMove.Col, gameTiles); if (horTiles.Count == 0 && verTiles.Count == 0) { errorMessage = "Invalid Move! Tile should be placed near some other tile."; return false; } if (horTiles.Exists( t => string.Equals(t.Tile, nextMove.Tile))) { errorMessage = "Invalid move! Same tile exist in the horizontal row."; return false; } if (verTiles.Exists(t => string.Equals(t.Tile, nextMove.Tile))) { errorMessage = "Invalid move! Same tile exist in the verticle row."; return false; } var isValidRow = IsValidTiles(horTiles, nextMove.Tile); var isValidCol = IsValidTiles(verTiles, nextMove.Tile); if (!isValidRow || !isValidCol) { errorMessage = "Invalid move! " + (isValidCol ? "Horizontal" : "Verticle") + " tiles doesnt match."; return false; } var inSameRow = InSameLine("hor", nextMove.Row, currentTiles); var inSameCol = InSameLine("ver", nextMove.Col, currentTiles); if (!inSameRow && !inSameCol) { errorMessage = "Invalid Move direction!"; return false; } foreach (var currentTile in currentTiles) { if (horTiles.FindIndex(h => string.Equals(h.Tile, currentTile.Tile) && h.Row == currentTile.Row && h.Col == currentTile.Col) == -1 && verTiles.FindIndex(v => string.Equals(v.Tile, currentTile.Tile) && v.Row == currentTile.Row && v.Col == currentTile.Col) == -1) { errorMessage = "Invalid Move! Tile should be placed near other tiles you placed in this move"; return false; } } return true; }
private GameTile GetValidEmptyTile(List<GameTile> gameTiles, int row, int col, string tile) { GameTile nextMove = null; if (row < 17 && row >=0 && col < 17 && col >=0) { nextMove = new GameTile() { Row = row, Col = col, Tile = tile }; string errorMessage; if (IsValidMove(nextMove, gameTiles, new List<GameTile>(), out errorMessage)) { return nextMove; } } return nextMove; }
private List<GameTile> GetEmptyTilePositions(List<GameTile> gameTiles, GameTile matchingGameTile, string tile) { //Empty and valid move List<GameTile> emptyPositions = new List<GameTile>(); GameTile nextMove = GetValidEmptyTile(gameTiles, matchingGameTile.Row + 1, matchingGameTile.Col, tile); if(nextMove != null) emptyPositions.Add(nextMove); nextMove = GetValidEmptyTile(gameTiles, matchingGameTile.Row - 1, matchingGameTile.Col, tile); if (nextMove != null) emptyPositions.Add(nextMove); nextMove = GetValidEmptyTile(gameTiles, matchingGameTile.Row, matchingGameTile.Col + 1, tile); if (nextMove != null) emptyPositions.Add(nextMove); nextMove = GetValidEmptyTile(gameTiles, matchingGameTile.Row, matchingGameTile.Col - 1, tile); if (nextMove != null) emptyPositions.Add(nextMove); return emptyPositions; }
private void GetMaxPoints(string direction, List<GameTile> gameTiles, string tile, List<string> myTiles, GameTile emptyPosition, GameTile selectedMove, ref int maxPoints, ref List<GameTile> nextMoves) { int points = 0; var currentMoves = new List<GameTile>(); List<List<string>> tileCombinations = GetMytileCombinations(tile,myTiles); foreach (var tileCombination in tileCombinations) { points = 0; for (int i = 0; i < tileCombination.Count; i++) { var nextMove = new GameTile() { Row = emptyPosition.Row, Col = emptyPosition.Col, Tile = tileCombination[i]}; if (direction == "-x") nextMove.Col = nextMove.Col - i; if (direction == "+x") nextMove.Col = nextMove.Col + i; if (direction == "-y") nextMove.Row = nextMove.Row - i; if (direction == "+y") nextMove.Row = nextMove.Row - i; string errorMessage = string.Empty; if (nextMove.Row <= 16 && nextMove.Row >-1 && nextMove.Col <=16 && nextMove.Col >-1 && IsValidMove(nextMove, gameTiles, currentMoves, out errorMessage)) { points = points + CalculatePoints(gameTiles, nextMove, currentMoves); } else { break; } } if (points > maxPoints) { maxPoints = points; nextMoves = currentMoves; } } }
private int CalculatePoints(List<GameTile> gameTiles, GameTile nextMove, List<GameTile> currentMoves) { currentMoves.Add(nextMove); if (nextMove.Row == 8 && nextMove.Col == 8) return 1; int points = 0; var isHor = InSameLine("hor", nextMove.Row, currentMoves); var tiles = GetRowTiles(isHor, nextMove.Row, nextMove.Col, gameTiles); points = points + (tiles.Count == 0 ? 0 : tiles.Count + 1); var bonus = points == 6 ? 6 : 0; foreach (var currentMove in currentMoves) { tiles = GetRowTiles(!isHor, currentMove.Row, currentMove.Col, gameTiles); points = points + (tiles.Count == 0 ? 0 : tiles.Count + 1); if ((tiles.Count + 1)==6) { bonus = bonus + 6; } } points = points + bonus; return points; }