Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7604563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:56:31+00:00 2026-05-30T23:56:31+00:00

I have a 5×5 2D array in C#. I need to check the 2

  • 0

I have a 5×5 2D array in C#. I need to check the 2 spaces above, below, right and left of the array. What is the best way to do this? The try catch statements are for when it has to check a point that would be out of bounds.

This is what I have so far, and it works, but it just looks sloppy.

bool[,] boardSpaces = new bool[5, 5] { 
    { true, true, true, true, true }, 
    { true, true, true, true, true },
    { true, true, true, true, true },
    { true, true, true, true, true },
    { true, true, true, true, true } 
};


for (int x = 0; x < 5; x++)
{
    for (int y = 0; y < 5; y++)
    {
        if (boardSpaces[x, y] == false)
        {
            try
            {
                if (boardSpaces[x - 1, y] == true && boardSpaces[x - 2, y] == true)
                {
                    validMoveRemaining = true;
                    break;
                }
            }
            catch { }

            try
            {
                if (boardSpaces[x + 1, y] == true && boardSpaces[x + 2, y] == true)
                {
                    validMoveRemaining = true;
                    break;
                }
            }
            catch { }
            try
            {
                if (boardSpaces[x, y - 1] == true && boardSpaces[x, y - 2] == true)
                {
                    validMoveRemaining = true;
                    break;
                }
            }
            catch { }
            try
            {
                if (boardSpaces[x, y + 1] == true && boardSpaces[x, y + 2] == true)
                {
                    validMoveRemaining = true;
                    break;
                }
            }
            catch { }

        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T23:56:32+00:00Added an answer on May 30, 2026 at 11:56 pm

    Instead of exception handling, use arithmetic bounds checking. You already have the row and column index as well as the dimensions of the matrix. Why don’t you simply if() that?

    if(x - 2 >= 0 && x + 2 < matrixWidth)
    {
      //...
    }
    if(y - 2 >= 0 && y + 2 < matrixHeight)
    {
      //...
    }
    

    You can even make a compound boolean expression out of it:

    validMoveRemaining =
      !board[x,y] && (
        (x >= 2 && board[x - 1, y] && board[x - 2, y]) ||
        (x < boardWidth - 2 && board[x + 1, y] && board[x + 2, y]) ||
        (y >= 2 && board[x, y - 1] && board[x, y - 2]) ||
        (y < boardHeight - 2 && board[x, y + 1] && board[x, y + 2])
      );
    

    There you go! 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP array that I need to sort. I have included the
This is a php question. I have an array of arrays: Array ( [1]
I have a php array like this: [ ['url_id' => 2191238, 'time_spent' => 41],
I have an array char[] Select = {'A','B','C','D','E','F','G','H','I','J'} and each element in this array
I have an array in php like this : Array ( [0] => Array
Have you managed to get Aptana Studio debugging to work? I tried following this,
suppose that we have three array int a[]=new int[]{4,6,8,9,11,12}; int b[]=new int[]{3,5,7,13,14}; int c[]=new
I have the following numpy array: # A B C Y my_arr = np.array([
I basically have this piece of code. char (* text)[1][80]; text = calloc(2821522,80); The
I have to put my database's data in this format (values will differ, obviously),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.