HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Mapping square objects to specific cells in a TableLayoutPanel in WindowsForms game

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
objectstablelayoutpanelsquarewindowsformscellsgamespecificmapping

Problem

I have solved this problem but I am looking for a better way to do it, in fewer lines of code.

The problem was from this previous question on Stack Overflow.

My solution:

/// 
        /// For a given square number, tells you the corresponding row and column numbers.
        /// Pre:  none.
        /// Post: returns the row and column numbers, via "out" parameters.
        /// 
        /// The input square number.
        /// The output row number.
        /// The output column number.
        private static void MapSquareNumToScreenRowAndColumn(int squareNumber, out int rowNumber, out int columnNumber){

        // ######################## Add more code to this method and replace the next two lines by something more sensible.  ###############################
        rowNumber = 0; // Use 0 to make the compiler happy for now.
        columnNumber = 0; // Use 0 to make the compiler happy for now.

        columnNumber = squareNumber % 6;
        rowNumber = squareNumber/6;

        //switch statement for assigning column values to odd rows
        int remainder = squareNumber%6;
        switch (remainder){
            case 0:
                columnNumber = 5;
                break;
            case 1:
                columnNumber = 4;
                break;
            case 2:
                columnNumber = 3;
                break;
            case 3:
                columnNumber = 2;
                break;
            case 4:
                columnNumber = 1;
                break;
            case 5:
                columnNumber = 0;
                break;

        }

        if (squareNumber >=0 && squareNumber =6 && squareNumber =12 && squareNumber =18 && squareNumber = 24 && squareNumber = 30 && squareNumber = 36 && squareNumber <= 41){
            rowNumber = 0;
            columnNumber = squareNumber % 6;
        }

    }//end MapSquareNumToScreenRowAndColumn


I'm fairly new to programming, so I just wanted a second opinion on it. Is there a way fo

Solution

The problem of your code is that you use separate conditions for each particular row and for each particular column.

But it can be easily aggregated into one - check if the row number is odd. A rest of the desired functionality can be reached using arithmetics.

Please try this code:

private static void MapSquareNumToScreenRowAndColumn(int squareNumber, out int rowNumber, out int columnNumber)
{
    const int Rows = 7;
    const int Cols = 6;

    // Calculate the rowNumber and the columnNumber at once.
    // See Math.DivRem for details: http://msdn.microsoft.com/en-us/library/yda5c8dx(v=vs.110).aspx
    rowNumber = Rows - 1 - Math.DivRem(squareNumber, Cols, out columnNumber);

    // If the rowNumber is odd:
    if ((rowNumber & 1) != 0)  // (rowNumber & 1) is the same as (rowNumber % 2)
        columnNumber = Cols - 1 - columnNumber;  // Invert the columnNumber
}

Code Snippets

private static void MapSquareNumToScreenRowAndColumn(int squareNumber, out int rowNumber, out int columnNumber)
{
    const int Rows = 7;
    const int Cols = 6;

    // Calculate the rowNumber and the columnNumber at once.
    // See Math.DivRem for details: http://msdn.microsoft.com/en-us/library/yda5c8dx(v=vs.110).aspx
    rowNumber = Rows - 1 - Math.DivRem(squareNumber, Cols, out columnNumber);

    // If the rowNumber is odd:
    if ((rowNumber & 1) != 0)  // (rowNumber & 1) is the same as (rowNumber % 2)
        columnNumber = Cols - 1 - columnNumber;  // Invert the columnNumber
}

Context

StackExchange Code Review Q#67092, answer score: 6

Revisions (0)

No revisions yet.