15 puzzle game: Difference between revisions
Most cleared realization in C#
(Most cleared realization in C#) |
|||
Line 3,979:
{{works with|C sharp|6}}
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
public class FifteenPuzzle
{
const int
const
private List<Button>
private int
private DateTime Start;
private class Puzzle
{
private int
get { return mOrderedNumer; }
}
public int CurrentNumber;
public int X;
public Puzzle(int OrderedNumer)
X = OrderedNumer %
}
public bool IsMaxPuzzle
{
}
{
if ((dx == 0) && (dy <= 1) && (dy >= -1)) return true;
if ((dy == 0) && (dx <= 1) && (dx >= -1)) return true;
return false;
}
public bool IsTruePlace
{
get { return (mOrderedNumer == CurrentNumber); }
}
public override string ToString()
{
return (CurrentNumber + 1).ToString();
}
}
public static void Main(string[] args)
{
FifteenPuzzle Game = new FifteenPuzzle();
Application.Run(Game.CreateForm());
}
private Form CreateForm()
{
int
int FormEdge = 9;
Font ButtonFont = new Font("Arial", 15.75F, FontStyle.Regular);
Button StartButton = new Button();
StartButton.Location = new Point(FormEdge, (GridSize * (ButtonMargin + ButtonSize)) + FormEdge);
StartButton.Size = new Size(86, 23);
StartButton.Font = new Font("Arial", 9.75F, FontStyle.Regular);
StartButton.Text = "New Game";
StartButton.UseVisualStyleBackColor = true;
StartButton.TabStop = false;
StartButton.Click += new EventHandler(NewGame);
int FormWidth = (GridSize * ButtonSize) + ((GridSize - 1) * ButtonMargin) + (FormEdge * 2);
int FormHeigth = FormWidth + StartButton.Height;
Form Form = new Form();
Form.Text = "Fifteen";
Form.ClientSize = new Size(FormWidth, FormHeigth);
Form.SuspendLayout();
for (int i = 0; i < BlockCount; i++)
{
Button Bt = new Button();
Puzzle Pz = new Puzzle(i);
int PosX = FormEdge + (Pz.X) * (ButtonSize + ButtonMargin);
int PosY = FormEdge + (Pz.Y) * (ButtonSize + ButtonMargin);
Bt.Location = new Point(PosX, PosY);
Bt.Size = new Size(ButtonSize, ButtonSize);
Bt.Font = ButtonFont;
Bt.Text = Pz.ToString();
Bt.Tag = Pz;
Bt.UseVisualStyleBackColor = true;
Bt.TabStop = false;
Bt.Enabled = false;
Bt.Click += new EventHandler(MovePuzzle);
Puzzles.Add(Bt);
Form.Controls.Add(Bt);
}
Form.Controls.Add(StartButton);
Form.ResumeLayout();
return Form;
}
private void
{
for (int i = 0; i <
{
}
for (int i = 0; i <
}
}
private void MovePuzzle(object Sender, EventArgs E)
{
Button Bt1 = (Button)Sender;
Puzzle Pz1 = (Puzzle)Bt1.Tag;
// Dirty! Try way get a empty spase check Puzzle inside Button.
Button Bt2 = Puzzles.SingleOrDefault(Bt => !Bt.Visible);
Puzzle Pz2 = (Puzzle)Bt2.Tag;
if (Pz1.NearestWith(Pz2))
{
Swap(Bt1, Bt2);
Moves++;
}
CheckWin();
}
private void CheckWin()
{
bool
for (int i = 0; i <
{
Puzzle Pz
if (!Pz.IsTruePlace)
{
UWin = false;
break;
}
}
{
for (int i = 0; i < Puzzles.Count; i++)
{
Puzzles[i].Enabled = false;
}
TimeSpan Elapsed = DateTime.Now - Start;
Elapsed = TimeSpan.FromSeconds(Math.Round(Elapsed.TotalSeconds, 0));
MessageBox.Show(String.Format("Solved in {0} moves. Time: {1}", Moves, Elapsed));
}
}
private void Swap(
{
Puzzle Pz2 = (Puzzle)Bt2.Tag;
int g = Pz1.CurrentNumber;
Pz1.CurrentNumber = Pz2.CurrentNumber;
Pz2.CurrentNumber = g;
if (Pz2.IsMaxPuzzle) Bt2.Visible = false;
}
}</syntaxhighlight>
|