0

I'm comparatively new to C# -language and have studied it only about a month at school till this day. I'm trying to create simple Tic - Tac - Toe game for a Windows Mobile phone, where the game area is set of buttons. I read from this site (Stackoverflow) that C# is strongly typed language and it's not possible to create variables dynamically.

My game area buttons are named as button1, button 2, button3, etc and there's a statistics in int[] -array for the checking the game results. How should I loop the buttons for legal move? I've planned to use buttonX.Content.Equals("X") and so on, but how can I loop the buttons since dynamic variables are not allowed?

Is it possible to create an array containing only references to the button -objects in the memory, and check the values in for each -loop then?

Jere_Sumell
  • 543
  • 1
  • 4
  • 8
  • 1
    Of course dynamic variables are allowed, dynamic variable _types_ are what's not common in C# (they exist since C# 4 with the `dynamic` keyword, but are frowned upon for a good reason). For example, I would use a `List` over an array any day unless I had an extremely good reason not to. – Benjamin Gruenbaum May 24 '14 at 14:35

2 Answers2

0

You are looking for control arrays. Take a look at the answer to this question

Community
  • 1
  • 1
Kasper Holdum
  • 12,993
  • 6
  • 45
  • 74
0

I am unsure if you found your solution because the question is not marked answered but:

Button[,] MovementPiece; //Creates the array
    private void Form1_Load(object sender, EventArgs e)
    {
        //Initializes the array
        MovementPiece = new Button[,]{ { button1, button2, button3 }, 
                                  { button4, button5, button6 },
                                  { button7, button8, button9 } };
    }
rguarascia.ts
  • 694
  • 7
  • 19
  • My solution was similar to your answer. I placed the references of the Button objects to the array, and checked the legal move in the for -loop. Thanks anyway! Now my Tic-Tac-Toe is ready and waiting for Microsoft sertification to publish at Microsoft Store. – Jere_Sumell May 25 '14 at 04:01
  • Please mark this question as answered. Either with my answer, the one above, or your own. – rguarascia.ts May 25 '14 at 04:10