Advertisement
NPSF3000

LightGameSolver

Jul 13th, 2011
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.99 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace LightsGameSolver
  7. {
  8.     class Program
  9.     {
  10.         struct Step
  11.         {
  12.             public byte x;
  13.             public byte y;
  14.             public Step(byte x, byte y)
  15.             {
  16.                 this.x = x;
  17.                 this.y = y;
  18.             }
  19.         }
  20.  
  21.         static Random rnd = new Random();
  22.  
  23.         static void Main(string[] args)
  24.         {
  25.  
  26.             Console.WriteLine("Creating Lights");
  27.  
  28.             bool[,] lights = new bool[3 + rnd.Next(3), 3 + rnd.Next(3)];
  29.  
  30.             for (int x = 0; x < lights.GetLength(0); x++)
  31.             {
  32.                 Console.WriteLine();
  33.                 for (int y = 0; y < lights.GetLength(1); y++)
  34.                 {
  35.                     lights[x, y] = rnd.Next(2) == 0;
  36.                     if (lights[x, y]) Console.Write("X"); else Console.Write("O");
  37.                 }
  38.             }
  39.  
  40.             Step[] steps = Solve(ref lights);
  41.  
  42.             Console.WriteLine(); Console.WriteLine();
  43.  
  44.             Console.WriteLine("Solved in " + steps.Length + " steps:");
  45.  
  46.             for (int x = 0; x < lights.GetLength(0); x++)
  47.             {
  48.                 Console.WriteLine();
  49.                 for (int y = 0; y < lights.GetLength(1); y++)
  50.                 {
  51.                     if (lights[x, y]) Console.Write("X"); else Console.Write("O");
  52.                 }
  53.             }
  54.             Console.WriteLine(); Console.WriteLine();
  55.             for (int i = 0; i < steps.Length; i++)
  56.             {
  57.                 Console.WriteLine("Step " + (i + 1) + ": " + steps[i].x + " , " + steps[i].y);
  58.                 Console.ReadLine();
  59.             }
  60.  
  61.             Console.ReadLine();
  62.         }
  63.  
  64.         static Step[] Solve(ref bool[,] lights)
  65.         {
  66.             List<Step> steps = new List<Step>();
  67.  
  68.             while (!Solved(lights))
  69.             {
  70.                 var x = rnd.Next(lights.GetLength(0));
  71.                 var y = rnd.Next(lights.GetLength(1));
  72.                 steps.Add(new Step((byte)x, (byte)y));
  73.  
  74.                 lights[x, y] = !lights[x, y];
  75.  
  76.                 if (x - 1 > 0) lights[x - 1, y] = !lights[x - 1, y];
  77.                 if (y - 1 > 0) lights[x, y - 1] = !lights[x, y - 1];
  78.                 if (x + 1 < lights.GetLength(0)) lights[x + 1, y] = !lights[x + 1, y];
  79.                 if (y + 1 < lights.GetLength(1)) lights[x, y + 1] = !lights[x, y + 1];
  80.  
  81.                 if (steps.Count % 100000 == 0) Console.WriteLine("Solver: Maybe...");
  82.                 if (steps.Count == 10000000)
  83.                 {
  84.                     Console.WriteLine("Solver: Probably Not.");
  85.                     Console.ReadLine();
  86.                     Environment.Exit(42);
  87.                 }
  88.             }
  89.             return steps.ToArray();
  90.         }
  91.  
  92.         static bool Solved(bool[,] lights)
  93.         {
  94.             foreach (var light in lights)
  95.                 if (light) return false;
  96.             return true;
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement