private void PlaceOneWord(string subword, int x, int y, int incX, int incY) { var isFirstLetter = true; if (x < _tmpminX) { _tmpminX = x; } if (y < _tmpminY) { _tmpminY = y; } foreach (var ltr in subword) { var ltrPlaced = new LtrPlaced() { nX = x, nY = y, ltr = ltr, IsHoriz = incX > 0 }; if (isFirstLetter) { _dictPlacedWords[subword] = ltrPlaced; isFirstLetter = false; } _ltrsPlaced.Add(ltrPlaced); _chars[x, y] = ltr; x += incX; y += incY; nLtrsPlaced++; } if (x > _tmpmaxX) { _tmpmaxX = x - incX; } if (y > _tmpmaxY) { _tmpmaxY = y - incY; } }
private bool TryPlaceWord(string subword, LtrPlaced ltrPlaced) { var didPlaceWord = false; var theChar = ltrPlaced.ltr; // if the cur ltr is part of a horiz word, then we'll try to go vert and vv var DoHoriz = !ltrPlaced.IsHoriz; var ndxAt = 0; while (true) { var at = subword.IndexOf(theChar, ndxAt); if (at < 0) { break; } int x0 = -1, y0 = -1, incx = 0, incy = 0; if (DoHoriz) { // if it fits on grid if (ltrPlaced.nX - at >= 0) { if (ltrPlaced.nX - at + subword.Length <= _MaxX) { // if the prior and post squares are empty if they exist if (ltrPlaced.nX - at == 0 || _chars[ltrPlaced.nX - at - 1, ltrPlaced.nY] == Blank) { if (ltrPlaced.nX - at + subword.Length == _MaxX || _chars[ltrPlaced.nX - at + subword.Length, ltrPlaced.nY] == Blank) { x0 = ltrPlaced.nX - at; y0 = ltrPlaced.nY; incx = 1; } } } } } else { if (ltrPlaced.nY - at >= 0) { if (ltrPlaced.nY - at + subword.Length <= _MaxY) { // if the prior and post squares are empty if they exist if (ltrPlaced.nY - at == 0 || _chars[ltrPlaced.nX, ltrPlaced.nY - at - 1] == Blank) { if (ltrPlaced.nY - at + subword.Length == _MaxY || _chars[ltrPlaced.nX, ltrPlaced.nY - at + subword.Length] == Blank) { x0 = ltrPlaced.nX; y0 = ltrPlaced.nY - at; incy = 1; } } } } } if (x0 >= 0) { var doesfit = true; int ndxc = 0; foreach (var chr in subword) { var val = _chars[x0 + incx * ndxc, y0 + incy * ndxc]; if (val != Blank && val != chr) // reject? { doesfit = false; break; } // if blank and the adjacent ones are not empty, we need to reject (brit xword) if (val == Blank) { if (DoHoriz) // incx>0 { if (y0 - 1 >= 0 && _chars[x0 + incx * ndxc, y0 - 1] != Blank) { doesfit = false; break; } if (y0 + 1 < _MaxY && _chars[x0 + incx * ndxc, y0 + 1] != Blank) { doesfit = false; break; } } else { // incy>0 if (x0 - 1 >= 0 && _chars[x0 - 1, y0 + incy * ndxc] != Blank) { doesfit = false; break; } if (x0 + 1 < _MaxX && _chars[x0 + 1, y0 + incy * ndxc] != Blank) { doesfit = false; break; } } } ndxc++; } if (doesfit) { PlaceOneWord(subword, x0, y0, incx, incy); //ndxc = 0; //foreach (var chr in subword) //{ // _chars[x0 + incx * ndxc, y0 + incy * ndxc] = chr; // ndxc++; //} didPlaceWord = true; } } ndxAt = at + 1; } return(didPlaceWord); }