Monday 13 October 2008

I have only wasted time...

My Life Sucks... there you have it... I've said it!

Now on to greater stuff. This weekend was... interesting.
- I attended an Igbo party in ATL (I'm that starved for anything 9ja), Saturday
- Spent the whole of Saturday doing all the wrong things (apart making it in just in time to hear the sermon)
- Spent the bulk of Sunday trying to wake up from the long night on Saturday... Finally woke up at about 4

I stumbled across this blog entry on Saturday while browsing dzone. It talked about how Motivation and Positive public pressure are the two things that really matter in meeting goals. I really needed to read that - and perhaps you do too. I could go on and on about all the things that are wrong in my life but that's not only one for another day, it's also a sad waste of time - a resource that obviously have very little of - so I'll just go ahead and try to start again. Hope is a beautiful thing... "And hope does not disappoint".

BTW, here's the song that's crossed my mind right now...

"Every morning I
have a chance to rise and give my all
but every afternoon I find I have only wasted time
In light of Your awe
Isn't love amazing, I forgot how to speak
knowing You are near and I am finally free"

- Say wont you say (Jennifer Knapp)

Monday 28 July 2008

AYO in SCALA 2

In case you have not noticed I've technically blogged as many times as I did last year so I guess congrats are appropriate. Ok, on to more serious things. Today, like I promised yesterday, I'll be going over the ayo code that I pasted yesterday. I'll start with a brief explanation copied verbatim from http://www.bbc.co.uk/dna/h2g2/A833816 below. One more thing, I'm still learning the whole blogging idea (the only reason I'll do something as crazy as blogging is b/c I've heard that I might become a better person by blogging) so please forgive the issues with the fonts and text-alignment.


The Objective of the Game

At the start of the game, each player 'owns' the six houses on his side of the board, and 24 tokens, distributed four to each house.

Player 2

House 11

House

10

House

9

House 8

House 7

House 6

4

4

4

4

4

4


4

4

4

4

4

4

House 0

House 1

House 2

House 3

House 4

House 5

Player 1

During a round of play, the players can win and withdraw from play tokens in groups of four, each group gaining him possession of a house for the next round. At the end of the round, a player who has increased his share of the tokens, has an increased number of houses under his control and has therefore gained an advantage.

Any player who wins all of the tokens during a round has won the game.

Rules of Play

Players draw lots to see who will start the first round; subsequently they alternate.

At his turn, if a player has no tokens in any of his houses, play reverts to his opponent. Otherwise, he must select one of his houses and pick all the tokens in it. Proceeding anti-clockwise, one token is placed in each of the succeeding houses. Excepting the play of the final token, if the number of tokens in any house is made up to four, those four tokens are won and withdrawn by the player owning the house. At the play of the final token, there are three possibilities:

  • If the house was empty, the turn is ended.

  • If the house is made up to four tokens, the player wins and withdraws those tokens irrespective of who owns the house. Again play reverts to the opponent.

  • Excepting the above, the player picks the contents of the last house and continues his play.

The player who wins the penultimate (11th) group of four tokens also wins the last.




Ok, now if you still don't know the details of the game, well, click on that link above and read the entire write-up. Otherwise, let's get our hands with some scala and ayo shall we?


Initialization

  • select '0' as the current player

  • make sure each player has no captured stones to start with

  • arbitrarily give the names: “him” and “her” to players 0 and 1 resp.

  • put 4 stones in each hole.

The main algorithm is ...


var holes = List(4,4,4,4,4,4,4,4,4,4,4,4)
var capturedStones = Array(0,0)
var names = Array("him", "her")
var turn = 0

while(canMove(turn, holes)){ //while the player can move
println //print out the the board
prnt(holes, names, capturedStones) //get player's move
print(names(turn)+": ")
val pos = readInt
if(isValidMove1(turn, pos)){ //if the move is invalid goto step 1
var result1 = move1(turn, pos, holes) //make the move
var result2 = cleanup(turn, result1._1, result1._2) //check for any captured stones
capturedStones(0) += result2._1 //store the result of the move
capturedStones(1) += result2._2
holes = result2._3
turn = nextPlayer(turn) //switch players
}
}


I've added comments at the appropriate positions and I assume this part should be easy enough to follow, so I'll not go into any details here. Instead I'll pick the important parts of the code from now on.

  • canMove...

    Each move starts from one of the holes that belong to a player. So for a player to be able to move, he has to have at least one non-empty pocket. canMove is the function that implements this check. It determines the pockets that belong to a user and then uses the filter method on the List of pockets that remain. Since I'm trying to stick more to the functional paradigm in this code I've decided to stick with immutable List - this means, among many other things, that there will be no side effects in any of my functions (did I hear somebody mutter something about Demeter?). One of the benefits of using List in scala is that there are all these cool methods that the List class has that allows you to perform operations on all the elements of a list without changing the contents of the list itself. In canMove, I use the 'filter' method. Like the names says filter will return all the elements of a list that meet a criteria (the criteria is defined in a function that takes one argument – remember that functions are first class objects in scala). Since we want to check to see if there a non-empty pockets, we pass an anonymous function that simply checks each passed in value to see if it is greater than zero. 'x=>x>0' is the anonymous passed in function in question. Scala purist will be quick to point out that I could have typed '0<' as the function. But I'm from a java background and I still crave some form of verbosity in my code. However for kicks here is an alternative to the canMove function in the code below:

  • def canMove(player:Int, holes:List[Int]) = playerHoles(player,holes).filter(0<).isEmpty


    • prnt

      This function prints out the board of the game. This method makes use of reverse function of scala's List class. A more interesting method though is foreach. This method carries out a specific operation (defined in a method) to each element of the List. In our case, the operation is a simple print.

      PROGRAM CORE

      The next 3 methods are the most important as far as this program is concerned. The first thing to notice in these method is that each statement in scala potentially returns something. In fact, but for the fact that I'm scared that I might be wrong I'll say every statement in scala returns something – of course that something can be 'Unit' a value that can be thought of as 'void' for those of us java dudes. The logical conclusion of the above statements is that 'if' statements can return values. This infact is one of the reasons there's no ternary operator in scala: the 'if' statement in scala returns a value. Extensive use of this fact is made in the following 3 methods...

    • move1

      move1 is the first move made by a player when her turn comes around. So basically we'll check to make sure the player is starting from a pocket that belongs to her.

      Once this is verified, we make sure that we remove all the stones in the pocket we are starting from and start putting the stones, one at a time, into the succeeding pockets. This second type of move – putting the stones one at a time into succeeding pockets – is refered to as move2

    • move2

      move2, the second type of move, simply puts one stone in the a succeeding pocket and calls itself if there's still a stone left. However, at the commencement of this function, the board can be in 3 states.

    1. There are no stones left and the last pocket in which a seed/stone was dropped contains exactly either 4 or 1 stone (read the rules of the game at the start of this...). In this case, the method returns the a Tuple that contains the last position a stone was dropped in and the current configuration of the board

    2. There are no stones left but the last pocket in which a seed/stone was dropped contains neither 4 nor 1 stone. In this case, we pick all the stones in this previous pocket and use it to continue the move2 process.

    3. There are stones left. This obviously is the simplest case and we simply continue doing what we've been doing before ;)

      Before we go to the last function I'll talk about here, let's talk a little about what a Tuple is. A tuple is like a list but obviously it's not a List. I don't know that I can explain what it is in full but I'll say here that 2 very important differences between a list and a Tuple is that a tuple can be created by simply listing the items you want in the tuple within a bracket e.g. ('mi','shel') and, second, you access the items in the tuple using a syntax like tupleName._1, tupleName._2 (note here that the first element of a tuple is index with _1 not _0).

    • cleanup (please read the disclaimer above before you read this section)

      This function checks to see if the players have any stones that they can capture using the rules explained above. The interesting scala function here is the foldRight function. In the example, there's a little of currying (which I wont talk about), but I'll explain what a foldRight basically does (an almost impossible task without explaining currying). Well, foldRight function basically performs a operation on a pair of values. It then repeats that operation on the next value in the list and the result of the previous iteration. The first bracket pair, (0), passes the initial value that will be used as the first call of foldRight since of course there isnt a previous iteration result. But I'm not been totally truthful here. The fact that the first bracket pair, (0), is followed by another bracket pair, ((x,y)=>{ if(x==4) x+y else y}), simply shows that the foldRight(0) returns a function (this in essence is what I think currying is). This second bracket pair therefore is us passing a function literal, '(x,y)=>{ if(x==4) x+y else y}', to the function returned by foldRight(0). This returned function takes a function that accepts two parameters as it's argument and returns a value of the same type as the passed in parameters. In our case, the passed in function literal returns the sum of the passed in variables. In summary our foldRight is used here to sum the elements of the List. Doubtless, there are many ways to catch a fish (well many ways to perform the sum operation) and foldRight is just one of them

    • That's all folks! I'll do a part 3 if ever I decide to make a web-based fancy port and also there are still one or two more steps left. For instance “The player who wins the penultimate (11th) group of four tokens also wins the last”.

    • On a final note, here's the updated code from yesterday. Once I understand how blogger supports uploads I'll remove the code and put it as an upload.



    package ayo;

    object Game {
    def nextPlayer(player:Int) = (player+1)%2
    def playerHoles(player:Int, holes:List[Int]) = if (player==0) holes.slice(0, 6) else holes.slice(6, 12)
    def canMove(player:Int, holes:List[Int]) = playerHoles(player,holes).filter(x => x>0).length > 0
    def isValidMove1(player:Int, position:Int) = (player==0 && position<6) player="="1">5)
    def nextPos(pos:Int) = (pos+1)%12
    def previousPos(pos:Int) = (pos+11)%12

    def map(holes:List[Int], changes:List[Int], nuValue:Int=>Int):List[Int] =
    {for(i <- 0 until holes.length) yield if(changes.contains(i)) nuValue(i) else holes(i)}.toList def move1(player:Int, pos:Int, holes:List[Int]):Tuple2[Int, List[Int]] = if(!isValidMove1(player:Int, pos:Int)) (pos, Nil) else move2(player, nextPos(pos), holes(pos), map(holes, List(pos), x=>0))

    def move2(player:Int, pos:Int, stones:Int, holes:List[Int]):Tuple2[Int, List[Int]] =
    if(stones == 0 && (holes(previousPos(pos))<=1 || holes(previousPos(pos))==4)) (previousPos(pos), holes) else{ if(stones==0 && holes(previousPos(pos))>1)
    move2(player, pos, holes(previousPos(pos)), map(holes, List(previousPos(pos)), x=>0))
    else
    move2(player, nextPos(pos), stones-1, map(holes, List(pos), holes(_) +1))
    }

    def prnt(holes:List[Int], names:Array[String], stones:Array[Int]){
    print("\n|")
    playerHoles(1, holes).reverse.foreach(x => print("\t"+x+"\t|"))
    print(" "+names(1)+": "+stones(1)+"\n|")
    playerHoles(0, holes).foreach(x => print("\t"+x+"\t|"))
    print(" "+names(0)+": "+stones(0)+"\n")
    }

    def cleanup(player:Int, end:Int, holes:List[Int]):Tuple3[Int, Int, List[Int]] = {
    var playerCount = if (holes(end)==4) 4 else 0
    val nuHoles = {for(i<-0 until holes.length) yield if(end==i && playerCount!=0) 0 else holes(i)}.toList playerCount += playerHoles(player, nuHoles).foldRight(0)((x,y)=>{ if(x==4) x+y else y})
    val nextPlayerCount = playerHoles(nextPlayer(player), nuHoles).foldRight(0)((x,y)=>{ if(x==4) x+y else y})

    if(player==0)
    (playerCount, nextPlayerCount, nuHoles.map(x => if(4==x) 0 else x))
    else
    (nextPlayerCount, playerCount, nuHoles.map(x => if(4==x) 0 else x))
    }

    def main(args : Array[String]) : Unit = {
    var holes = List(4,4,4,4,4,4,4,4,4,4,4,4)
    var capturedStones = Array(0,0)
    var names = Array("him", "her")
    var turn = 0

    while(canMove(turn, holes)){ //while the player can move
    println //print out the the board
    prnt(holes, names, capturedStones) //get player's move

    print(names(turn)+": ")
    val pos = readInt

    if(isValidMove1(turn, pos)){ //if the move is invalid goto step 1
    var result1 = move1(turn, pos, holes) //make the move
    var result2 = cleanup(turn, result1._1, result1._2) //check for any captured stones

    capturedStones(0) += result2._1 //store the result of the move
    capturedStones(1) += result2._2
    holes = result2._3
    turn = nextPlayer(turn) //switch players
    }
    }
    }
    }

    Sunday 27 July 2008

    AYO in SCALA

    Have I mentioned that I'm on a mission to take on all the ideas I've ever had and make something of each one? Well I've suddenly realized that life is toooo short. In any case, here's another one of those childish dreams I used to have - to write an implementation of a local 9ja game.

    I'll obviously have to blog some more about this largely untested code and the scala implementation that I've come up with but here's the source code any way...

    If you are geekie (I have a friend that thinks I'm a geek... imagine!) enuf though, you could try out the code (it currently has only a command line interface but I plan to have a web based interface - lift, sweet or jsf are the prime suspects - sometime)

    Tomorrow, hopefully, I'll be able to go through the code...

    One important thing though is that this code is largely based on this bcc page


    package ayo;

    object Game {
    def nextPlayer(player:Int) = (player+1)%2
    def playerHoles(player:Int, holes:List[Int]) = if (player==0) holes.slice(0, 6) else holes.slice(6, 12)
    def canMove(player:Int, holes:List[Int]) = playerHoles(player,holes).filter(x => x>0).length > 0
    def isValidMove1(player:Int, position:Int) = (player==0 && position<6) player="="1">5)
    def nextPos(pos:Int) = (pos+1)%12
    def previousPos(pos:Int) = (pos+11)%12

    def move1(player:Int, pos:Int, holes:List[Int]):Tuple2[Int, List[Int]] =
    if(!isValidMove1(player:Int, pos:Int))
    (pos, Nil)
    else{
    val nuHoles:List[Int] = {for(i <- 0 until holes.length) yield if(i==pos) 0 else holes(i)}.toList //prnt(nuHoles, Array("him", "her"), Array(0,0)) move2(player, nextPos(pos), holes(pos), nuHoles) } def move2(player:Int, pos:Int, stones:Int, holes:List[Int]):Tuple2[Int, List[Int]] = if(stones == 0 && (holes(previousPos(pos))<=1 || holes(previousPos(pos))==4)) (previousPos(pos), holes) else{ if(stones==0 && holes(previousPos(pos))>1){
    val nuHoles = {for(i <- 0 until holes.length) yield if(i==previousPos(pos)) 0 else holes(i)}.toList //println("move2 type 1") //prnt(nuHoles, Array("him", "her"), Array(0,0)) move2(player, pos, holes(previousPos(pos)), nuHoles) }else{ val nuHoles = {for(i<-0 until holes.length) yield if(i==pos) holes(i)+1 else holes(i)}.toList //println("move2 type 2") //prnt(nuHoles, Array("him", "her"), Array(0,0)) move2(player, nextPos(pos), stones-1, nuHoles) } } def prnt(holes:List[Int], names:Array[String], stones:Array[Int]){ print("\n|") playerHoles(1, holes).reverse.foreach(x => print("\t"+x+"\t|"))
    print(" "+names(1)+": "+stones(1)+"\n|")
    playerHoles(0, holes).foreach(x => print("\t"+x+"\t|"))
    print(" "+names(0)+": "+stones(0)+"\n")
    }

    def cleanup(player:Int, end:Int, holes:List[Int]):Tuple3[Int, Int, List[Int]] = {
    var playerCount = if (holes(end)==4) 4 else 0
    val nuHoles = {for(i<-0 until holes.length) yield if(end==i && playerCount!=0) 0 else holes(i)}.toList playerCount += playerHoles(player, nuHoles).foldRight(0)((x,y)=>{ if(x==4) x+y else y})
    val nextPlayerCount = playerHoles(nextPlayer(player), nuHoles).foldRight(0)((x,y)=>{ if(x==4) x+y else y})

    if(player==0)
    (playerCount, nextPlayerCount, nuHoles.map(x => if(4==x) 0 else x))
    else
    (nextPlayerCount, playerCount, nuHoles.map(x => if(4==x) 0 else x))
    }

    def main(args : Array[String]) : Unit = {
    var holes = List(4,4,4,4,4,4,4,4,4,4,4,4)
    var capturedStones = Array(0,0)
    var names = Array("him", "her")
    var turn = 0

    while(canMove(turn, holes)){
    println
    prnt(holes, names, capturedStones)

    print(names(turn)+": ")
    val pos = readInt

    if(isValidMove1(turn, pos)){
    var result1 = move1(turn, pos, holes)
    var result2 = cleanup(turn, result1._1, result1._2)

    capturedStones(0) += result2._1
    capturedStones(1) += result2._2
    holes = result2._3
    turn = nextPlayer(turn)
    }
    }
    }
    //while the player can move
    //print out the the board
    //get player's move
    //if the move is invalid goto step 1
    //cleanup the move and store the players stones as well as the list to start again

    def junk:Unit = {
    /*
    println(canMove(1, holes))

    println("move is valid, "+isValidMove1(0,5))
    println(move1(0, 5, holes)._1)
    println("move is valid, "+isValidMove1(1,6))
    println(move1(1, 6, move1(0, 5, holes)._2)._2)

    print(cleanup(0, 5, move1(0, 1, (move1(1, 6, move1(0, 5, holes)._2)._2))._2)._1)
    */
    }
    }




    Just for kicks here's the result of running two steps (mirroring the example in the bbc link above)



    | 4 | 4 | 4 | 4 | 4 | 4 | her: 0
    | 4 | 4 | 4 | 4 | 4 | 4 | him: 0
    him: 2


    | 1 | 0 | 6 | 6 | 6 | 1 | her: 0
    | 6 | 6 | 2 | 7 | 1 | 6 | him: 0
    her: 11


    | 1 | 1 | 7 | 7 | 0 | 2 | her: 4
    | 1 | 8 | 0 | 8 | 2 | 7 | him: 0
    him:

    Sunday 8 June 2008

    Yoruba Custom Keyboard in Ubuntu

    I found it interesting that there was any support at all for yoruba when I first started using ubuntu two years ago. So since I'm not in school this summer and work is getting progressively more boring - i never thot I'd get fed up programming - I decided to spend the better part of this sunday getting the yoruba keyboard working on my system. Here are the steps I took to get it to work:

    1. Create a file called yo.
    2. Copy the following lines into the file you just created

    // $XKeyboardConfig: xkeyboard-config/symbols/gb,v 1.11 2006-10-03 22:25:41 svu Exp $
    // based on a keyboard map from an 'xkb/symbols/gb' file
    // $XFree86: xc/programs/xkbcomp/symbols/gb,v 1.6 2003/10/04 10:25:14 pascal Exp $

    partial default alphanumeric_keys
    xkb_symbols "basic" {

    // Describes the differences between a very simple en_US
    // keyboard and a very simple yoruba keyboard layout defined by ... blah blah blah

    include "latin"

    name[Group1]="Yoruba Nigeria";

    key { [ obelowdot, Obelowdot, q, Q ] };
    key { [ U1E63, U1E62, z, Z ] };
    key { [ ebelowdot, Ebelowdot, x, X ] };

    //grave
    key { [ U0340, U0341, v, V ] };
    //acute
    key { [ U0341, U0340, c, C ] };

    include "level3(ralt_switch_multikey)"
    };

    3. Move the file to /etc/X11/xkb/symbol

    - open your terminal
    - run the following command. (I'm assuming that the file you just created was stored your desktop). Also note that you may have to specify your admin password to successful complete this command.

    sudo mv ~/Desktop/yo /etc/X11/xkb/symbols/yo

    4. Append the following lines to the file /etc/X11/xkb/rules/xorg.xml under the layout section


    <layout>
    <configitem>
    <name>yo</name>
    <shortdescription>Yo</shortdescription>
    <description>Yoruba</description>
    </configitem>
    <variantlist>
    </variantlist>
    </layout>

    5. Append the following line to the file /etc/X11/xkb/rules/xorg.lst under the ! layout section

    yo Yoruba

    6. Load the custom keyboard we just created.
    - Open System -> Preferences -> Keyboard
    - Select the Layouts Tab and Click the Add button. Look for the Layout matching the description you entered in step 4. If you've been following this tutorial verbatim then you should be looking for a layout called Yoruba
    - One last thing you may want to do is make sure you know what the shortcut for switching keyboard layouts on your computer is. On my laptop pressing both Alt buttons together switches the keyboard layout. I also added you the MS Icon button. You can edit the shortcut for switching keyboard layouts by clicking the "Layout Options" button and then selecting "Layout Switching". Check/Uncheck whatever keys you wish to use for your shortcuts.
    - You can close the keyboard configuration window now.

    7. logout and log báck in. (Ctrl-Alt-Backspace if you like)

    As always Ubuntu (and X based systems in general) rox.

    Ok. Now if you want to know the how and why of all the voodoo that just took place here are a few links you can look up...

    http://wikisource.org/wiki/Transwiki:Table_of_Unicode_characters,_32_to_9999
    http://people.uleth.ca/~daniel.odonnell/Blog/custom-keyboard-in-linuxx11
    http://www.linux.com/articles/113715

    http://www.alt-i.org/projects.htm

    you'd also want to look up /usr/include/X11/keysymdef.h which contains the key name definitions

    On a final note, I've got to say thanks to Daniel O' Donnell for blogging on how to create custom keyboards. Also if you click on the last link I gave above you'd notice that the keymap I used in this tutorial is based on the QWERTY keyboard you've in that project. I figured since they say that they've done their research theirs would be a good example to follow.



    I might put up pictures if the need arises but I'm lazy so dont count on it :D



    Just for the fun of it... here's some stuff I typed on my keyboard: ... Eniyan ni eyi ṣòro fun; ṣugbọn fun Ọlọrun ohun gbogbo ni ṣiṣe