示例#1
0
        private void GridButtonPressed(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            var    bPos = table_Grid.GetCellPosition(b);
            int    x = bPos.Column, y = bPos.Row;

            //System.Diagnostics.Debug.WriteLine(String.Format("Button pressed: ({0},{1})",x,y));

            // Logic for text. If the button was to be pressed to reach the solution, delete the text.
            if (b.Text == "1")
            {
                b.Text = "";
            }

            switch (mode)
            {
            case "Play":
                // Flip self and neighbours
                mainBoard.SimulatePress(x, y);
                // Remove the press indicator
                if (mainBoard.presses[x, y])
                {
                    mainBoard.presses[x, y] = false;
                }
                break;

            case "Set":
                // Flip only self
                mainBoard.FlipAtIndex(x, y);
                break;
            }

            PushBoard(mainBoard);
        }
示例#2
0
        private void ButtonPressed_Solve(object sender, EventArgs e)
        {
            // This method is called 'Chase the lights'

            // Virtual copy of the board to keep track of origional board state
            Board virtualBoard;

            mainBoard.ResetPresses();

            // This loops through the permutations of the possible move sets.
            // [0,0,0 , 0,0,0 , 0,0,0]...[1,1,1 , 1,1,1 , 1,1,1]

            int lightAmount = mainBoard.sizeX * mainBoard.sizeY;
            int permAmount  = Convert.ToInt32(Math.Pow(2, lightAmount));

            for (int permI = 0; permI < permAmount; permI++)
            {
                // Reset board
                virtualBoard = new Board(mainBoard.state);

                string line = Convert.ToString(permI, 2).PadLeft(lightAmount, '0');

                //System.Diagnostics.Debug.WriteLine("Trying: " + line);

                // For each char in the permutation
                char[] chars = line.ToArray();
                for (int i = 0; i < chars.Length; i++)
                {
                    int x = i % mainBoard.sizeX;
                    int y = i / 3;
                    //System.Diagnostics.Debug.WriteLine("("+x+","+y+")");
                    if (chars[i] == '1')
                    {
                        virtualBoard.SimulatePress(x, y);
                        //System.Diagnostics.Debug.WriteLine(virtualBoard.GetString());
                        //System.Threading.Thread.Sleep(1000);
                    }
                }

                // Determine if this is a solution
                bool allOff = true;
                foreach (bool b in virtualBoard.state)
                {
                    if (b)
                    {
                        allOff = false;
                        break;
                    }
                }

                // If permute was succesfull
                if (allOff)
                {
                    // Push presses to mainBoard
                    for (int i = 0; i < chars.Length; i++)
                    {
                        int x = i % mainBoard.sizeX;
                        int y = i / 3;

                        if (chars[i] == '1')
                        {
                            mainBoard.presses[x, y] = true;
                        }
                    }

                    // Push mainBoard
                    PushBoard(mainBoard);

                    // Stop
                    return;
                }
            }
            // No solution found
            NoSolutionPopupForm popup = new NoSolutionPopupForm();

            popup.ShowDialog();
            popup.Dispose();
        }