public PieceI() { units[0] = new PentominoePuzzleUnit(0, -1, -1, -1, -1, -1, 1, -1, -1); units[1] = new PentominoePuzzleUnit(1, 0, -1, -1, -1, -1, 2, -1, -1); units[2] = new PentominoePuzzleUnit(2, 1, -1, -1, -1, -1, 3, -1, -1); units[3] = new PentominoePuzzleUnit(3, 2, -1, -1, -1, -1, 4, -1, -1); units[4] = new PentominoePuzzleUnit(4, 3, -1, -1, -1, -1, -1, -1, -1); }
//TODO.. needs to ignore units already placed. need additional param private bool DoesUnitPieceCoverLocation(IPentominoePuzzlePiece piece, int unitNumber, int xIndex, int yIndex, HashSet <int> unitsPlaced) { PentominoeGameBoardLocation location; if (unitsPlaced != null && unitsPlaced.Count == 5) { return(true); } //ensure x,y locaiton on the game board and not covered. if (xIndex < 0 || yIndex < 0 || xIndex >= boardWidth || yIndex >= boardHeight) { Trace.WriteLine(piece.pieceName() + " unit " + unitNumber + "DOES NOT fit at location " + xIndex + " , " + yIndex + "OUT OF BOUNDS"); return(false); } location = gameBoard[xIndex, yIndex]; if (location.Covered) { Trace.WriteLine(piece.pieceName() + " unit " + unitNumber + "DOES NOT fit at location " + xIndex + " , " + yIndex + "ALREADY COVERED"); return(false); } if (unitsPlaced == null) { unitsPlaced = new HashSet <int>(); } unitsPlaced.Add(unitNumber); bool ret = true; PentominoePuzzleUnit unitSquare = piece.getUnits()[unitNumber]; foreach (AdjacentLocation loc in unitSquare.adjacentLocations) { if (loc.Covered && !unitsPlaced.Contains(loc.UnitNumber)) //not every adjacent location is covered { ret = DoesUnitPieceCoverLocation(piece, loc.UnitNumber, xIndex + loc.Xoffset, yIndex + loc.Yoffset, unitsPlaced); if (!ret) { break; } } } Trace.WriteLine(piece.pieceName() + " unit " + unitNumber + " fits " + ret + " at location " + xIndex + " , " + yIndex); return(ret); }