[TUT] How to complete the 2048 tile game

Do you ever heard about Flappy Bird? Flappy Bird is a hottest game a time ago, but it was deleted by it's creator, and the new hot game appear: 2048 tile game. You can download the game on App Store or Google Play, or you can play online at http://gabrielecirulli.github.io/2048/

2048 is a tile game, you need to use your brain. The game has a board included 4x4 tile, the rule is easy: you have to use arrow keys to move entire tile up, down, left, right to complete the game:

 - Each time you move, the number tiles on board will be move to the direction you want and another tile will appear randomly (tile with number 2 or 4) on the empty tiles.
 - 2 tiles with the same number on the way you move will be merge together and become 1 number tile with the value is the sum from 2 tiles before.

To win, you have to merge tiles to create the 2048 tile. It sound easy, but it's hard to win. Anyway, I found a strategy to complete the game, that strategy I called it "3 magic directions".

I called it with that name because the method of this strategy is to use only 3 adjacent directions movement to sort the tile (example: up-right-down, or right-down-left), you shouldn't or avoid using the other direction.

This strategy is use to sort the tile by [n,n*2,n*4,n*8] (example: [64,128,256,512] ) of a row or column at the middle direction in 3 adjacent directions.
Example:
 - If you use left-down-right: then sort the 4th row (the last row at the bottom standing at the DOWN direction - the middle direction in 3 adjacent directions)
 - If you use up-right-down: sort the 4th column (the last column on the right - the middle direction in 3 adjacent directions)
2048 tile game how to win
Use left-down-right

Note: Use should only use the other direction if you can't move any more using 3 adjacent directions.

This is my result using up-right-down
2048 tile game win
Use UP-RIGHT-DOWN
P/S: use this strategy will increase the chance to win up to 90%
Wish you have a fun time with this game. Any problem, feel free to leave comment. Thanks all ^^

[TUT] Cách để hoàn thành game 2048

Như các bạn đã biết, hiện tượng Flappy Bird nổi đình đám trong thời gian qua nay đã lắng xuống, thay vào đó là hiện tượng mới tên là 2048. Bạn có thể tải game trên App Store hoặc Google Play, hoặc chơi online tại http://gabrielecirulli.github.io/2048/

2048 là một dạng tile game, một trò chơi mang tính trí tuệ, trò chơi là 1 bàn cờ gồm 4x4 ô, cách chơi rất đơn giản, đó là dùng các phím di chuyển lên, xuống, trái, phải để hoàn thành trò chơi:

 - Mỗi khi bạn dùng 1 phím di chuyển thì các ô số hiên có trên bàn cờ sẽ dồn về phía đó và sẽ xuất hiện thêm 1 ô số khác (ngẩu nhiên 2 hoặc 4) ở những ô trống còn lại một cách ngẫu nhiên.
 - 2 ô có số giống nhau khi được dồn về 1 phía sẽ cộng dồn vào nhau và sẽ mất đi 1 ô trong 2 ô đó.

Mục tiêu của trò chơi là ghép các ô với nhau sao cho ghép thành ô có số 2048. Thoạt nhìn thì có vẻ đơn giản, nhưng khi chơi rồi mới biết nó khó. Tuy nhiên, mình đã tìm hiểu được chiến thuật để có thể hoàn thành trò chơi, chiến thuật đó mình gọi là chiến thuật "3 hướng liền kề".

Vì sao mình lại gọi như vậy, đó là vì phương thức của chiến thuât này là chỉ dùng 3 hướng di chuyển kế nhau để sắp xếp các ô (ví dụ: trên-phải-dưới, hoặc phải-dưới-trái), không nên hoặc hạn chế dùng tới hướng di chuyển còn lại.

Mục tiêu của chiến thuật này là làm sao để sắp xếp các ô theo thứ tự [n,n*2,n*4,n*8] (ví dụ: [64,128,256,512] ) của hàng hoặc cột nằm ở hướng giữa trong 3 hướng liền kề.
Ví dụ:
 - Đối với trái-dưới-phải: thì sắp xếp hàng thứ 4 từ trên đếm xuống (hàng này nằm hướng dưới - hướng giữa trong 3 hướng)
 - Đối với trên-phải-dưới: thì sắp xếp cột thứ 4 từ trái đếm qua (cột này nằm ở hướng phải - hướng giữa trong 3 hướng)
2048 tile game how to win
Đối với 3 hướng trái-dưới-phải (các ô được sắp xếp thứ tự)

Lưu ý: Chỉ nên dùng hướng di chuyển còn lại khi không còn hướng di chuyển nào trong 3 hướng liền kề được nữa.

Và đây là kết quả của mình khi dùng 3 hướng trên-phải-dưới
2048 tile game win
Đối với trên-phải-dưới
P/S: sử dụng chiến thuật sẽ tăng khả năng chiến thắng của bạn lên khoảng 90%
Chúc bạn chinh phục được game này, có góp ý hay thắc mắc, các bạn cứ để lại lời nhắn. Thanks all ^^

How to read emails on mail server ?

First of all there are multiple protocols to retreive mail:
POP3, IMAP, etc...
I suggest you start by familiarizing yourself with the various components that make up an e-mail system.
  • Mail Transfer Agent (Protocol: SMTP)
  • Mail Delivery Agent (Protocols: POP3, IMAP)
  • Mail User Agent (Outlook, Webmail, Thunderbird, your application)
