Prabhu Sunderaraman

Fullstack Engineer, Programmer, Writer, Trainer

prabhu.bits@gmail.com,     GitHub     Youtube     LinkedIn
  • Home
  • Profile
  • Books
  • Reading List
Browsing: / Home
Shortlink

Functional style Guessing Game – III, with the Author

By Prabhu Sunderaraman on July 14, 2013 in Scala

I went hiking on the Rocky mountains, Colarado with Venkat Subramaniam, author of Programming Scala. More than the hike, the numerous technical discussions we had during the hike, were more enjoyable. After we came back we decided to give the Guessing game a complete immutability shot with recursion and lazy collections. Given below is the immutable Guessing Game using recursion. In the process, thanks to Venkat, ironed out issues on Scala language that were bothering me for quite sometime.

Here’s the class Game that has a play method which is recursively called till the game is over. The input and output providers are injected into the method using Closures.

object Game {
  val target = (Math.random * 100).toInt
  
  def play(attempts : Int = 1)(printer: String => Unit)(reader: Unit => Int) {
    printer("Enter your guess:")
    val guess = reader()
    val compared = guess.compare(target)

    val (message, playAgain) = 
      compared match {
        case -1 =>  ("Aim higher", true)
        case 0 => (s"You got it in $attempts attempts", false)
        case 1 => ("Aim lower", true)
      }
    
    printer(s"$messagen")
    if(playAgain) play(attempts + 1)(printer)(reader)
  }
}

println("I've picked a number, can you guess it?")
Game.play() { msg => print(msg) } { Unit => Console.readInt }

Apparently Venkat is also writing an article on this topic. So I have published this code with due permission.

Share this on: Mixx Delicious Digg Facebook Twitter
Shortlink

Spring in Summer

By Prabhu Sunderaraman on July 10, 2013 in Uncategorized

I am on a whirlwind tour to US this summer, doing programs in Spring Framework.
Looking forward to meeting some interesting developers and crack some solid code with these guys.

Share this on: Mixx Delicious Digg Facebook Twitter
Shortlink

Ext JS 4 vs Sencha Touch API differences

By Prabhu Sunderaraman on June 30, 2013 in Ext JS, Sencha Touch

A number of developers who want to build web applications want to create a mobile version of the same. Ext JS 4 and Sencha Touch seem to be the preferred libraries for the desktop and mobile versions respectively. This is due to the similarities in the API in both. However there are a number of minute differences that you may need to take care while using both.

The table below shows some of the common differences between Ext JS 4 and Sencha Touch.

Concept Ext JS 4 Sencha Touch
Inheritance Ext.define(“MyPanel”,{
extend : “Ext.form.Panel”,
items : […],
cls : “”
})
Ext.define(“MyPanel”,{
extend : “Ext.form.Panel”,
config:{
items : […],
cls : “”
}
})
Viewport Explicitly create Ext.container.Viewport instance and specify the layout Viewport instance is created with Card layout automatically
Rendering to the screen renderTo:Ext.getBody() fullscreen:true
Form components’ label {xtype:”textfield”,fieldLabel:””} {xtype:”textfield”,label:””}
Button event {xtype:”button”,listeners:{click:””}} {xtype:”button”,listeners:{tap:””}}
MVC Controller references refs: [{ref:”variableName”,selector:””}] refs:{ variableName:”selector” }
MVC Controller event handlers init:function(){this.control({})} control:{}
Share this on: Mixx Delicious Digg Facebook Twitter
Shortlink

Hangman in Scala

By Prabhu Sunderaraman on June 21, 2013 in Scala

A Hangman game is usually played by people who enjoy the vocabulary. You have to play by guessing a word by choosing the letters one by one. For every wrong attempt a picture of a man hanging is drawn step by step. When the picture of the hanged man is complete (when the man is hanged!!!) the game is over.

