Hello -
on my noughts and crosses / tic tac toe game I have initialised a 3 x 3 array and addded buttons using all the code below. If I want to also have a 5X5 array game and aa 7X7 array game - how do I go about it without repeating all the button code ? I know that I obviously change 3X3 for 5X5 and 7X7 but after that I 'm getting a bit stuck:-/

thank you for any ideas .... all the best John

private void Form1_Load(object sender, EventArgs e)
        {           

            for (int x = 0; x < 3; x++)//loop relating to rows
            {
              
               for (int y = 0; y < 3; y++)//loop relating to columns
               {
                  nandcButton[x, y] = new Button();//Create the Buttons
                  nandcButton[x,y].Name = "nandcButton" + x + ","+ y;//name and coordinates and set up number, number to show
                  nandcButton[x,y].Width = 60;
                  nandcButton[x,y].Height = 60;
                  nandcButton[x,y].Left = nandcButton[x,y].Left + nandcButton[x,y].Width + (x * 60);
                  nandcButton[x,y].Top = nandcButton[x,y].Top + nandcButton[x,y].Top + 50 + (y * 60); //Centre the button 
                  nandcButton[x,y].Click += new EventHandler(Button_Click); //links the above button spec.
                  //Add them to the container
                  nandcHolder.Controls.Add(nandcButton[x,y]);
                  nandcButton[x, y].Text =" "; //declares text

               }   

            }

        }

Recommended Answers

All 3 Replies

How will you choose 3,5 or 7 option? You have to do an option to let the user chooses it, and then its simple - get the number user has choosen, and pass it to the method which will create the field of buttons to play:

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

namespace Jan08Exercise
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.comboBox1.Items.AddRange(new string[] { "3x3", "5x5", "7x7" });
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string item = comboBox1.GetItemText(comboBox1.SelectedItem);
            if (item != String.Empty)
            {
                string sNumber = item.Substring(0, item.Length - 2);
                int value = Convert.ToInt32(sNumber);
                CreatingField(value);
            }
        }

        private void CreatingField(int value)
        {
            for (int x = 0; x < value; x++)//loop relating to rows
            {
                for (int y = 0; y < value; y++)//loop relating to columns
                {
                    nandcButton[x, y] = new Button();//Create the Buttons
                    nandcButton[x, y].Name = "nandcButton" + x + "," + y;//name and coordinates and set up number, number to show
                    nandcButton[x, y].Width = 60;
                    nandcButton[x, y].Height = 60;
                    nandcButton[x, y].Left = nandcButton[x, y].Left + nandcButton[x, y].Width + (x * 60);
                    nandcButton[x, y].Top = nandcButton[x, y].Top + nandcButton[x, y].Top + 50 + (y * 60); //Centre the button 
                    nandcButton[x, y].Click += new EventHandler(Button_Click); //links the above button spec.
                    //Add them to the container
                    nandcHolder.Controls.Add(nandcButton[x, y]);
                    nandcButton[x, y].Text = " "; //declares text
                }
            }
        }
    }
}

Hope it helps a bit.
If this is not something you wanted, let me know.
Mitja

Thanks for that - it took me a while to undestand - but when I did it turned out to be the perfect solution:)

Hehe, I am glad I way in some help.
Enjoy.
Mitja

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.