Ejemplo n.º 1
0
        private void Joiner_Listen_Thread()
        {
            //groupBox1.Visible = false;

            textBox1.AppendText("\r\n监听线程创建完毕..");
            int tmp = 0;
            int nowhas = 0;
            Point loc = new Point();
            CardComparer cardcompare = new CardComparer();

            try
            {
                while (true)
                {

                    recvbuf = udper_asjoiner.Receive(ref Ipend);

                    if (recvbuf[0] == 'A')
                    {
                        connected = true;                   //更改连接标志
                        label_game_status.Text = "已经连接";
                        button1.Visible = groupBox1.Visible = false;
                        button_chupai.Enabled = button_cancel.Enabled = false;

                        textBox1.AppendText("\r\nConnected, 等待发牌~");
                    }

                    else if (recvbuf[0] == 'Z')
                    {
                        textBox1.AppendText("\r\n****对方已经开始游戏****");
                        label_game_status.Text = "连接断开";
                    }

                    else if (recvbuf[0] == 'B')
                    {
                        textBox1.AppendText("\r\nGame Starting...");
                        cards.Clear();      //清空本地存牌
                        hiscards.Clear();
                        nowhas = (int)recvbuf[1];

                        loc.X = location.X;
                        loc.Y = location.Y;

                        for (int i = 0; i < nowhas; i++)
                        {
                            cards.Add(new Card());
                            ((Card)cards[i]).Init((int)recvbuf[i + 2], loc);
                            loc.X += 15;

                            hiscards.Add(nowhas);       //滥竽充数一下
                        }
                        textBox1.AppendText("\r\n获得" + nowhas.ToString() + "张牌\r\n等待主机出牌ing");
                        label_game_status.Text = "等待出牌";

                        ReDrawCards(ref cards, ref location);
                        ReDrawHisCards(hiscards.Count);
                        playing = true;

                    }
                    else if (recvbuf[0] == 'C')
                    {
                        label_game_status.Text = "游戏中(勿扰谢谢配合)";
                        cardrecv.Clear();
                        tmp = (int)recvbuf[1];

                        decleard_ = true;
                        last__send = false;

                        loc.X = location_playzone.X;
                        loc.Y = location_playzone.Y;

                        for (int i = 0; i < tmp; i++)
                        {
                            cardrecv.Add(new Card());
                            ((Card)cardrecv[i]).Init((int)recvbuf[i + 2], loc);
                            loc.X += 15;

                            hiscards.RemoveAt(0);       //继续滥竽充数...
                        }

                        if (hiscards.Count == 0)
                        {
                            MessageBox.Show("Sorry but U have Lost...~");
                            this.Close();
                        }

                        button_chupai.Enabled = true;

                        ReDrawPlayZone(ref cardrecv);

                        stack.Push(new CardsBak());
                        ((CardsBak)(stack.Peek())).BackUpRecv(ref cardrecv);

                         if(tmp != 0)
                        {
                            ReDrawHisCards(hiscards.Count);
                        }
                    }

                    else if (recvbuf[0] == 'I')
                    {
                        recvbuf[0] = recvbuf[1] = (byte)' ';
                        textBox1.AppendText("\r\nTa:" + System.Text.Encoding.Default.GetString(recvbuf));
                    }

                    else if (recvbuf[0] == 'D')
                    {
                        sendbuf[0] = (byte)'E';
                        if (MessageBox.Show("对方要求悔牌,是否同意?", "?", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            sendbuf[1] = (byte)1;
                            cardrecv.Clear();
                            tmp = ((CardsBak)stack.Peek()).recvbak.Count;
                            for (int i = 0; i < tmp; i++)
                            {
                                hiscards.Add(i);
                            }
                            if (tmp != 0)
                            {
                                ReDrawHisCards(hiscards.Count);
                            }
                            stack.Pop();

                            if (stack.Count != 0)
                            {
                                tmp = ((CardsBak)stack.Peek()).recvbak.Count;
                                for (int i = 0; i < tmp; i++)
                                    cardrecv.Add(((CardsBak)stack.Peek()).recvbak[i]);

                                ReDrawPlayZone(ref cardrecv);
                                ReDrawCards(ref cardsend, ref location_playzone);
                            }
                            else
                                this.CreateGraphics().DrawImage(playzone, location_playzone);
                            decleard_ = false;
                            button_chupai.Enabled = button_chupai.Enabled = false;
                        }
                        else
                            sendbuf[1] = (byte)0;
                        udper_asjoiner.Send(sendbuf, 2);

                    }

                    else if (recvbuf[0] == 'E')
                    {
                        if (recvbuf[1] == 1)
                        {
                            cardrecv.Clear();
                            tmp = ((CardsBak)stack.Peek()).recvbak.Count;
                            for (int i = 0; i < tmp; i++)
                            {
                                cardrecv.Add(((CardsBak)stack.Peek()).recvbak[i]);
                            }

                            tmp = ((CardsBak)stack.Peek()).sendbak.Count;
                            for (int i = 0; i < tmp; i++)
                                cards.Add(((CardsBak)stack.Peek()).sendbak[i]);
                            cards.Sort(cardcompare);

                            ReDrawPlayZone(ref cardrecv);
                            ReDrawCards(ref cards, ref location);

                            decleard_ = true;
                            button_chupai.Enabled = true;
                            button_cancel.Enabled = true;

                        }
                        else
                            MessageBox.Show("对方不允许此次悔牌");
                    }

                }

            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.ToString());

            }
        }
