Files
Sharpy_Picross/NonoGramme/Line.cs
T

132 lines
3.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace NonoGramme
{
internal class Line
{
private static Random rnd = new Random();
private Tile[] _data;
private int _length;
private List<int> _hints;
public Tile[] Data { get => _data; set => _data = value; }
public int Length { get => _length; set => _length = value; }
public List<int> Hints { get => _hints; set => _hints = value; }
public Line(Tile[] states)
{
//This method will create a new Line wich can then be edited
Data = states;
RecalculateHints();
}
public Line(int length)
{
//This method will just create the Line but totally empty
Length = length;
Data = new Tile[Length];
for (int i = 0; i < Length; i++)
{
Data[i] = new Tile();
}
RecalculateHints();
}
public string GetHorizontalHints()
{
string result = "";
int counter = 0;
foreach (int hint in Hints)
{
counter++;
if (counter != Hints.Count)
{
result += hint + ",";
}
else
{
result += hint;
}
}
return result;
}
public string GetVerticalHints()
{
string result = "";
foreach (int hint in Hints)
{
result += hint + Environment.NewLine;
}
return result;
}
public void Edit(int index, Tile.State state)
{
Data[index].CurrentState = state;
RecalculateHints();
}
public void Click(int index)
{
Tile tile = Data[index];
//MessageBox.Show("Tile pressed :"+tile.CurrentState);
switch (tile.CurrentState)
{
case Tile.State.Empty:
tile.CurrentState = Tile.State.Full;
break;
case Tile.State.Undiscovered:
tile.CurrentState = Tile.State.Discovered;
break;
case Tile.State.Full:
tile.CurrentState = Tile.State.Empty;
break;
case Tile.State.Discovered:
tile.CurrentState = Tile.State.Undiscovered;
break;
default:
//We dont do anything
break;
}
}
public void RecalculateHints()
{
Hints = new List<int>();
bool previousTile = false;
bool currentTile = false;
int counter = 0;
for (int i = 0; i < Data.Length; i++)
{
switch (Data[i].CurrentState)
{
case Tile.State.Discovered:
case Tile.State.Empty:
case Tile.State.Full:
currentTile = false;
break;
case Tile.State.Undiscovered:
currentTile = true;
break;
}
if (currentTile)
{
counter++;
}
if (!currentTile && previousTile)
{
//We had a full segment that just finished
Hints.Add(counter);
counter = 0;
}
previousTile = currentTile;
}
if (currentTile)
{
//if the last tile is full then we need to add it at the end
Hints.Add(counter);
}
}
}
}