C#军旗设计报告文档格式.docx
- 文档编号:18595807
- 上传时间:2022-12-29
- 格式:DOCX
- 页数:26
- 大小:2.41MB
C#军旗设计报告文档格式.docx
《C#军旗设计报告文档格式.docx》由会员分享,可在线阅读,更多相关《C#军旗设计报告文档格式.docx(26页珍藏版)》请在冰豆网上搜索。
布阵,走棋模块
d:
判断是否符合走棋规则模块
3、详细设计
在本模块中是单独设计的一个独立隐藏窗体,直接调用,是利用System.Runtime.InteropServices;
实现的,下面给出具体的代码:
namespace军棋
{
///<
summary>
///PlaySound的摘要说明。
/summary>
publicclassPlaySound
{
privateconstintSND_SYNC=0x0;
privateconstintSND_ASYNC=0x1;
privateconstintSND_NODEFAULT=0x2;
privateconstintSND_LOOP=0x8;
privateconstintSND_NOSTOP=0x10;
publicstaticvoidPlay(stringfile)
{
intflags=SND_ASYNC|SND_NODEFAULT;
sndPlaySound(file,flags);
}
[DllImport("
winmm.dll"
)]
privateexternstaticintsndPlaySound(stringfile,intuFlags);
}
}
在这个模块中通过调用Sockets;
来实现网络通信,具体代码如下:
privateboolReadFlag=true;
//设定侦听标示位,通过它来设定是否侦听端口号
privateThreadth;
//定义一个线程,在线程接收信息
privateIPEndPointremote;
privateboolReset_flag=false;
//重新开始标记
privateboolth_flag=false;
//定义一个远程结点,用以获取远程计算机IP地址和发送的信息
privateUdpClientudpclient;
//创建一个UDP网络服务
//////////////////////////////////以上是创建端口
privatevoidread()
//侦听本地的端口号
udpclient=newUdpClient(Convert.ToInt32(txt_port.Text));
remote=null;
//设定编码类型
Encodingenc=Encoding.Unicode;
intx,y,old_x,old_y,idx,old_idx;
while(ReadFlag==true)
try
Byte[]data=udpclient.Receive(refremote);
//得到对方发送来的信息
StringstrData=enc.GetString(data);
string[]a=newstring[5];
a=strData.Split('
|'
);
switch(a[0])
case"
join"
:
//获取传送信息到本地端口号的远程计算机IP地址
stringremoteIP=remote.Address.ToString();
//显示对方计算机IP地址
toolStripStatusLabel1.Text=remoteIP+"
对方已经加入,你是红方请布棋阵"
;
//**************************
//允许己方布阵
button1.Visible=true;
button2.Visible=true;
button3.Visible=true;
player=PlayerColor.Red;
button4.Enabled=false;
//联机按钮
//允许对方布阵
//stringstr_send="
begin_layout|"
+player.ToString();
//send(str_send);
break;
begin_layout"
//允许对方布阵
if(player==PlayerColor.Green&
&
toolStripStatusLabel1.Text=="
程序处于等待联机状态!
"
)
toolStripStatusLabel1.Text="
你是绿方,可以开始布阵了"
layout"
//布阵信息
收到对方布阵信息"
get_message(a[1]);
//读取对方布阵棋子信息
if(player==PlayerColor.Red)
IsMyTurn=true;
//能走棋
对方已经布阵,你是红方请先走棋"
move"
//对方棋子移动信息
x=Convert.ToInt16(a[1]);
y=Convert.ToInt16(a[2]);
idx=Convert.ToInt16(a[3]);
old_x=Convert.ToInt16(a[4]);
old_y=Convert.ToInt16(a[5]);
old_idx=Convert.ToInt16(a[6]);
对方下棋子位置:
+x.ToString()+"
"
+y.ToString();
//移动对方棋子,修改Map
//判断是否有子被吃
if(Map[x,y]==101)//仅仅走棋,而不是吃子
MoveChess(old_idx,x,y);
Map[old_x,old_y]=101;
else//吃子
if(Map[x,y]!
=idx)//目标处棋子被吃掉
Qizi_Pic[Map[x,y]].Visible=false;
if(idx==101)//同时去掉
Map[x,y]=101;
Qizi_Pic[old_idx].Visible=false;
elseMoveChess(old_idx,x,y);
else//目标处棋子保留
//MoveChess(old_idx,x,y);
reverse();
over"
toolStripStatusLabel1.Text=a[1]+"
赢了此局"
if(a[1]=="
绿方"
)//将对方棋子全部去掉
for(intk=0;
k<
25;
k++)
Qizi_Pic[k].Visible=false;
else
for(intk=25;
50;
can_go=false;
button5.Enabled=true;
reset"
对方重新开局了"
Cls_Box();
//隐藏棋盘所有棋子
IsMyTurn=false;
catch
//退出循环,结束线程
privatevoidsend(stringinfo)
//创建UDP网络服务
UdpClientSendUdp=newUdpClient();
IPAddressremoteIP;
//判断IP地址的正确性
remoteIP=IPAddress.Parse(txt_IP.Text);
MessageBox.Show("
请输入正确的IP地址!
"
错误"
return;
IPEndPointremoteep=newIPEndPoint(remoteIP,Convert.ToInt32(txt_remoteport.Text));
Byte[]buffer=null;
stringstr=info;
buffer=enc.GetBytes(str.ToCharArray());
//传送信息到指定计算机的txt_remoteport端口号
SendUdp.Send(buffer,buffer.Length,remoteep);
//textBox1.Clear();
//关闭UDP网络服务
SendUdp.Close();
privatevoidradioButton1_CheckedChanged(objectsender,System.EventArgse)
txt_port.Text="
3333"
txt_remoteport.Text="
4444"
privatevoidradioButton2_CheckedChanged(objectsender,System.EventArgse)
privatevoidbutton4_Click(objectsender,System.EventArgse)//联机
send("
join|"
//创建一个线程
th=newThread(newThreadStart(read));
th_flag=true;
//启动线程
th.Start();
privatevoidCls_Box()
}/////////////////////////////////联机状态
c:
privatevoidForm1_Load(objectsender,System.EventArgse)
Pic_Width=qi_pan.Width;
//棋盘大小
r=(Pic_Width)/17;
Map=newint[18,18];
for(inti=1;
i<
18;
i++)
for(intj=1;
j<
j++)
Map[i,j]=101;
//101表示(i,j)处没放置棋子
qi_index();
//棋子编号并设置对应棋子含义
begin_pos
(2);
//加载不可见棋子
button1.Visible=false;
//保存布阵按钮
button2.Visible=false;
//读取布阵按钮
button3.Visible=false;
//开始对战按钮
button5.Enabled=false;
//重新开始按钮
privatevoidqi_index()
Q=newint[25];
Q[0]=29;
//军旗29
Q[1]=30;
Q[2]=30;
Q[3]=30;
//地雷30
Q[4]=31;
Q[5]=31;
//炸弹31
Q[6]=32;
Q[7]=32;
Q[8]=32;
//工兵32
Q[9]=33;
Q[10]=33;
Q[11]=33;
//排长33
Q[12]=34;
Q[13]=34;
Q[14]=34;
//连长34
Q[15]=35;
Q[16]=35;
//营长35
Q[17]=36;
Q[18]=36;
//团长36
Q[19]=37;
Q[20]=37;
//旅长37
Q[21]=38;
Q[22]=38;
//师长38
Q[23]=39;
//军长39
Q[24]=40;
//司令40
//添加处理棋子起始位置方法
privatevoidbegin_pos(intm)
stringfilename="
path=System.Windows.Forms.Application.StartupPath;
//bin路径
Qizi_Pic=newPictureBox[25*m];
inti;
intn=1;
for(i=0;
25*m;
i++)//添加25*m个棋子
Qizi_Pic[i]=newPictureBox();
this.Controls.Add(Qizi_Pic[i]);
Qizi_Pic[i].Width=22;
Qizi_Pic[i].Height=22;
Qizi_Pic[i].Name="
R"
+i.ToString();
Qizi_Pic[i].Tag=i.ToString();
Qizi_Pic[i].Parent=qi_pan;
if(i<
25)
filename=path+"
\\..\\..\\bmp\\"
+Q[i].ToString()+"
.bmp"
if(i>
=25&
50)
\\..\\..\\bmp\\G"
+Q[i%25].ToString()+"
//暗棋filename=path+"
\\..\\..\\bmp\\G.bmp"
Qizi_Pic[i].Image=Image.FromFile(filename);
Qizi_Pic[i].Click+=newSystem.EventHandler(bt_Click);
Qizi_Pic[i].MouseDown+=newSystem.Windows.Forms.MouseEventHandler(bt_MouseDown);
Qizi_Pic[i].MouseMove+=newSystem.Windows.Forms.MouseEventHandler(bt_MouseMove);
Qizi_Pic[i].MouseUp+=newSystem.Windows.Forms.MouseEventHandler(bt_MouseUp);
if(i%5==0)n++;
Qizi_Pic[i].Top=150+n*24;
Qizi_Pic[i].Left=10+23*(i%5);
Qizi_Pic[i].Visible=false;
privatevoidbt_Click(objectsender,System.EventArgse)//这里处理单击事件过程
intx1,y1;
PictureBoxpicBox1=(PictureBox)sender;
inti=Convert.ToInt16(picBox1.Tag);
//转换成棋盘坐标(x1,y1)
x1=(picBox1.Left-10+picBox1.Width/2)/r+1;
y1=(picBox1.Top-10+picBox1.Width/2)/r+1;
if(Layout_Flag==true)//是否布局
if(first==true){tempx=x1;
tempy=y1;
first=false;
else{old_x=tempx;
old_y=tempy;
first=true;
if(Layout_Juge(old_x,old_y,x1,y1))//是否可以改变布局
if(Map[x1,y1]==101)//没有棋子
MoveChess(Map[old_x,old_y],x1,y1);
else//对调棋子
{MoveChess(Map[old_x,old_y],x1,y1);
MoveChess(i,old_x,old_y);
//toolStripStatusLabel1.Text="
不能改变原有布局"
第一排不允许放置炸弹,放置地雷在后两排,军旗只能在大本营"
"
违反军旗布局规则"
privatevoidbt_MouseMove(objectsender,System.Windows.Forms.MouseEventArgse)
if(can_go!
=true)
{toolStripStatusLabel1.Text="
不能走棋,请等对方"
//将被击的赋给定义的picBox1变量
if(Q[i%25]==30||Q[i%25]==29)return;
//地雷不能动
if(IsBigHome(old_x,old_y))return;
//大本营中棋子不能动
if(!
IsMyTurn){toolStripStatusLabel1.Text="
该对方走棋,请等对方"
if(_isDragging)
picBox1.Left=picBox1.Left-mouse_x+e.X;
picBox1.Top=picBox1.Top-mouse_y+e.Y;
picBox1.BringToFront();
privatevoidbt_MouseUp(objectsender,System.Windows.Forms.MouseEventArgse)
_isDragging=false;
intx,y;
stringpath=System.Windows.Forms.Application.StartupPath;
path+="
\\..\\..\\wav\\"
intidx=Convert.ToInt16(picBox1.Tag);
x=(picBox1.Left-10+picBox1.Width/2)/r+1;
y=(picBox1.Top-10+picBox1.Width/2)/r+1;
(x<
=12&
x>
=7&
y>
y<
=17))
picBox1.Left=old_Left;
picBox1.Top=old_Top;
只能在南区布局"
违反军旗布局区域"
else//走棋状态
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C# 军旗 设计 报告