您好,欢迎访问三七文档
有识有容有守有为第1页共16页2012-2013学年第二学期无锡商业职业技术学院信息工程学院期终课程设计班级软件122学号12477121姓名王蓓班级软件122学号12477143姓名苏宁2013年6月有识有容有守有为第2页共16页一、课程设计要求以设计报告的形式上交,设计报告包含以下内容:1、封面2、设计题目3、设计思路及分析4、程序清单5、上机调试过程及结果6、设计总结:包括每个人所做的工作,以及本次课程设计的心得体会7、答辩纪录9、成绩汇总:包括设计成绩、上机调试成绩、答辩成绩和设计报告成绩有识有容有守有为第3页共16页二、设计题目三、设计思路及分析(可附页)先创建一个窗体,用默认系统中的画画函数去设置画一个15*15的棋盘(用来保存每一个棋子),设置两个棋子,变量white和black,默认设置为白棋;第一步:1.重载重画函数protectedoverridevoidOnPaint(PaintEventArgse){System.Drawing.Graphicsg=this.CreateGraphics();for(intx=0;x15;x++){for(inty=0;y15;y++){DrawChess(newPoint(x,y),checkerBoard[y,x]);}}ReDrawNextPlayerMark();base.OnPaint(e);}2.重载鼠标按下事件protectedoverridevoidOnMouseDown(System.Windows.Forms.MouseEventArgse){}3.把鼠标坐标转换成棋盘坐标第二步:1.开始新的棋局,所有数据复位并重画棋盘2.重画棋盘:privatevoidDrawCheckerBoard(){this.BackColor=System.Drawing.SystemColors.Desktop;this.BackColor=System.Drawing.SystemColors.Control;}3.重画下一个选手的标志棋子privatevoidReDrawNextPlayerMark()有识有容有守有为第4页共16页{System.Drawing.Graphicsg=this.CreateGraphics();imageList1.Draw(g,2,309,30,30,Player);}4.在棋盘上画一个棋子privatevoidDrawChess(PointpCoordinates,intiPlayer){System.Drawing.Graphicsg=this.CreateGraphics();imageList1.Draw(g,15+pCoordinates.X*20,6+pCoordinates.Y*20,20,20,iPlayer);}5.在指定的棋盘坐标位置添加一个棋子6.判断是否超出边界if((p.X0||p.X14)||(p.Y0||p.Y14))return;7.判断该位置有无棋子,如果已经有了棋子则退出(这颗棋不能下)if(checkerBoard[p.Y,p.X]!=None)return;第三步:检查游戏结果只有先放成5个相连的棋子的一方或剩,所以只要以刚刚下过的那个棋子为中心,检查有没有5个相连的同颜色棋子就好了。privatevoidCheckGameResult(PointsourcePoint){intx,y;intLastPlayer=(Player==White)?Black:White;intn;1.检查行,列,左斜线,右斜线,是否满足游戏规则n=1;y=sourcePoint.Y;for(x=sourcePoint.X-1;x=0;x--){if(checkerBoard[y,x]==LastPlayer)n++;elsebreak;}for(x=sourcePoint.X+1;x15;x++){if(checkerBoard[y,x]==LastPlayer)n++;elsebreak;}if(n=5){Win(LastPlayer);return;}}2.悔棋有识有容有守有为第5页共16页privatevoidPenitence(){if(History.Count0){Pointp=(Point)History.Pop();checkerBoard[p.Y,p.X]=None;nextPlayer=(Player==White)?Black:White;DrawCheckerBoard();}}四、程序清单(可附页)usingSystem;usingSystem.Collections;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespaceFiveChessPC{publicpartialclassForm1:Form{privateconstintNone=2;//没有棋子privateconstintWhite=0;//代表白棋privateconstintBlack=1;//代表黑棋privateint[,]checkerBoard=newint[15,15];//棋盘(用来保存每一个棋子)privateintnextPlayer;//下一个选手privateintPlayer{get{returnnextPlayer;}set{nextPlayer=value;ReDrawNextPlayerMark();}}privateStackHistory;//下棋的历史记录有识有容有守有为第6页共16页publicForm1(){InitializeComponent();History=newStack();Player=White;//默认设置为白棋先下Reset();}privatevoidexit_Click(objectsender,EventArgse){this.Close();}privatevoidForm1_Load(objectsender,System.EventArgse){}//重载重画函数protectedoverridevoidOnPaint(PaintEventArgse){System.Drawing.Graphicsg=this.CreateGraphics();for(intx=0;x15;x++){for(inty=0;y15;y++){DrawChess(newPoint(x,y),checkerBoard[y,x]);}}ReDrawNextPlayerMark();base.OnPaint(e);}//重载鼠标按下事件protectedoverridevoidOnMouseDown(System.Windows.Forms.MouseEventArgse){base.OnMouseDown(e);switch(e.Button){caseMouseButtons.Left:if((History.Count==0)&&(2e.X)&&(e.X22)&&(309e.Y)&&(e.Y329))Player=(Player==White)?Black:White;elseAddChess(MToA(newPoint(e.X,e.Y)));break;caseMouseButtons.Right:有识有容有守有为第7页共16页break;}}privatePointMToA(Pointp)//把鼠标坐标转换成棋盘坐标{returnnewPoint((p.X-15)/20,(p.Y-6)/20);}privatevoidReset(){for(inti=0;i15;i++){for(intj=0;j15;j++){checkerBoard[i,j]=None;}}History.Clear();//清空历史记录DrawCheckerBoard();//重画棋盘}privatevoidDrawCheckerBoard(){this.BackColor=System.Drawing.SystemColors.Desktop;this.BackColor=System.Drawing.SystemColors.Control;}privatevoidReDrawNextPlayerMark(){System.Drawing.Graphicsg=this.CreateGraphics();imageList1.Draw(g,2,309,30,30,Player);}privatevoidDrawChess(PointpCoordinates,intiPlayer){System.Drawing.Graphicsg=this.CreateGraphics();imageList1.Draw(g,15+pCoordinates.X*20,6+pCoordinates.Y*20,20,20,iPlayer);}privatevoidAddChess(Pointp){if((p.X0||p.X14)||(p.Y0||p.Y14))return;if(checkerBoard[p.Y,p.X]!=None)return;DrawChess(p,Player);if(Player==White)//该白棋下{checkerBoard[p.Y,p.X]=White;Player=Black;}有识有容有守有为第8页共16页else{checkerBoard[p.Y,p.X]=Black;Player=White;}History.Push(newPoint(p.X,p.Y));//添加历史记录CheckGameResult(p);}privatevoidCheckGameResult(PointsourcePoint){intx,y;intLastPlayer=(Player==White)?Black:White;intn;n=1;//检查行y=sourcePoint.Y;for(x=sourcePoint.X-1;x=0;x--){if(checkerBoard[y,x]==LastPlayer)n++;elsebreak;}for(x=sourcePoint.X+1;x15;x++){if(checkerBoard[y,x]==LastPlayer)n++;elsebreak;}if(n=5){Win(LastPlayer);return;}n=1;//检查列x=sourcePoint.X;for(y=sourcePoint.Y-1;y=0;y--){if(checkerBoard[y,x]==LastPlayer)n++;elsebreak;}for(y=sourcePoint.Y+1;y15;y++){if(checkerBoard[y,x]==LastPlayer)n++;elsebreak;}if(n=5)有识有容有守有为第9页共16页{Win(LastPlayer);return;}n=1;for(x=sourcePoint.X+1,y=sourcePoint.Y-1;x15&&y=0;x++,y--){if(checkerBoard[y,x]==LastPlayer)n++;elsebreak;}for(x=sourcePoint.X-1,y=sourcePoint.Y+1;x=0&&y15;x--,y++){if(checkerBoard[y,x]==LastPlayer)n++;elsebreak;}if(n=5){Win(LastPlayer);return;}n=1;//检查右斜线for(x=sourcePoint.X-1,y=sourcePoint.Y-1;x=0&
本文标题:五子棋游戏设计总结
链接地址:https://www.777doc.com/doc-5466436 .html