2024-03-22 17:03:01 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Xml;
|
|
|
|
|
using CDSAE3_Lian_Lian_Kan;
|
2024-03-29 14:18:38 +08:00
|
|
|
|
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Rebar;
|
2024-03-22 17:03:01 +08:00
|
|
|
|
namespace CDSAE3_Lian_Lian_Kan.Board_funcs
|
|
|
|
|
{
|
2024-04-02 21:57:04 +08:00
|
|
|
|
public partial class Board:IBoard
|
2024-03-22 17:03:01 +08:00
|
|
|
|
{
|
2024-04-02 21:57:04 +08:00
|
|
|
|
private int[,] Bd = { { } };//y,x
|
2024-03-29 14:18:38 +08:00
|
|
|
|
public Dictionary<int, List<(int, int)>> board_Index { get; } = new Dictionary<int, List<(int, int)>>();
|
|
|
|
|
public int[] Vals_per_Image { get; set; } = { };
|
2024-03-22 17:03:01 +08:00
|
|
|
|
public int total;
|
2024-03-29 14:18:38 +08:00
|
|
|
|
public int Total
|
|
|
|
|
{
|
|
|
|
|
get
|
2024-03-22 17:03:01 +08:00
|
|
|
|
{
|
|
|
|
|
return total;
|
2024-03-29 14:18:38 +08:00
|
|
|
|
}
|
2024-03-22 17:03:01 +08:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
total = value;
|
2024-03-29 14:18:38 +08:00
|
|
|
|
if (total == 0)
|
2024-04-02 21:57:04 +08:00
|
|
|
|
Etcs.game_mode_form?.Finished_Handler(this, new Forms.FinishArgs { finish_Type = Forms.FinishArgs.Finish_Type.All_done });
|
2024-03-29 14:18:38 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-02 21:57:04 +08:00
|
|
|
|
public int[,] make_board()
|
2024-03-22 17:03:01 +08:00
|
|
|
|
{
|
|
|
|
|
var rand = new Random();
|
2024-04-02 21:57:04 +08:00
|
|
|
|
size = Etcs.get_length_width();
|
2024-03-22 17:03:01 +08:00
|
|
|
|
var (width, height) = size;
|
|
|
|
|
Bd = new int[height + 2, width + 2];
|
|
|
|
|
for (int i = 0; i < height + 2; i++)
|
|
|
|
|
for (int j = 0; j < width + 2; j++)
|
|
|
|
|
Bd[i, j] = -1;
|
|
|
|
|
int sum = width * height;
|
|
|
|
|
if (sum % 2 != 0)
|
|
|
|
|
sum--;
|
|
|
|
|
total = sum;
|
2024-04-02 21:57:04 +08:00
|
|
|
|
int types = Etcs.Images_size();
|
2024-03-29 14:18:38 +08:00
|
|
|
|
Vals_per_Image = new int[types];
|
|
|
|
|
int single = sum / types;
|
|
|
|
|
for (int k = sum; k > 0; k -= 2)
|
2024-03-22 17:03:01 +08:00
|
|
|
|
{
|
|
|
|
|
int t = rand.Next(0, types);
|
2024-03-29 14:18:38 +08:00
|
|
|
|
if (Vals_per_Image[t] >= 1.5 * single)
|
2024-03-22 17:03:01 +08:00
|
|
|
|
{
|
|
|
|
|
k += 2;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2024-03-29 14:18:38 +08:00
|
|
|
|
Vals_per_Image[t] += 2;
|
2024-03-22 17:03:01 +08:00
|
|
|
|
}
|
|
|
|
|
int cur_width = 1, cur_height = 1;
|
2024-03-29 14:18:38 +08:00
|
|
|
|
var temp_val_per_Image = (int[])Vals_per_Image.Clone();
|
|
|
|
|
int last_val = -1;
|
2024-03-22 17:03:01 +08:00
|
|
|
|
Func<int> getval = () =>
|
|
|
|
|
{
|
2024-03-29 14:18:38 +08:00
|
|
|
|
int not_zero = 0;
|
|
|
|
|
foreach (int x in temp_val_per_Image)
|
|
|
|
|
if (x != 0)
|
|
|
|
|
{
|
|
|
|
|
not_zero++;
|
|
|
|
|
if (not_zero > 1)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (not_zero <= 1)
|
|
|
|
|
{
|
2024-04-02 21:57:04 +08:00
|
|
|
|
int k = 0;
|
|
|
|
|
for(int i=0;i<temp_val_per_Image.Length;i++)
|
|
|
|
|
if (temp_val_per_Image[i]!=0)
|
|
|
|
|
{
|
|
|
|
|
k = i;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2024-03-29 14:18:38 +08:00
|
|
|
|
temp_val_per_Image[k]--;
|
|
|
|
|
return k;
|
|
|
|
|
}
|
2024-03-22 17:03:01 +08:00
|
|
|
|
int t = rand.Next(0, types);
|
2024-04-02 21:57:04 +08:00
|
|
|
|
if (temp_val_per_Image[t] <= 0 || t == last_val)
|
2024-03-22 17:03:01 +08:00
|
|
|
|
{
|
2024-03-29 14:18:38 +08:00
|
|
|
|
int i = (t + 1) % types;
|
2024-04-02 21:57:04 +08:00
|
|
|
|
for (; temp_val_per_Image[i] <= 0 || i == last_val; i %= types)
|
2024-03-22 17:03:01 +08:00
|
|
|
|
i++;
|
|
|
|
|
temp_val_per_Image[i]--;
|
2024-03-29 14:18:38 +08:00
|
|
|
|
last_val = i;
|
2024-03-22 17:03:01 +08:00
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
temp_val_per_Image[t]--;
|
2024-03-29 14:18:38 +08:00
|
|
|
|
last_val = t;
|
2024-03-22 17:03:01 +08:00
|
|
|
|
return t;
|
|
|
|
|
};
|
2024-03-29 14:18:38 +08:00
|
|
|
|
for (int i = 0; i < sum; i++)
|
2024-03-22 17:03:01 +08:00
|
|
|
|
{
|
2024-03-29 14:18:38 +08:00
|
|
|
|
Bd[cur_height, cur_width] = getval();
|
|
|
|
|
if (board_Index.TryGetValue(Bd[cur_height, cur_width], out var index))
|
|
|
|
|
index.Add((cur_width, cur_height));
|
|
|
|
|
else
|
|
|
|
|
board_Index.Add(Bd[cur_height, cur_width], new List<(int, int)> { (cur_width, cur_height) });
|
2024-03-22 17:03:01 +08:00
|
|
|
|
cur_width++;
|
|
|
|
|
if (cur_width > width)
|
2024-03-29 14:18:38 +08:00
|
|
|
|
{
|
2024-03-22 17:03:01 +08:00
|
|
|
|
cur_height++;
|
|
|
|
|
cur_width = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-02 21:57:04 +08:00
|
|
|
|
return Bd;
|
2024-03-22 17:03:01 +08:00
|
|
|
|
}
|
2024-04-02 21:57:04 +08:00
|
|
|
|
public int[,] remake_board()
|
2024-03-29 14:18:38 +08:00
|
|
|
|
{
|
|
|
|
|
board_Index.Clear();
|
|
|
|
|
var rand = new Random();
|
|
|
|
|
int[] temp_val_per_Image = (int[])Vals_per_Image.Clone();
|
|
|
|
|
int types = Vals_per_Image.Length;
|
|
|
|
|
int height = Bd.GetLength(0);
|
|
|
|
|
int width = Bd.GetLength(1);
|
|
|
|
|
int last_val = -1;
|
|
|
|
|
Func<int> getval = () =>
|
|
|
|
|
{
|
|
|
|
|
int not_zero = 0;
|
|
|
|
|
foreach (int x in temp_val_per_Image)
|
|
|
|
|
if (x != 0)
|
|
|
|
|
{
|
|
|
|
|
not_zero++;
|
|
|
|
|
if (not_zero > 1)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (not_zero <= 1)
|
|
|
|
|
{
|
|
|
|
|
int k = temp_val_per_Image.Single(x => x != 0);
|
|
|
|
|
temp_val_per_Image[k]--;
|
|
|
|
|
return k;
|
|
|
|
|
}
|
|
|
|
|
int t = rand.Next(0, types);
|
|
|
|
|
if (temp_val_per_Image[t] == 0 || t == last_val)
|
|
|
|
|
{
|
|
|
|
|
int i = (t + 1) % types;
|
|
|
|
|
for (; temp_val_per_Image[i] == 0 || i == last_val; i %= types)
|
|
|
|
|
i++;
|
|
|
|
|
temp_val_per_Image[i]--;
|
|
|
|
|
last_val = i;
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
temp_val_per_Image[t]--;
|
|
|
|
|
last_val = t;
|
|
|
|
|
return t;
|
|
|
|
|
};
|
|
|
|
|
for (int i = 0; i < height; i++)
|
|
|
|
|
for (int j = 0; j < width; j++)
|
|
|
|
|
if (Bd[i, j] != -1)
|
|
|
|
|
{
|
|
|
|
|
Bd[i, j] = getval();
|
|
|
|
|
if (board_Index.TryGetValue(Bd[i, j], out var index))
|
|
|
|
|
index.Add((j, i));
|
|
|
|
|
else
|
|
|
|
|
board_Index.Add(Bd[i, j], new List<(int, int)> { (j, i) });
|
|
|
|
|
}
|
2024-04-02 21:57:04 +08:00
|
|
|
|
return Bd;
|
2024-03-29 14:18:38 +08:00
|
|
|
|
}
|
|
|
|
|
public (bool, List<(int, int)>?) test((int, int) a, (int, int) b)//x,y
|
2024-03-22 17:03:01 +08:00
|
|
|
|
{
|
|
|
|
|
bool reverse = false;
|
|
|
|
|
if (a == b)
|
2024-03-29 14:18:38 +08:00
|
|
|
|
return (false, null);
|
|
|
|
|
if (Bd[a.Item2, a.Item1] != Bd[b.Item2, b.Item1])
|
|
|
|
|
return (false, null);
|
2024-03-22 17:03:01 +08:00
|
|
|
|
var (xa, ya) = a;
|
|
|
|
|
var (xb, yb) = b;
|
2024-03-29 14:18:38 +08:00
|
|
|
|
Func<bool, int, int, int, bool> line_test = (bool x, int ct, int from, int to) =>
|
2024-03-22 17:03:01 +08:00
|
|
|
|
{//ct is x (x==true)left to right ///up to down
|
|
|
|
|
if (from > to)
|
|
|
|
|
(from, to) = (to, from);
|
|
|
|
|
if (x)
|
|
|
|
|
{
|
|
|
|
|
for (int i = from + 1; i < to; i++)
|
2024-03-29 14:18:38 +08:00
|
|
|
|
if (Bd[ct, i] != -1)
|
2024-03-22 17:03:01 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
for (int i = from + 1; i < to; i++)
|
2024-03-29 14:18:38 +08:00
|
|
|
|
if (Bd[i, ct] != -1)
|
2024-03-22 17:03:01 +08:00
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
if (xa == xb)
|
|
|
|
|
if (line_test(false, xa, ya, yb))
|
|
|
|
|
return (true, new List<(int, int)> { a, b });
|
|
|
|
|
if (ya == yb)
|
|
|
|
|
if (line_test(true, ya, xa, xb))
|
|
|
|
|
return (true, new List<(int, int)> { a, b });
|
|
|
|
|
(int, int)[] squareA = new (int, int)[4];
|
|
|
|
|
(int, int)[] squareB = new (int, int)[4];//urdl
|
|
|
|
|
{//two line test
|
|
|
|
|
HashSet<(int, int)> reachableA = new HashSet<(int, int)>(), reachableB = new HashSet<(int, int)>();
|
|
|
|
|
Func<(int, int), HashSet<(int, int)>, bool> check_insert = ((int, int) pos, HashSet<(int, int)> insert) =>//x,y
|
|
|
|
|
{
|
2024-03-29 14:18:38 +08:00
|
|
|
|
if (pos.Item1 < 0 || pos.Item2 < 0 || pos.Item1 >= Bd.GetLength(1) || pos.Item2 >= Bd.GetLength(0))
|
2024-03-22 17:03:01 +08:00
|
|
|
|
return false;
|
2024-03-29 14:18:38 +08:00
|
|
|
|
if (Bd[pos.Item2, pos.Item1] == -1)
|
2024-03-22 17:03:01 +08:00
|
|
|
|
{
|
|
|
|
|
insert.Add(pos);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
Func<(int, int), HashSet<(int, int)>, HashSet<(int, int)>, (int, int)[], (bool, int, int)> cross_test = ((int, int) pos, HashSet<(int, int)> insert, HashSet<(int, int)> test, (int, int)[] square) =>//x,y
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
var (x, y) = pos;
|
|
|
|
|
for (x--; ; x--)
|
|
|
|
|
if (check_insert((x, y), insert))
|
|
|
|
|
{
|
|
|
|
|
if (test.Contains((x, y)))
|
|
|
|
|
return (true, x, y);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
square[3] = (x + 1, y);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
(x, y) = pos;
|
|
|
|
|
for (x++; ; x++)
|
|
|
|
|
if (check_insert((x, y), insert))
|
|
|
|
|
{
|
|
|
|
|
if (test.Contains((x, y)))
|
|
|
|
|
return (true, x, y);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
square[1] = (x - 1, y);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
(x, y) = pos;
|
|
|
|
|
for (y--; ; y--)
|
|
|
|
|
if (check_insert((x, y), insert))
|
|
|
|
|
{
|
|
|
|
|
if (test.Contains((x, y)))
|
|
|
|
|
return (true, x, y);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
square[0] = (x, y + 1);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
(x, y) = pos;
|
|
|
|
|
for (y++; ; y++)
|
|
|
|
|
if (check_insert((x, y), insert))
|
|
|
|
|
{
|
|
|
|
|
if (test.Contains((x, y)))
|
|
|
|
|
return (true, x, y);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
square[2] = (x, y - 1);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return (false, -1, -1);
|
|
|
|
|
};
|
|
|
|
|
cross_test(a, reachableA, reachableB, squareA);
|
|
|
|
|
var (find_ans, pax, pay) = cross_test(b, reachableB, reachableA, squareB);
|
|
|
|
|
if (find_ans)
|
|
|
|
|
return (true, new List<(int, int)> { a, (pax, pay), b });
|
|
|
|
|
if (reachableA.Count > reachableB.Count)
|
2024-03-29 14:18:38 +08:00
|
|
|
|
{
|
2024-03-22 17:03:01 +08:00
|
|
|
|
(a, b) = (b, a);
|
|
|
|
|
reverse = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
{//three line test
|
2024-03-29 14:18:38 +08:00
|
|
|
|
Func<(int, int), (int, int), (bool, (int, int))> intersection = ((int, int) a, (int, int) b) =>//first small last big
|
2024-03-22 17:03:01 +08:00
|
|
|
|
{
|
|
|
|
|
if (a.Item1 > b.Item1)
|
|
|
|
|
(a, b) = (b, a);
|
|
|
|
|
if (a.Item2 < b.Item1)
|
|
|
|
|
return (false, (-1, -1));
|
|
|
|
|
if (a.Item2 > b.Item2)
|
2024-03-29 14:18:38 +08:00
|
|
|
|
return (true, (b.Item1, b.Item2));
|
2024-03-22 17:03:01 +08:00
|
|
|
|
return (true, (b.Item1, a.Item2));
|
|
|
|
|
};
|
|
|
|
|
var (throughx, xrange) = intersection((squareA[3].Item1, squareA[1].Item1), (squareB[3].Item1, squareB[1].Item1));
|
|
|
|
|
var (throughy, yrange) = intersection((squareA[0].Item2, squareA[2].Item2), (squareB[0].Item2, squareB[2].Item2));
|
2024-04-02 21:57:04 +08:00
|
|
|
|
Func<(int, int), int, List<int>> swing_check = ((int, int) range, int center) =>
|
2024-03-29 14:18:38 +08:00
|
|
|
|
{
|
2024-04-02 21:57:04 +08:00
|
|
|
|
List<int> result = new List<int>();
|
|
|
|
|
if (center < range.Item1)
|
2024-03-29 14:18:38 +08:00
|
|
|
|
{
|
2024-04-02 21:57:04 +08:00
|
|
|
|
for (int k = range.Item1; k <= range.Item2; k++)
|
|
|
|
|
result.Add(k);
|
|
|
|
|
return result;
|
2024-03-29 14:18:38 +08:00
|
|
|
|
}
|
2024-04-02 21:57:04 +08:00
|
|
|
|
if (center > range.Item2)
|
|
|
|
|
{
|
|
|
|
|
for (int k = range.Item2; k >= range.Item1; k--)
|
|
|
|
|
result.Add(k);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
result.Add(center);
|
|
|
|
|
Func<int, bool> inrange = (int t) => t >= range.Item1 && t <= range.Item2;
|
|
|
|
|
bool go_on = true;
|
|
|
|
|
for (int i = 1; go_on; i++)
|
|
|
|
|
{
|
|
|
|
|
go_on = false;
|
|
|
|
|
if (go_on |= inrange(center + i))
|
|
|
|
|
result.Add(center + i);
|
|
|
|
|
if (go_on |= inrange(center - i))
|
|
|
|
|
result.Add(center - i);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
2024-03-29 14:18:38 +08:00
|
|
|
|
};
|
2024-03-22 17:03:01 +08:00
|
|
|
|
if (throughx)
|
2024-04-02 21:57:04 +08:00
|
|
|
|
foreach (int i in swing_check(xrange, a.Item1))
|
2024-03-22 17:03:01 +08:00
|
|
|
|
if (line_test(false, i, a.Item2, b.Item2))
|
|
|
|
|
if (reverse)
|
|
|
|
|
return (true, new List<(int, int)> { b, (i, b.Item2), (i, a.Item2), a });
|
|
|
|
|
else
|
|
|
|
|
return (true, new List<(int, int)> { a, (i, a.Item2), (i, b.Item2), b });
|
2024-03-29 14:18:38 +08:00
|
|
|
|
|
2024-03-22 17:03:01 +08:00
|
|
|
|
if (throughy)
|
2024-04-02 21:57:04 +08:00
|
|
|
|
foreach (int i in swing_check(yrange, a.Item2))
|
2024-03-22 17:03:01 +08:00
|
|
|
|
if (line_test(true, i, a.Item1, b.Item1))
|
|
|
|
|
if (reverse)
|
|
|
|
|
return (true, new List<(int, int)> { b, (b.Item1, i), (a.Item1, i), a });
|
|
|
|
|
else
|
|
|
|
|
return (true, new List<(int, int)> { a, (a.Item1, i), (b.Item1, i), b });
|
|
|
|
|
}
|
|
|
|
|
return (false, null);
|
|
|
|
|
}
|
2024-04-02 21:57:04 +08:00
|
|
|
|
public void decrease(params (int, int)[] poss)
|
2024-03-29 14:18:38 +08:00
|
|
|
|
{
|
|
|
|
|
foreach (var (x, y) in poss)
|
|
|
|
|
{
|
|
|
|
|
int type = Bd[y, x];
|
|
|
|
|
Bd[y, x] = -1;
|
|
|
|
|
if (!board_Index[type].Remove((x, y)))
|
|
|
|
|
throw new Exception("Val not Found in board_Index");
|
|
|
|
|
Vals_per_Image[type]--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-02 21:57:04 +08:00
|
|
|
|
internal List<List<(int, int)>> get_tip((int, int) start)
|
2024-03-29 14:18:38 +08:00
|
|
|
|
{
|
|
|
|
|
List<List<(int, int)>> ans = new List<List<(int, int)>>();
|
2024-04-02 21:57:04 +08:00
|
|
|
|
if (board_Index.TryGetValue(Bd[start.Item2, start.Item1], out var tip))
|
2024-03-29 14:18:38 +08:00
|
|
|
|
foreach (var pos in tip)
|
|
|
|
|
{
|
|
|
|
|
var (result, path) = test(start, pos);
|
2024-04-02 21:57:04 +08:00
|
|
|
|
if (result && path != null)
|
2024-03-29 14:18:38 +08:00
|
|
|
|
ans.Add(path);
|
|
|
|
|
}
|
|
|
|
|
return ans;
|
|
|
|
|
}
|
2024-03-22 17:03:01 +08:00
|
|
|
|
public (int, int) size { get; set; }//width,height
|
2024-04-02 21:57:04 +08:00
|
|
|
|
public Etcs.Mode mode { get; set; }
|
2024-03-22 17:03:01 +08:00
|
|
|
|
}
|
|
|
|
|
}
|