April 2007 - Posts

Writing very dynamic Puzzle

I wrote this puzzle while ago and thought it's time to share it, as when i searched the internet back the time od developing this i did not find any similar puzzle that expose this flexibility.

To have this flexibility on hand we have to do couple of things first of all generate buttons which will be the peaceis of the puzzle and then devuide the image and paint the peaices with it.

So first of all generating the number of the the peacies which can be non-symetric like 2 x 5 or 7 x 13 as you want.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 private void GenerateButtons(int rows, int columns)
{
//Resize panel
panel.Height = columns * 70;
panel.Width = rows * 70;

//double loop to create buttoms and in the way they fill in coulmns and rows.
for (int r = 0; r <= columns - 1; r++)//row variable
{
for (int c = 0; c <= rows - 1; c++)//coulmn variable
{
Button temp = new Button();
temp.Height = temp.Width = 70;//set square dimentions for the buttom
temp.Location = new Point(c * temp.Width, r * temp.Height);
temp.Click += new EventHandler(ImagesOnClick); //Attach the handler for the click event.
temp.FlatStyle = FlatStyle.Popup;
ALAllImages.Add(temp);
ALImagesLocations.Add(temp.Location); //Add location on every buttons on specific array
}
}
foreach (Button b in ALAllImages)
{
panel.Controls.Add(b); //Add buttons to the panel on the form.
}
//Assign the location of the last left cornerd buttom to the empty space.
FreeSquare.X = panel.Controls[panel.Controls.Count - 1].Location.X;
FreeSquare.Y = panel.Controls[panel.Controls.Count - 1].Location.Y;
//Preserve the last left cornerd buttom for movment
panel.Controls.RemoveAt(panel.Controls.Count - 1);
ALAllImages.RemoveAt(ALAllImages.Count - 1);
}

Now we will read from the user desierd image and paint every peace of the puzzle we have.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 private void PaintPuzzle(Bitmap UserInput, int rows, int columns)
{
//calculate area to pick color pixels
ArrayList SmallPics = new ArrayList(rows * columns);
int hp = 0;//lenth from the eadge of the panel
int vp = 0;
int x = 70;//dimention of the image
int y = 70;
for (int i = 0; i < (rows * columns); i++)
{
Bitmap b = new Bitmap(x, y);

for (int n = 0; n < x; n++)
{
for (int m = 0; m < y; m++) //creating small images 70*70 for every button.
{
int np = n + hp;
int mp = m + vp;
Color c = UserInput.GetPixel(np, mp);
b.SetPixel(n, m, c);
}
}

SmallPics.Add(b);

hp += 70; // for the movment of coloring.
if (hp >= rows * 70)
{ hp = 0; vp += 70; }
}

for (int i = 0; i < ALAllImages.Count; i++)
{
((Button)ALAllImages[i]).Image = (Image)SmallPics[i];//bind the small images to each button
}
}

After accomplish these two steps we are now have a bunch of buttoms with the picture painted on them, now we have to shuffle things a little.

1
2
3
4
5
6
7
8
9
10
11
12
13
private void ShuffleImages()
{
if (ALAllImages == null)
return;
Random factory = new Random();//create randome object
Button crazyButton = new Button();
int c = int.Parse(numericUpDown1.Value.ToString())*int.Parse(numericUpDown2.Value.ToString());
for (int i = 0; i < 25*c; i++)//100 movment at most to be done.
{
crazyButton = (Button)ALAllImages[factory.Next(ALAllImages.Count)];//choose random button
OneStep(crazyButton);//move the random button
}
}

All what left is to move the buttons to the right free direction.

This code will move the clicked button single step (depend on the button/peace dimentions) towrd the direction of the free square .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
private void OneStep(Button ClickedButton)
{
//Make sure that the clicked buttom is near free space horizontally or vertically
if (FreeSquare.Y == ClickedButton.Location.Y || FreeSquare.X == ClickedButton.Location.X)
{
//create a holder for the empty point which will change...
Point freesquareHolder = new Point();
freesquareHolder = ClickedButton.Location;

//tow possible group of movments up&down , right&left
//implementation for the movment vertically
if (FreeSquare.X == ClickedButton.Location.X)
{
foreach (Button b in panel.Controls)
{
if (((
b.Location.Y >= ClickedButton.Location.Y)
&& (b.Location.Y < FreeSquare.Y)
&& (b.Location.X == FreeSquare.X))
|| ((b.Location.Y <= ClickedButton.Location.Y)
&& (b.Location.Y > FreeSquare.Y)
&& (b.Location.X == FreeSquare.X)))
{
if (ClickedButton.Location.Y > FreeSquare.Y
&& ClickedButton.Location.X == FreeSquare.X)
{
b.Location = new Point(b.Location.X, b.Location.Y - b.Size.Height);// move up
}
if (ClickedButton.Location.Y < FreeSquare.Y
&& ClickedButton.Location.X == FreeSquare.X)
{
b.Location = new Point(b.Location.X, b.Location.Y + b.Size.Height); //move down
}
}
}
}

//implementation for horizontal movment
foreach (Button b in panel.Controls)
{
if (((
b.Location.X >= ClickedButton.Location.X)
&& (b.Location.X < FreeSquare.X)
&& (b.Location.Y == FreeSquare.Y))
|| ((b.Location.X <= ClickedButton.Location.X)
&& (b.Location.X > FreeSquare.X)
&& (b.Location.Y == FreeSquare.Y)))
{
if (ClickedButton.Location.X < FreeSquare.X
&& ClickedButton.Location.Y == FreeSquare.Y)
{
b.Location = new Point(b.Location.X + b.Size.Width, b.Location.Y);//move right
}
if (ClickedButton.Location.X > FreeSquare.X
&& ClickedButton.Location.Y == FreeSquare.Y)
{
b.Location = new Point(b.Location.X - b.Size.Width, b.Location.Y);//move left
}
}

}

FreeSquare = freesquareHolder;//return the original coordinate for the empty space
}
}

