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;
}
}
}