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 gridSizeGridSize = 4; //Standard 15 puzzle is 4x4
const boolint evenSizedBlockCount = gridSize % 2 == 016;
const int blockCount = gridSize * gridSize;
const int last = blockCount - 1;
const int buttonSize = 50;
const int buttonMargin = 3; //default = 3
const int formEdge = 9;
static readonly Random rnd = new Random();
static readonly Font buttonFont = new Font("Arial", 15.75F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
readonly Button[] buttons = new Button[blockCount];
readonly int[] grid = new int[blockCount];
readonly int[] positionOf = new int[blockCount];
int moves = 0;
DateTime start;
 
public static voidreadonly Main(string[]Random argsR = new Random();
 
{
private List<Button> FifteenPuzzle pPuzzles = new FifteenPuzzleList<Button>();
private int Form fMoves = p.BuildForm()0;
private DateTime Start;
Application.Run(f);
}
 
private class Puzzle
public FifteenPuzzle()
{
forprivate (int i = 0mOrderedNumer; i < blockCount; i++) {
private int grid[i] = i;OrderedNumer
positionOf[i] = i;{
get { return mOrderedNumer; }
}
}
 
public int CurrentNumber;
Form BuildForm()
{
Button startButton = new Button {
Font = new Font("Arial", 9.75F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0))),
Size = new Size(86, 23),
Location = new Point(formEdge,
(buttonSize + buttonMargin * 2) * gridSize + buttonMargin + formEdge),
Text = "New Game",
UseVisualStyleBackColor = true
};
startButton.Click += (sender, e) => Shuffle();
 
public int X;
int size = buttonSize * gridSize + buttonMargin * gridSize * 2 + formEdge * 2;
Formpublic formint = new Form {Y;
 
Text = "Fifteen",
public Puzzle(int OrderedNumer)
ClientSize = new Size(width: size, height: size + buttonMargin * 2 + startButton.Height)
};{
form.SuspendLayout() mOrderedNumer = OrderedNumer;
for (int index = 0;CurrentNumber index= < blockCountOrderedNumer; index++) {
 
Button button = new Button {
X = OrderedNumer % Font = buttonFont,GridSize;
SizeY = newOrderedNumer Size(buttonSize,/ buttonSize),GridSize;
//Margin = new Padding(buttonMargin),
Text = (index + 1).ToString(),
UseVisualStyleBackColor = true
};
SetLocation(button, index);
form.Controls.Add(button);
buttons[index] = button;
int i = index;
button.Click += (sender, e) => ButtonClick(i);
}
form.Controls.Add(startButton);
form.ResumeLayout();
return form;
}
 
public bool IsMaxPuzzle
void ButtonClick(int i)
{
if (buttons[last].Visible) get { return CurrentNumber >= (BlockCount - 1); }
int target = positionOf[i];
if (positionOf[i] / gridSize == positionOf[last] / gridSize) {
while (positionOf[last] < target) {
Swap(last, grid[positionOf[last] + 1]);
moves++;
}
while (positionOf[last] > target) {
Swap(last, grid[positionOf[last] - 1]);
moves++;
}
} else if (positionOf[i] % gridSize == positionOf[last] % gridSize) {
while (positionOf[last] < target) {
Swap(last, grid[positionOf[last] + gridSize]);
moves++;
}
while (positionOf[last] > target) {
Swap(last, grid[positionOf[last] - gridSize]);
moves++;
}
}
ifpublic bool NearestWith(Solved(Puzzle OtherPz)) {
{
TimeSpan elapsed = DateTime.Now - start;
elapsedint dx = TimeSpan.FromSeconds(MathX - OtherPz.Round(elapsed.TotalSeconds, 0)X);
buttons[last].Visibleint dy = true(Y - OtherPz.Y);
 
MessageBox.Show($"Solved in {moves} moves. Time: {elapsed}");
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)
bool Solved() => Enumerable.Range(0, blockCount - 1).All(i => positionOf[i] == i);
{
FifteenPuzzle Game = new FifteenPuzzle();
Application.Run(Game.CreateForm());
}
 
private Form CreateForm()
static void SetLocation(Button button, int index)
{
int rowButtonSize = index / gridSize, column = index % gridSize50;
button.Locationint =ButtonMargin new= Point(3;
int FormEdge = 9;
(buttonSize + buttonMargin * 2) * column + buttonMargin + formEdge,
 
(buttonSize + buttonMargin * 2) * row + buttonMargin + formEdge);
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 ShuffleNewGame(object Sender, EventArgs E)
{
for (int i = 0; i < blockCountPuzzles.Count; i++) {
{ int r = rnd.Next(i, blockCount);
intButton gBt1 = gridPuzzles[rR.Next(i, Puzzles.Count)];
grid[r]Button Bt2 = gridPuzzles[i];
grid[i]Swap(Bt1, = gBt2);
}
for (int i = 0; i < blockCountPuzzles.Count; i++) {
positionOf[grid[i]] = i;{
SetLocation(buttons[gridPuzzles[i]],.Enabled i)= true;
}
if (!Solvable()) Swap(0, 1); //Swap any 2 blocks
 
buttons[last].VisibleMoves = false0;
movesStart = 0DateTime.Now;
start = DateTime.Now;
}
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))
bool Solvable()
{
Swap(Bt1, Bt2);
Moves++;
}
 
CheckWin();
}
 
private void CheckWin()
{
bool parityUWin = true;
for (int i = 0; i < blockCount - 2Puzzles.Count; i++) {
{
for (int j = i + 1; j < blockCount - 1; j++) {
Puzzle Pz if= (positionOf[j] < positionOfPuzzle)Puzzles[i]) parity = !parity.Tag;
 
if (!Pz.IsTruePlace)
{
UWin = false;
break;
}
}
 
if (evenSized && positionOf[last] / gridSize % 2 == 0) parity = !parity;
returnif parity;(UWin)
{
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(intButton aBt1, intButton bBt2)
{
Pointif location(Bt1 == Bt2) buttons[a].Locationreturn;
 
buttons[a].Location = buttons[b].Location;
buttons[b].LocationPuzzle Pz1 = location(Puzzle)Bt1.Tag;
Puzzle Pz2 = (Puzzle)Bt2.Tag;
 
int g = Pz1.CurrentNumber;
Pz1.CurrentNumber = Pz2.CurrentNumber;
Pz2.CurrentNumber = g;
 
int pBt1.Visible = positionOf[a]true;
positionOf[a]Bt1.Text = positionOf[b]Pz1.ToString();
positionOf[b]if (Pz1.IsMaxPuzzle) Bt1.Visible = pfalse;
 
grid[positionOf[a]]Bt2.Visible = atrue;
grid[positionOf[b]]Bt2.Text = bPz2.ToString();
if (Pz2.IsMaxPuzzle) Bt2.Visible = false;
}
}</syntaxhighlight>
7

edits