Page 1 of 2 12 LastLast
Results 1 to 10 of 17
  1. #1
    Elite Member
    Join Date
    Aug 2008
    Posts
    1,053
    Blog Entries
    1

    Thumbs up C# Puzzle of the Week 11-22-2009


    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:
    Hunter : 300
    King : 1000
    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…
    Last edited by MarkCuering; 11-22-2009 at 08:58 AM. Reason: Added #3

  2. #2
    hhhm.. it seems like a school assignment?
    hehehe, I rather spend my time picking my nose and watch anime, or date some sexy chick, or eat some BBQ in A.S. Fortuna.

  3. #3
    Elite Member
    Join Date
    Aug 2008
    Posts
    1,053
    Blog Entries
    1
    if you can't solve nor interested to do it...just don't bother posting

  4. #4
    I'm just not interested. Spoon feeding makes bad programmer. Each programmer has their own ways.
    I open my mind/thoughts towards others. Sometimes I don't keep my weird ideas for myself.

    Showing your codes is like welding a sword, it could mean that you are hunger for battle and challenges anyone in your way.

    I prefer not showing off codes, i don't like snapping a nerve to a fellow programmer. Each one of us has their own ways, and everybody knows that.

    If I were to teach a newbie programmer, I just want him/her to realize what is better (in their own way).

    Obviously, it's your code... it's your own way, not theirs.
    Last edited by dodie; 11-22-2009 at 01:46 PM.

  5. #5
    Elite Member
    Join Date
    Aug 2008
    Posts
    1,053
    Blog Entries
    1
    Hey, what's wrong with you? so what if you're not interested. Who asked you by the way?

    Spoon feeding makes bad programmer. Each programmer has their own ways.
    what's the connection with this on my post? and where is the spoon feeding here?

    Showing your codes is like welding a sword, it could mean that you are hunger for battle and challenges anyone in your way.
    hunger for battle? FYI, I have 5-7 regular groups who I use to talk online... 2 of them worked at UAE. 3 students, and the other 2 is just come and go, sometimes we able to talk, sometimes not, coz I know they are busy with their JAVA stuff.

    I just post the problem here, for 2 students who wanted to know, as well as to the rest of their classmates.

    I prefer not showing off codes, i don't like snapping a nerve to a fellow programmer. Each one of us has their own ways, and everybody knows that.
    So what? there are just some guys out there who are willing to see how other people implement the problem. For they might know something that they don't know. So do i...!

    If I were to teach a newbie programmer, I just want him/her to realize what is better (in their own way).
    Unfortunately, I don't know what exactly newbie is...what's the level of their knowledge to be called as one. These guys I'm talking about are well knowledgeable in other subjects, and I respect their opinion in most of the time.

    I'm a newbie in C#, and I'm accepting any criticism on my code.... if you feel awkward why I post that, then its no longer my problem. The post isn't mean for a person like you. Sorry...

    you are not not a complete idiot, but some parts of you are missing.
    Last edited by MarkCuering; 11-22-2009 at 02:18 PM.

  6. #6
    that's what i thought, you snapped your nerve.... I'm not even showing a code even once.

    Give the programmers to think of their own. Not dictate them..... from there you breed good programmers.

    You write like a girl, explaining each sentence. You are not realizing the 2nd meaning of my entire post. I'm too lazy to explain my own post.
    Last edited by dodie; 11-22-2009 at 02:54 PM.

  7. #7
    OT:
    Quote Originally Posted by dodie View Post
    You write like a girl, explaining each sentence.
    That's an insult to the female population.

    Both of you should grow up.

  8. #8
    Quote Originally Posted by MarkCuering View Post
    Date Created: November 22, 2009.
    Date Expire: November 29, 2009.
    Do you think you can just tell the new learners to learn that speed? Feels like you are forcing them to solve it. It's a bad practise for their minds...... think about the readers of this forum specially the new learners.

  9. #9
    @dwardwarbinx
    ah okay..... sorry about that, please excuse that...... for this thread only.

  10. #10
    Elite Member
    Join Date
    Aug 2008
    Posts
    1,053
    Blog Entries
    1
    Quote Originally Posted by dodie View Post
    Do you think you can just tell the new learners to learn that speed? Feels like you are forcing them to solve it. It's a bad practise for their minds...... think about the readers of this forum specially the new learners.
    solving this particular problem will let someone mirror an idea away from his computer for awhile. Adding time unto isn't a good practice?

    If somebody presented to you a 5 different kinds of approach on this problem, aside from the one he had made, he possibly will discover that one of them wrote it effectively, Or a combination of both, from him and them.

    It has to be explained. Why you have to do that and why you need to be like this aside from the other round. Because In real environment, you can’t just code what you want... you can’t just code without knowing the impact of the entire development. There’s always a reason behind the scene, and that’s what I’m trying to show it here, rather than thinking about ostentatiously showing some code stuff...
    Last edited by MarkCuering; 11-22-2009 at 05:26 PM.

  11.    Advertisement

Page 1 of 2 12 LastLast

Similar Threads

 
  1. Replies: 477
    Last Post: 02-26-2019, 03:33 PM
  2. What's Your Favorite Day of the Week?Why?
    By tatakalz in forum General Discussions
    Replies: 63
    Last Post: 07-26-2009, 04:05 AM
  3. Replies: 0
    Last Post: 02-13-2009, 02:09 PM
  4. Unsa may 7th day of the week? Sabado o Domingo?
    By NASYO in forum Spirituality & Occult - OLDER
    Replies: 135
    Last Post: 11-02-2008, 04:58 PM
  5. Best New Movies of the Week
    By speed_element in forum General Discussions
    Replies: 1
    Last Post: 09-19-2008, 11:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top