Hi, I am implementing TictacToe using AI so following is some of my
code snippet.

1. How to recognize computer move and how would i implement that?
2. What I will add in private void box2_Click(object sender, EventArgs e) method?
3. I want to assign best possible move for PC against user.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;



namespace TicTacToeApp
{
    public partial class TicTacToe : Form
    {
        public int player = 1;
       
        public int computer = 1;
        private List<int>[] moves = new List<int>[2] { new List<int>(), new List<int>() };


        public TicTacToe()
        {
            InitializeComponent();

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void switchPlayers()
        {
            if (player == 1)
            {
                player = 2;
            }
            else
            {
                player = 1;
            }

            checkAll();
        }

        private void box1_Click(object sender, EventArgs e)
        {
            box1.SizeMode = PictureBoxSizeMode.StretchImage;
            comparisonofImages();
            box1.Enabled = false;
        }

        private void box2_Click(object sender, EventArgs e)
        {
            box2.SizeMode = PictureBoxSizeMode.StretchImage;
            moves[player - 1].Add(2);
            box2.Enabled = false;
            switchPlayers();
        }

        private void box3_Click(object sender, EventArgs e)
        {
            box3.SizeMode = PictureBoxSizeMode.StretchImage;        
            moves[player - 1].Add(3);
            box3.Enabled = false;
            switchPlayers();
        }

        private void box4_Click(object sender, EventArgs e)
        {
            box4.SizeMode = PictureBoxSizeMode.StretchImage;          
            moves[player - 1].Add(4);
            box4.Enabled = false;
            switchPlayers();
        }

        

        public bool CompareImages(Image image1, Image image2, Image image3)
        {
            if (!Object.Equals(image1, image2))
            {
                return false;
            }
            if (!Object.Equals(image2, image3))
            {
                return false;
            }
            if ((image1 == null) || (image2 == null) || (image3 == null))
            {
                return false;
            }
            return true;

        }

        public void checkAll()
        {
            if (CompareImages(box1.Image, box2.Image, box3.Image))
            {
                DisplayWinner();

            }
            else if (CompareImages(box4.Image, box5.Image, box6.Image))
            {
                DisplayWinner();

            }
            else if (CompareImages(box7.Image, box8.Image, box9.Image))
            {
                DisplayWinner();

            }
            else if (CompareImages(box1.Image, box5.Image, box9.Image))
            {
                DisplayWinner();

            }
            else if (CompareImages(box3.Image, box5.Image, box7.Image))
            {
                DisplayWinner();

            }
            else if (CompareImages(box1.Image, box4.Image, box7.Image))
            {
                DisplayWinner();

            }
            else if (CompareImages(box2.Image, box5.Image, box8.Image))
            {
                DisplayWinner();

            }
            else if (CompareImages(box3.Image, box6.Image, box9.Image))
            {
                DisplayWinner();

            }
            else if (box1.Image != null && box2.Image != null && box3.Image != null && box4.Image != null && box5.Image != null
           && box6.Image != null && box7.Image != null && box8.Image != null && box9.Image != null)
            {
                lblDraw.Visible = true;
                lblDraw.Enabled = true;
                //reset();
            }

        }

        

        private void TicTacToe_Load(object sender, EventArgs e)
        {
            
            pictureBox1.Load("C:\\Documents and Settings\\hahmed\\My Documents\\family pics\\Atif.JPG");
            pictureBox2.Load("c:\\WINDOWS\\system32\\setup.bmp");
        }

        public void comparisonofImages()
        {
            if (box1.Image == pictureBox2.Image && box2.Image == pictureBox2.Image && box3.Image == null)
            {
                box3.Image = pictureBox2.Image;
            }
            if (box1.Image == pictureBox2.Image && box4.Image == pictureBox2.Image && box7.Image == null)
            {
                box7.Image = pictureBox2.Image;
            }
            if (box1.Image == pictureBox2.Image && box5.Image == pictureBox2.Image && box9.Image == null)
            {
                box9.Image = pictureBox2.Image;
            }
            if (box3.Image == pictureBox2.Image && box5.Image == pictureBox2.Image && box7.Image == null)
            {
                box7.Image = pictureBox2.Image;
            }
            if (box2.Image == pictureBox2.Image && box5.Image == pictureBox2.Image && box8.Image == null)
            {
                box8.Image = pictureBox2.Image;
            }
            if (box3.Image == pictureBox2.Image && box6.Image == pictureBox2.Image && box9.Image == null)
            {
                box9.Image = pictureBox2.Image;
            }
            if (box4.Image == pictureBox2.Image && box5.Image == pictureBox2.Image && box6.Image == null)
            {
                box6.Image = pictureBox2.Image;
            }
            if (box7.Image == pictureBox2.Image && box8.Image == pictureBox2.Image && box9.Image == null)
            {
                box9.Image = pictureBox2.Image;
            }

        }
}

what do you mean how to recognize the computer move?

you are writing the code that will basically control the AI for the computer.

what you need to recognize is when the players turn is over so you can start your AI which would be simple.

you are controlling the user input when that is done you can call your methods to manpilate the gameplay.

since it is tic tac toe you might be playing on a 3x3 board IE 9 spaces (that is if you are going for three in a row)

you can simply poll all of the spaces to see what is taken and what is not and randomly generate an X or O appropriately in an empty space.

or if you can program in some general strategies and the ability to understand and recognize them.

IE if spaces 1 and 2 are checked and are checked by the enemy then they are about to get a victory, place computer check in space three

that is assuming the layout is

123
456
789

with that same logic you could program in

if 7,5 is taken make sure to take 3 so the user cannot win

or if 7,5,9 is taken make sure to take the space that will ensure the most saftey based on current moves...etc

good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.