224 lines
8.8 KiB
C#
224 lines
8.8 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using System.Xml;
|
|||
|
using CDSAE3_Lian_Lian_Kan;
|
|||
|
namespace CDSAE3_Lian_Lian_Kan.Board_funcs
|
|||
|
{
|
|||
|
public partial class Board
|
|||
|
{
|
|||
|
public Board() {
|
|||
|
}
|
|||
|
public int[,]? Bd { get; set; } = null;//y,x
|
|||
|
public int[]? vals_per_Image = null;
|
|||
|
public int total;
|
|||
|
public int Total { get
|
|||
|
{
|
|||
|
return total;
|
|||
|
}
|
|||
|
set
|
|||
|
{
|
|||
|
total = value;
|
|||
|
if(total == 0)
|
|||
|
Settings.game_mode_form?.Finished_Handler(this, new Forms.FinishArgs { finish_Type = Forms.FinishArgs.Finish_Type.All_done });
|
|||
|
} }
|
|||
|
public void make_board()
|
|||
|
{
|
|||
|
var rand = new Random();
|
|||
|
size = Settings.get_length_width();
|
|||
|
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;
|
|||
|
int types = Settings.Images_size();
|
|||
|
vals_per_Image = new int[types];
|
|||
|
int single = sum/types;
|
|||
|
for(int k = sum; k>0; k-=2)
|
|||
|
{
|
|||
|
int t = rand.Next(0, types);
|
|||
|
if (vals_per_Image[t] >= 1.5*single)
|
|||
|
{
|
|||
|
k += 2;
|
|||
|
continue;
|
|||
|
}
|
|||
|
vals_per_Image[t]+=2;
|
|||
|
}
|
|||
|
int cur_width = 1, cur_height = 1;
|
|||
|
var temp_val_per_Image = (int[])vals_per_Image.Clone();
|
|||
|
Func<int> getval = () =>
|
|||
|
{
|
|||
|
int t = rand.Next(0, types);
|
|||
|
if (temp_val_per_Image[t] == 0)
|
|||
|
{
|
|||
|
int i = (t + 1)%types;
|
|||
|
for (; temp_val_per_Image[i] == 0; i %= types)
|
|||
|
i++;
|
|||
|
temp_val_per_Image[i]--;
|
|||
|
return i;
|
|||
|
}
|
|||
|
temp_val_per_Image[t]--;
|
|||
|
return t;
|
|||
|
};
|
|||
|
for (int i=0;i<sum;i++)
|
|||
|
{
|
|||
|
Bd[cur_height,cur_width] = getval();
|
|||
|
cur_width++;
|
|||
|
if (cur_width > width)
|
|||
|
{
|
|||
|
cur_height++;
|
|||
|
cur_width = 1;
|
|||
|
}
|
|||
|
}
|
|||
|
return;
|
|||
|
}
|
|||
|
public (bool,List<(int,int)>?) test((int,int) a,(int,int) b)//x,y
|
|||
|
{
|
|||
|
bool reverse = false;
|
|||
|
if (a == b)
|
|||
|
return (false,null);
|
|||
|
if (Bd?[a.Item2, a.Item1] != Bd?[b.Item2, b.Item1])
|
|||
|
return (false,null);
|
|||
|
var (xa, ya) = a;
|
|||
|
var (xb, yb) = b;
|
|||
|
Func<bool, int, int, int,bool> line_test = (bool x,int ct,int from,int to) =>
|
|||
|
{//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++)
|
|||
|
if (Bd?[ct, i] != -1)
|
|||
|
return false;
|
|||
|
}
|
|||
|
else
|
|||
|
for (int i = from + 1; i < to; i++)
|
|||
|
if (Bd?[i, ct] != -1)
|
|||
|
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
|
|||
|
{
|
|||
|
if (pos.Item1 < 0 || pos.Item2 < 0 || pos.Item1 >= Bd?.GetLength(1) || pos.Item2 >= Bd?.GetLength(0))
|
|||
|
return false;
|
|||
|
if (Bd?[pos.Item2, pos.Item1] == -1)
|
|||
|
{
|
|||
|
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)
|
|||
|
{
|
|||
|
(a, b) = (b, a);
|
|||
|
reverse = true;
|
|||
|
}
|
|||
|
}
|
|||
|
{//three line test
|
|||
|
Func<(int, int), (int, int), (bool, (int, int))> intersection = ((int, int) a, (int, int) b) =>
|
|||
|
{
|
|||
|
if (a.Item1 > b.Item1)
|
|||
|
(a, b) = (b, a);
|
|||
|
if (a.Item2 < b.Item1)
|
|||
|
return (false, (-1, -1));
|
|||
|
if (a.Item2 > b.Item2)
|
|||
|
return (true,( b.Item1, b.Item2));
|
|||
|
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));
|
|||
|
if (throughx)
|
|||
|
for (int i = xrange.Item1; i <= xrange.Item2; i++)
|
|||
|
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 });
|
|||
|
if (throughy)
|
|||
|
for (int i = yrange.Item1; i <= yrange.Item2; i++)
|
|||
|
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);
|
|||
|
}
|
|||
|
public (int, int) size { get; set; }//width,height
|
|||
|
public Settings.Mode mode { get; set; }
|
|||
|
}
|
|||
|
}
|