Caza
Caza

Reputation: 3

c# gomoku game label array

I am trying to make a simple five in a row (gomoku) game for two players using windows forms and c#. I put a picturebox with a picture and stretched it out on the form. Now I want to put labels at all the intersections on the picture board so a user can click them and change their background color to black or white.

  1. How can I make the labels created clickable on the form?

    public partial class Form1 : Form
    {
        int labelCount = 0;
        int iteration = 0;
    
        public Form1()
        {
            InitializeComponent();
            Label[] board = new Label[361];
    
            for (int i = 0; i < 361; i++)
            {
                board[i] = new Label
                {
                    Name = "label" + i,
                    Height = 55,
                    Width = 55,
                    MinimumSize = new Size(55, 55),
                    Text = "label " + i
                };
            }
    
            int x = 0;
            int y = 0;
    
            foreach (var Label in board)
            {
                if (x >= 580)
                {
                    x = 0;
                    y = y + Label.Height + 55;
                }
    
                Label.Location = new Point(x, y);
                this.Controls.Add(Label);
                x += Label.Width;
            }        
        }
    }
    
  2. Should I make a one-dimensional [361] or two-dimensional array[{A,1}, {A,2}....{D,1}] to easily check for a winner? How can I connect it to the created labels so the array data corresponds to the objects on the board?

Upvotes: -2

Views: 1352

Answers (2)

Han
Han

Reputation: 3072

I prefer using a 2D array because it's easier if you want to check the surrounding boxes.

Form design:

enter image description here

Full source:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication6
{
    public enum Player
    {
        Empty = 0,
        White,
        Black
    }

    public partial class Form1 : Form
    {
        // initialize board of 5x5
        private Player[,] board = new Player[5, 5];

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DrawBoard();
        }

        private void DrawBoard()
        {
            for (var i = 0; i <= board.GetUpperBound(0); i++)
            {
                for (var j = 0; j <= board.GetUpperBound(1); j++)
                {
                    // for name and text
                    var name = string.Format("{0}, {1}", i, j);

                    var label = new Label()
                    {
                        Name = name, // name of label
                        Size = new Size(55, 55),
                        BorderStyle = BorderStyle.FixedSingle,
                        Location = new Point(i * 55, j * 55), // location depends on iteration
                        Text = name
                    };

                    label.Click += ClickLabel; // subscribe the Click event handler

                    pictureBox1.Controls.Add(label); // add label to a container
                }
            }
        }

        // this event handler will handle all the labels click event
        private void ClickLabel(object sender, EventArgs e)
        {
            var label = (Label)sender; // this is the label that you click
            var x = Convert.ToInt32(label.Name.Split(',')[0]);
            var y = Convert.ToInt32(label.Name.Split(',')[1]);

            // change the color
            if (radPlayerBlack.Checked)
            {
                // Player Black
                label.ForeColor = Color.White;
                label.BackColor = Color.Black;
                board[x, y] = Player.Black;
            }
            else
            {
                // Player White
                label.ForeColor = Color.Black;
                label.BackColor = Color.White;
                board[x, y] = Player.White;
            }
        }
    }
}

You can check the value of the 2D array for black or white. Here's the value when I QuickWatch it in Visual Studio.

enter image description here enter image description here

Upvotes: 0

Jr. Captain
Jr. Captain

Reputation: 43

Well Sorry If don`t understand your question. For the Q.1 to add 361 labels you can try the code below. I hope it will help you.

    public int x = 0;
    public int y = 0;

    private Label[] moku =  new Label[361];

    private void Form1_Load(object sender, EventArgs e)
    {
        try
        {
            for (int i = 0; i < 361; i++)
            {
                moku[i] = new Label();
                moku[i].Parent = pictureBox1;//make the picturebox parent
                moku[i].Location = new Point(x, y);
                moku[i].Text = "O";
                moku[i].Name = "moku" + i;
                moku[i].BackColor = Color.Transparent;
                pictureBox1.Controls.Add(moku[i]);
                y += 55;
                if (y >= 361) { x += 55; y = 0; x+=55; }
            }
        }catch(Exception er)
        {
            MessageBox.Show(er.ToString());
        }


    }

Upvotes: 0

Related Questions