Пример #1
0
        bool IMessageFilter.PreFilterMessage(ref Message m)
        {
            if (m.Msg == 0x0200)
            {
                highlightRight = null;
                highlightLeft  = null;

                var pt = PointToClient(Cursor.Position);
                if (!Bounds.Contains(pt))
                {
                    ReleaseMouseAll();
                }
                else
                {
                    var square = HitTestSquare(pt);
                    if (square != null)
                    {
                        highlightRight = board[GetRight(square)];
                        highlightLeft  = board[GetLeft(square)];
                    }
                }
            }
            else if (m.Msg == 0x101)
            {
                Rebuild();
            }
            return(false);
        }
Пример #2
0
        private void Rebuild()
        {
            var path = new Stack <ButtonSquare>();

            board = new ButtonSquare[numButtons];

            // pick a random position within the board
            int i = random.Next(numButtons);

            // assign a value to the button
            board[i] = new ButtonSquare
            {
                Index    = i,
                Value    = NextValue(),
                Selected = false,
            };

            path.Push(board[i]);

            int j, d, v;

            while (board.Any(b => b == null))
            {
                do
                {
                    j = random.Next(numButtons);
                }while (board[j] != null);

                var square = new ButtonSquare
                {
                    Index    = j,
                    Selected = false,
                };
                board[j] = square;

                path.Push(square);

                v = 0;
                d = (random.Next() % 2 == 0) ? -1 : 1;
                while (j != i)
                {
                    j = mod(j + d, numButtons);
                    ++v;
                }

                square.Value = v;
                i            = square.Index;
            }

            path.Peek().Selected = true;
            CalculateBounds();
        }
Пример #3
0
        private void ClickSquare(ButtonSquare square)
        {
            UnselectAll();
            square.Cleared = true;

            SelectSquare(GetLeft(square));
            SelectSquare(GetRight(square));

            if (!board.Any(b => b.Selected))
            {
                if (board.All(b => b.Cleared))
                {
                    MessageBox.Show(this, "You win! Press [Space] to start over");
                }
                else
                {
                    MessageBox.Show(this, "You LOSE! Press [Space] to start over");
                }
            }
        }
Пример #4
0
 private int GetLeft(ButtonSquare square)
 {
     return(mod(square.Index - square.Value, board.Length));
 }