基本信息
源码名称:c# 井字棋 小游戏源码
源码大小:0.36M
文件格式:.rar
开发语言:C#
更新时间:2018-06-14
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 3 元×
微信扫码支付:3 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
井字棋游戏
井字棋游戏
public partial class Form1 : Form
{
private int[,] statusMatrix; //棋盘上的状态,0为无子,1为电脑下子,10为人下子
bool finished;
bool Computerfirst;
public Form1()
{
InitializeComponent();
statusMatrix = new int[3, 3];
initializeGame();
Computerfirst = false;
}
public void initializeGame()
{
resetMatrix();
finished = false;
initializeDisplay();
}
private void resetMatrix()
{
for (int i = 0; i < 3; i )
for (int j = 0; j < 3; j )
statusMatrix[i, j] = 0;
}
private void initializeDisplay() //初始化棋盘
{
Bitmap userchess = new Bitmap(38, 38); //绘制玩家棋子图例
Graphics g;
g = Graphics.FromImage(userchess);
Pen p1 = new Pen(Color.DarkRed, 4);
g.DrawLine(p1, new Point(1, 1), new Point(37, 37));
g.DrawLine(p1, new Point(1, 37), new Point(37, 1));
human_pic.Image = userchess;
Bitmap computerchess = new Bitmap(38, 38);//绘制电脑棋子图例
g = Graphics.FromImage(computerchess);
Pen p2 = new Pen(Color.DarkBlue, 4);
g.DrawEllipse(p2, 1, 1, 35, 35);
compu_pic.Image = computerchess;
Bitmap mainGround = new Bitmap(450, 450);
g = Graphics.FromImage(mainGround);
Pen p = new Pen(Color.Gray, 4);
for (int i = 0; i < 2; i )
{
g.DrawLine(p, new Point((i 1) * 150, 0), new Point((i 1) * 150, 450));
g.DrawLine(p, new Point( 0,(i 1) * 150), new Point(450,(i 1) * 150 ));
}
pictureBox1.Image = mainGround;
}
private void displayChess()
{
Bitmap mainGround = new Bitmap(450, 450);
Graphics g;
g = Graphics.FromImage(mainGround);
Pen p1 = new Pen(Color.DarkBlue, 4);
Pen p2=new Pen(Color.DarkRed,4);
Pen p = new Pen(Color.Gray, 4);
for (int i = 0; i < 2; i )
{
g.DrawLine(p, new Point((i 1) * 150, 0), new Point((i 1) * 150, 450));
g.DrawLine(p, new Point(0, (i 1) * 150), new Point(450, (i 1) * 150));
}
for (int i = 0; i < 3;i )
for (int j = 0; j < 3; j )
{
if (statusMatrix[i, j] == 1)
g.DrawEllipse(p1, i * 150 10, 450 - j * 150 - 120 - 10, 120, 120);
if (statusMatrix[i, j] ==10)
{
g.DrawLine(p2, new Point(i * 150 10, 450 - j * 150 - 10), new Point(i * 150 130, 450 - j * 150 - 130));
g.DrawLine(p2, new Point(i * 150 10, 450 - j * 150 - 130), new Point(i * 150 130, 450 - j * 150 - 10));
}
}
pictureBox1.Image = mainGround;
}
public void toNextStep()
{
THINK chess=new THINK();
statusMatrix =chess.getNextStep(statusMatrix);
int gameresult=chess.checkFinished();
switch (gameresult)
{
case 1:
finished = true;
label3.Text = "游戏结束,电脑赢了!";
break;
case 10:
finished = true;
label3.Text = "游戏结束,你赢了!";
break;
case 100:
finished = true;
label3.Text = "游戏结束,打成平手!";
break;
default:
finished = false;
label3.Text = "请下子^-^";
break;
}
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
if (!finished)
{
int positionX = e.X / 150;
int positionY = (450 - e.Y) / 150;
if (statusMatrix[positionX, positionY] == 0)
{
statusMatrix[positionX, positionY] = 10;
toNextStep();
displayChess();
}
}
}
private void button1_Click(object sender, EventArgs e)
{
initializeGame();
if (Computerfirst)
{
toNextStep();
displayChess();
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
Computerfirst = false;
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
Computerfirst = true;
}
}