Early this year, disturbed by some ideas made me open notepad and started to whip up my first “web application” of the year.
A simple countdown DHTML page - a combination of html, javascript, css and dom - was created. The purpose was simple, to create a count down in different units (ie. years, months, weeks, days, hours, mins, seconds, ms) to my ORD date. A windows xp wallpaper is added as the background, then use Active Desktop to cover the entire desktop background, with the time remaining running continuously like magic. (css was used to create a bar chart to show percentage of time remaining in my whole ns, and the working hours left when run.. well i thought this could be improved to be come a booklet or wordpress plugin too)
This addiction to programming led me to coin the description “programmerholic” on my msn nick. This may give the impression of my love for programming, but it meant time, brain cells, effort were much used to compensate of my technical limitations. The byproducts are frustrations, messy coding but yet hopefully some working code, practicality and satisfactions.
Here’s another example how I’m program impulsively in a spurn of moment. My 2nd program of 2007 - another javascript “software” to solve a puzzle using brute force to find a solution to the many different combinations.
One weekend, I saw a link to a difficult Russian puzzle over at digg.com, a tessellation puzzle where 5 pieces were needed to be arranged into a confined area. The popular game called Tetris was invented by a russian, which makes me feel that they are very good with shapes.
Later that day, my friend searched “russian puzzle” and we eneded trying to solve another puzzle called 8 Queens - to place eight queens on a chessboard so that no queen attacks another over at http://sakharov.net/puzzle/.
This is a interesting puzzle, something which may not seem difcult, as you try to place the last few chese pieces on the board, you’ve find that its not so simple. Simple mathematics shows that there were going to be many combinations, but I knew the human mind was good enough to process and filter them quickly to get the answer. However, knowing that writting a script or program would spoil some fun, I did otherwise with the following reason
- We were having headaches that we didn’t really have concentration and patience to solve the puzzle
- Lazy to solve it the “normal human way”
- The computer could come up results faster and better than we do
- Computers are good at crunching numbers, which was what I was going to make it do
Therefore, I type the following into a html file (you can try it too), load it in the browser, and within seconds gave me 92 solutions (considered the board can be rotated 4 times, 23 solutions. with mirror images, perhaps less than 23 unique solutions with at least 1 solution non mirrorable pattern). Note: for the numbers displayed, each digit refer to the position of the queen- i thought using number could make a good data structure too.
return “have fun”; /* end of my post now for now.. */
solvequeens.htm
————————
<script>
function valid(situation, level, position)
{
for (var x=0;x<level;x++)
{
digit = Math.floor( situation / Math.pow(10,x)) % 10
// check column
if (position==digit) return false;
// check diagonal
left = position - x - 1
right = position + x + 1
if ((left>0) && (left==digit)) return false;
if ((right<10) && (right==digit)) return false;
}
return true;
}
function solveQueen (situation, level)
{
//alert( "Solving " + situation + " " +level)
if (level==8) {
sn ++;
document.write
("Solution " +sn + " found! "+ situation +"<br/>")
//alert ("Solution found! "+ situation)
return true
}
for (var x=1;x<=8;x++)
{
if (valid(situation, level, x)) {
//alert(situation+ " "+ x1)
//solveQueen ((situation + x *
Math.pow(10,level)), level+1) // reverse number "data structure"
solveQueen (situation * 10 + x , level+1)
}
}
// end solvequeen
}
sn = 0
alert("start")
solveQueen (0,0)
alert("end")
</script>



Recent Comments