Sidharth Ghoshal
Sidharth Ghoshal

Reputation: 720

Creating button methods for a SUPER TIC TAC TOE game

I'm making a Super Tac Toe game (its basically a 3x3 grid of tic tac toe boards)

where the board consists of a 3x3 block of buttons with the following click method

    private void buttonI_Click(object sender, EventArgs e)
{
    if(jar == true)
    {
        buttonI.Text = "X";
    }
    if(jar == false)
    {
        buttonI.text = "O";
    }
    jar = !(jar)
}

where jar is a boolean variable that is used to keep track of each player's turn. I used a Python script to mass produce the code for 1 <= I <= 81 but... only my buttons 1-9 have correct functionality. These buttons were manually built using the Visual Studio IDE (I just double clicked them to create the button click method) but I see absolutely no difference in syntax between those buttons and the buttons that I massproduced code for.

Some help and a clear explanation would be awesome!

Upvotes: 0

Views: 1178

Answers (2)

poy
poy

Reputation: 10507

Avoid "mass produced code"... If later you need to change the functionality, you would have to change it for 81 methods. That's not maintainable.

So, what to do?

First of all, I wouldn't even manually drop each button into window. I would have my code add each button for me. Now, you didn't say if you were doing WinForms or WPF. So I'll assume WPF because it's newer and I think far better.

Programmatically adding Buttons

Create a Window with a Grid named gMain. Then tie to the window's Initialize event:

private void handleWindowInitialized(object sender, EventArgs e)
{
  // Add 3 rows and 3 columns.
  for (int i = 0; i < 3; i++)
  {
    gMain.ColumnDefinitions.Add(new ColumnDefinition());
    gMain.RowDefinitions.Add(new RowDefinition());
  }

  // Add button to each cell.
  for(int row=0; row<3; row++)
    for (int column = 0; column < 3; column++)
      createButton(row, column);
}

So now that we have setup gMain, lets look at the method createButton().

Building the Button

private void createButton(int row, int column)
{
  var button = new Button();

  // Add to gMain
  gMain.Children.Add(button);

  // Place the button in the correct cell.
  Grid.SetRow(button, row);
  Grid.SetColumn(button, column);

  // Tie to the click event.
  button.Click += handleButtonClick;
}

Simple enough? We just create a Button, add it to gMain, put in the correct cell, and then tie to the Click event. Now lets look at the event handler.

Event Handler

void handleButtonClick(object sender, RoutedEventArgs e)
{
  var button = (Button)sender;
  int row = Grid.GetRow(button);
  int column = Grid.GetColumn(button);

  // Do what ever you would like to do...
}

Notice that we are able to tell the column and row... from this you should be able to do whatever logic you need. This is great because then you don't need a bunch of different methods.

Upvotes: 1

Shai
Shai

Reputation: 25619

Wow.. ok. Basic rule #1: You're OBJECT ORIENTED. not MODULAR. Why duplicate the function?

Instead, you could SET an ID for the button, and have the same onClick event for all buttons

Define 9 buttons, each button with an id 0..8 So, each button will invoke

private void anyButton_Click(object sender, EventArgs e)
{
    // sender is your button! you can use it's .id or anything alike
    if(jar == true)
    {
        buttonI.Text = "X";
    }
    if(jar == false)
    {
        buttonI.text = "O";
    }
    jar = !(jar)
}

Upvotes: 0

Related Questions