//重新开始游戏
 public void Restart(string str)
 {
     MessageBox.Show(str, "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
     service.AddItemToListBox(str);
     initGrid();
     order = false;
     SetButton(buttonStart, true);
 }
Exemple #2
0
        //处理接收的数据
        private void ReceiveData()
        {
            bool exitWhile = false;

            while (exitWhile == false)
            {
                string receiveString = null;
                try
                {
                    receiveString = sr.ReadLine();
                }
                catch
                {
                    service.AddItemToListBox("接收数据失败");
                }
                if (receiveString == null)
                {
                    if (normalExit == false)
                    {
                        MessageBox.Show("与服务器失去联系,游戏无法继续!");
                    }
                    if (side != 1)
                    {
                        ExitFormPlaying();
                    }
                    side       = -1;
                    normalExit = true;
                    break;
                }
                service.AddItemToListBox("收到:" + receiveString);
                string[] splitString = receiveString.Split(',');
                string   command     = splitString[0].ToLower();
                switch (command)
                {
                //大厅已满
                case "sorry":
                    MessageBox.Show("连接成功,但大厅已满");
                    exitWhile = true;
                    break;

                //游戏桌情况
                //格式:Tables,各桌是否有人的字符串
                //1表示有人,0表示没人
                case "tables":
                    string s = splitString[1];
                    //若maxPlayingTables为0,说明未创建checkBoxGameTables
                    if (maxPlayingTables == 0)
                    {
                        //计算桌数
                        maxPlayingTables   = s.Length / 2;
                        checkBoxGameTables = new CheckBox[maxPlayingTables, 2];
                        isReceiveCommand   = true;
                        //将CheckBox对象添加到数组
                        for (int i = 0; i < maxPlayingTables; i++)
                        {
                            AddCheckBoxToPanel(s, i);
                        }
                        isReceiveCommand = false;
                    }
                    else
                    {
                        isReceiveCommand = true;
                        for (int i = 0; i < maxPlayingTables; i++)
                        {
                            for (int j = 0; j < 2; j++)
                            {
                                if (s[2 * i + j] == '0')
                                {
                                    UpdateCheckBox(checkBoxGameTables[i, j], false);
                                }
                                else
                                {
                                    UpdateCheckBox(checkBoxGameTables[i, j], true);
                                }
                            }
                            isReceiveCommand = false;
                        }
                    }
                    break;

                //入座,格式:SitDown,座位号,用户名
                case "sitdown":
                    formPlaying.SetTableSideText(splitString[1], splitString[2],
                                                 string.Format("{0}进入", splitString[2]));
                    break;

                //离座
                case "getup":
                    if (side == int.Parse(splitString[1]))
                    {
                        side = -1;
                    }
                    else
                    {
                        formPlaying.SetTableSideText(splitString[1], "",
                                                     string.Format("{0}退出", splitString[2]));
                        formPlaying.Restart("敌人逃跑,我方胜利");
                    }
                    break;

                //对方与服务器断开连接
                case "lost":
                    formPlaying.SetTableSideText(splitString[1], "",
                                                 string.Format("[{0}]与服务器失去联系", splitString[2]));
                    formPlaying.Restart("对家与服务器失去联系,游戏无法继续");
                    break;

                //聊天
                //格式:Talk,说话者,内容
                case "talk":
                    if (formPlaying != null)
                    {
                        formPlaying.ShowTalk(splitString[1],
                                             receiveString.Substring(splitString[0].Length +
                                                                     splitString[1].Length + 2));
                    }
                    break;

                //服务器发送的信息
                //格式:Message,信息
                case "message":
                    formPlaying.ShowMessage(splitString[1]);
                    break;

                //棋子变动信息,格式:ChessInfo, side, 棋子编号,原始x,原始y,目的x,目的y
                case "chessinfo":
                    int tside, cno, oriX, oriY, endX, endY;
                    tside = int.Parse(splitString[1]);
                    cno   = int.Parse(splitString[2]);
                    oriX  = int.Parse(splitString[3]);
                    oriY  = int.Parse(splitString[4]);
                    endX  = int.Parse(splitString[5]);
                    endY  = int.Parse(splitString[6]);
                    formPlaying.ChangeChess(-1, oriX, oriY);
                    formPlaying.ChangeChess(cno, endX, endY);
                    formPlaying.RePaint();
                    formPlaying.drawFrame("blue", endX, endY);
                    formPlaying.ChangeOrder(tside);
                    formPlaying.CheckWin();
                    break;

                //胜利,格式:Win,side
                case "win":
                    int winner = int.Parse(splitString[1]);
                    if (winner == side)
                    {
                        formPlaying.Restart("我方胜利!!!");
                    }
                    else
                    {
                        formPlaying.Restart("我方失败。。。");
                    }

                    break;

                //双方都准备好了
                case "allready":
                    formPlaying.ShowMessage("双方都已准备,游戏开始!");
                    formPlaying.Ready(side);
                    break;
                }
            }
            Application.Exit();
        }