1

I have a class called Game.cs and in the class I have the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Simon
{
    class Game
    {
        public int[] TheArray = new int[1000];

        private bool m_Play;
        public bool Play
        {
            set { m_Play = value; }
            get { return m_Play; }
        }

        public Game()
        {
            Random rnd = new Random();

            for (int i = 0; i < 8; i++)
            {
                TheArray[i] = rnd.Next(0, 4); // between 0 and 3
            }

        }


    }
}

I want to be able to call TheArray from my form. I want the loop to iterate based on the times I click button5 and then I want to click my buttons programmatically based on what my array returns. On my form I have 4 buttons called, button1,button2,button3 and button4.

Once I click on button5 my code needs to click the button based on the array each time it loops through TheArray

So far I have this:

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 Simon
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private Game m_game;

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("box1");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("box2");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            MessageBox.Show("box3");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            MessageBox.Show("box4");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            // Determine which button to click based on TheArray
        }
    
    }
}
6
  • 1
    What do you mean by "latest int"? Commented Feb 15, 2014 at 14:44
  • I want the loop to iterate based on the times i click button5 and then i want to click my buttons programmatically based on what my array returns.. Commented Feb 15, 2014 at 14:45
  • Unless you make Game static, you need an object to get TheArray. Commented Feb 15, 2014 at 14:46
  • How are you going to determine when button5 can stop collecting clicks? Commented Feb 15, 2014 at 14:47
  • 2
    side comment: your for loop can be rewritten in proper C#: TheArray = Enumerable.Range(0,8).Select(x => random.Next(0,4).ToArray(); - and I strongly suggest using a List<int> instead. Commented Feb 15, 2014 at 14:53

2 Answers 2

2

To click on the buttons based on the array (I'm assuming 0 in the array corresponds to button1, etc, you could try something like this:

private void button5_Click(object sender, EventArgs e)
{
     Button[] button = { button1, button2, button3, button4 };
     for (int i = 0; i < m_game.TheArray.Length; i++)
     {
         button[m_game.TheArray[i]].PerformClick();
     }
}

As a side note, as @ThunderGr pointed out in a comment, you'd have to create an instance of your game or else make it static.

Sign up to request clarification or add additional context in comments.

1 Comment

@PriceCheaperton, did either of these answers help?
1

In the form declaration define Game MyGameClass=new Game();.

You code has lots of gaps, though. In order to get the last int, you need to also define a public int CurrentInt=0; in the Game Class and do a CurrentInt++; from within Game().

Now, you can do a int theInt=MyGameClass.TheArray[MyGameClass.CurrentInt]; from within the button5 event and implement a switch statement to find which button to click.

Of course, this is still inefficient. Perhaps it would be better if you declared a public int GetLastInt() in Game that would return TheArray[CurrentInt];.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.