Full Code (VS 2005)

kick it on DotNetKicks.com
Posted by Adel Khalil with no comments
Filed under: , ,

Debug Windows Services

Phillip Jacobs has posted describing how to debug a Windows Service without having the process attached to the service... and i agree as attaching the process to the service and compile, deploy every time is just painfull, from my perspective the best and easiest way to do so is to have the statment Debugger.Launch and you will be able to go through your breakpoints.

http://msdn2.microsoft.com/en-us/library/system.diagnostics.debugger.launch.aspx

Phillip Jacobs post

 

Posted by Adel Khalil with no comments
Filed under: , ,

C# actually has Goto statment !!!

This comes like news for me, C# actually has Goto statment !!! WHY i would of said that no one will ever use it but i discovered this in production code.

here's what MSDN have to say about the Goto statment.

http://msdn2.microsoft.com/en-us/library/13940fs2(VS.80).aspx

UPDATE: I didn't find this in the company i work for production code.

Posted by Adel Khalil with no comments
Filed under:

New IT podcast in the atmosphere

Hi, great news theres new weekly podcast hosted by Richard Campbell & Greg Hughes ( and YES! produced by powp production )

the podcast will foucs in Microsoft side of technology and will be following the .NET Rocks pattern of having noted technology leads as guests..

as their first guest they interviewd Pat hynds the CTO of CriticalSites.

check it out at RunAs Radio

Posted by Adel Khalil with no comments
Filed under: ,

Self reminder..

Some things in life are meant to be, and sometimes they're not. It's our choice to find out.

Posted by Adel Khalil with no comments
Filed under:

UML and the Maverick style

I'm very confused about UML i didn't had a chance to deeply work with it but now i guess it's already prisoned inside the boundaries of whiteboards nothing like UML will appear in any place other than the white board in the meeting room. Maybe i don't know I'm not working like you in a fancy software vendor - or it's not a fancy big

sw vendor characteristic - so i don't have all the fancy must well done water flow model so we can have the time to do the work, my style is a Maverick style of coding - don't remember who came up with the name - and potential testing where you just wanted to get the initial specs and run into your favorite kinda of IDE and start getting things done - if you know what i mean - even many of the companies that adopt the Agile methodologies dot relaying on UML in design like the idea of Agile and freedom of developer is liberate them from having everything figured out prior.

I see software methodologies - management, design, developing ..etc.. - are interrelated together it's hard for me to imagine a Ruby developer doing some deep design before start coding on the fly, in the other hand Java folks or other old OOP languages are more imaginable as design duds.

So if this is correct then Agile management will affect other sides of software practise to be agile too as the mentality that choose a style of doing design will choose similar style of management and so on..

Back to UML for instance, i see that converting from the high business level to a DB entities for example is alto harder for developers and starting Architects as we learned and worked with the DB point of view very much that the human entities is now so hard to understand in someway we are thinking like Computer better in a certain way than thinking in normal human way, that why we may spending a lot of time on Visio tying to model the business to create the back-end schema when we - as a developer thinking like computers - can easily created the desired table into the DBMS.

Maybe this is why i hated UML - well not hated it -  but at least not prefer working with it outside the whiteboard.

So how you design and what is the pattern of methodologies you are using ? Are you a fan of the Maverick Coders style despite of all it's drawbacks ?

Posted by Adel Khalil with 2 comment(s)

What is your 5 years old code look like ?

Scott Hanselman blog recently about the Tiny OS he wrote in C# while ago, also he mentioned the Singularity project which is a managed operatiing system written entirly in C# on top of smaller CLR implementation.

Anyway this post made me try to find any code i wrote 5+ years ago.. and i didn't that long ago i wasn't writing code for living so i wasn't pretty much about taking care of my code in safe place, so i end up losing all the small programs i wrote in my eairly trials into programming world, i was using Visual Basic 4.x back then so i went back and the only peace of code i fould, not actually peace of code..it was the excutable hosted by programs portal - like www.download.com - but much local/smaller called www.absba.com the bad thing is that i couldn't even download the program , the link was broken.. but at least see what the guy wrote about my little software at that time... this was like 8+ years ago.

One of the visitors to the site programmers privileged program put passwords on files idea of the program : a people program develops coding at the beginning and end of the file ..... This encoder and coding of a mixture of Altselisls figure of the painting Mother and data from the same body and add coding at the beginning and end of files to prevent the opening ..... The file could not be opened even if the survey had been hand-coding that if the deadline-______________________, PWSETUP first is the file instead of the stabilization program for reducing the area ... It Balfmer that this method the security of roads by programs such as magic folder and folder guard - here
It would of been awsome if i found the coe i wrote in VB 8 years ago, have you done any and still have it?
Posted by Adel Khalil with no comments
Filed under: