Advertisement
piotrek77

TajemniczeSygnalyClass.cs

Oct 8th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.53 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 LightBulbs;
  7.  
  8.  
  9.  
  10. namespace TAJEMNICZE_SYGNALY
  11. {
  12.     public class TajemniczeSygnalyClass:ILightBulbs
  13.     {
  14.  
  15.  
  16.         public new static int boardSize;
  17.         private readonly static Boolean trybTestowy = false;
  18.  
  19.  
  20.         private void writeLn(string t)
  21.         {
  22.             if (trybTestowy)
  23.             {
  24.                 Console.WriteLine(t);
  25.             }
  26.         }
  27.  
  28.         private void wyswietl(bool[,] t)
  29.         {
  30.             if (trybTestowy)
  31.             {
  32.                 Console.WriteLine();
  33.                 for (int i = 0; i < boardSize; i++)
  34.                 {
  35.                     for (int j = 0; j < boardSize; j++)
  36.                     {
  37.                         Console.Write(t[i, j] ? "#" : ".");
  38.                     }
  39.                     Console.WriteLine();
  40.                 }
  41.                 Console.WriteLine();
  42.             }
  43.         }
  44.  
  45.  
  46.  
  47.         private TajemniczeSygnalyClass()
  48.         {
  49.         }
  50.  
  51.         public new static ILightBulbs GetInstance()
  52.         {
  53.             if (lightBulbsInstance == null)
  54.             {
  55.                 lightBulbsInstance = new TajemniczeSygnalyClass();
  56.             }
  57.             return lightBulbsInstance;
  58.         }
  59.  
  60.  
  61.  
  62.  
  63.         private int ileZapalonychSasiadow(bool[,] t, int x, int y)
  64.         {
  65.             int ilosc = 0;
  66.  
  67.             //L-U
  68.             if ((x != 0) && (y!=0))
  69.             {
  70.                 ilosc += t[x - 1, y - 1] ? 1 : 0;
  71.             }
  72.  
  73.             //U
  74.             if (y != 0)
  75.             {
  76.                 ilosc += t[x, y - 1] ? 1 : 0;
  77.             }
  78.  
  79.             //U-R
  80.             if ((y != 0) && (x < boardSize - 1))
  81.             {
  82.                 ilosc += t[x + 1, y - 1] ? 1 : 0;
  83.             }
  84.  
  85.             //R
  86.             if (x < boardSize - 1)
  87.             {
  88.                 ilosc += t[x + 1, y] ? 1 : 0;
  89.             }
  90.  
  91.  
  92.             //D-R
  93.             if ((x < boardSize - 1) && (y < boardSize - 1))
  94.             {
  95.                 ilosc += t[x + 1, y + 1] ? 1 : 0;
  96.             }
  97.  
  98.             //D
  99.             if (y < boardSize - 1)
  100.             {
  101.                 ilosc += t[x, y + 1] ? 1 : 0;
  102.             }
  103.  
  104.             //D-L
  105.             if ((y < boardSize - 1) && (x != 0))
  106.             {
  107.                 ilosc += t[x - 1, y + 1] ? 1 : 0;
  108.             }
  109.  
  110.             //L
  111.             if (x != 0)
  112.             {
  113.                 ilosc += t[x-1,y]?1:0;
  114.             }
  115.  
  116.             return ilosc;
  117.         }
  118.  
  119.         public override int CountLightsOn(bool[,] lightsBoard, int s)
  120.         {
  121.  
  122.             boardSize = lightsBoard.GetLength(0);
  123.             int secondSize = lightsBoard.GetLength(1);
  124.             if (boardSize != secondSize)
  125.             {
  126.                 return 0;
  127.             }
  128.             writeLn(boardSize.ToString());
  129.             wyswietl(lightsBoard);
  130.            
  131.             while (s > 0)
  132.             {
  133.              
  134.                 //skasowac
  135.                 wyswietl(lightsBoard);
  136.  
  137.                 //tablica tymczasowa
  138.                 bool[,] tTemp = new bool[boardSize, boardSize];
  139.  
  140.                 //obliczamy nowe stany
  141.  
  142.                 for (int i = 0; i < boardSize; i++)
  143.                 {
  144.                     for (int j = 0; j < boardSize; j++)
  145.                     {
  146.                         //sprawdzamy czy nie narozna
  147.                         if (!(((i==0) && (j==0)) || ((i==0) && (j==boardSize-1)) || ((i==boardSize)&& (j==0)) || ((i==boardSize) && (j==boardSize))))
  148.                         {
  149.  
  150.  
  151.                             int ilosc = ileZapalonychSasiadow(lightsBoard, i, j);
  152.                             //zapalona
  153.                             if (lightsBoard[i, j])
  154.                             {
  155.                                 //sprawdzamy, obliczamy ile się sąsiadów pali
  156.                                
  157.                                 if ((ilosc == 2) || (ilosc == 3))
  158.                                 {
  159.                                     tTemp[i, j] = true;
  160.                                 }
  161.                                 else
  162.                                 {
  163.                                     tTemp[i, j] = false;
  164.                                 }
  165.  
  166.                             }
  167.                             else
  168.                             {
  169.                                 //zgaszona
  170.                                 if (ilosc == 3)
  171.                                 {
  172.                                     tTemp[i, j] = true;
  173.                                 }
  174.                                 else
  175.                                 {
  176.                                     tTemp[i, j] = false;
  177.                                 }
  178.  
  179.                             }
  180.  
  181.  
  182.  
  183.                         }
  184.  
  185.  
  186.                     }
  187.                 }
  188.  
  189.  
  190.                 //kopiowanie tablcy
  191.                 for (int i = 0; i < boardSize; i++)
  192.                 {
  193.                     for (int j = 0; j < boardSize; j++)
  194.                     {
  195.                         lightsBoard[i, j] = tTemp[i, j];
  196.                     }
  197.                    
  198.                 }
  199.  
  200.                     s--;
  201.             }
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.             wyswietl(lightsBoard);
  209.  
  210.  
  211.             int zapalonych = 0;
  212.  
  213.             for (int i = 0; i < boardSize; i++)
  214.             {
  215.                 for (int j = 0; j < boardSize; j++)
  216.                 {
  217.                     zapalonych += lightsBoard[i, j] ? 1 : 0;
  218.                 }
  219.  
  220.             }
  221.             return zapalonych;
  222.  
  223.         }
  224.     }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement