Date Created: November 22, 2009.
Date Expire: November 29, 2009.
It was my 3rd week in C# last night

Here’s a nice thing to share, I made this problem 2 hours ago.
John is developing a modified version of board game. A
“Swap” method, is a predefined method of a board game engine that swaps a pair of an object.
The scenario is use to carry the situation where two board pieces cross on a certain point/line and there’s a need of swapping, think about board game like Chinese checkers, or Chess, that when two object can cross each other or the other, both can be swapped. This followed by another method, where one of it will be taken out as board piece.
Okay, here’s the exposed code of board game engine that you are not allowed to modify.
using System;
namespace ConsoleDelegate
{
public class BoardGameEngine
{
public delegate bool DDSwapFunc(
object LSP,
object RSP);
object[] PairPiece =
new object[2];
public void SetPair(
object o1,
object o2)
{
this.PairPiece[0] = o1;
this.PairPiece[1] = o2;
}
//A method that swaps two objects
//swapping method depends on the users.
public void Swap(
DDSwapFunc OtherFunc)
{
if ( OtherFunc(PairPiece[0], PairPiece[1]) )
{
object tmp =
this.PairPiece[0];
this.PairPiece[0] =
this.PairPiece[1];
this.PairPiece[1] = tmp;
}
}
public void PairDisplay()
{
Console.WriteLine(PairPiece[0]);
Console.WriteLine(PairPiece[1]);
}
}
Swap method has a parameter of
DDSwapFunc type, a
delegate to any function that returns a boolean (true or false) and accepts two object LSP (left side piece) RSP (right side piece),
the swapping will only occurs if the delegated function returns True, if it is False, no swapping will occurs in Game Engine. The swapping decision or this Boolean decision depends upon the programmer.[/font]
So here’s the game created by john.
public
class BoardPiece
{
private string rank;
private int value;
public BoardPiece(
string rank,
int value)
{
this.rank = rank;
this.value = value;
}
static public bool FirstPieceGo(
object FPA,
object SPA)
{
BoardPiece FObject = (
BoardPiece)FPA;
BoardPiece SObject = (
BoardPiece)SPA;
return FObject.value > SObject.value;
}
public override string ToString()
{
return this.rank +
" : " +
this.value;
}
}
class Program
{
static void Main()
{
//Step 1: The making of Pieces.
BoardPiece BPKing =
new BoardPiece(
"King", 1000);
BoardPiece BPHunter =
new BoardPiece(
"Hunter", 300);
//Step 2: Initializing the Board Game Engine as BGE
BoardGameEngine BGE =
new BoardGameEngine();
//Step 3: Setting a pair of piece in BGE.
//Assuming that they cross in John’s Game.
BGE.SetPair(BPKing, BPHunter);
//Step 4: delegating the BoardPiece.FirstPieceGo method to be use as a base
//decision. Again a Swap method accepts a delegate from its class, which is
//delegating a function outside from game engine.
BoardGameEngine.
DDSwapFunc BGEFunc =
new BoardGameEngine.
DDSwapFunc(
BoardPiece.FirstPieceGo);
//Step 5: Perform Swapping and Display.
BGE.Swap(BGEFunc);
BGE.PairDisplay();
Console.ReadLine();
}
}
}
Output:
John wanted to
eliminate the Step 4, He want to have a member on his class that makes a direct call of swapping rather than performing step 4 beforehand. Eg.
BGE.Swap(BoardPiece.FirstPieceGo);
Judging on the code which john wanted to achieved
can be done by applying static member of his class "BoardPiece" that helps him to achieved the code.[/font]
The 2nd version of his
Main() should be like this:
//Step 1: The making of Pieces.
BoardPiece BPKing =
new BoardPiece(
"King", 1000);
BoardPiece BPHunter =
new BoardPiece(
"Hunter", 300);
//Step 2: Initializing the Board Game Engine as BGE
BoardGameEngine BGE =
new BoardGameEngine();
//Step 3: Setting a pair of piece in BGE.
BGE.SetPair(BPKing, BPHunter);
//Step 4: Perform Swapping
BGE.Swap(
BoardPiece.FirstPieceGo);
BGE.PairDisplay();
Console.ReadLine();
Notice that we only now have 4 steps. But the problem is you have to modify the class that john created previously, with this 3 instructions to follow:
1. Add a static member which you can use in step 4.
2. No need to modfy the constructor, FirstPieceGo and ToString Method.
3. Most important is, saves Memory
This is a 1 week puzzle for you to solve. I will post my answer and explanation next week. Hence, you can discuss topics which involve here, like C# delegate, Static Member (advantage and disadvantage), Property, Override Methods, or any related on this puzzle.
This is a common situation also C++ and JAVA development, when we talk about delegating and performance issue, and commonly asked during interview.
Granted that you will learned and never forget this lesson. I will give and explain my answer only to those who tried.
You can PM me here or at my YM.
Have fun…