0

i want to make a tictactoe game. But i want all the rules are on the classes and call them to GUI. How can i send the ClickHandler public to the GUI???

here is the class code: ( public void ClickHandler )

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

namespace tictactoe
{
    public class Class1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Button[] _buttonArray;
        public bool _isX;
        public bool _isGameOver;
        //private IContainer components;
        //Form1 load = new Form1();

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /*protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }*/
        public void InitTicTacToe()
        {   
            this._isX = true;
            this._isGameOver = false;
        }

        public void ClickHandler(object sender, System.EventArgs e)
        {
            Button tempButton = (Button)sender;
            if (this._isGameOver)
            {
                MessageBox.Show("please start new game!", "Game End", MessageBoxButtons.OK);
                return;
            }
            if (tempButton.Text != "")
            {
                return;
            }
            if (_isX)
                tempButton.Text = "X";
            else
                tempButton.Text = "O";
            _isX = !_isX;
            this._isGameOver = Class2.CheckWinner(_buttonArray);
        }

    }
    public class Class2
    {

        static private int[,] Winners = new int[,]
				   {
						{0,1,2},
						{3,4,5},
						{6,7,8},
						{0,3,6},
						{1,4,7},
						{2,5,8},
						{0,4,8},
						{2,4,6}
				   };
        static public bool CheckWinner( Button [] myControls)
        {
            bool gameOver = false;
            for (int i = 0; i < 8; i++)
            {
                int a = Winners[i, 0], b = Winners[i, 1], c = Winners[i, 2];
                Button b1 = myControls[a], b2 = myControls[b], b3 = myControls[c];
                if (b1.Text == "" || b2.Text == "" || b3.Text == "")
                    continue;
                if (b1.Text == b2.Text && b2.Text == b3.Text)
                {
                    gameOver = true;
                    MessageBox.Show(b1.Text + " .... Wins the game!", "Game End", MessageBoxButtons.OK);
                    break;
                }
            }
            return gameOver;
        }
    }
}

here is the GUI code ( i want to send ClickHandler in but1_Click)

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 tictactoe
{

    public partial class Form1 : Form
    {
        private System.Windows.Forms.Button[] _buttonArray;
        Class1 load = new Class1();


        public Form1()
        {
            InitializeComponent();
            _buttonArray = new Button[9] { but1, but2, but3, but4, but5, but6, but7, but8, but9 };
            load.InitTicTacToe();
           
           
        }

        private void but1_Click(object sender, EventArgs e)
        {
            load.ClickHandler(); // <<<<< here come the problem!!!!!!!
            /*Button tempButton = (Button)sender;
            if (load._isGameOver)
            {
                MessageBox.Show("please start new game!", "Game End", MessageBoxButtons.OK);
                return;
            }
            if (tempButton.Text != "")
            {
                return;
            }
            if (load._isX)
                tempButton.Text = "X";
            else
                tempButton.Text = "O";
            load._isX = !load._isX;
            load._isGameOver = Class2.CheckWinner(_buttonArray);
             * */
        }
    }
}

some1 knows how to do it?? or i have to do it another way??

help plz

thx
GaBack

2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by GaBack
0

You do not pass the parameters to the event.
If there is no actual parameters need, you can pass null, like:

private void but1_Click(object sender, EventArgs e)
{
    load.ClickHandler(null, null);
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.