3目並べとはこのようなものですが、
三目並べ - Wikipedia
人工知能ならぬ「人工無能」でC#で作成しました。
無能な所以は、Computerは乱数で指すだけだから。
このゲームは今、AI界隈で流行りの「将棋」や「囲碁」と比べて、すこぶる単純で、勝敗判定も総当たりで実現できます。
ぐぐると、プログラミングしている例はあると思いますが、今回はこんなですが完全自力です。
素人なので、効率的なアルゴリズムではないかもしれませんし、一つどうしても修正できない部分もあります。
//ここだけ強引
と書いてあるところ。
いずれ考えなおして修正します。
using System; using System.Windows.Forms; namespace _3x3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } bool teban = true; int shouhai = 0; int count = 0; int[,] Array = new int[,] { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } }; private void Form1_Load(object sender, EventArgs e) { } private void hantei() { if (Array[0, 0] == 1) { button00.Text = "〇"; } else if(Array[0,0]==-1) { button00.Text = "●"; } if (Array[0, 1] == 1) { button01.Text = "〇"; } else if (Array[0, 1] == -1) { button01.Text = "●"; } if (Array[0, 2] == 1) { button02.Text = "〇"; } else if (Array[0, 2] == -1) { button02.Text = "●"; } if (Array[1, 0] == 1) { button10.Text = "〇"; } else if (Array[1, 0] == -1) { button10.Text = "●"; } if (Array[1, 1] == 1) { button11.Text = "〇"; } else if (Array[1, 1] == -1) { button11.Text = "●"; } if (Array[1, 2] == 1) { button12.Text = "〇"; } else if (Array[1, 2] == -1) { button12.Text = "●"; } if (Array[2, 0] == 1) { button20.Text = "〇"; } else if (Array[2, 0] == -1) { button20.Text = "●"; } if (Array[2, 1] == 1) { button21.Text = "〇"; } else if (Array[2, 1] == -1) { button21.Text = "●"; } if (Array[2, 2] == 1) { button22.Text = "〇"; } else if (Array[2, 2] == -1) { button22.Text = "●"; } int sum = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { sum += Array[i, j]; } if (sum == 3) { shouhai = 1; } else if(sum ==-3) { shouhai = -1; } sum = 0; } for (int j = 0; j < 3; j++) { for (int i = 0; i < 3; i++) { sum += Array[i, j]; } if (sum == 3) { shouhai = 1; } else if (sum == -3) { shouhai = -1; } sum = 0; } if (Array[0, 0] + Array[1, 1] + Array[2, 2] == 3) //斜め { shouhai = 1; } else if (Array[0, 0] + Array[1, 1] + Array[2, 2] == -3) { shouhai = -1; } if (Array[2, 0] + Array[1, 1] + Array[0, 2] == 3) //斜め { shouhai = 1; } else if (Array[2, 0] + Array[1, 1] + Array[0, 2] == -3) { shouhai = -1; } if (teban == true) { count++; } else if (shouhai == 0) { DateTime now = DateTime.Now; int set; set = now.Second + 2; if (set > 59) { set = set - 60; } while (DateTime.Now.Second - set != 0) { } Random r = new Random(); int randX = r.Next(0, 3); int randY = r.Next(0, 3); while (Array[randX, randY] != 0 && shouhai == 0) { randX = r.Next(0, 3); randY = r.Next(0, 3); } Array[randX, randY] = -1; teban = !teban; hantei(); count++; } if (shouhai == 1) { MessageBox.Show("You Win!"); } else if (shouhai == -1) { MessageBox.Show("You Lose!"); shouhai = 2; count = 10; //ここだけ強引 } else if (count == 9) { MessageBox.Show("Draw!"); shouhai = 2; } } private void button00_Click(object sender, EventArgs e) { if (teban == true) { Array[0, 0] = 1; } else { Array[0, 0] = -1; } teban = !teban; hantei(); } private void button01_Click(object sender, EventArgs e) { if (teban == true) { Array[0, 1] = 1; } else { Array[0, 1] = -1; } teban = !teban; hantei(); } private void button02_Click(object sender, EventArgs e) { if (teban == true) { Array[0, 2] = 1; } else { Array[0, 2] = -1; } teban = !teban; hantei(); } private void button10_Click(object sender, EventArgs e) { if (teban == true) { Array[1, 0] = 1; } else { Array[1, 0] = -1; } teban = !teban; hantei(); } private void button11_Click(object sender, EventArgs e) { if (teban == true) { Array[1, 1] = 1; } else { Array[1, 1] = -1; } teban = !teban; hantei(); } private void button12_Click(object sender, EventArgs e) { if (teban == true) { Array[1, 2] = 1; } else { Array[1, 2] = -1; } teban = !teban; hantei(); } private void button20_Click(object sender, EventArgs e) { if (teban == true) { Array[2, 0] = 1; } else { Array[2, 0] = -1; } teban = !teban; hantei(); } private void button21_Click(object sender, EventArgs e) { if (teban == true) { Array[2, 1] = 1; } else { Array[2, 1] = -1; } teban = !teban; hantei(); } private void button22_Click(object sender, EventArgs e) { if (teban == true) { Array[2, 2] = 1; } else { Array[2, 2] = -1; } teban = !teban; hantei(); } private void buttonRestart_Click(object sender, EventArgs e) { shouhai = 0; count = 0; teban = true; button00.Text = ""; button01.Text = ""; button02.Text = ""; button10.Text = ""; button11.Text = ""; button12.Text = ""; button20.Text = ""; button21.Text = ""; button22.Text = ""; for (int j = 0; j < 3; j++) { for (int i = 0; i < 3; i++) { Array[j, i] = 0; } } } } }