using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
namespace Slozuvalka
{
public partial class Form1 : Form
{
private Bitmap bmpMainImage;
private ArrayList buttonsList;
private ArrayList imageParts;
private System.Drawing.Point pntEmpty;
private bool gameInProgress;
public Form1()
{
InitializeComponent();
buttonsList = new ArrayList();
imageParts = new ArrayList();
pntEmpty = new Point();
gameInProgress = false;
foreach (Button b in panelMain.Controls)
buttonsList.Add(b);
InitPicture();
pntEmpty.X = 360;
pntEmpty.Y = 360;
StartNewGame();
}
private void StartNewGame()
{
SplitPictureButtons();
ScrambleButtons();
}
private void InitPicture()
{
try
{
bmpMainImage = (Bitmap) Bitmap.FromFile(Application.StartupPath + "\\ducklings.jpg");
}
catch (Exception ex)
{
ex.ToString();
MessageBox.Show("There was an internal error. The picture was not found.\nThe program will now exit.", "Error #4541");
Environment.Exit(-1);
}
if (bmpMainImage.Height != 480 || bmpMainImage.Width != 480 )
{
MessageBox.Show("Corrupted Image.\nThe program will now exit.", "Error #4542");
Environment.Exit(-1);
}
}
private ArrayList CropPicture(int width, int height)
{
ArrayList alTemp = new ArrayList();
int nHeight = 0;
int nWidth = 0;
Bitmap bmpPictureBlock;
for (int k=0; k<15; k++)
{
bmpPictureBlock = new Bitmap(width, height);
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++)
bmpPictureBlock.SetPixel(i, j, bmpMainImage.GetPixel((i + nWidth), (j + nHeight)));
alTemp.Add(bmpPictureBlock);
nWidth += 120;
if (nWidth == 480)
{
nWidth = 0;
nHeight += 120;
}
}
return alTemp;
}
private void MoveButton(Button btnBlockClicked)
{
if (pntEmpty.X == btnBlockClicked.Location.X
|| pntEmpty.Y == btnBlockClicked.Location.Y)
{
Point pntTemp = new Point();
pntTemp = btnBlockClicked.Location;
int d = HelperFunctions.GetDirection(btnBlockClicked.Location, pntEmpty);
if (pntEmpty.X == btnBlockClicked.Location.X)
{
foreach (Button bx in panelMain.Controls)
{
if (((bx.Location.Y >= btnBlockClicked.Location.Y)
&& (bx.Location.Y < pntEmpty.Y) && (bx.Location.X == pntEmpty.X)) || ((bx.Location.Y <= btnBlockClicked.Location.Y)
&& (bx.Location.Y > pntEmpty.Y) && (bx.Location.X == pntEmpty.X)))
{
switch (d)
{
case 1:
HelperFunctions.MoveUp(bx);
break;
case 3:
HelperFunctions.MoveDown(bx);
break;
}
}
}
}
if (pntEmpty.Y == btnBlockClicked.Location.Y)
{
foreach (Button bx in panelMain.Controls)
{
if (((bx.Location.X >= btnBlockClicked.Location.X)
&& (bx.Location.X < pntEmpty.X) && (bx.Location.Y == pntEmpty.Y)) || ((bx.Location.X <= btnBlockClicked.Location.X)
&& (bx.Location.X > pntEmpty.X) && (bx.Location.Y == pntEmpty.Y)))
{
switch (d)
{
case 0:
HelperFunctions.MoveRight(bx);
break;
case 2:
HelperFunctions.MoveLeft(bx);
break;
}
}
}
}
pntEmpty = pntTemp;
}
}
private void ScrambleButtons()
{
Random r = new Random();
Button tempBtn = new Button();
for (int i = 0; i < 100; i++)
{
tempBtn = (Button)buttonsList[r.Next(buttonsList.Count)];
MoveButton(tempBtn);
}
}
private void SplitPictureButtons()
{
imageParts = CropPicture(120, 120);
button1.Image = (Image)imageParts[0];
button2.Image = (Image)imageParts[1];
button3.Image = (Image)imageParts[2];
button4.Image = (Image)imageParts[3];
button5.Image = (Image)imageParts[4];
button6.Image = (Image)imageParts[5];
button7.Image = (Image)imageParts[6];
button8.Image = (Image)imageParts[7];
button9.Image = (Image)imageParts[8];
button10.Image = (Image)imageParts[9];
button11.Image = (Image)imageParts[10];
button12.Image = (Image)imageParts[11];
button13.Image = (Image)imageParts[12];
button14.Image = (Image)imageParts[13];
button15.Image = (Image)imageParts[14];
}
private void OnButtonClick(object sender, System.EventArgs e)
{
gameInProgress = true;
MoveButton((Button)sender);
}
private void newGameToolStripMenuItem_Click(object sender, EventArgs e)
{
if (gameInProgress == true)
{
if (MessageBox.Show("Are you sure you want to start a new game?\n Any progress will be lost.", "New Game", MessageBoxButtons.YesNo) == DialogResult.Yes)
StartNewGame();
}
else
StartNewGame();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Are you sure you want to exit?\nAny progress will be lost.", "Quit Game",
MessageBoxButtons.YesNo) == DialogResult.Yes)
Application.Exit();
}
private void originalImageToolStripMenuItem_Click(object sender, EventArgs e)
{
PictureViewer picView = new PictureViewer(bmpMainImage);
picView.Show();
}
}
}