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 8776841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:09:03+00:00 2026-06-13T19:09:03+00:00

Possible Duplicate: c# using 2d arrays for buttons I’m working on a game using

  • 0

Possible Duplicate:
c# using 2d arrays for buttons

I’m working on a game using a 2×2 board that will be extended to a 7×6.

I’m doing the winning detection at the moment, but I think I’m doing it the long way. There must be a much shorter way.

The winn

  1. Horizontally
  2. Vertically
  3. Diagonally

Here’s a pic of game board:

enter image description here

This is how I’m currently detecting winner

if (btns[0, col].BackColor.Equals(Color.Red) && btns[1, col].BackColor.Equals(Color.Red))
{
    MessageBox.Show("Red Win");
}

if (btns[0, col].BackColor.Equals(Color.Blue) && btns[1, col].BackColor.Equals(Color.Blue))
{
    MessageBox.Show("Blue Win");
}

This way seems like I have to list all combinations, and it would not be very ideal when I extend to 7×6.

Here is the whole code of the program

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        private Button[,] btns;

        public Form1()
        {
            InitializeComponent();

            btns = new Button[,] { { button2 , button1 },
                                   { button4 , button3 }};
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            foreach (var btn in btns)
            {
                btn.Enabled = false;
            }
        }
        int cc = 0;


        private void button5_Click(object sender, EventArgs e)
        {
          // Button[] row1 = new Button[] {button2, button1};

            for (int col = 0; col < btns.GetLength(1); ++col)
            {
                var btn = btns[0, col];

                if (!btn.Enabled)
                {
                    btn.Enabled = true;

                    if (cc == 0)
                    {
                        cc = 1;
                        btn.BackColor = Color.Red;
                    }
                    else
                    {
                        cc = 0;
                        btn.BackColor = Color.Blue;
                    }

                    if (btns[0, col].BackColor.Equals(Color.Red) && btns[1, col].BackColor.Equals(Color.Red))
                    {
                        MessageBox.Show("Red Win");
                    }

                    if (btns[0, col].BackColor.Equals(Color.Blue) && btns[1, col].BackColor.Equals(Color.Blue))
                    {
                        MessageBox.Show("Blue Win");
                    }

                    return;
                }
            }
        }


        private void button6_Click(object sender, EventArgs e)
        {
           // Button[] row2 = new Button[] { button4, button3 };
            for (int col = 0; col < btns.GetLength(1); ++col)
            {
                var btn = btns[1, col];

                if (!btn.Enabled)
                {
                    btn.Enabled = true;

                    if (cc == 0)
                    {
                        cc = 1;
                        btn.BackColor = Color.Red;
                    }
                    else
                    {
                        cc = 0;
                        btn.BackColor = Color.Blue;
                    }

                    if (btns[1, col].BackColor.Equals(Color.Red) && btns[0, col].BackColor.Equals(Color.Red))
                    {
                        MessageBox.Show("Red Win");
                    }

                    if (btns[1, col].BackColor.Equals(Color.Blue) && btns[0, col].BackColor.Equals(Color.Blue))
                    {
                        MessageBox.Show("Blue Win");
                    }
                    return;
                }
            }
        }
    }
}

I have tried a lot of other ways but I can’t seem to get it working.

  • 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-06-13T19:09:04+00:00Added an answer on June 13, 2026 at 7:09 pm

    Maybe this answer is complicated and I’ll get many downvotes but I couldn’t resist solving this as optimal as possible. Try to inspect this code to the detail:

            int n; //dimension of the matrix
            Button [,] btns;
            public Form1()
            {
                InitializeComponent();
                n = 2;/*You should set here the dimension of your matix. I considered it nxn because of diagonals. If you want nxm matrix than the code is a little bit complicated but not too much*/
                btns = new Button[n, n];
                for(int i = 0;i<n;i++)
                   for(int j = 0; j<n; j++)
                   {
                       Button btn = new Button();
                       btn.Location = new Point(i*20,j*40);
                       btn.Size = new Size(18,38);
                       btns[i,j] = btn;
                       this.Controls.Add(btn);
                   }
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                int mainDiag = 0;
                int secDiag = 0;
                int i = 0;
                int j = 0;
                int [] cols = new int[n];
                int winner = 0; //no winner
                while(winner == 0 && i<n)
                {
                    int row = 0;
                    j = 0;
                    while(j<n)
                    {
                        if (btns[i, j].BackColor == Color.Blue)
                        {
    
                            if (i == j)
                                mainDiag++;//inrement main diagonal
                            if(i + j == n-1)
                                secDiag++;//increment second diagonal
                            row++; //increment row
                            cols[i]++; //increment column
                        }
                        else if (btns[i, j].BackColor == Color.Red)
                        {
                            if (i == j)
                                mainDiag--;
                            if(i + j == n-1)
                                secDiag--;
                            row--;
                            cols[i]++;
                        }
                        j++;
                    }
                    if(row == n) //if row value == n whole row is blue and blue player wins
                        winner = 1;
                    else if(row == -n)
                        winner = -1; //if row value == -n whole row is red and red player wins
                    i++;
                }
                if(winner == 0)
                {
                    if(mainDiag == n)
                        winner = 1; //similar for the diagonal
                    else if(mainDiag == -n)
                        winner = -1; 
                    else if(secDiag == n)
                        winner = 1;//similar for the second diagonal
                    else if(secDiag == -n)
                        winner = -1;
                    else
                    {
                        i = 0;
                        while (winner == 0 && i < n)
                        {
                            if (cols[i] == n)
                                winner = 1; //i-th column is whole blue and blue player wins
                            else if (cols[i] == -n)
                                winner = -1; //i-th column is whole red and red player wins
                        }
                    }
                }
                if (winner == 1)
                    MessageBox.Show("Blue wins");
                else if(winner == -1)
                    MessageBox.Show("Red wins");
    
    
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: JSON pretty print using JavaScript I'm working on a project that will
Possible Duplicate: Comparing Two Arrays Using Perl I am trying to find elements that
Possible Duplicate: c++ using too much cpu my game uses over 50% of cpu.
Possible Duplicate: GCC problem : using a member of a base class that depends
Possible Duplicate: How do you realloc in C++? I know that C++ arrays can
Possible Duplicate: Comparing Two Arrays Using Perl How can I print values which exist
Possible Duplicate: JavaScript “For …in” with Arrays In which situations using for (var i
Possible Duplicate: Ruby: Sorting 2 arrays using values from one of them I have
Possible Duplicate: Difference between using character pointers and character arrays What's the difference between:
Possible Duplicate: Literal Syntax For byte[] arrays using Hex notation..? I am trying to

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.