Jan 10, 2013

Convert Number to Words: TCL

The below TCL proc converts a Number (Eg: 5, 55, 89) to the word corresponding to that number (Eg: Five, FiftyFive, EightyNine)


proc NumberToWord {n} {
if {[expr $n / 10] == 0} {
switch $n {
1 { return "One" }
2 {return "Two" }
3 {return "Three"}
4 {return "Four"}
5 {return "Five"}
6 {return "Six"}
7 {return "Seven"}
8 {return "Eight"}
9 {return "Nine"}
0 {return "Zero"}
}
} elseif {[expr $n / 100] == 0} {
if {[expr [expr $n / 10] % 10] == 1} {
switch $n {
11 { return "Eleven" }
12 {return "Twelve" }
13 {return "Thirteen" }
    15 {return "Fifteen"}
10 {return "Ten"}
default { return "[NumberToWord [expr $n % 10]]teen"}
}
} else {
switch [expr $n / 10] {
2 {if {[expr $n % 10] == 0} {return "Twenty"} else {return "Twenty[NumberToWord [expr $n % 10]]" }}
3 {if {[expr $n % 10] == 0} {return "Thirty"} else {return "Thirty[NumberToWord [expr $n % 10]]"}}
4 {if {[expr $n % 10] == 0} {return "Fourty"} else {return "Fourty[NumberToWord [expr $n % 10]]"}}
5 {if {[expr $n % 10] == 0} {return "Fifty"} else {return "Fifty[NumberToWord [expr $n % 10]]"}}
6 {if {[expr $n % 10] == 0} {return "Sixty"} else {return "Sixty[NumberToWord [expr $n % 10]]"}}
7 {if {[expr $n % 10] == 0} {return "Seventy"} else {return "Seventy[NumberToWord [expr $n % 10]]"}}
8 {if {[expr $n % 10] == 0} {return "Eighty"} else {return "Eighty[NumberToWord [expr $n % 10]]"}}
9 {if {[expr $n % 10] == 0} {return "Ninety"} else {return "Ninety[NumberToWord [expr $n % 10]]"}}
}
}
} elseif {[expr $n / 1000] == 0} {
return "[NumberToWord [expr $n / 100]]Hundred[NumberToWord [expr $n % 100]]"
} elseif {[expr $n / 1000000] == 0} {
return "[NumberToWord [expr $n /1000]]Thousand[NumberToWord [expr $n % 1000]]"
} else {
return "Number must be less than OneMillion"
}
}

Jun 23, 2011

EggyBank: My first XNA Game

This is a simple game which i made using XNA Game Studio. My very first game. The main objective of the game is to catch as many eggs as you can. Eggs will be falling from the sky and the player has to catch it. Simple!!! There are 6 levels and scoring differs with each level and with each egg.

Some screenshots:






Download link: http://www.mediafire.com/file/nw45jmn7p62a6nd/EggyBank.rar

How to install:
1) Before you install the game, check whether you have Windows Imaging Component and Windows Installer 3.1 installed. Windows 7, by default, has them installed. If your an XP user, you must check these or else the game will not be installed.
Links: Windows Imaging Component, Windows Installer 3.1 redistributable

2) After checking the above requirement, extract the EggyBank.rar and double click the file setup.exe. The setup will download the components required to run the game from the internet and will install them.

3) If the setup doesn't download and install the components, or if you get any installation errors, you must have Microsoft .NET Framework 4.0 redistributable and XNA Game Studio 4.0 redistributable installed on your machine. Download and install them.
Link: Microsoft .NET Framework 4 Client Profile (Standalone installer), Microsoft XNA framework Redistributable 4.0
After downloading and installing the above files, run setup.exe again and it will work for sure.

Have fun playing the game. :-)

Jun 5, 2011

Microsoft XNA Game Studio : A Learning Guide

This post details you not on how to develop a game using XNA Game Studio(GS) but on how to go about learning the game studio.


Firstly, XNA game studio is build on top of C#. That is, it uses syntaxes and semantics of C# Programming language. So I first suggest you to be strong on fundamentals of C# programming language. The fundamentals include knowing the following:
  • Data types and operators
  • Control statements
  • Classes and objects
  • Constructors, destructors, and methods
  • Interfaces, arrays, enumerations, and structures
  • Inheritance and virtual methods
  • Exception handling
  • Delegates, properties, events, and Lambda expressions
  • Attributes
  • Generics
  • Named and optional arguments
  • Implicitly typed variables
  • Using IO
  • Namespaces, Preprocessors and assemblies
  • Nullable Types
  • Collections, Enumerators and Iterators
    The latest version of .NET framework is 4.0. So I suggest you to read any C# book related to that version. C# 4.0 The Complete Reference by Herbert Schildt is the book i feel is good for beginners. Andrew Troelsen's Pro C# 2010 and the .NET 4 Platform is also a good book. The first book of Troelsen that i read was based on .NET 1.1. And he has written books on each .NET framework major releases. So reading his book is recommended.

    Having a strong base in C# basics is essential for quick learning of XNA GS. So the time spent in learning XNA can be reduced significantly if you know C# basics. So don't hesitate to learn it.

    For XNA GS 4.0, which is the latest, i suggest 2 books: 

    The first book is written by Tom Miller and Dean Johnson. They both are involved in developing XNA GS. So their book talks most of the features of XNA GS. But their books has minor errors. If you know the basics of C# then these errors are nothing. And there are very few grammatical mistakes. So if you can grab the logic they are explaining, its a very good book. And trust me, the logic they are explaining is easy to grab. They have written their book like that. And as stated, it is a Developer's Library. Many XNA books teach you the APIs (Application Programming Interface) by making Games. But this book doesn't. It teaches you only the APIs with short examples but not with a complete game. So it doesn't really need you to know the logic of any Game. This is an advantage as it can used as a reference material. Thought the title says programming for Xbox 360 and Windows Phone 7, it also includes making games for PC. So it really is a good book. The only problem i find with this book is, certain features are explained with respect to Xbox 360. And since i don't own an Xbox 360 console, i found it difficult to use it practically. Even thought those features can be used in PC, you should refer to an online material or another book for completely understanding those topics. You can use the second book in that case.

    The second book written by Aaron Reed is the most popular. If you read this book after reading Miller's book, it will be very easy. You can scan through most of the topics in this book, if you have read Miller's book first. This book explains XNA GS's features by making a complete game. This is both advantageous and disadvantageous. Advantageous because you can have a practical approach on how to make a real game. Disadvantageous because you got to follow the game logic throughout the entire book or else you will be lost. You got to remember the method names and logics that are game specific also. This book can be read if you want to also know how to approach in making a professional gaming.

    If you read both the books, i can assure you, you will be well learnt in XNA GS. For references, you can use MSDN that provides an online repository for various XNA APIs.

    May 30, 2011

    Managed Versus Unmaged Code

    The only main difference between managed and unmanaged code is that in managed code, the memory is managed. That is, in general, something called as 'The Garbage Collector' takes care of memory allocation and deallocation; Garbage Collector(GC) is a part of the compiler. In other words, memory management is done by software. In an unmanaged code, the programmer should take care of memory management.

    We can also say that by managing memory through software, pointer concepts can be almost completely eliminated. I use 'almost' here because some languages, like C#, which produces managed code allows pointers. Programmers who are comfortable with C or C++ or similar languages may feel that eliminating pointers is like cutting an hand off. But pointers really make a programmer's life, 'a living hell'. They are the main source of memory leaks. Even security can be compensated. Using pointers, we can even completely corrupt the entire system, accessing critical memories of the system.

    But some critics say that unmanaged code runs faster than managed code. The main reason for that is, on compilation, unmanaged code generates raw machine level instructions but a managed code generates an intermediate file that contains instruction from something called as the 'Intermediate Language(IL)'. This intermediate file is then interpreted by a 'Virtual Machine(VM)', that executes machine level instructions corresponding to the IL instructions. That is, the main job of a VM is to map IL instructions to machine level instructions. The other jobs of VM are to perform GC, and compile some IL instructions to machine level instructions(this process is done by a software popularly called as just-in-time compiler or jitter).

    I previously mentioned that GC is part of compiler. But the actual garbage collection is performed by the VM. The job of the compiler is to introduce GC related instructions in the intermediate file. The VM can then use these 'GC instructions' to do garbage collection. The VM is also the main 'thing' that makes a language 'platform independent' like Java or 'language independent' like .NET aware languages.

    Examples of languages generating unmanaged code: C, C++.
    Examples of languages generatinf managed code: Java, .NET aware languages like C#, A#, F#, E#.