using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace chess2
{
public partial class Form1 : Form
{
int c = 0;
public Form1()
{
InitializeComponent();
}
private int[] rand(int n, int x)
{
Random random = new Random(x + DateTime.Now.Millisecond);
int t = random.Next(0, n);
int[] inta = new int[n];
int c;
string[] abc = new string[n]; ;
for (int i = 0; i < n; i++) { a: t = random.Next(0, n); c = 1; while ((i - c) >= 0)
{
if (inta[i - c] == t)
{
abc[i] = t.ToString();
goto a;
}
c++;
}
for (int i2 = 0; i2 <= i; i2++)
{
if (abc[i2] == t.ToString())
goto a;
}
inta[i] = t;
}
return inta;
}
private Double fib(int n)
{
if (n == 0)
return 0;
if (n == 1)
return 1;
Double tmp = 0;
for (int i = 0; i <= n; i++)
{
tmp += i;
}
return tmp + 0.0;
}
private int[,] cross(int[] pop1, int[] pop2, int n)
{
int[,] pop_4_return = new int[2, n];
for (int c = 0; c < 2; c++)
{
for (int i = 0; i < n; i++) { if ((n / 2) > i)
{
if (c == 0)
{
pop_4_return[c, i] = pop2[i];
}
else
{
pop_4_return[c, i] = pop1[i];
}
}
else
{
if (c == 0)
{
pop_4_return[c, i] = pop1[i];
}
else
{
pop_4_return[c, i] = pop2[i];
}
}
}
}
return pop_4_return;
}
private Double fit(int[] nff, int n)
{
int[,] inta = new int[n, n];
int tmp = 0, fitness = 0;
for (int c = 0; c < n; c++)
{
inta[c, nff[c]] = 1;
}
for (int i = 0; i < n; i++)
{
for (int ii = 0; ii < n; ii++)
{
if (inta[i, ii] == 1)
{
tmp = ii;
goto a;
}
}
a:
for (int i2 = 1; i2 < n; i2++)
{
if (((i + i2) < n))
if ((tmp + i2) < n)
if (inta[i + i2, tmp + i2] == 1)
fitness++;
if (((i + i2) < n)) if ((tmp - i2) >= 0)
if (inta[i + i2, tmp - i2] == 1)
fitness++;
if ((i + i2) < n)
if (inta[i + i2, tmp] == 1)
fitness++;
}
}
Double fibonachi = fib(n - 1) + 0.0;
Double r = 100 - (fitness * (100 / fibonachi));
return r;
}
private int[,] pop_generator(int n)
{
int[,] inta = new int[n, n];
int[] intb = new int[n];
intb = rand(n, c);
c++;
for (int i = 0; i < n; i++)
{
intb = rand(n, c);
while ((fit(intb, n)) < 50.0)
intb = rand(n, c); c++;
for (int j = 0; j < n; j++)
{
inta[i, j] = intb[j];
}
c++;
}
return inta;
}
private int arr_to_var(int[] narr, int n)
{
string nvar = "";
for (int i = 0; i < n; i++)
{
nvar += narr[i].ToString();
}
Int32 n_for_return = Int32.Parse(nvar);
return n_for_return;
}
private int[,] crossover(int[,] pop, int n)
{
int[,] pop_for_return = new int[n, n];
int[,] crosstemp = new int[2, 2];
int[] tmp1 = new int[n];
int[] tmp2 = new int[n];
for (int i = 0; i < n; i += 2)
{
for (int j = 0; j < n; j++)
{
tmp1[j] = pop[i, j];
tmp2[j] = pop[i + 1, j];
}
crosstemp = cross(tmp1, tmp2, n);
for (int j2 = 0; j2 < n; j2++)
{
pop_for_return[i, j2] = crosstemp[0, j2];
pop_for_return[i + 1, j2] = crosstemp[1, j2];
}
}
return pop_for_return;
}
private void printc(int[] arr_4_print, int n)
{
int[,] tmp = new int[n, n];
for (int i = 0; i < n; i++)
tmp[arr_4_print[i], i] = 1;
for (int i = 0; i < n; i++)
{
label1.Text += "\n\r";
for (int j = 0; j < n; j++)
{
if (tmp[i, j] == 1)
label1.Text += "*";
else
label1.Text += "0";
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Int32 n = Int32.Parse(textBox1.Text);
int[,] pop = new int[n, n];
int[,] cros = new int[n, n];
int[] tmp = new int[n];
pop = pop_generator(n);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
tmp[j] = pop[i, j];
}
if (fit(tmp, n) == 100.0)
{
printc(tmp, n);
goto a;
}
}
int comp = 1;
int counter = 0;
cros = crossover(pop, n);
while (comp == 1)
{
this.Text = counter.ToString();
counter++;
label1.Text = "";
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
label1.Text += cros[i, j];
}
label1.Text += "\n\r";
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
tmp[j] = cros[i, j];
}
if (fit(tmp, n) == 100.0)
{
printc(tmp, n);
comp = 0;
goto a;
}
}
cros = crossover(cros, n);
}
a:
this.Text = "find";
}
}
}