本代码思路借鉴于
https://www.cnblogs.com/phdeblog/p/9062432.html(js实现ai井字棋)
但是没有和上面网页中js实现的一样巧妙。
更像是最蠢的办法一步一步去查询各种情况,判断落子。
只展示后台cs代码。前台界面代码不做展示。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace jinziyouxi
{
public partial class Form1 : Form
{
bool clicked = true;
bool playvai = true;
int click_count = 0;
public Form1()
{
InitializeComponent();
}
private void 游戏ToolStripMenuItem_Click(object sender, EventArgs e)
{
clicked = true;
click_count = 0;
foreach(Control c in Controls)
{
try
{
Button b = (Button)c;
b.Enabled = true;
b.Text= "";
}
catch
{
}
}
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("系统开发者:zwh");
}
private void 退出ToolStripMenuItem1_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void A1_Click(object sender, EventArgs e)
{
Changebutton(sender, e);
}
private void A2_Click(object sender, EventArgs e)
{
Changebutton(sender, e);
}
private void A3_Click(object sender, EventArgs e)
{
Changebutton(sender, e);
}
private void B1_Click(object sender, EventArgs e)
{
Changebutton(sender, e);
}
private void B2_Click(object sender, EventArgs e)
{
Changebutton(sender, e);
}
private void B3_Click(object sender, EventArgs e)
{
Changebutton(sender, e);
}
private void C1_Click(object sender, EventArgs e)
{
Changebutton(sender, e);
}
private void C2_Click(object sender, EventArgs e)
{
Changebutton(sender, e);
}
private void C3_Click(object sender, EventArgs e)
{
Changebutton(sender, e);
}
private void Changebutton(object sender, EventArgs e)
{
Button b = (Button)sender;
if (clicked)
{
b.Text = "X";
}
else
{
b.Text = "O";
}
click_count++;
clicked = !clicked;
b.Enabled = false;
ifendgame();
if ((!clicked) && (playvai))
{
AImove();
}
}
private void AImove()
{
Button where = null;
where = findo("O");
if (where==null)
{
where = findx("X");
if (where == null)
{
where = findjiaoluo();
if (where ==null)
{
where = findother();
}
}
}
try
{
where.PerformClick();
}
catch
{
}
}
private Button findo(String b)
{
//第一行
if ((A1.Text==b)&& (A2.Text == b)&& (A3.Text == ""))
{
return A3;
}
if ((A1.Text == b) && (A2.Text == "") && (A3.Text == b))
{
return A2;
}
if ((A1.Text == "") && (A2.Text == b) && (A3.Text == b))
{
return A1;
}
//第二行
if ((B1.Text == b) && (B2.Text == b) && (B3.Text == ""))
{
return B3;
}
if ((B1.Text == b) && (B2.Text == "") && (B3.Text == b))
{
return B2;
}
if ((B1.Text == "") && (B2.Text == b) && (B3.Text == b))
{
return B1;
}
//第三行
if ((C1.Text == b) && (C2.Text == b) && (C3.Text == ""))
{
return C3;
}
if ((C1.Text == b) && (C2.Text == "") && (C3.Text == b))
{
return C2;
}
if ((C1.Text == "") && (C2.Text == b) && (C3.Text == b))
{
return C1;
}
//第一l
if ((A1.Text == b) && (B1.Text == b) && (C1.Text == ""))
{
return C1;
}
if ((A1.Text == b) && (B1.Text == "") && (C1.Text == b))
{
return B1;
}
if ((A1.Text == "") && (B1.Text == b) && (C1.Text == b))
{
return A3;
}
//第二l
if ((A2.Text == b) && (B2.Text == b) && (C2.Text == ""))
{
return C2;
}
if ((A2.Text == b) && (B2.Text == "") && (C2.Text == b))
{
return B2;
}
if ((A2.Text == "") && (B2.Text == b) && (C2.Text == b))
{
return A2;
}
//第三l
if ((A3.Text == b) && (B3.Text == b) && (C3.Text == ""))
{
return C3;
}
if ((A3.Text == b) && (B3.Text == "") && (C3.Text == b))
{
return B3;
}
if ((A3.Text == "") && (B3.Text == b) && (C3.Text == b))
{
return A3;
}
//对角线
if ((A1.Text == b) && (B2.Text == b) && (C3.Text == ""))
{
return C3;
}
if ((A1.Text == b) && (B2.Text == "") && (C3.Text == b))
{
return B2;
}
if ((A1.Text == "") && (B2.Text == b) && (C3.Text == b))
{
return A1;
}
if ((A3.Text == b) && (B2.Text == b) && (C1.Text == ""))
{
return C1;
}
if ((A3.Text == b) && (B2.Text == "") && (C1.Text == b))
{
return B2;
}
if ((A3.Text == "") && (B2.Text == b) && (C1.Text == b))
{
return A3;
}
return null;
}
private Button findx(String b)
{
if ((A1.Text == b) && (A2.Text == b) && (A3.Text == ""))
{
return A3;
}
if ((A1.Text == b) && (A2.Text == "") && (A3.Text == b))
{
return A2;
}
if ((A1.Text == "") && (A2.Text == b) && (A3.Text == b))
{
return A1;
}
if ((B1.Text == b) && (B2.Text == b) && (B3.Text == ""))
{
return B3;
}
if ((B1.Text == b) && (B2.Text == "") && (B3.Text == b))
{
return B2;
}
if ((B1.Text == "") && (B2.Text == b) && (B3.Text == b))
{
return B1;
}
if ((C1.Text == b) && (C2.Text == b) && (C3.Text == ""))
{
return C3;
}
if ((C1.Text == b) && (C2.Text == "") && (C3.Text == b))
{
return C2;
}
if ((C1.Text == "") && (C2.Text == b) && (C3.Text == b))
{
return C1;
}
//第一l
if ((A1.Text == b) && (B1.Text == b) && (C1.Text == ""))
{
return C1;
}
if ((A1.Text == b) && (B1.Text == "") && (C1.Text == b))
{
return B1;
}
if ((A1.Text == "") && (B1.Text == b) && (C1.Text == b))
{
return A3;
}
//第二l
if ((A2.Text == b) && (B2.Text == b) && (C2.Text == ""))
{
return C2;
}
if ((A2.Text == b) && (B2.Text == "") && (C2.Text == b))
{
return B2;
}
if ((A2.Text == "") && (B2.Text == b) && (C2.Text == b))
{
return A2;
}
//第三l
if ((A3.Text == b) && (B3.Text == b) && (C3.Text == ""))
{
return C3;
}
if ((A3.Text == b) && (B3.Text == "") && (C3.Text == b))
{
return B3;
}
if ((A3.Text == "") && (B3.Text == b) && (C3.Text == b))
{
return A3;
}
if ((A1.Text == b) && (B2.Text == b) && (C3.Text == ""))
{
return C3;
}
if ((A1.Text == b) && (B2.Text == "") && (C3.Text == b))
{
return B2;
}
if ((A1.Text == "") && (B2.Text == b) && (C3.Text == b))
{
return A1;
}
if ((A3.Text == b) && (B2.Text == b) && (C1.Text == ""))
{
return C1;
}
if ((A3.Text == b) && (B2.Text == "") && (C1.Text == b))
{
return B2;
}
if ((A3.Text == "") && (B2.Text == b) && (C1.Text == b))
{
return A3;
}
return null;
}
private Button findjiaoluo()
{
if (A1.Text=="O")
{
if (A3.Text=="")
{
return A3;
}
if (C3.Text=="")
{
return C3;
}
if (C1.Text=="")
{
return C1;
}
}
if (A3.Text == "O")
{
if (A1.Text == "")
{
return A1;
}
if (C3.Text == "")
{
return C3;
}
if (C1.Text == "")
{
return C1;
}
}
if (C3.Text == "O")
{
if (A1.Text == "")
{
return A1;
}
if (A3.Text == "")
{
return A3;
}
if (C1.Text == "")
{
return C1;
}
}
if (C1.Text == "O")
{
if (A1.Text == "")
{
return A1;
}
if (C3.Text == "")
{
return C3;
}
if (A3.Text == "")
{
return A3;
}
}
if (A1.Text=="")
{
return A1;
}
if (A3.Text=="")
{
return A3;
}
if (C1.Text=="")
{
return C1;
}
if (C3.Text=="")
{
return C3;
}
return null;
}
private Button findother()
{
Button b = null;
foreach (Control c in Controls)
{
b = c as Button;
if (b!=null)
{
if (b.Text=="")
{
return b;
}
}
}
return null;
}
private void ifendgame()
{
bool ifwinerout = false;
if (A1.Text == A2.Text && A3.Text == A2.Text && !A1.Enabled)
{
ifwinerout = true;
}
else if (C1.Text == C2.Text && C3.Text == C2.Text && !C1.Enabled)
{
ifwinerout = true;
}
else if (B1.Text == B2.Text && B3.Text == B2.Text && !B1.Enabled)
{
ifwinerout = true;
}
else if (A1.Text == B1.Text && B1.Text == C1.Text && !A1.Enabled)
{
ifwinerout = true;
}
else if (A2.Text == B2.Text && B2.Text == C2.Text && !A2.Enabled)
{
ifwinerout = true;
}
else if (A3.Text == B3.Text && B3.Text == C3.Text && !A3.Enabled)
{
ifwinerout = true;
}
else if (A1.Text == B2.Text && B2.Text == C3.Text && !A1.Enabled)
{
ifwinerout = true;
}
else if (A3.Text == B2.Text && B2.Text == C1.Text && !C1.Enabled)
{
ifwinerout = true;
}
if (ifwinerout)
{
bangame();
if (clicked)
{
Ograde.Text = (int.Parse(Ograde.Text) + 1).ToString();
MessageBox.Show("0 赢得了游戏!");
}
else
{
Xgrade.Text = (int.Parse(Xgrade.Text) + 1).ToString();
MessageBox.Show("X 赢得了游戏!");
}
}
else
{
if (click_count == 9)
{
try
{
MessageBox.Show("游戏结束,没有人获胜!");
}
catch
{
}
}
}
}
private void bangame()
{
try
{
foreach (Control button in Controls)
{
Button b = (Button)button;
b.Enabled = false;
}
}
catch
{
}
}
private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
private void showwhoturn(object sender, EventArgs e)
{
Button b = (Button)sender;
if (b.Enabled)
{
if (clicked)
{
b.Text = "X";
}
else
{
b.Text = "O";
}
}
}
private void cleantext(object sender, EventArgs e)
{
Button b = (Button)sender;
if (b.Enabled)
{
b.Text = "";
}
}
}
}