Ejemplo n.º 2
0
        private void ClickCard(int index)
        {
            Graphics g = this.CreateGraphics();
            Size recsize = new Size((index == (cards.Count - 1) ? 71 : 15), 96);
            Rectangle rec = new Rectangle(((Card)cards[index]).location, recsize);

            if (((Card)cards[index]).selected)
            {//非选中->选中

                SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(128, 0, 0, 255));

                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.GammaCorrected;  //覆盖绘图时进行灰度修正
                g.FillRectangle(semiTransBrush, rec);

                cardsend.Add(cards[index]);
                CardComparer cardcompare = new CardComparer();  //声明一个Icomparer接口
                cardsend.Sort(cardcompare);              //排序

            }
            else
            {//选中->非选中
                g.DrawImageUnscaledAndClipped(((Card)cards[index]).cardimg, rec);
                cardsend.Remove(cards[index]);
            }

            int x = 50 + 35;
            int y = 300 + 13;

            g.DrawImage(yulan, 84, 313, 290, 54);
            for (int i = 0; i < cardsend.Count; i++)
            {
                g.DrawImage(((Card)cardsend[i]).cardimg, x, y, 40, 53);
                x += 10;
            }

            int mini = 0;

            if (decleard_)
                button_cancel.Enabled = true;
            else
                button_cancel.Enabled = button_chupai.Enabled = false;

            if (cardsend.Count == 0)
            {
                label_cardselected.Text = "未选牌";
                button_chupai.Enabled = false;
            }

            else if (!playrule.legal(ref cardsend, ref mini))
            {
                label_cardselected.Text = "无此牌型";
                button_chupai.Enabled = false;
            }

            else
            {
                if (playrule.legal(ref cardsend, ref cardrecv))
                {
                    label_cardselected.Text = playrule.cardstype(ref cardsend, ref mini).ToString();
                    if (first && ImHost)
                    {
                        button_chupai.Enabled = true;
                    }

                    if (decleard_)
                        button_chupai.Enabled = true;
                }

                else
                {
                    label_cardselected.Text = "对方大过你哦~";
                    button_chupai.Enabled = false;
                }
            }
        }
