问题描述
今天做的中国象棋,编译通过,运行出错。。棋子基类,位置,移动方法。。。usingSystem;namespacezgxq{///<summary>///Descriptionofqizi.///</summary>publicclassqizi{publicboolb=true;publicintx;publicinty;publicboolgo(intx,inty){this.x=x;this.y=y;returntrue;}}}后面是车,炮,马usingSystem;namespacezgxq{///<summary>///Descriptionofzu.///</summary>publicclasszu:qizi{publiczu(intx,inty){this.x=x;this.y=y;}publicboolgo(){if(x>=0&&x<=8&&y>=0&&y<=9&&(x==this.x||y==this.y)){this.x=x;this.y=y;returntrue;}else{returnfalse;}}}}usingSystem;namespacezgxq{///<summary>///Descriptionofma.///</summary>publicclassma:qizi{publicma(intx,inty){this.x=x;this.y=y;}publicboolgo(){if(x>=0&&x<=8&&y>=0&&y<=9){boolb=false;if(x==this.x+1&&(y==this.y+2||y==this.y-2)){this.x=x;this.y=y;b=true;}if(x==this.x-1&&(y==this.y+2||y==this.y-2)){this.x=x;this.y=y;b=true;}if(x==this.x-2&&(y==this.y-1||y==this.y+1)){this.x=x;this.y=y;b=true;}if(x==this.x+2&&(y==this.y-1||y==this.y+1)){this.x=x;this.y=y;b=true;}returnb;}else{returnfalse;}}}}usingSystem;namespacezgxq{///<summary>///Descriptionofzui.///</summary>publicclasszui:qizi{publiczui(intx,inty){this.x=x;this.y=y;}publicboolgo(){if(x>=0&&x<=8&&y>=0&&y<=9){boolb=false;if(y>5){if((this.x==x-1&&y==this.y)||(this.x==x+1&&y==this.y)||(this.y+1==y&&x==this.x)){this.x=x;this.y=y;b=true;}}if(y<=5){if(this.y+1==y&&x==this.x){this.x=x;this.y=y;b=true;}}returnb;}else{returnfalse;}}}}usingSystem;namespacezgxq{///<summary>///Descriptionofjs.///</summary>publicclassjs:qizi{publicjs(intx,inty){this.x=x;this.y=y;}}}usingSystem;namespacezgxq{///<summary>///Descriptionofsi.///</summary>publicclasssi:qizi{publicsi(intx,inty){this.x=x;this.y=y;}}}usingSystem;namespacezgxq{///<summary>///Descriptionofxiang.///</summary>publicclassxiang:qizi{publicxiang(intx,inty){this.x=x;this.y=y;}}}主窗体。。。。usingSystem;usingSystem.Collections.Generic;usingSystem.Drawing;usingSystem.Windows.Forms;namespacezgxq{///<summary>///DescriptionofMainForm.///</summary>publicpartialclassMainForm:Form{[STAThread]publicstaticvoidMain(string[]args){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(newMainForm());}publicMainForm(){////TheInitializeComponent()callisrequiredforWindowsFormsdesignersupport.//InitializeComponent();////TODO:AddconstructorcodeaftertheInitializeComponent()call.//}staticqizi[,]l=newqizi[9,10];zuz1=newzu(0,0);zuz2=newzu(8,0);paop1=newpao(1,2);paop2=newpao(7,2);mam1=newma(1,0);mam2=newma(7,0);xiangx1=newxiang(2,0);xiangx2=newxiang(6,0);jsj=newjs(4,0);sis1=newsi(3,0);sis2=newsi(5,0);zuizui1=newzui(0,3);zuizui2=newzui(2,3);zuizui3=newzui(4,3);zuizui4=newzui(6,3);zuizui5=newzui(8,3);voidPictureBox1Paint(objectsender,PaintEventArgse){Imageimg=newBitmap(800,700);Graphicsg=Graphics.FromImage(img);Penpen=newPen(Color.Red);pictureBox1.Image=img;g.Clear(Color.White);for(inti=0;i<=9;i++){g.DrawLine(pen,200,100+50*i,600,100+50*i);}for(inti=0;i<=8;i++){g.DrawLine(pen,200+50*i,100,200+50*i,300);}for(inti=0;i<=8;i++){g.DrawLine(pen,200+50*i,350,200+50*i,550);}g.DrawLine(pen,200,300,200,350);g.DrawLine(pen,600,300,600,350);Fontf=newFont("Bold",20);g.DrawString("楚河",f,Brushes.Black,250,310);g.DrawString("汉界",f,Brushes.Black,500,310);g.DrawLine(pen,350,100,450,200);g.DrawLine(pen,450,100,350,200);g.DrawEllipse(pen,180+z1.x*50,80+z1.y*50,40,40);g.DrawString("车",f,Brushes.Black,180+z1.x*50,80+z1.y*50);l[z1.x,z1.y]=z1;g.DrawEllipse(pen,180+z2.x*50,80+z2.y*50,40,40);g.DrawString("车",f,Brushes.Black,180+z2.x*50,80+z2.y*50);l[z2.x,z2.y]=z2;g.DrawEllipse(pen,180+p1.x*50,80+p1.y*50,40,40);g.DrawString("炮",f,Brushes.Black,180+p1.x*50,80+p1.y*50);l[p1.x,p1.y]=p1;g.DrawEllipse(pen,180+p2.x*50,80+p2.y*50,40,40);g.DrawString("炮",f,Brushes.Black,180+p2.x*50,80+p2.y*50);l[p2.x,p2.y]=p2;g.DrawEllipse(pen,180+s1.x*50,80+s1.y*50,40,40);g.DrawString("士",f,Brushes.Black,180+s1.x*50,80+s1.y*50);l[s1.x,s1.y]=s1;g.DrawEllipse(pen,180+s2.x*50,80+s1.y*50,40,40);g.DrawString("士",f,Brushes.Black,180+s2.x*50,80+s2.y*50);l[s2.x,s2.y]=s2;g.DrawEllipse(pen,180+m1.x*50,80+m1.y*50,40,40);g.DrawString("马",f,Brushes.Black,180+m1.x*50,80+m1.y*50);l[m1.x,m1.y]=m1;g.DrawEllipse(pen,180+m2.x*50,80+m2.y*50,40,40);g.DrawString("马",f,Brushes.Black,180+m2.x*50,80+m2.y*50);l[m2.x,m2.y]=m2;g.DrawEllipse(pen,180+j.x*50,80+j.y*50,40,40);g.DrawString("将",f,Brushes.Black,180+j.x*50,80+j.y*50);l[j.x,j.y]=j;g.DrawEllipse(pen,180+zui2.x*50,80+zui2.y*50,40,40);g.DrawString("卒",f,Brushes.Black,180+zui2.x*50,80+zui2.y*50);l[zui1.x,zui1.y]=zui2;g.DrawEllipse(pen,180+x1.x*50,80+x1.y*50,40,40);g.DrawString("象",f,Brushes.Black,180+x1.x*50,80+x1.y*50);l[x1.x,x1.y]=x1;g.DrawEllipse(pen,180+x2.x*50,80+x2.y*50,40,40);g.DrawString("象",f,Brushes.Black,180+x2.x*50,80+x2.y*50);l[x2.x,x2.y]=x2;g.DrawEllipse(pen,180+zui1.x*50,80+zui1.y*50,40,40);g.DrawString("卒",f,Brushes.Black,180+zui1.x*50,80+zui1.y*50);l[zui1.x,zui1.y]=zui1;g.DrawEllipse(pen,180+zui3.x*50,80+zui3.y*50,40,40);g.DrawString("卒",f,Brushes.Black,180+zui3.x*50,80+zui3.y*50);l[zui3.x,zui3.y]=zui3;g.DrawEllipse(pen,180+zui4.x*50,80+zui4.y*50,40,40);g.DrawString("卒",f,Brushes.Black,180+zui4.x*50,80+zui4.y*50);l[zui4.x,zui4.y]=zui4;g.DrawEllipse(pen,180+zui5.x*50,80+zui5.y*50,40,40);g.DrawString("卒",f,Brushes.Black,180+zui5.x*50,80+zui5.y*50);l[zui5.x,zui5.y]=zui5;}voidMainFormPaint(objectsender,PaintEventArgse){}staticintx;staticinty;voidPictureBox1MouseClick(objectsender,MouseEventArgse){x=e.X;y=e.Y;x=(x-200)/50;y=(y-100)/50;label1.Text=x.ToString();//+y.ToString();label1.Text+=y.ToString();if(l[x,y].b){l[x,y].b=false;}else{boolt=l[x,y].go(x,y);if(!t){label1.Text="错误";}l[x,y].b=true;}}}}
解决方案
本帖最后由 yf1323570554 于 2011-12-04 20:14:25 编辑
解决方案二:
看的头晕啊!说说思路吧
解决方案三:
if(l[x,y].b){l[x,y].b=false;}else{boolt=l[x,y].go(x,y);if(!t){label1.Text="错误";}l[x,y].b=true;}就这一段出错,说未将引用应用到实例。思路,棋子基类派生车,马。。。定义对象数组,【9,10】棋盘大小staticqizi[,]l=newqizi[9,10];然后画图,摆棋,把棋子的值赋给对象数组在picturebox里的点击事件里得想,x,y即棋子对象未选中,就选择,选中的就走if(l[x,y].b){l[x,y].b=false;}else{boolt=l[x,y].go(x,y);}