public override void Init (object initData) { Puzzle = initData as Puzzle; if (Puzzle == null) { //Restore saved grid LetterGrid = _storageService.LoadPuzzle (); if (LetterGrid == null) {// Load failed, return random instead Random rnd = new Random (); int rndPuzzle = rnd.Next (_databaseService.GetPuzzles ().Count); Puzzle = _databaseService.GetPuzzles () [rndPuzzle]; LetterGrid = new LetterGrid (_databaseService.GetWords (Puzzle)); } else { Puzzle = _databaseService.GetPuzzle (LetterGrid.PuzzleName); } } else { LetterGrid = new LetterGrid (_databaseService.GetWords (Puzzle)); } Words = new ObservableCollection<Word> (LetterGrid.AssembledWords); LetterGrid.PuzzleName = Puzzle.Name; PuzzleName = Puzzle.Name.ToUpper (); SelectedLetters = ""; List<Word> Wordlist = (from w in Words orderby w.Text orderby w.Text.Replace(" ", String.Empty).Length select w).ToList(); Word GroupWord = new Word { Found = true }; int columnLength = 16; int curRow = 0; int curColumn = 0; int curLength = 0; foreach (Word word in Wordlist) { if (word.Text.Replace(" ", String.Empty).Length > curLength) { if (curRow >= columnLength - 3) { curColumn++; curRow = 0; } else if (curRow > 0) { Label newBlank = new Label (); newBlank.Text = " "; newBlank.FontSize = 18; newBlank.VerticalOptions = LayoutOptions.Center; newBlank.FontAttributes = FontAttributes.Bold; CurrentPage.FindByName<Grid> ("Wordgrid").Children.Add (newBlank, curColumn, curRow); curRow++; } curLength = word.Text.Replace(" ", String.Empty).Length; Label newGroup = new Label (); newGroup.Text = curLength.ToString() + " LETTERS:"; newGroup.FontSize = 14; newGroup.VerticalOptions = LayoutOptions.Center; newGroup.FontAttributes = FontAttributes.Bold; newGroup.SetBinding (Label.TextColorProperty, new Binding("Found", BindingMode.Default, new BooleanStyleConverter(), CurrentPage.FindByName<ResourceDictionary>("BackgroundColors") )); newGroup.BindingContext = GroupWord; CurrentPage.FindByName<Grid> ("Wordgrid").Children.Add (newGroup, curColumn, curRow); curRow++; } else if (curRow >= columnLength) { curColumn++; curRow = 0; } Label newWord = new Label (); newWord.Text = word.Text; newWord.FontSize = 18; newWord.VerticalOptions = LayoutOptions.Center; newWord.FontAttributes = FontAttributes.Bold; newWord.SetBinding (Label.TextColorProperty, new Binding("Found", BindingMode.Default, new BooleanStyleConverter(), CurrentPage.FindByName<ResourceDictionary>("BackgroundColors") )); newWord.BindingContext = word; CurrentPage.FindByName<Grid> ("Wordgrid").Children.Add (newWord, curColumn, curRow); curRow++; } foreach (Letter l in LetterGrid.Letters) { Label newLetter = new Label (); newLetter.FontSize = 18; newLetter.FontAttributes = FontAttributes.Bold; newLetter.VerticalOptions = LayoutOptions.Center; newLetter.HorizontalOptions = LayoutOptions.Center; newLetter.Text = l.Text; newLetter.SetBinding (Label.TextColorProperty, new Binding("Found", BindingMode.Default, new BooleanStyleConverter(), CurrentPage.FindByName<ResourceDictionary>("BackgroundColors") )); newLetter.BindingContext = l; CurrentPage.FindByName<Grid> ("Lettergrid").Children.Add (newLetter, l.x, l.y); } Highlight1 = new LetterHighlight(); Highlight2 = new LetterHighlight(); Highlight3 = new LetterHighlight(); Highlight4 = new LetterHighlight(); Highlight5 = new LetterHighlight(); Highlight6 = new LetterHighlight(); Highlight7 = new LetterHighlight(); Highlight8 = new LetterHighlight(); Highlight9 = new LetterHighlight(); Highlight10 = new LetterHighlight(); Highlight11 = new LetterHighlight(); Highlight12 = new LetterHighlight(); Highlights.Add (Highlight1); Highlights.Add (Highlight2); Highlights.Add (Highlight3); Highlights.Add (Highlight4); Highlights.Add (Highlight5); Highlights.Add (Highlight6); Highlights.Add (Highlight7); Highlights.Add (Highlight8); Highlights.Add (Highlight9); Highlights.Add (Highlight10); Highlights.Add (Highlight11); Highlights.Add (Highlight12); }
private void AssembleWord(int x, int y, Direction dir, int offset, Word NewWord) { switch (dir) { case Direction.Right: x = x + offset; break; case Direction.RightDown: x = x + offset; y = y + offset; break; case Direction.Down: y = y + offset; break; case Direction.LeftDown: x = x - offset; y = y + offset; break; case Direction.Left: x = x - offset; break; case Direction.LeftUp: x = x - offset; y = y - offset; break; case Direction.Up: y = y - offset; break; case Direction.RightUp: x = x + offset; y = y - offset; break; } NewWord.x1 = x; NewWord.y1 = y; foreach (char c in NewWord.Text.Replace(" ", String.Empty)) { if (Letters.Exists(l => l.x == x && l.y == y)) { Letter letter = Letters.Where(l => l.x == x && l.y == y).First(); letter.Text = c.ToString(); } else { Letter letter = new Letter(); letter.x = x; letter.y = y; letter.Text = c.ToString(); Letters.Add(letter); } NewWord.x2 = x; NewWord.y2 = y; switch (dir) { case Direction.Right: x++; break; case Direction.RightDown: x++; y++; break; case Direction.Down: y++; break; case Direction.LeftDown: x--; y++; break; case Direction.Left: x--; break; case Direction.LeftUp: x--; y--; break; case Direction.Up: y--; break; case Direction.RightUp: x++; y--; break; } } }