15 puzzle game: Difference between revisions
Last fix C#. Ideal now :D
(Add IsSolvable method in C#) |
(Last fix C#. Ideal now :D) |
||
Line 3,993:
private int Moves = 0;
private DateTime Start;
public class Puzzle
{
private int mOrderedNumer;
public int CurrentNumber;
Line 4,019 ⟶ 4,015:
{
mOrderedNumer = OrderedNumer;
CurrentNumber = OrderedNumer;
Line 4,034 ⟶ 4,027:
}
public bool
{
get { return CurrentNumber >= (BlockCount - 1); }
}▼
public bool IsTruePlace▼
{▼
get { return (CurrentNumber == mOrderedNumer); }▼
}
public bool NearestWith(Puzzle OtherPz)
Line 4,047 ⟶ 4,044:
return false;
▲ }
▲ public bool IsTruePlace
▲ {
▲ get { return (CurrentNumber == mOrderedNumer); }
}▼
{▼
}
Line 4,093 ⟶ 4,082:
Form.Text = "Fifteen";
Form.ClientSize = new Size(FormWidth, FormHeigth);
Form.FormBorderStyle = FormBorderStyle.FixedSingle;
Form.MaximizeBox = false;
Form.SuspendLayout();
for (int i = 0; i < BlockCount; i++)
Line 4,130 ⟶ 4,104:
Bt.Enabled = false;
if (Pz.
Bt.Click += new EventHandler(MovePuzzle);
Line 4,146 ⟶ 4,120:
private void NewGame(object Sender, EventArgs E)
{
{
Button Bt2 = Puzzles[i];
Swap(Bt1, Bt2);
}
while (!IsSolvable());
for (int i = 0; i < Puzzles.Count; i++)
{
Puzzles[i].Enabled = true;
}
Line 4,178 ⟶ 4,145:
Puzzle Pz1 = (Puzzle)Bt1.Tag;
Button Bt2 = Puzzles.Find(Bt => ((Puzzle)Bt.Tag).
Puzzle Pz2 = (Puzzle)Bt2.Tag;
Line 4,193 ⟶ 4,160:
{
Button WrongPuzzle = Puzzles.Find(Bt => !((Puzzle)Bt.Tag).IsTruePlace);
▲ bool UWin = ((WrongPuzzle == null) || (AltWrongPuzzle == null));
if (UWin)
Line 4,203 ⟶ 4,168:
Puzzles[i].Enabled = false;
}
TimeSpan Elapsed = DateTime.Now - Start;
Line 4,226 ⟶ 4,188:
Bt1.Visible = true;
Bt1.Text = Pz1.ToString();
if (Pz1.
Bt2.Visible = true;
Bt2.Text = Pz2.ToString();
if (Pz2.
}
private bool IsSolvable(
{
// WARNING: size of puzzle board MUST be even(like 4)!
// For explain see: https://www.geeksforgeeks.org/check-instance-15-puzzle-solvable/
int InvCount = 0;
for (int i = 0; i < Puzzles.Count - 1; i++)
Line 4,244 ⟶ 4,205:
for (int j = i + 1; j < Puzzles.Count; j++)
{
if (
Puzzle Pz2 = (Puzzle)Puzzles[j].Tag;
if (Pz2.IsEmptyPuzzle) continue;
if (
}
}
Puzzle EmptyPz = (Puzzle)EmptyBt.Tag;
bool Result = false;
if ((
{
// is odd
|