Ejemplo n.º 3
0
        private void Host_Listen_Thread()
        {
            long counter = 0;
            int tmp = 0;
            Point loc = new Point();
            button_chupai.Enabled = true;
            CardComparer cardcompare = new CardComparer();

            textBox1.AppendText("线程启动\r\n等待玩家加入游戏ing...");

            button1.Enabled = false;
            button_chupai.Enabled = false;
            button_cancel.Enabled = false;
            IPEndPoint ipend_bak = new IPEndPoint(Ipend.Address, Ipend.Port);

            try
            {
                while (true)
                {

                    recvbuf = udper_ashost.Receive(ref Ipend);

                    if (counter == 0)
                    {
                        ipend_bak = new IPEndPoint(Ipend.Address, Ipend.Port);
                    }
                    else
                    {
                        if (!ipend_bak.Address.Equals(Ipend.Address))
                        {
                            textBox1.AppendText("\r\n另一玩家[" + Ipend.Address.ToString() + "]试图连接");
                            Ipend.Address = ipend_bak.Address;
                            Ipend.Port = ipend_bak.Port;

                            sendbuf[0] = (byte)'Z';
                            udper_ashost.Send(sendbuf, 1, Ipend);
                            counter++;
                            continue;
                        }
                    }

                    if (recvbuf[0] == 'A')
                    {
                        textBox1.AppendText("\r\n玩家[" + Ipend.Address.ToString() + "] 正在建立连接..");
                        connected = true;       //已经连接到游戏
                        label_game_status.Text = "\r\n 等候发牌";
                        sendbuf[0] = (byte)'A';
                        udper_ashost.Send(sendbuf, 1, Ipend);
                        textBox1.AppendText("\r\n1玩家加入游戏,等待发牌..");
                        label_game_status.Text = "已连接";
                        button1.Enabled = true;

                    }

                    else if (recvbuf[0] == 'C')
                    {

                        cardrecv.Clear();
                        decleard_ = true;
                        last__send = false;

                        tmp = (byte)recvbuf[1];

                        loc.X = location_playzone.X;
                        loc.Y = location_playzone.Y;
                        for (int i = 0; i < tmp; i++)
                        {
                            cardrecv.Add(new Card());
                            ((Card)cardrecv[i]).Init((int)recvbuf[i + 2], loc);
                            loc.X += 15;

                            hiscards.RemoveAt(0);
                        }
                        if (hiscards.Count == 0)
                        {
                            MessageBox.Show("Sorry but U have Lost...~");
                            this.Close();
                        }

                        ReDrawPlayZone(ref cardrecv);
                        ((CardsBak)stack.Peek()).BackUpRecv(ref cardrecv);

                        if (tmp != 0)
                        {
                            ReDrawHisCards(hiscards.Count);
                        }
                    }

                    else if (recvbuf[0] == 'I')
                    {
                        recvbuf[0] = recvbuf[1] = (byte)' ';
                        textBox1.AppendText("\r\nTa:" + System.Text.Encoding.Default.GetString(recvbuf));
                    }

                    else if (recvbuf[0] == 'D')
                    {
                        sendbuf[0] = (byte)'E';
                        if (MessageBox.Show("对方要求悔牌,是否同意?", "?", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            sendbuf[1] = (byte)1;

                            cardrecv.Clear();
                            tmp = ((CardsBak)stack.Peek()).recvbak.Count;
                            for (int i = 0; i < tmp; i++)
                            {
                                hiscards.Add(i);
                            }

                            if (tmp != 0)
                            {
                                ReDrawHisCards(hiscards.Count);
                            }

                            tmp = ((CardsBak)stack.Peek()).sendbak.Count;
                            for (int i = 0; i < tmp; i++)
                                cardsend.Add(((CardsBak)stack.Peek()).sendbak[i]);

                            this.CreateGraphics().DrawImage(playzone, location_playzone);
                            ReDrawCards(ref cardsend, ref location_playzone);

                            cardsend.Clear();
                            decleard_ = false;
                            button_chupai.Enabled = button_cancel.Enabled = false;
                        }
                        else
                            sendbuf[1] = (byte)0;
                        udper_ashost.Send(sendbuf, 2, Ipend);

                    }

                    else if (recvbuf[0] == 'E')
                    {
                        if (recvbuf[1] == 1)
                        {
                            decleard_ = true;
                            cardrecv.Clear();

                            tmp = ((CardsBak)stack.Peek()).sendbak.Count;
                            for (int i = 0; i < tmp; i++)
                                cards.Add(((CardsBak)stack.Peek()).sendbak[i]);
                            cards.Sort(cardcompare);
                            stack.Pop();

                            if (stack.Count != 0)
                            {
                                tmp = ((CardsBak)stack.Peek()).recvbak.Count;
                                for (int i = 0; i < tmp; i++)
                                {
                                    cardrecv.Add(((CardsBak)stack.Peek()).recvbak[i]);
                                }

                                ReDrawPlayZone(ref cardrecv);
                            }
                            else
                                this.CreateGraphics().DrawImage(playzone, location_playzone);
                            ReDrawCards(ref cards, ref  location);
                            button_chupai.Enabled = true;
                            button_cancel.Enabled = true;

                        }
                        else
                            MessageBox.Show("对方不允许此次悔牌");
                    }

                    else
                        continue;

                    counter++;
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.ToString(), "遇到异常");
            }
        }
Ejemplo n.º 4
0
        //发牌
        private void button1_Click_1(object sender, EventArgs e)
        {
            //发牌

            int i;
            int nowhas = Nowhas;
            Point loc = new Point(location.X, location.Y);

            for (i = 9; i <= nowhas + 8; i++)
            {

                ((Card)cards[i - 9]).Init(i, loc);
                loc.X += 15;
            }

            int tmp;
            if (nowhas <= discard * 2)
                return;

            //剔除多余的牌
            for (i = 0; i < discard; i++)
            {

                Random d = new Random(unchecked((int)DateTime.Now.Ticks * i));//则 d.Next(52) 满足
                tmp = d.Next(nowhas - i);

                cards.RemoveAt(tmp);                               //从ArrayList中剔出
                nowhas--;
            }

            //发牌
            for (i = 0; i < nowhas / 2; i++)
            {
                Random d = new Random(unchecked((int)DateTime.Now.Ticks * i));
                tmp = d.Next(nowhas - i);

                hiscards.Add(cards[tmp]);
                cards.RemoveAt(tmp);
            }

            nowhas /= 2;

            ReDrawCards(ref cards, ref location);

            /*
            for (i = 0; i < tmp; i++)
                this.CreateGraphics().DrawImage(((Card)hiscards[i]).cardimg, ((Card)hiscards[i]).location);
            */

            button1.Hide();                 //隐藏按钮
            CardComparer cardcompare = new CardComparer();  //声明一个Icomparer接口
            hiscards.Sort(cardcompare);     //排序

            sendbuf[0] = (byte)'B';
            sendbuf[1] = (byte)hiscards.Count;

            for (i = 0; i < hiscards.Count; i++)
            {
                sendbuf[i + 2] = (byte)((Card)hiscards[i]).index;
            }

            playing = true;
            while (connected)
            {
                udper_ashost.Send(sendbuf, sendbuf.Length, Ipend);     //把牌的索引发给对方
                break;
            }
            if (decleard_)
                button_cancel.Enabled = button_chupai.Enabled = true;
            label_game_status.Text = "游戏中";

            /*
            ///////////////////////////////查看对方牌型[测试使用]
            loc.X = 50;
            loc.Y = 200;

            for (i = 0; i < hiscards.Count; i++)
            {
                ((Card)hiscards[i]).location.X = loc.X;
                ((Card)hiscards[i]).location.Y = loc.Y;
                loc.X += 15;
            }

            tmp = hiscards.Count;
            for (i = 0;  i < tmp; i++)
                this.CreateGraphics().DrawImage(((Card)hiscards[i]).cardimg, ((Card)hiscards[i]).location);
               //////////////////////////////
             */

            ReDrawHisCards(hiscards.Count);

            if (cardsend.Count == 0)
            {
                label_cardselected.Text = "请选牌";
                button_chupai.Enabled = false;
            }
        }