Advertisement
Hattiwatti

EESolver2015

Dec 21st, 2015
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15.  
  16. namespace EESolver2015
  17. {
  18.     /// <summary>
  19.     /// Interaction logic for MainWindow.xaml
  20.     /// </summary>
  21.     ///
  22.  
  23.     class Light // Class for a single light, pretty straightforward
  24.     {
  25.         private int m_id;
  26.         private bool m_state;
  27.         private bool m_original;
  28.  
  29.         public Light(int ID, bool state)
  30.         {
  31.             m_id = ID;
  32.             m_state = state;
  33.             m_original = state;
  34.         }
  35.  
  36.         public void Toggle()
  37.         {
  38.             m_state = !m_state;
  39.         }
  40.  
  41.         public void Reset()
  42.         {
  43.             m_state = m_original;
  44.         }
  45.  
  46.         public int GetID() { return m_id; }
  47.         public bool GetState() { return m_state; }
  48.     }
  49.  
  50.     class Button // Class for the buttons
  51.     {
  52.         List<int> m_lights = new List<int>();
  53.         string m_name;
  54.  
  55.         public Button(TextBox box, string name)
  56.         {
  57.             m_name = name;
  58.             string[] lightsString = box.Text.Split(',');
  59.             for (int i = 0; i < lightsString.Length; ++i)
  60.                 m_lights.Add(int.Parse(lightsString[i]));
  61.         }
  62.  
  63.         public void Push(Light[] lightArray)
  64.         {
  65.             for (int i = 0; i < m_lights.Count; ++i)
  66.                 lightArray[m_lights[i] - 1].Toggle();
  67.         }
  68.  
  69.         public string GetName() { return m_name; }
  70.     }
  71.  
  72.     public partial class MainWindow : Window
  73.     {
  74.         Light[] Lights = new Light[20];
  75.         Button[] Buttons = new Button[7];
  76.         List<List<int>> ButtonCombinations = new List<List<int>>();
  77.         public MainWindow()
  78.         {
  79.             InitializeComponent();
  80.         }
  81.  
  82.         void AddNext(int[] buttons, int[] uB, int add, int totalCount)
  83.         {
  84.             List<int> buttonsList = buttons.ToList();
  85.             List<int> uBList = uB.ToList();
  86.  
  87.             buttonsList.Add(add);
  88.             if(buttonsList.Count == totalCount) // If the combination has enough buttons, add it to the list
  89.             {
  90.                 ButtonCombinations.Add(buttonsList);
  91.                 return;
  92.             }
  93.  
  94.             // Else remove the button from list of usable buttons and move on
  95.             uBList.Remove(add);
  96.             for(int i = 0; i< uBList.Count; ++i)
  97.             {
  98.                 AddNext(buttonsList.ToArray(), uBList.ToArray(), uBList[i], totalCount); //Work down the usable buttons list
  99.             }
  100.         }
  101.  
  102.         void FindCombinations()
  103.         {
  104.             List<int> uButtons = new List<int> { 1, 2, 3, 4, 5, 6, 7 }; // Buttons we can use
  105.             List<int> buttons = new List<int>();    // Current combination
  106.  
  107.             for (int i = 1; i < 8; ++i) // How long the combination could be
  108.             {
  109.                 for (int x = 1; x < 8; ++x) // Which button to add first
  110.                 {
  111.                     AddNext(buttons.ToArray(), uButtons.ToArray(), x, i);
  112.                 }
  113.             }
  114.         }
  115.  
  116.         void TryCombinations()
  117.         {
  118.             // Iterate all combinations
  119.             for (int i = 0; i < ButtonCombinations.Count; ++i)
  120.             {
  121.                 // Go through the combination and push the buttons
  122.                 List<int> Combination = ButtonCombinations[i];
  123.                 for (int x = 0; x < Combination.Count; ++x)
  124.                 {
  125.                     Buttons[Combination[x]-1].Push(Lights);
  126.                 }
  127.  
  128.                 // Check if all the lights are on
  129.                 bool allTrue = true;
  130.                 for (int b = 0; b < 20; ++b)
  131.                 {
  132.                     if (!Lights[b].GetState())
  133.                         allTrue = false;
  134.                 }
  135.  
  136.                 // If they are, show solution and return
  137.                 if (allTrue)
  138.                 {
  139.                     string buttonsString = "";
  140.                     for (int x = 0; x < Combination.Count; ++x)
  141.                         buttonsString += Buttons[Combination[x] - 1].GetName() + " ";
  142.                     MessageBox.Show("Found a solution!\nPress " + buttonsString);
  143.                     return;
  144.                 }
  145.  
  146.                 // Else reset lights back to their original state and continue iteration
  147.                 for (int b = 0; b < 20; ++b)
  148.                     Lights[b].Reset();
  149.             }
  150.         }
  151.  
  152.         public void SolveIt()
  153.         {
  154.  
  155.             // Create buttons
  156.             Buttons[0] = new Button(TreeLightsBox, "Tree");
  157.             Buttons[1] = new Button(PierLightsBox, "Pier");
  158.             Buttons[2] = new Button(RockLightsBox, "Rock");
  159.             Buttons[3] = new Button(WaterLightsBox, "Water");
  160.             Buttons[4] = new Button(FurnaceLightsBox, "Furnace");
  161.             Buttons[5] = new Button(TempleLightsBox, "Temple");
  162.             Buttons[6] = new Button(ShrineLightsBox, "Shrine");
  163.  
  164.  
  165.             // Set the starting state
  166.             string[] startString = BeginStateBox.Text.Split(',');
  167.             int[] startInt = new int[startString.Length];
  168.  
  169.             for(int i = 0; i< startString.Length; ++i)
  170.                 startInt[i] = int.Parse(startString[i]);
  171.  
  172.             for(int i = 0; i < 20; ++i)
  173.             {
  174.                 if(startInt.Contains(i+1))
  175.                     Lights[i] = new Light(i + 1, true);
  176.                 else
  177.                     Lights[i] = new Light(i + 1, false);
  178.             }
  179.  
  180.             FindCombinations(); //First find all possible button combinations
  181.             TryCombinations(); //Then find the right one to get all the lights on
  182.         }
  183.  
  184.         private void button_Click(object sender, RoutedEventArgs e)
  185.         {
  186.             SolveIt();
  187.         }
  188.  
  189.     }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement