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 LightBulbs;
- namespace TAJEMNICZE_SYGNALY
- {
- public class TajemniczeSygnalyClass:ILightBulbs
- {
- public new static int boardSize;
- private readonly static Boolean trybTestowy = false;
- private void writeLn(string t)
- {
- if (trybTestowy)
- {
- Console.WriteLine(t);
- }
- }
- private void wyswietl(bool[,] t)
- {
- if (trybTestowy)
- {
- Console.WriteLine();
- for (int i = 0; i < boardSize; i++)
- {
- for (int j = 0; j < boardSize; j++)
- {
- Console.Write(t[i, j] ? "#" : ".");
- }
- Console.WriteLine();
- }
- Console.WriteLine();
- }
- }
- private TajemniczeSygnalyClass()
- {
- }
- public new static ILightBulbs GetInstance()
- {
- if (lightBulbsInstance == null)
- {
- lightBulbsInstance = new TajemniczeSygnalyClass();
- }
- return lightBulbsInstance;
- }
- private int ileZapalonychSasiadow(bool[,] t, int x, int y)
- {
- int ilosc = 0;
- //L-U
- if ((x != 0) && (y!=0))
- {
- ilosc += t[x - 1, y - 1] ? 1 : 0;
- }
- //U
- if (y != 0)
- {
- ilosc += t[x, y - 1] ? 1 : 0;
- }
- //U-R
- if ((y != 0) && (x < boardSize - 1))
- {
- ilosc += t[x + 1, y - 1] ? 1 : 0;
- }
- //R
- if (x < boardSize - 1)
- {
- ilosc += t[x + 1, y] ? 1 : 0;
- }
- //D-R
- if ((x < boardSize - 1) && (y < boardSize - 1))
- {
- ilosc += t[x + 1, y + 1] ? 1 : 0;
- }
- //D
- if (y < boardSize - 1)
- {
- ilosc += t[x, y + 1] ? 1 : 0;
- }
- //D-L
- if ((y < boardSize - 1) && (x != 0))
- {
- ilosc += t[x - 1, y + 1] ? 1 : 0;
- }
- //L
- if (x != 0)
- {
- ilosc += t[x-1,y]?1:0;
- }
- return ilosc;
- }
- public override int CountLightsOn(bool[,] lightsBoard, int s)
- {
- boardSize = lightsBoard.GetLength(0);
- int secondSize = lightsBoard.GetLength(1);
- if (boardSize != secondSize)
- {
- return 0;
- }
- writeLn(boardSize.ToString());
- wyswietl(lightsBoard);
- while (s > 0)
- {
- //skasowac
- wyswietl(lightsBoard);
- //tablica tymczasowa
- bool[,] tTemp = new bool[boardSize, boardSize];
- //obliczamy nowe stany
- for (int i = 0; i < boardSize; i++)
- {
- for (int j = 0; j < boardSize; j++)
- {
- //sprawdzamy czy nie narozna
- if (!(((i==0) && (j==0)) || ((i==0) && (j==boardSize-1)) || ((i==boardSize)&& (j==0)) || ((i==boardSize) && (j==boardSize))))
- {
- int ilosc = ileZapalonychSasiadow(lightsBoard, i, j);
- //zapalona
- if (lightsBoard[i, j])
- {
- //sprawdzamy, obliczamy ile się sąsiadów pali
- if ((ilosc == 2) || (ilosc == 3))
- {
- tTemp[i, j] = true;
- }
- else
- {
- tTemp[i, j] = false;
- }
- }
- else
- {
- //zgaszona
- if (ilosc == 3)
- {
- tTemp[i, j] = true;
- }
- else
- {
- tTemp[i, j] = false;
- }
- }
- }
- }
- }
- //kopiowanie tablcy
- for (int i = 0; i < boardSize; i++)
- {
- for (int j = 0; j < boardSize; j++)
- {
- lightsBoard[i, j] = tTemp[i, j];
- }
- }
- s--;
- }
- wyswietl(lightsBoard);
- int zapalonych = 0;
- for (int i = 0; i < boardSize; i++)
- {
- for (int j = 0; j < boardSize; j++)
- {
- zapalonych += lightsBoard[i, j] ? 1 : 0;
- }
- }
- return zapalonych;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement