找传奇、传世资源到传世资源站!

c# 井字棋 小游戏源码

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

井字棋游戏

from clipboard

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

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复