form2.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace _hlavolam
{
public partial class Form2 : Form
{
public State start;
public State end;
public string errormsg;
public Form2()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
int mm;
int nn;
bool generuj = false;
if (int.TryParse(textBox1.Text, out mm))
{
if (int.TryParse(textBox2.Text, out nn))
{
//nove stavy
start = new State(mm, nn);
end = new State(mm, nn);
//zistime, ci boly zadane pociatocne hodnoty, ak nie, ideme ich generovat
if (string.IsNullOrEmpty(textBox3.Text)) generuj = true;
//ak hej, spracujeme
else {
if (!start.parse(textBox3.Text, "Pociatocny"))
{
return;
}
}
if (!end.parse(textBox4.Text, "Ziadany"))
{
return;
}
this.Close();
}
}
label5.Text = "Nespravne rozmery!";
return;
}
}
}
stav.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace _hlavolam
{
public class State
{
//rozmery
public int m;//riadku
public int n;//stlpce
//rozmiestnenie prvkov v stave
public int[][] stav;
//poloha prazdneho policka
public int empty_x;
public int empty_y;
public bool empty = false;
public State(int mm, int nn)
{
m = mm;
n = nn;
stav = new int[mm][];
for (int i = 0; i < mm; i++)
{
stav[i] = new int[nn];
}
}
public bool MakeView(string file)
{//vytvori html prezentaciu stavu
string htmlfile = "<html><head></head><body leftmargin=\"0\" topmargin=\"0\"><table width=\"300\" height=\"300\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">";
for (int i = 0; i < m; i++)
{
htmlfile += "<tr align=\"center\" valign=\"middle\">";
for (int j = 0; j < n; j++)
{
htmlfile += "<td>" + stav[i][j].ToString() +"</td>";
}
htmlfile += "</tr>";
}
htmlfile += "</table></body></html>";
File.WriteAllText(String.Format("{0}/{1}.html", Form1.dirName, file), htmlfile);
return true;
}
public bool parse(string text, string stav) {
//kolko riadkov bolo v pociatocnych hodnotach
int tmp;
string [] riadky = text.Split(new char[] {'\n'});
string[] riadok;
//zhoduje sa pocet riadkov zadany v rozmeroch s poctom riadkov s poctom riadkov v pociatocnych hodnotach?
if (riadky.Length!=this.m) {
//Form2.label5.Text=string.Format("Nespravny pocet riadkov ({0} stav)!",stav);
Form2.label5.Text = "kokot";
return false;
}
//naplnenie pola zaciatocnych hodnot zo zadanych hodnot
for (int i = 0; i < this.m; i++)
{
riadok = riadky[i].Split(new char[] { ' ' });
if (riadok.Length != this.n)
{
//Form2.label5.Text = string.Format("Nespravny pocet hodnot v riadku ({0} stav)!", stav);
return false;
}
for (int j = 0; j < this.n; j++)
{
if (int.TryParse(riadok[j], out tmp))
{
this.stav[i][j] = tmp;
}
else
{
if (this.empty)
{
//Form2.label5.Text = string.Format("Viac prazdnych poli! ({0} stav)!", stav);
return false;
}
if (string.Equals("x", riadok[j]))
{
this.stav[i][j] = 0;
this.empty = true;
this.empty_x = i;
this.empty_y = j;
}
}
}
}
if (!this.empty)
{
//Form2.label5.Text = string.Format("Nezadali ste polohu prazdneho pola! ({0} stav)!", stav);
return false;
}
return true;
}
}
}