功能: 菜单页面有单人模式和双人模式 点击按钮可以进入游戏
用九个button按钮来布置棋盘
布局:
button1 button2 button3
button4 button5 button6
button7 button8 button9
启动窗口是form2(菜单窗口)
form2窗口代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics.Eventing.Reader;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 三子棋
{
public partial class Form2 : Form
{
public static bool play { get; set; } //控制玩家选择模式
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
play = true;
Form1 form1 = new Form1();
form1.Show();
//this.Hide();
//form1.FormClosed += (s, args) => Application.Exit(); // 当Form1关闭时退出应用程序
}
private void button2_Click(object sender, EventArgs e)
{
play = false;
Form1 form1 = new Form1();
form1.Show();
//this.Hide();
//form1.FormClosed += (s, args) => Application.Exit(); // 当Form1关闭时退出应用程序
}
}
}
游戏界面form1页面代码
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 三子棋
{
public partial class Form1 : Form
{
int click_count = 0; //记录点击次数
int[] bools = new int[9];
bool[] a = new bool[9];
int i1;
Random rand = new Random();
Button[] buttons;
public Form1()
{
InitializeComponent(); ;
buttons = new Button[]
{
button1, button2, button3,
button4, button5, button6,
button7, button8, button9
};
}
void UpQi(Button button, int i)
{
if (!a[i])
{
//双人
if (Form2.play)
{
if (click_count % 2 == 0)
{
bools[i] = 1;
click_count++;
button.BackColor = Color.Red;
label2.ForeColor = Color.Black;
label2.Text = "下一个: 黑棋";
}
else
{
bools[i] = 2;
click_count++;
button.BackColor = Color.Black;
label2.ForeColor = Color.Red;
label2.Text = "下一个: 红棋";
}
a[i] = true;
panduan();
}
//单人模式 玩家下载一个后系统随机下一个
else
{
i1 = i;
bools[i] = 1;
click_count++;
button.BackColor = Color.Red; //先下一个红棋
a[i] = true;
panduan();
//随机下
int a1;
do
{
a1 = rand.Next(0, 9);
if (!a[a1])
{
bools[a1] = 2;
click_count++;
buttons[a1].BackColor = Color.Black;
a[a1] = true;
panduan();
break;
}
if(click_count > 8)
{
break;
}
} while (a[a1]);
}
}
else
{
MessageBox.Show("当前位置已有棋子");
}
}
//判断胜负
void panduan()
{
if ((bools[0] == 1 && bools[1] == 1 && bools[2] == 1) ||
(bools[0] == 1 && bools[3] == 1 && bools[6] == 1) ||
(bools[0] == 1 && bools[4] == 1 && bools[8] == 1) ||
(bools[1] == 1 && bools[4] == 1 && bools[7] == 1) ||
(bools[2] == 1 && bools[5] == 1 && bools[8] == 1) ||
(bools[3] == 1 && bools[4] == 1 && bools[5] == 1) ||
(bools[6] == 1 && bools[7] == 1 && bools[8] == 1))
{
MessageBox.Show("红棋胜利");
cz();
}
if ((bools[0] == 2 && bools[1] == 2 && bools[2] == 2) ||
(bools[0] == 2 && bools[3] == 2 && bools[6] == 2) ||
(bools[0] == 2 && bools[4] == 2 && bools[8] == 2) ||
(bools[1] == 2 && bools[4] == 2 && bools[7] == 2) ||
(bools[2] == 2 && bools[5] == 2 && bools[8] == 2) ||
(bools[3] == 2 && bools[4] == 2 && bools[5] == 2) ||
(bools[6] == 2 && bools[7] == 2 && bools[8] == 2))
{
MessageBox.Show("黑棋胜利");
cz();
}
if (click_count == 9)
{
MessageBox.Show("平局");
cz();
}
}
//重置棋盘
void cz()
{
click_count = 0;
label2.ForeColor = Color.Red;
label2.Text = "下一个: 红棋";
//清空棋盘
button1.BackColor = button2.BackColor = button3.BackColor = button4.BackColor =
button5.BackColor = button6.BackColor = button7.BackColor = button7.BackColor =
button8.BackColor = button9.BackColor = Color.White;
//置零bools
for (int i = 0; i < bools.Length; i++)
{
bools[i] = 0;
a[i] = false;
}
}
private void button1_Click(object sender, EventArgs e)
{
UpQi(button1, 0);
}
private void button2_Click(object sender, EventArgs e)
{
UpQi(button2, 1);
}
private void button3_Click(object sender, EventArgs e)
{
UpQi(button3, 2);
}
private void button4_Click(object sender, EventArgs e)
{
UpQi(button4, 3);
}
private void button5_Click(object sender, EventArgs e)
{
UpQi(button5, 4);
}
private void button6_Click(object sender, EventArgs e)
{
UpQi(button6, 5);
}
private void button7_Click(object sender, EventArgs e)
{
UpQi(button7, 6);
}
private void button8_Click(object sender, EventArgs e)
{
UpQi(button8, 7);
}
private void button9_Click(object sender, EventArgs e)
{
UpQi(button9, 8);
}
private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = DateTime.Now.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
buttons[0] = button1;
buttons[1] = button2;
buttons[2] = button3;
buttons[3] = button4;
buttons[4] = button5;
buttons[5] = button6;
buttons[6] = button7;
buttons[7] = button8;
buttons[8] = button9;
}
}
}
把上面代码的按钮事件绑定到窗口属性上面的button按钮属性