Search code examples
c#winformstic-tac-toe

Tic tac toe vs computer class


I am writing a tic tac toe game. My logic is in a class that's why I have some difficulties. My board is a matrix. Firstly, I put X when I click a button, then the computer choose a block where to put O. In the class I save the value of the matrix element but I don't know how to show it on the winform. Here is a part of my code..

public string ToggleNextLetter()
{
        if (!changedLetterX)
        {
            changedLetterX = true;
            return this.nextLetter = "X";
        }

        changedLetterX = false;
        MoveComputer();
        return this.nextLetter = "O";
}

//Reserve the position of the X from the form

public string[,] Matrix(int row, int col, string value)
{
        this.field[row, col] = value;

        return this.field;
    }

public string[,] MoveComputer()
    {

        //priority 1: get tick tac toe
        //priority 2: block x tic tac toe
        //priority 3: go for corner space
        //priority 4: pick open space

        string move;

        move = LookForWinOrBlock("O"); //look for win
        if(move == null)
        {
            move = LookForWinOrBlock("X"); //look for block
            if (move == null)
            {
                move = LookForCorner();
                if (move == null)
                {
                    move = LookForOpenSpace();
                }
            }
        }

        return move;
}

private void btn_click(object sender, EventArgs e)
{
        Button btn = (Button)sender;
        int btnRow = Convert.ToInt32(btn.Tag) / 10;
        int btnCol = Convert.ToInt32(btn.Tag) % 10;
        btn.Text = game.ToggleNextLetter();
        game.Matrix(btnRow, btnCol, btn.Text);
        btn.Enabled = false;
        //btn.Text = game.ToggleNextLetter();

        if (game.HasWinner())
        {
            foreach (var control in Controls)
            {
                try
                {
                    Button b = (Button)control;
                    b.Enabled = false;
                }
                catch { }
            }
            MessageBox.Show("The winner is " + btn.Text);
        }
    }

Solution

  • Just separate your functions

    public string[,] MoveComputer() {
        // just add this line
        MakePlay(btnRow, btnCol, btnText);
    
        return move;
    } 
    
    private void btn_click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        int btnRow = Convert.ToInt32(btn.Tag) / 10;
        int btnCol = Convert.ToInt32(btn.Tag) % 10;
        btn.Text = game.ToggleNextLetter();
        MakePlay((btnRow, btnCol, btn.Text);
    }
    
    private void MakePlay(int btnRow, int btnCol, string btnText) {
        game.Matrix(btnRow, btnCol, btn.Text);
        btn.Enabled = false;
        //btn.Text = game.ToggleNextLetter();
    
        if (game.HasWinner())
        {
            foreach (var control in Controls)
            {
                try
                {
                    Button b = (Button)control;
                    b.Enabled = false;
                }
                catch { }
            }
            MessageBox.Show("The winner is " + btn.Text);
        }
    }