0

I am programming a small two-player game called "GoMoku" at the moment. It's just like "connect 4" with the difference that you can set the stone everywhere on the field. I realized it with some buttons which I create with a for-loop and set the number of the player in the array of the buttons. It works quite well, but always when I click on the buttons near to the frames of the form, it says that I have an out of range exception. I debugged and checked the locations and they are sometimes wrong and sometimes not; kind of randomly. What am I doing wrong and how can I solve this issue?

Here is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Gomoku
{
    public partial class Form1 : Form
    {
        int player = 1;
        int[,] positions = new int[25, 25];

        public Form1()
        {
            InitializeComponent();
            label2.Text = "player one's turn";

            for (int y = 0; y < 18; y++)
            {
                for (int x = 0; x < 18; x++)
                {
                    Button button = new Button();
                    button.Location = new Point(x * 20, y * 20);
                    button.Size = new Size(20, 20);
                    button.Click += button1_Click;
                    tabPage1.Controls.Add(button);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int Xcount = 0;
            int Ycount = 0;

            Button button = sender as Button;

            if (button.BackColor == Color.Transparent)
            {
                if (spieler == 1)
                {
                    label2.Text = "player two's turn";
                    button.BackColor = Color.Red;
                    positions[button.Location.X / 20 + 1, button.Location.Y / 20 + 1] = player;
                    for (int i = 0; i < 4; i++)
                    {
                        if (positions[button.Location.X / 20 + 1 + i, button.Location.Y / 20 + 1] == player)
                        {
                            Xcount++;
                        }

                        if (positions[button.Location.X / 20 + 1 - i, button.Location.Y / 20 + 1] == player)
                        {
                            Xcount++;
                        }

                        if (positions[button.Location.X / 20 + 1, button.Location.Y / 20 + 1 + i] == player)
                        {
                            Ycount++;
                        }

                        if (positions[button.Location.X / 20 + 1, button.Location.Y / 20 + 1 - i] == player)
                        {
                            Ycount++;
                        }

                        if (Xcount == 5 || Ycount == 5)
                        {
                            label2.Text = "player two wins";
                        }
                    }

                    player = 2;
                }
                else if (player == 2)
                {
                    for (int i = 0; i < 4; i++)
                    {
                        if (positions[button.Location.X / 20 + 1 + i, button.Location.Y / 20 + 1] == player)
                        {
                            Xcount++;
                        }

                        if (positions[button.Location.X / 20 + 1 - i, button.Location.Y / 20 + 1] == player)
                        {
                            Xcount++;
                        }

                        if (positions[button.Location.X / 20 + 1, button.Location.Y / 20 + 1 + i] == player)
                        {
                            Ycount++;
                        }

                        if (positions[button.Location.X / 20 + 1, button.Location.Y / 20 + 1 - i] == player)
                        {
                            Ycount++;
                        }

                        if (Xcount == 5 || Ycount == 5)
                        {
                            label2.Text = "player two wins";
                        }

                        label2.Text = "player one's turn";
                        button.BackColor = Color.Blue;
                        positions[button.Location.X / 20 + 1, button.Location.Y / 20 + 1] = player;

                        player = 1;
                    }
                }
            }
        }
    }
}
ragerory
  • 1,360
  • 1
  • 8
  • 27
  • The debugger will probably help you better than we will. Do watch out for integer division errors though. As long as each number is a multiple of 20 you can divide by 20, but only then. – TaW Dec 07 '16 at 19:39
  • I am not following exactly what you are doing here but on the line: `positions[button.Location.X / 20 + 1 - i, button.Location.Y / 20 + 1] == player` This will cause an index out of range if `button.Location.X` equals 0. And it will if you click a button in the first column. If Location.X equals 0, you then divide it by 20 which will still be 0, then you add 1, but then you subtract `i` which could be 0,1,2 or 3. So when `i` gets to 2 the array index will be a negative number… i.e. out of range. You are going to have to fix this incorrect logic to get the proper index of the button. – JohnG Dec 07 '16 at 20:13
  • 1
    You're committing crimes against humanity here. Your button grid is 18 x 18, but you made the array 25 x 25. Those arrays should match. Also, you simply aren't checking the boundaries of your array. You are checking to see if the buttons next to the one clicked have the same value, but you need to check if those buttons even exist. If you click on the Top row, then the `Location.Y / 20 + 1 - i` is going to equal -1, -2, etc, which is outside your array. You have to check for that. – LarsTech Dec 07 '16 at 20:54
  • Because you generate same size are (18X18), instead of playing with `Location`, add `TableLayoutPanel` with 18 rows and 18 columns, add button to every cell and use Row and Column Indexes to determine the positions of buttons – Fabio Dec 07 '16 at 23:54
  • Left click to "Properties" and find "Location" and you can see the values there. I'm sure I'm answering your title, but correct me if I am wrong? – Shawn Coker Dec 07 '16 at 21:18

0 Answers0