Пятнашек захотелось? Извиняй, но есть только на С#. Переделывать на плюсы влом - хочешь, поиграйся... ))
[codebox]
using System;
using System.Drawing;
using System.Windows.Forms;
namespace CSharpApp.WindowAppExample
{
class Game : Form
{
private const int Side = 4;
private const int Void = Side * Side;
private int Voidx = Side - 1, VoidY = Side - 1;
private Button[,] Field;
private int[,] Numbers;
private int Moves;
private bool IsGameRun;
private Label clock = new Label();
private Timer timer = new Timer();
private Random Randomize = new Random();
private TimeSpan time;
static void Main()
{
Application.Run(new Game());
}
public Game()
{
Text = "Пятнашки";
FormBorderStyle = FormBorderStyle.Fixed3D;
MaximizeBox = false;
ClientSize = new Size(Side * 50 + 20, Side * 50 + 50);
BackColor = Color.Silver;
Field = new Button[Side, Side];
Numbers = new int [Side, Side];
MenuItem miNewGame = new MenuItem("Новая игра", new EventHandler(OnMenuStart), Shortcut.F2);
MenuItem miSeparator = new MenuItem("-");
MenuItem miExit = new MenuItem("Выход", new EventHandler(OnMenuExit), Shortcut.CtrlX);
MenuItem miGame = new MenuItem("&Игра", new MenuItem[] { miNewGame, miSeparator, miExit });
Menu = new MainMenu(new MenuItem[] { miGame });
IsGameRun = false;
timer.Interval = 1000;
timer.Tick += new EventHandler(OnTimer);
clock.Location = new Point(10, 10);
clock.Width = Side * 50;
clock.Height = 20;
clock.Parent = this;
clock.BorderStyle = BorderStyle.FixedSingle;
clock.BackColor = Color.DarkGray;
clock.TextAlign = ContentAlignment.MiddleCenter;
clock.Font = new Font("Century", 14, FontStyle.Bold);
clock.Text = "00:00:00";
int i, j;
for (i = 0; i < Side; i++ )
{
for (j = 0; j < Side; j++)
{
Field[i, j] = new Button();
Field[i, j].Parent = this;
Numbers[i, j] = i * Side + j + 1;
if (Numbers[i, j] != Void)
Field[i, j].Text = Convert.ToString(Numbers[i, j]);
Field[i, j].Left = 10 + j * 50;
Field[i, j].Top = 40 + i * 50;
Field[i, j].Width = 50;
Field[i, j].Height = 50;
Field[i, j].Font = new Font("Century", 12, FontStyle.Bold);
Field[i, j].Tag = new Point(i, j);
Field[i, j].Click += new EventHandler(OnCellClick);
Field[i, j].ForeColor = Color.Coral;
Field[i, j].BackColor = Color.Gray;
}
}
CenterToScreen();
}
public void OnMenuExit(object obj, EventArgs ea)
{
Close();
}
public void OnMenuStart(object obj, EventArgs ea)
{
int i, j, k;
int direction;
int Times = Side * 100;
for (k = 0; k < Times; k++)
{
direction = Randomize.Next(4);
if (direction == 0)
{
if (Voidx - 1 >= 0)
{
Numbers[Voidx, VoidY] = Numbers[Voidx - 1, VoidY];
Voidx--;
}
else
{
for (i = 0; i < Side - 1; i++)
{
Numbers[i, VoidY] = Numbers[i + 1, VoidY];
}
Voidx = Side - 1;
}
}
else if (direction == 1)
{
if (Voidx + 1 < Side)
{
Numbers[Voidx, VoidY] = Numbers[Voidx + 1, VoidY];
Voidx++;
}
else
{
for (i = Side - 1; i > 0; i--)
{
Numbers[i, VoidY] = Numbers[i - 1, VoidY];
}
Voidx = 0;
}
}
else if (direction == 2)
{
if (VoidY - 1 >= 0)
{
Numbers[Voidx, VoidY] = Numbers[Voidx, VoidY - 1];
VoidY--;
}
else
{
for (j = 0; j < Side - 1; j++)
{
Numbers[Voidx, j] = Numbers[Voidx, j + 1];
}
VoidY = Side - 1;
}
}
else
{
if (VoidY + 1 < Side)
{
Numbers[Voidx, VoidY] = Numbers[Voidx, VoidY + 1];
VoidY++;
}
else
{
for (j = Side -1; j > 0; j--)
{
Numbers[Voidx, j] = Numbers[Voidx, j - 1];
}
VoidY = 0;
}
}
Numbers[Voidx, VoidY] = Void;
}
for (i = 0; i < Side; i++)
{
for (j = 0; j < Side; j++)
{
if (Numbers[i, j] != Void)
Field[i, j].Text = Convert.ToString(Numbers[i, j]);
else
Field[i, j].Text = "";
}
}
Moves = 0;
IsGameRun = true;
time = new TimeSpan(0, 0, 0);
clock.Text = "00:00:00";
timer.Start();
}
public void OnCellClick(object obj, EventArgs ea)
{
if (IsGameRun == false)
return;
Button btn = (Button)obj;
int i = ((Point)btn.Tag).X;
int j = ((Point)btn.Tag).Y;
if (Math.Abs(i - Voidx) + Math.Abs(j - VoidY) == 1)
{
Numbers[Voidx, VoidY] = Numbers[i, j];
Field[Voidx, VoidY].Text = Field[i, j].Text;
Voidx = i;
VoidY = j;
Numbers[Voidx, VoidY] = Void;
Field[Voidx, VoidY].Text = "";
Moves++;
}
if (Voidx == Side - 1 && VoidY == Side - 1)
{
if (IsWinner() == true)
{
timer.Stop();
string msg = "Поздравляю!!!\nВы победили за ";
msg += Moves;
if (Moves % 10 > 1 && Moves % 10 < 5 && Moves % 100 / 10 != 1)
msg += "хода.";
else if(Moves % 10 == 1 && Moves % 100/10 != 1)
msg += "ход.";
else
msg += "ходов";
IsGameRun = false;
MessageBox.Show(msg, "Победа!!!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
public bool IsWinner()
{
int i, j, k = 1;
for (i = 0; i < Side; i++)
{
for (j = 0; j < Side; j++)
{
if (Numbers[i, j] != k)
return false;
k++;
}
}
return true;
}
public void OnTimer(object obj, EventArgs ea)
{
time += new TimeSpan(0, 0, 1);
clock.Text = time.ToString();
}
protected override void OnActivated(EventArgs ea)
{
base.OnActivated(ea);
if (IsGameRun == true)
timer.Start();
}
protected override void OnDeactivate(EventArgs ea)
{
base.OnDeactivate(ea);
if (IsGameRun == true)
timer.Stop();
}
}
}
[/codebox]