I tried implementing this game using Scala. Of course, no picture was drawn, but the alphabets H A N G M A N are written for every wrong guess. You have to guess simple four letter words.

It was quite easy to implement this using imperative style in Scala. The biggest challenge was implementing this game using functional style in Scala. The code is given below.

import scala.util.Random

object Hangman {
 val words = Map(1->"bore",2->"clue",3->"mint",4->"star",5->"cart",6->"land",7->"tame",8->"role",9->"bask",10->"maze")
	
 def hangman(wrongAttempt:Int):String = {
  wrongAttempt match{
    case 0=>""
    case 1=>"H_ _ _ _ _ _"
    case 2=>"HA_ _ _ _ _"
    case 3=>"HAN _ _ _ _"
    case 4=>"HANG _ _ _"
    case 5=>"HANGM _ _"
    case 6=>"HANGMA _"
    case 7=>"HANGMAN"  
  }
}
def checkMatch(wrongAttempt:Int,letter:String,enteredWord:String,word:String):
 (Boolean,Int,String,String) = {
  val pos:Int = word.indexOf(letter)
  if(pos != -1){
    val output = enteredWord.replace(pos+"",letter)
    return (true,wrongAttempt,output,hangman(wrongAttempt))
  }
  else{
    return (false,wrongAttempt+1,enteredWord,hangman(wrongAttempt+1))
  }
}
def printOutput(op:(Boolean,Int,String,String)) {
  if(op._1)
    op._3 foreach(it=>if(Character.isDigit(it))print("_ ") else print(it))
  else
    println(op._4)
}
def main(args:Array[String]){
  val word = words(new Random().nextInt(10)+1)
  println("_ _ _ _")		
  var op = checkMatch(0,readLine(),"0123",word)  
  printOutput(op)
  while(op._2 != 7 && word != op._3){
   op = checkMatch(op._2,readLine(),op._3,word)
   printOutput(op)
  }
 if(op._2 == 7 && word != op._3)
   println("The word is " + word)
 }
}

Couple of console-based screenshots of the game is shown below

There’s still a lot of scope for the code to be refactored and made better.

Share this on: Mixx Delicious Digg Facebook Twitter
Shortlink

My book on Ext JS 4

By Prabhu Sunderaraman on June 15, 2013 in Books

My book “Practical Ext JS 4” published by Apress, is due for release in September, 2013. The early bird offer for the book is out on Amazon.

Share this on: Mixx Delicious Digg Facebook Twitter
« Previous 1 … 36 37 38 … 64 Next »

Youtube Channel




Categories

  • JavaScript (48)
    • RequireJS (5)
  • Go (44)
  • Golang (44)
  • Ext JS (23)
  • Spring (22)
  • Mobile (21)
  • Scala (20)
    • Play (3)
  • Uncategorized (19)
  • Video Sessions (18)
  • GoG (17)
  • Sencha Touch (16)
  • jQuery (14)
  • Languages (13)
  • Java 8 (12)
  • React JS (11)
  • Kotlin (11)
  • HealthyCodeMagazine (9)
  • Video (9)
  • Objective-C (8)
  • NoSQL (8)
  • Android (7)
  • MongoDB (7)
  • GWT (6)
  • Tools (6)
  • HTML 5 (5)
  • Cloud (5)
  • General (5)
  • Micro services (5)
  • Java (5)
  • Books (4)
  • AWS (4)
  • Software Architecture (4)
  • .NET (3)
  • Elixir (3)
  • Docker (3)
  • Reactive (3)
  • NodeJS (2)
  • RoR (2)
  • Backbone (1)
  • AngularJS (1)

Archives

  • 2020 (49)
  • 2019 (7)
  • 2018 (34)
  • 2017 (15)
  • 2016 (7)
  • 2015 (18)
  • 2014 (31)
  • 2013 (55)
  • 2012 (100)

Search

Subscribe




Copyright © 2025 Prabhu Sunderaraman.

Powered by WordPress and News.