Posts

Creating a Simple "9 Lights Off" game in html

Image
In this tutorial I am going to create a simple game using plain JavaScript and HTML. It is a remake of one of the puzzles in the Android app I created some years back called " Cream of Puzzles ". The completed HTML version looks like below: Here are a few points to note: In a full fledged game you typically draw game elements on the canvas. You don't use common UI elements to represent game objects. In this tutorial we are not going to include sound effects and animation effects. All the game elements in this tutorial are simple rigid elements so it is enough to depend entirely on basic HTML elements and not use usual game elements (Sprites) Hear is the entire HTML source code: <html> <head> <title> 9 Lights Off! </title> <style> body { background-color : #008080 ; } table { width : 50 % ; ...

Multiply Characters in a String

Image
Write method to make decryption for the String as the following: Input: a8b2mBa3d9x7uA Output: aaaaaaaabbmmmmmmmmmmmaaadddddddddxxxxxxxuuuuuuuuuu Let's first see how this problem can be solved using Groovy before we get to Java. We shall create a method that takes in a string as input and returns the desired string.  Our first attempt fails because there is something somehow subtle in the input string -the numbers are given in hexadecimal format, that is base 16. We definitely have to modify the algorithm. We have to change the way we are converting Strings to integers.  And it works! Next we should move on to the implementation in Java.  public class HelloWorld { public static void main ( String [] args ){ System . out . println ( transform ( "a8b2mBa3d9x7uA" ) ); } public static String transform ( String str ){ String toReturn = "" ; String letter = "...

Counting Characters in a String using Java

Write a program that coverts input like "aaaaabbccc" to output: a5b2c3 Using functional programming the solution to this problem is trivial. See for example the solution in Groovy: input = "aaaaabbccc" println input. toList (). countBy {it}. collect {it. key + it. value }. join () However, the solution using procedural programming is not as straightforward especially if you don't want to use utility classes like List and maps or dictionaries. Look for example at the the following solution in Java. package realcodeexamples ; /** * * @author Trevor Sinkala */ public class RealCodeExamples { /** * @param args the command line arguments */ public static void main ( String [] args ) { System . out . println ( countChars ( "aaaaaaaaabbcc" )); } public static String countChars ( String input ) { String [] arr = new String [ input . length ()]; for ( i...

Printing Patterns with the For Loop - Part One

Image
(A)              1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 (B) 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 (C) 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 (D)   1  2 2 3 3 3  2 2   1 (E) 1  2  3 4  5  6 7  8  9     Printing patterns is often part of the school exercises you should do when you are doing your introductory programming course. It helps you learn how to apply your brains as a programmer. Whilst many examples of how to print patterns exist online and in books, not many of them explain the step by step thinking process involved to come up with such examples. It is the aim of this post to explain how to approach problems. It is often easy for an experienced programmer to mentally figure out the whole algorithm  needed to solve a given a solution even before they start writing the code. But it is usually not so with beginners. So how...

Printing a Calendar

I was impressed by how you can easily print a calendar in python and it looks pretty. I decided to write my own implementation in Groovy. Below is the code. def getMonthStart (date){ new Date (date. year , date. month , 1 ) } def getMonthEnd (date){ getMonthStart( new Date(date. year , date. month , 27 ) + 6 ) - 1 } def getCalendar (year, month){ getCalendar(year, month, 1 ) } def getCalendar (year, month, day){ getCalendar( new Date(year - 1900 , month - 1 , day)) } def getCalendar (d){ def l = [] def today = new Date(). date def header = d. format ( 'MMMM, Y' ) l << header. center ( 28 ) << '\n' [ 'Su' , 'Mo' , 'Tu' , 'We' , 'Th' , 'Fr' , 'Sa' ]. each { l << " $it " } def start = getMonthStart(d) if (start. day != 0 ) l << ...

Ba Be Bi Bo Bu

One day I decided to start teaching my daughter how to read. So I decided to prepare a sheet of the phonetics based on the English alphabet. Here is the Groovy code I wrote: vowels = 'aeiou' . toList () ( 'A' .. 'Z' ). each {letter -> if (!vowels*. toUpperCase (). contains (letter)){ vowels. each { vowel -> print "$letter$vowel\t" } println "" } } And here is the output: Ba Be Bi Bo Bu Ca Ce Ci Co Cu Da De Di Do Du Fa Fe Fi Fo Fu Ga Ge Gi Go Gu Ha He Hi Ho Hu Ja Je Ji Jo Ju Ka Ke Ki Ko Ku La Le Li Lo Lu Ma Me Mi Mo Mu Na Ne Ni No Nu Pa Pe Pi Po Pu Qa Qe Qi Qo Qu Ra Re Ri Ro Ru Sa Se Si So Su Ta Te Ti To Tu Va Ve Vi Vo Vu Wa We Wi Wo Wu Xa Xe Xi Xo Xu Ya Ye Yi Yo Yu Za Ze Zi Zo Zu Let me get you feedback. :)

99 Bottles of Beer

I don't drink beer but the song "99 Bottles of Beer" is an interesting one to program. Here is the source code in Groovy: def bottles (n){ if (n == 1 ) "1 bottle" else if (n == 0 ) "no more bottles" else "${n} bottles" ; } for (x in ( 99 .. 0 )) { println "${bottles(x).replaceFirst(" n ", " N ")} of beer on the wall, ${bottles(x)} of beer." if (x!= 0 ) println "Take one down and pass it around, ${bottles(x-1)} of beer on the wall." else println "Go to the store and buy some more, 99 bottles of beer on the wall." } And here is the output showing the song:          99 bottles of beer on the wall, 99 bottles of beer.             Take one down and pass it around, 98 bottles of beer on the wall.             98 bottles of beer on the wall, 98 bottles of beer.           ...