Basically what you are trying to write is a Mail User Agent. A mail user agent has to "fetch" the mails from a Mail Delivery Agent using either POP or IMAP.
This means you will have to learn about these two protocols:
POP3 RFC: http://www.faqs.org/rfcs/rfc1939.html
IMAPv4 RFC: http://james.apache.org/server/rfclist/imap4/rfc2060.txt
Since e-mail communication happens using TCP/IP you will have to learn how to use the classes in the System.Net.Sockets namespace.
Also take a look at the TcpClient class.
Try to understand these concepts first and then I suggest you start out with POP3, this protocol is quite easy. If you have problems then with very specific TcpClient code please update your question or post a new question.
Hope this sets you on the right track.

P/S: I've complete my first program that read emails in Gmail & Download the attachments in selected emails (using VB.NET 2008).

How to Calculating the Perceived Brightness of a Color

When I'm searching on the Internet for the Image Processing Algorithm, I found this useful article, so I copy it to my blog. Thanks the Author of nbdtech.com.


I needed a way to test if a background color is light or dark in order to choose an appropriate text color (black on light colors and white on dark colors), you can find yourself in the same problem if you try to convert an image to grayscale.
I found many approaches that didn’t work well, I’ll describe them and the problems I discovered below – but first – the successful solution.
I finally found a solution that actually works on this web page the formula is:
brightness  =  sqrt( .241 R2 + .691 G2 + .068 B2 )
Or, in C# using the WPF's Color structure System.Windows.Media.Color (the same exact code should also work with the System.Drawing.Color structure used in WinForms and WebForms):
private static int Brightness(Color c)
{
   return (int)Math.Sqrt(
      c.R * c.R * .241 + 
      c.G * c.G * .691 + 
      c.B * c.B * .068);
}
This method will return a number in the rage of 0 (black) to 255 (White) and to set the foreground color based on the Brightness method:
Color textColor = Brightness(backgroundColor) < 130 ? Colors.White : Colors.Black;
I selected cutoff value of 130 by trial and error and it reflects my taste, every value in the rage 128-145 will give acceptable results.
Here is a table of all the named colors in .net 3.0, each with its name in readable text on it based on the code above, the number in parenthesis is the brightness – click on the image to view it in full size (in a new browser window).
Usually, when talking about color theory the range 0-1 is used for each component – so (0,0,0) is black, (1,1,1) is white and (0.5,0.5,0.5) is 50% gray, I’m not using this range, I’m using the 0-255 range used in most programming environments.

Why Not HSL or HSV?

You may think that the luminance (or lightness) component of the HSL color system or the value component of HSV will solve this problem, I did, but I was wrong.
The L component of the HSL and the V component of HSV describe the brightness of a color relative to a base color, if this base color is blue you will get a darker color than if this base color is yellow, HSL and HSV are very useful if you need to create a lighter or darker version of a color but aren’t very useful if you want to know how bright a color is.

The Naïve RGB Algorithm

If RGB 0,0,0 is black and RGB 255,255,255 is white then R+B+G should give us a good approximation of the color brightness, shouldn’t it?
The problem is that some colors are brighter than others, red is brighter then blue and green is brighter then red, maybe we just need to find the right coefficients for them?

The W3C Algorithm

The W3C working draft on accessibility has a formula for the perceived brightness of a color (based on the YIQ color system):
((Red value X 299) + (Green value X 587) + (Blue value X 114)) / 1000
This formula and references to it dominate the search results, probably because the W3C has high search engine rank.
This is better than using HSL, but it tends to give wrong results for many colors, especially shades of yellow.

RGB Color Difference

The W3C also has an algorithm to calculate the difference between colors:
(maximum (Red value 1, Red value 2) - minimum (Red value 1, Red value 2)) + (maximum (Green value 1, Green value 2) - minimum (Green value 1, Green value 2)) + (maximum (Blue value 1, Blue value 2) - minimum (Blue value 1, Blue value 2))
Or, in shorter form:
dR + dG + dB
Where dR, dG and dB are the difference in the Red, Green and Blue component.
If you calculate difference from white and difference from black – and compare them (if difference from black>difference from white then the color is dark) you actually get better results than using the brightness formula, but then you get wrong results for light greens and dark blues (probably because this formula doesn’t take into account the brightness difference of the colors).

3D Distance in RGB Space

You can think of the RGB color space as a cube where each of the 3 colors are axis, in one corner you have black (RGB 0,0,0) and in the opposite corner you have white RGB (255,255,255), so if a color is closer to black it should be darker.
The formula for 3D distance is:
Sqrt(dx2+dy2+dz2)
Where dx, dy and dz are the difference on the x, y and z axis.
This algorithm also gives mixed results because it doesn’t take into account the fact that some colors look brighter than others -  and that’s gets us back to …

Weighted Distance in 3D RGB Space (or the HSP algorithm)

If you scroll up to the beginning of this post and look at the brightness formula there you will see it’s just like the 3D distance formula except it gives different weight to each axis.
So now we finished our tour to color brightness land we have a good formula for calculating perceived brightness and (if my explanations where clear) we can actually understand why it works.

Source: nbdtech.com

Sponsors