C#实现简单的五子棋游戏
程序员文章站
2022-06-24 18:29:26
最近利用业余时间写了一个简单的五子棋游戏,没有利用深层次的面向对象技术,自学一年,代码和程序设计有不妥之处,还望大神指出,先看下实现的功能,三个button按钮,黑棋和白棋选择先出,和重置。其他的不多...
最近利用业余时间写了一个简单的五子棋游戏,没有利用深层次的面向对象技术,自学一年,代码和程序设计有不妥之处,还望大神指出,先看下实现的功能,三个button按钮,黑棋和白棋选择先出,和重置。
其他的不多说了,直接上全部代码(通过测试)。计算输赢的时候,左斜和右斜用了数学y=kx+b的线性函数计算。
private image myimage; /// <summary> /// 初始化背景数组 /// int[x,y] x为行 y为列 /// </summary> private int[,] bgground = new int[11, 11]; /*{ {0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0} };*/ private int currentx;//当前bgground的x行 private int currenty;//当前bgground的y列 private bool iswhite = false;//判断白棋还是黑棋先 private bool isover = false;//记录游戏是否结束 private void form1_load(object sender, eventargs e) { myimage = new bitmap(panel1.width, panel1.height); } protected override void onpaint(painteventargs e) { draw(); base.onpaint(e); } /// <summary> /// 画棋盘 /// </summary> private void draw() { graphics g = graphics.fromimage(myimage); g.clear(this.backcolor); g.fillrectangle(brushes.white,new rectangle(new point(10,10),new size(400,400))); //循环次数应比背景bgground行、列少1 for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { g.drawrectangle(new pen(brushes.black), i * 40 + 10, j * 40 + 10, 40, 40); } } graphics gg = panel1.creategraphics(); gg.drawimage(myimage, 0, 0); } private void panel1_mouseclick(object sender, mouseeventargs e) { if (isover) { return; } graphics g = panel1.creategraphics(); //设置当鼠标点击坐标在某一落棋点坐标想x,y的+-10的范围内即可落子 int x = (e.x - 10) % 40; int y = (e.y - 10) % 40; if (x > 30) { x = (e.x - 10) / 40 + 1; } else { x = (e.x - 10) / 40; } if (y > 30) { y = (e.y - 10) / 40 + 1; } else { y = (e.y - 10) / 40; } if (bgground[x, y] == 0) { if (iswhite) { drawchess(g, x, y, new pen(brushes.white), brushes.white, 1); iswhite = false; judgeresult(1); } else { drawchess(g, x, y, new pen(brushes.black), brushes.black, 2); iswhite = true; judgeresult(2); } } } /// <summary> /// 判断输赢 /// </summary> /// <param name="flag">1为白棋 2为黑棋</param> private void judgeresult(int flag) { int x = currentx; int y = currenty; int minxnum = 0; int maxxnum = 0; int count = 0; if (x > 4) { minxnum = x - 4; if (x + 4 > 10) { maxxnum = 10; } else { maxxnum = x + 4; } } else { maxxnum = x + 4; } int minynum = 0; int maxynum = 0; if (y > 4) { minynum = y - 4; if (y + 4 > 10) { maxynum = 10; } else { maxynum = y + 4; } } else { maxynum = y + 4; } #region //横向 for (int i = minxnum; i < maxxnum+1; i++) { if (bgground[i, y] == flag) { count++; if (count > 4) goto label; } else { count = 0; if (i > maxxnum - 4) break; } } #endregion #region //竖向 for (int i = minynum; i < maxynum+1; i++) { if (bgground[x, i] == flag) count++; else { count = 0; if (i > maxynum - 4) break; } if (count > 4) goto label; } #endregion //左斜 for (int i = minxnum; i < maxxnum+1; i++) { if (currentx + currenty - i < 0) break; if (currentx + currenty - i <= 10) { if (bgground[i, currentx + currenty - i] == flag) { count++; } else { count = 0; if (i > maxynum - 4) break; } } if (count > 4) goto label; } //右斜 for (int i = minxnum; i < maxxnum+1; i++) { if (i < currentx - currenty) break; if (i + currenty - currentx > 10) break; if (bgground[i, i + currenty - currentx] == flag) { count++; } else { count = 0; if (i > maxynum - 4) break; } if (count > 4) goto label; } label: if (flag == 1 && count > 4) { isover = true; messagebox.show("白棋赢,游戏结束"); return; } else if (flag == 2 && count > 4) { isover = true; messagebox.show("黑棋赢,游戏结束"); return; } else { isover = false; } } /// <summary> /// 画棋子 /// </summary> /// <param name="g"></param> /// <param name="x">bgground中x位置</param> /// <param name="y">bgground中y位置</param> /// <param name="p">画笔</param> /// <param name="brush">棋子颜色</param> /// <param name="flag">1为白棋 2为黑棋</param> private void drawchess(graphics g, int x, int y, pen p, brush brush, int flag) { currentx = x; currenty = y; bgground[x, y] = flag; g.drawellipse(p, x * 40, y * 40, 20, 20); g.fillellipse(brush, x * 40, y * 40, 20, 20); } ///btn_chess_click是黑棋先和白棋先按钮的共同事件,设置白棋先button的tag值为1,黑棋先button的tag值为2 /// <summary> /// 判断哪个先下 设置button控件的tag值 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btn_chess_click(object sender, eventargs e) { button btn = sender as button; string tag = btn.tag.tostring(); if (tag.equals("1"))//白棋先 { iswhite = true; } else//黑棋先 tag=2 { iswhite = false; } } /// <summary> /// 重置 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btn_reset_click(object sender, eventargs e) { isover = false; draw(); bgground = new int[11, 11]; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 罗贯中的故事:罗贯中机智讨回亏损银两
下一篇: ExtJs的Date格式字符代码