Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- namespace EESolver2015
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- ///
- class Light // Class for a single light, pretty straightforward
- {
- private int m_id;
- private bool m_state;
- private bool m_original;
- public Light(int ID, bool state)
- {
- m_id = ID;
- m_state = state;
- m_original = state;
- }
- public void Toggle()
- {
- m_state = !m_state;
- }
- public void Reset()
- {
- m_state = m_original;
- }
- public int GetID() { return m_id; }
- public bool GetState() { return m_state; }
- }
- class Button // Class for the buttons
- {
- List<int> m_lights = new List<int>();
- string m_name;
- public Button(TextBox box, string name)
- {
- m_name = name;
- string[] lightsString = box.Text.Split(',');
- for (int i = 0; i < lightsString.Length; ++i)
- m_lights.Add(int.Parse(lightsString[i]));
- }
- public void Push(Light[] lightArray)
- {
- for (int i = 0; i < m_lights.Count; ++i)
- lightArray[m_lights[i] - 1].Toggle();
- }
- public string GetName() { return m_name; }
- }
- public partial class MainWindow : Window
- {
- Light[] Lights = new Light[20];
- Button[] Buttons = new Button[7];
- List<List<int>> ButtonCombinations = new List<List<int>>();
- public MainWindow()
- {
- InitializeComponent();
- }
- void AddNext(int[] buttons, int[] uB, int add, int totalCount)
- {
- List<int> buttonsList = buttons.ToList();
- List<int> uBList = uB.ToList();
- buttonsList.Add(add);
- if(buttonsList.Count == totalCount) // If the combination has enough buttons, add it to the list
- {
- ButtonCombinations.Add(buttonsList);
- return;
- }
- // Else remove the button from list of usable buttons and move on
- uBList.Remove(add);
- for(int i = 0; i< uBList.Count; ++i)
- {
- AddNext(buttonsList.ToArray(), uBList.ToArray(), uBList[i], totalCount); //Work down the usable buttons list
- }
- }
- void FindCombinations()
- {
- List<int> uButtons = new List<int> { 1, 2, 3, 4, 5, 6, 7 }; // Buttons we can use
- List<int> buttons = new List<int>(); // Current combination
- for (int i = 1; i < 8; ++i) // How long the combination could be
- {
- for (int x = 1; x < 8; ++x) // Which button to add first
- {
- AddNext(buttons.ToArray(), uButtons.ToArray(), x, i);
- }
- }
- }
- void TryCombinations()
- {
- // Iterate all combinations
- for (int i = 0; i < ButtonCombinations.Count; ++i)
- {
- // Go through the combination and push the buttons
- List<int> Combination = ButtonCombinations[i];
- for (int x = 0; x < Combination.Count; ++x)
- {
- Buttons[Combination[x]-1].Push(Lights);
- }
- // Check if all the lights are on
- bool allTrue = true;
- for (int b = 0; b < 20; ++b)
- {
- if (!Lights[b].GetState())
- allTrue = false;
- }
- // If they are, show solution and return
- if (allTrue)
- {
- string buttonsString = "";
- for (int x = 0; x < Combination.Count; ++x)
- buttonsString += Buttons[Combination[x] - 1].GetName() + " ";
- MessageBox.Show("Found a solution!\nPress " + buttonsString);
- return;
- }
- // Else reset lights back to their original state and continue iteration
- for (int b = 0; b < 20; ++b)
- Lights[b].Reset();
- }
- }
- public void SolveIt()
- {
- // Create buttons
- Buttons[0] = new Button(TreeLightsBox, "Tree");
- Buttons[1] = new Button(PierLightsBox, "Pier");
- Buttons[2] = new Button(RockLightsBox, "Rock");
- Buttons[3] = new Button(WaterLightsBox, "Water");
- Buttons[4] = new Button(FurnaceLightsBox, "Furnace");
- Buttons[5] = new Button(TempleLightsBox, "Temple");
- Buttons[6] = new Button(ShrineLightsBox, "Shrine");
- // Set the starting state
- string[] startString = BeginStateBox.Text.Split(',');
- int[] startInt = new int[startString.Length];
- for(int i = 0; i< startString.Length; ++i)
- startInt[i] = int.Parse(startString[i]);
- for(int i = 0; i < 20; ++i)
- {
- if(startInt.Contains(i+1))
- Lights[i] = new Light(i + 1, true);
- else
- Lights[i] = new Light(i + 1, false);
- }
- FindCombinations(); //First find all possible button combinations
- TryCombinations(); //Then find the right one to get all the lights on
- }
- private void button_Click(object sender, RoutedEventArgs e)
- {
- SolveIt();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement