Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Tuesday, November 20, 2007

.Net 3.5 Training kit

Microsoft just launched the .net 3.5 framework and announced the launch of Visual Studio 2008 RTM that is available to developers that are subscribed to MSDN. In order to help developers they have also shipped out a training kit that contains materials that should get most developers up to speed on .net 3.5 .
Have fun!

Sunday, October 14, 2007

Serializable dictionary in C#

Did you ever need to serialize a generic key-value dictionary like the Dictionary class? Well in .Net that class is not serializable, in fact the interface IDictionary is not serializable for some unknown reason so all the classes that implement the IDictionary interface (SortedDictionary , Dictionary ) can not be serialized unless you specify some custom code.
The best code I have found online is written by Paul Welter and you can see it here. I needed to implement a SortedDictionary so the only change I made to that code is implement the SortedDictionary instead of the Dictionary.

Thursday, July 19, 2007

Tuesday, July 17, 2007

3D rendering using javascript, dom and CSS

UselessPickles.com offers a nice example of building 3D models in using javascript and dom which is kind of cool. Also has a nice way of creating triangles using just plain html.

When LINQ meets the UI

Scott Guthrie has a nice set of tutorials on his page and the latest tutorial explains the link between linq and the User Interface. Nice reading.

Sunday, July 15, 2007

Learn Silverlight and Blend

Nibbles offers some quick tutorials for Silverlight and Blend. Quite a nice reading.

Sunday, July 01, 2007

Silverlight samples and code

If you enjoyed Richard Zadorozny`s bar chart, pie char or the graph you can now download the source code. This is the best way you can learn Silverlight programming.

(source)

Friday, June 29, 2007

ReSharper

If you are using ReSharper then you have to check out this tutorials. Will be worth your time.

Sunday, June 24, 2007

Are you a good programmer?

A language that doesn't affect the way you think about programming, is not worth knowing


Learning programming in 10 years..just so so true..

Sunday, June 10, 2007

The Start Function(Part 4)

This is part of a series of articles that shows the creation of a Silverlight game. Please read part 1,part 2 or part 3 if you haven`t done so yet.


   1:  void Start(int size,double blockProbability,Point StartPosition,Point EndPosition)


The Start function receives 4 parameters:

  • size - the width and the height of the board.

  • blockProbability - the probability of a blocking block to appear. Should increase as the level gets higher.

  • StartPosition - the point of origin for the red rectangle

  • EndPosition - the point where the red rectangle needs to get(yellow rectangle)



   2:          {

   3:              startPoint = new Point(StartPosition.X * multiplier, StartPosition.Y * multiplier);

   4:              endPoint = new Point(EndPosition.X * multiplier, EndPosition.Y * multiplier);

   5:   


We use the multiplier in order to transform from logical points to real pixels and we initialize the startPoint and the endPoint variable.

   6:              NextLevel.Visibility = Visibility.Collapsed;

   7:              NextLevelText.Visibility = Visibility.Collapsed;

   8:              Congratulations.Visibility = Visibility.Collapsed;


Here we just hide the Congratulations text(should only be visible when the user wins), the next level button which should also be visible only when the user wins and the text on top of the next level button.

   9:              steps = 0;

  10:              isMoving = false;

  11:              map = new int[size, size];

  12:              int unpassable = 0;


Here we initialize some counters. First the number of steps that the user has made must be 0. Then we say that the red rectangle is not moving. Also we initialize the map that will be composed of 0s and 1s and we initialize the variable that counts the number of unpassable regions.

  13:              for (int i = 0; i < size; i++)

  14:              {

  15:                  for (int j = 0; j < size; j++)

  16:                  {

  17:                      map[i, j] = (r.NextDouble() < blockProbability) ? 1 : 0;

  18:                      map[(int)StartPosition.X, (int)StartPosition.Y] = 0;

  19:                      map[(int)EndPosition.X, (int)EndPosition.Y] = 0;

  20:                      if (map[i, j] == 0)

  21:                      {

  22:                          AddNewRectangle(new Point(i , j ), Colors.Green);

  23:                      }

  24:                      if (map[i, j] == 1)

  25:                      {

  26:                          unpassable++;

  27:                          AddNewRectangle(new Point(i , j ), Colors.Black);

  28:                      }

  29:                  }

  30:              }


Setups the map that will be displayed. The map will be completely random and will display in avarage (blockProbability*100)% unpassable blocks. We also increase the counter of unpassable blocks each time we generate one. The function AddNewRectangle just adds a new rectangle to the board and will be discussed later.

  31:              Unpassable.Text = unpassable.ToString();

  32:              CurrentLevel.Text = level.ToString();


We display the number of unpassable blocks and the current level on the screen.

  33:              //setup visibility and location of helper objects

  34:   

  35:              Congratulations.SetValue(Canvas.TopProperty, size * multiplier + 5);

  36:              NumberOfSteps.SetValue(Canvas.TopProperty, size * multiplier + 5);

  37:              NumberOfStepsText.SetValue(Canvas.TopProperty, size * multiplier + 5);

  38:              NextLevelText.SetValue(Canvas.LeftProperty, size * multiplier + 40);

  39:              NextLevel.SetValue(Canvas.LeftProperty, size * multiplier + 5);

  40:              Restart.SetValue(Canvas.LeftProperty, size * multiplier + 5);

  41:              RestartText.SetValue(Canvas.LeftProperty, size * multiplier + 30);

  42:              UnpassableText.SetValue(Canvas.LeftProperty, size * multiplier + 19);

  43:              Unpassable.SetValue(Canvas.LeftProperty, size * multiplier + 107);

  44:              CurrentLevelText.SetValue(Canvas.LeftProperty, size * multiplier + 5);

  45:              CurrentLevel.SetValue(Canvas.LeftProperty, size * multiplier + 107);

  46:   

  47:   


Because the board of the game si expandable we need to keep the buttons out of the board region so we move them dinamically.

  48:              //setup the main objects:the start and the end(start is the moving object)

  49:              end = CreateSpecialRectangle(EndPosition, Colors.Yellow);

  50:              start = CreateSpecialRectangle(StartPosition, Colors.Red);

  51:              //setup event handler for the end object

  52:              end.MouseLeftButtonDown += new MouseEventHandler(r_MouseLeftButtonDown);

  53:          }


We then create the special rectangles that will move and will represent the end point. We also wire up an event that fires when the yellow rectangle is pressed.

The rest of the functions will be discussed in part 5.

To download the entire project just click here

Starting the C# code(Part 3)

So with the static part in place we need to get the game moving...But first we need some class variables that will help within the project:


   1:          //the level

   2:          static int level = 1;

   3:          //the number of times that the user moved the rectangle in this round

   4:          static int steps = 0;

   5:          //the probability that the block generated is a blocking block(cannot pass through it)

   6:          static double blockingProbability = 0.03;

   7:          //the current destination point of the moving rectangle

   8:          Point toMove;

   9:          //the start point of the round

  10:          Point startPoint;

  11:          //the end point of the round

  12:          Point endPoint;

  13:          //true if the user rectangle is moving

  14:          bool isMoving;

  15:          //keeps the map

  16:          int[,] map;

  17:          Random r;

  18:          //the height of the generated rectangles

  19:          const int height = 19;

  20:          //the width of the generated rectangles

  21:          const  int width = 19;

  22:          //the multiplier by which the matrix location is multiplied (Ex if multiplier is 

  23:          //20 than the location (1,2) on the map will actually be shown at

  24:          //(20,40)

  25:          const int multiplier = 20;

  26:          //the start rectangle

  27:          Rectangle start;

  28:          //the end rectangle

  29:          Rectangle end;

  30:          //the board size

  31:          static int boardSize = 10;


This variables are all straight forward and are explained through comments. They will be explained in more detail later on when we get to the functions implemented.


Now that we have the variables in place we need something to initialize them. This is where the Page_Loaded function comes into place:

   1:          public void Page_Loaded(object o, EventArgs e)

   2:          {

   3:              // Required to initialize variables

   4:              InitializeComponent();

   5:              r = new Random();

   6:              timer.Completed += new EventHandler(timer_Completed);

   7:              Restart.MouseLeftButtonDown += new MouseEventHandler(Restart_MouseLeftButtonDown);

   8:              RestartText.MouseLeftButtonDown += new MouseEventHandler(Restart_MouseLeftButtonDown);

   9:              NextLevel.MouseLeftButtonDown += new MouseEventHandler(NextLevel_MouseLeftButtonDown);

  10:              NextLevelText.MouseLeftButtonDown += new MouseEventHandler(NextLevel_MouseLeftButtonDown);

  11:              

  12:              Start(boardSize, blockingProbability, new Point(r.Next() % boardSize, r.Next() % boardSize), new Point(r.Next() % boardSize, r.Next() % boardSize));

  13:          }


This function initializes the random number generator on line 6. Then we use some event-handlers. The objects timer,Restart,RestartText,NextLevel,NextLevelText are all decalred in the XAML file.

  • The timer is a storyboard variable that is used in order to create the animation for the movement of the red rectangle.

  • The restart object is a rectangle used as a button.

  • The RestartText object is just the text that is displayed on the Restart rectangle.

  • The NextLevel object is a rectangle used as a button.

  • The NextLevelText object is the text displayed on the NextLevel rectangle.


All the events that were wired here will be explained later on.

Next we call the Start function that is the core function of the game as it creates a new level and initializes the gameplay. It will be explained in the next post.

(See part 4)

XAML Code for the Silverlight game(Part 2)

As I always like seeing the final product first...here is the whole XAML code needed for the game:


   1:  <Canvas xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="parentCanvas" Loaded="Page_Loaded" x:Class="PathFinder.Page;assembly=ClientBin/PathFinder.dll" Width="709" Height="558" Background="White">

   2:    <Canvas.Resources>

   3:      <Storyboard x:Name="timer">

   4:        <DoubleAnimation Duration="00:00:0.01" />

   5:      </Storyboard>

   6:    </Canvas.Resources>

   7:    <!--The restart rectangle with a gradient fill-->

   8:    <Rectangle Stroke="#FF000000" x:Name="Restart" Width="149" Height="40" Canvas.Left="550" Canvas.Top="13" RadiusX="15" RadiusY="15" Cursor="Hand">

   9:      <Rectangle.Fill>

  10:        <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">

  11:          <GradientStop Color="#FF3E9E35" Offset="0"/>

  12:          <GradientStop Color="#FF51FF40" Offset="1"/>

  13:        </LinearGradientBrush>

  14:      </Rectangle.Fill>

  15:    </Rectangle>

  16:    <!--The restart text-->

  17:    <TextBlock Cursor="Hand" Width="97" Height="20" Canvas.Left="581" Canvas.Top="23" TextWrapping="Wrap" x:Name="RestartText" Text="Restart Level"/>

  18:    <!--The congratulations text - Collapsed at first as it should only be visible when the user completes a level-->

  19:    <TextBlock x:Name="Congratulations" Width="303" Visibility="Collapsed" Height="26" Canvas.Left="189" Canvas.Top="529" Foreground="#FFC43E3E" Text="Congratulations you reached your target!" TextWrapping="Wrap"/>

  20:    <!--Static text-->

  21:    <TextBlock x:Name="NumberOfStepsText" RenderTransformOrigin="0.492,-1.333" Width="124" Height="21" Canvas.Left="8" Canvas.Top="529" Text="Number of steps" TextWrapping="Wrap"/>

  22:    <!--The number of moves that the user made-->

  23:    <TextBlock x:Name="NumberOfSteps" Width="49" Height="21" Canvas.Left="136" Canvas.Top="529" Text="0" TextWrapping="Wrap"/>

  24:    <!--The next level rectangle with a gradient fill-->

  25:    <Rectangle x:Name="NextLevel" Visibility="Visible" Stroke="#FF000000" RadiusX="15" RadiusY="15" Width="149" Height="40" Canvas.Left="550" Canvas.Top="77" Cursor="Hand">

  26:      <Rectangle.Fill>

  27:        <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">

  28:          <GradientStop Color="#FFF48F40" Offset="0"/>

  29:          <GradientStop Color="#FFDBF65C" Offset="1"/>

  30:        </LinearGradientBrush>

  31:      </Rectangle.Fill>

  32:    </Rectangle>

  33:    <!--The next level text-->

  34:    <TextBlock x:Name="NextLevelText" Visibility="Collapsed" Width="94" Height="21" Canvas.Left="584" Canvas.Top="87" Text="Next Level" TextWrapping="Wrap" Cursor="Hand"/>

  35:    <!--Static text-->

  36:    <TextBlock x:Name="UnpassableText" Width="89" Height="21" Canvas.Left="564" Canvas.Top="129" Text="Unpassable:" TextWrapping="Wrap"/>

  37:    <!--The number of unpaassable blocks-->

  38:    <TextBlock x:Name="Unpassable" Width="39" Height="21" Canvas.Left="650" Canvas.Top="130" Text="12" TextWrapping="Wrap"/>

  39:    <!--Static text-->

  40:    <TextBlock x:Name="CurrentLevelText" Width="103" Height="19" Canvas.Left="550" Canvas.Top="154" Text="Current Level:" TextWrapping="Wrap"/>

  41:    <!--The current level-->

  42:    <TextBlock x:Name="CurrentLevel" Width="28" Height="18" Canvas.Left="650" Canvas.Top="155" Text="12" TextWrapping="Wrap"/>

  43:  </Canvas>



The code by itself does absolutly nothing but display some button-like controls. The actual magic occurs behind the scene using C# but that`s another post.
This xaml was generated using Expression Blend May Preview. The file in this form looks like this:



(See Part 3 to see the C# code)

Creating a simple game in Silverlight 1.1 (Alpha)


I`ve always been interested in new technology so getting a quick understanding of how Silverlight(the flash killer) works was quite a challenge. I`ve worked on previous projects in Flash. Since I like C# a lot more than I like ActionScript I could say that Silverlight development is a lot better but that is something that you should decide.

The aim of the game: To get a red rectangle over the yellow rectangle. The red rectangle can only pass over green rectangles and not over black rectangles. As tou can see this is a primitive example of a map with a single moving object. Since the red rectangle can only move one step at a time (N - NE - E - SE - S - SW -W - NW) it should be very easy to get it on the required spot.

To see the game at work just go here


So this is what it will look like...


(See part 2 to start coding)

Wednesday, May 30, 2007

Silverlight

...is cool...
If you want to learn a bit about it try this

Saturday, March 31, 2007

Web UI for the TFS

Microsoft has acquired TeamPlain, a web UI for the Team Foundation Server and now the product is being distributed as a free download for the owners of TFS. You can download it here

Tuesday, March 20, 2007

Coding for kids

Just found this page from Microsoft that offers C# tutorials to kids.
The tutorial look cute and intuitive...

Thursday, February 22, 2007

Problems with SQL Server 2005?

I had a big problem with SQL server 2005 logins and ASP.net and the only tutorial that really helped was this one.
I hope it will help you too. Good luck!

Thursday, February 01, 2007

Google Checkout

If you have a store and you considered using Google checkout but on your dev team you only have .Net programmers...Don`t worry :)...Here is a nice tutorial that might help you integrate with this service.

Friday, January 26, 2007

ASP.Net Ajax is out

So...ASP.net Ajax is an ajax technology created by Micorosft in order to be used along with ASP.net...but not only with asp.net as you can easily use it with php or other scripting languages.This is the official data:

ASP.NET AJAX is a free framework
for quickly creating a new generation of more efficient, more interactive and highly-personalized Web experiences that work across all the most popular browsers

Saturday, December 16, 2006

Microsoft® Visual Studio® 2005 Service Pack 1

Download it here
Happy Programming!