private int recursiveGetSpread(List <Cell> curWordList, RowColPair dir, int lastScore, int spreadSum) { if (curWordList.Count == 1) { return(spreadSum); } spreadSum += getSpreadForLetterPair(curWordList[0], curWordList[1], ref dir, ref lastScore); curWordList.RemoveAt(0); return(recursiveGetSpread(curWordList, dir, lastScore, spreadSum)); }
private RowColPair getNextCell(RowColPair cell, string word) { int count = 0; while (++count < 50) { RowColPair newCell = getRandomAdjacentCell(cell); if (cellIsValid(newCell, word)) { return(newCell); } } return(null); }
internal bool insertWord(string word) { //start with a random location RowColPair cell = new RowColPair(Utils.RandomNumber(0, dim), Utils.RandomNumber(0, dim)); //we're building the level from the final state to the initial state, so iterate over the letters in reverse order for (int i = word.Length - 1; i >= 0; --i) { cell = getNextCell(cell, word); //Got into a bad state, so bail and start over! if (cell == null) { return(false); } insertCharacter(word[i], word, cell); } return(true); }
private void insertCharacter(char ch, string word, RowColPair cell) { if (board[cell.Row, cell.Col] != null) { //raise the characters to make room int curRow = dim - 1; while (curRow != cell.Row) { if (board[curRow, cell.Col] == null && board[curRow - 1, cell.Col] != null) { board[curRow, cell.Col] = board[curRow - 1, cell.Col]; board[curRow - 1, cell.Col] = null; } --curRow; } } board[cell.Row, cell.Col] = new Cell() { c = ch.ToString(), Word = word, h = hintIndex-- }; }
private int getSpreadForLetterPair(Cell cell1, Cell cell2, ref RowColPair dir, ref int lastScore) { int rowDiff = cell1.loc.Row - cell2.loc.Row; int colDiff = cell1.loc.Col - cell2.loc.Col; RowColPair newDir = new RowColPair(rowDiff, colDiff); bool isDiag = Math.Abs(newDir.Col) == 1 && Math.Abs(newDir.Row) == 1; if (dir == null) { lastScore = (isDiag ? 5 : 1); dir = newDir; } else { if (dir.equals(newDir)) { lastScore--; } } return(lastScore); }
//returns 1 of 9 possible locations private RowColPair getRandomAdjacentCell(RowColPair cell) { int pos = Utils.RandomNumber(0, 9); switch (pos) { case 0: return(new RowColPair(cell.Row - 1, cell.Col - 1)); case 1: return(new RowColPair(cell.Row - 1, cell.Col)); case 2: return(new RowColPair(cell.Row - 1, cell.Col + 1)); case 3: return(new RowColPair(cell.Row, cell.Col - 1)); case 4: return(new RowColPair(cell.Row, cell.Col)); case 5: return(new RowColPair(cell.Row, cell.Col + 1)); case 6: return(new RowColPair(cell.Row + 1, cell.Col - 1)); case 7: return(new RowColPair(cell.Row + 1, cell.Col)); case 8: return(new RowColPair(cell.Row + 1, cell.Col + 1)); default: throw new Exception("Should not get here!"); } }
private bool cellIsValid(RowColPair cell, string word) { //check it's inside grid if (cell.Row >= dim || cell.Row < 0 || cell.Col >= dim || cell.Col < 0) { return(false); } //check there is no null space immediately below the cell // -- you can only build up on top of other blocks if (cell.Row > 0 && board[cell.Row - 1, cell.Col] == null) { return(false); } //can push up ONLY if there is space - if top space is null, then there's room. If not, return false if (board[dim - 1, cell.Col] != null) { return(false); } //AND if no Cells above contain the current word (or it will be unsolvable!) for (int row = dim - 1; row >= cell.Row; --row) { //null cells are good for business, move along if (board[row, cell.Col] == null) { continue; } //not valid! if (board[row, cell.Col].Word.Equals(word)) { return(false); } } return(true); }
internal bool equals(RowColPair p) { return(Row == p.Row && Col == p.Col); }