void CheckHitBlock(object sender, MouseButtonEventArgs e, bool needPopBlock = true) { Canvas gameCanvas = UIHelper.FindVisualParent <Canvas>(sender as DependencyObject); //InitializeGame(); Point mousepos = e.GetPosition(gameCanvas); HitTestResult result = VisualTreeHelper.HitTest(gameCanvas, mousepos); GeneralTransform generalTransform1 = ((UIElement)(result.VisualHit)).TransformToAncestor(gameCanvas); Point currentPoint = generalTransform1.Transform(new Point(0, 0)); double left = currentPoint.X; double top = currentPoint.Y; uint row; uint col; LocateBlock(top, left, out row, out col); if (needPopBlock) { PopBlock(row, col); } else { SingleBlock item = FindBlock(row, col); MessageBox.Show("R" + item.Rowpos.ToString() + " C" + item.Columnpos.ToString() + " " + item.IsSelected.ToString() + " Color" + ((int)(item.BlockColor)).ToString()); } }
private void AssembleSiblings(SingleBlock block) { checkedlist.Clear(); CommonTypes.BlockColor checkcolor = block.BlockColor; // left: row, col - 1 // right: row, col + 1 // up: row+1, col //down: row-1, col if (openlist.Count != 0) { for (int i = 0; i < openlist.Count; i++) { SingleBlock item = openlist[i]; uint row = item.Rowpos; uint col = item.Columnpos; if (!checkedlist.Contains(item)) { var foundblock = FindBlock(row, col - 1); if (foundblock != null && !checkedlist.Contains(foundblock) && !openlist.Contains(foundblock) && foundblock.BlockColor == checkcolor) { openlist.Add(foundblock); } foundblock = FindBlock(row, col + 1); if (foundblock != null && !checkedlist.Contains(foundblock) && !openlist.Contains(foundblock) && foundblock.BlockColor == checkcolor) { openlist.Add(foundblock); } foundblock = FindBlock(row + 1, col); if (foundblock != null && !checkedlist.Contains(foundblock) && !openlist.Contains(foundblock) && foundblock.BlockColor == checkcolor) { openlist.Add(foundblock); } foundblock = FindBlock(row - 1, col); if (foundblock != null && !checkedlist.Contains(foundblock) && !openlist.Contains(foundblock) && foundblock.BlockColor == checkcolor) { openlist.Add(foundblock); } checkedlist.Add(item); } } } }
void PopBlock(uint row, uint col) { SingleBlock block = FindBlock(row, col); bool isSelectionMode = block.IsSelected; if (!isSelectionMode) { if (openlist.Count == 0) { openlist.Add(block); AssembleSiblings(block); } else { foreach (var item in openlist) { item.IsSelected = !item.IsSelected; } openlist.Clear(); openlist.Add(block); AssembleSiblings(block); } } if (openlist.Count > 1) { foreach (var item in openlist) { item.IsSelected = !item.IsSelected; } Refresh(); } else//selected single block { openlist.Clear(); } if (isSelectionMode) { openlist.Clear(); } }
public void LoadBlocks() { ObservableCollection <SingleBlock> blocks = new ObservableCollection <SingleBlock>(); long tick = DateTime.Now.Ticks; //Random ran = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32)); Random ran = new Random(); for (uint row = 0; row < CommonTypes.TOTALROWS; row++) { for (uint col = 0; col < CommonTypes.TOTALCOLUMNS; col++) { SingleBlock newBlock = new SingleBlock(); newBlock.Rowpos = row + 1; newBlock.Columnpos = col + 1; newBlock.BlockColor = (CommonTypes.BlockColor)ran.Next((int)CommonTypes.BlockColor.BlockColorRed, (int)CommonTypes.BlockColor.BlockColorPurple + 1); blocks.Add(newBlock); } } this.JudasBlocks = blocks; }