예제 #1
0
        private void ExitBtn_Click(object sender, EventArgs e)
        {
            SyncInForm MF = this.Owner as SyncInForm;

            MF.Show();
            this.Close();
        }
예제 #2
0
        private void ExitBtn_Click(object sender, EventArgs e)
        {
            if (connected)
            {
                byte[] message = new byte[1];
                message[0] = C_USER_DISCONNECTED;
                stream.Write(message, 0, 1);
                exitCheck = false;
                connected = false;
            }
            SyncInForm MF = this.Owner as SyncInForm;

            MF.Close();
            this.Close();
        }
예제 #3
0
 private void GameForm_FormClosed(object sender, FormClosedEventArgs e)
 {
     if (connected)
     {
         if (exitCheck == true)
         {
             byte[] message = new byte[1];
             message[0] = C_USER_DISCONNECTED;
             stream.Write(message, 0, 1);
         }
         connected = false;
         SyncInForm MF = this.Owner as SyncInForm;
         MF.Close();
     }
 }
예제 #4
0
        private void GameForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            SyncInForm MF = this.Owner as SyncInForm;

            MF.Show();
        }
예제 #5
0
        /* private void ListenerUDP()
         * {
         *   var udpCatcher = new UdpClient(udpPort);
         *   udpCatcher.EnableBroadcast = true;
         *   while (broadcastFlag)
         *   {
         *       IPEndPoint hostremote = null;
         *       var data = udpCatcher.Receive(ref hostremote);
         *       if (connected)
         *       {
         *           enemyIPV4Addr = hostremote.Address;
         *           if (!hostremote.Address.Equals(yourIPV4Addr))
         *           {
         *               yourTcpClient = new TcpClient();
         *               yourTcpClient.Connect(new IPEndPoint(enemyIPV4Addr, tcpPort));
         *               yourStream = yourTcpClient.GetStream();
         *               enemyStream = yourTcpClient.GetStream();
         *               var ConnectMessage = new byte[1];
         *               ConnectMessage[0] = C_USER_CONNECTED;
         *               yourStream.Write(ConnectMessage, 0, ConnectMessage.Length);
         *               enemyTcpClient = yourTcpClient;
         *               this.Invoke(new MethodInvoker(() =>
         *               {
         *                   TurnIdent.Text = "Your turn";
         *                   EnemyFleet.Enabled = true;
         *                   ExitBtn.Enabled = true;
         *               }));
         *               broadcastFlag = false;
         *               Task.Factory.StartNew(ListenerTCP);
         *           }
         *       }
         *   }
         * }
         *
         * private void ConnectionCatcher()
         * {
         *   TcpListener tcpListener = new TcpListener(yourIPV4Addr, tcpPort);
         *   tcpListener.Start();
         *   while (connected)
         *   {
         *       if (tcpListener.Pending())
         *       {
         *           enemyTcpClient = tcpListener.AcceptTcpClient();
         *           enemyIPV4Addr = ((IPEndPoint)enemyTcpClient.Client.RemoteEndPoint).Address;
         *           enemyStream = enemyTcpClient.GetStream();
         *           byte[] data = new byte[1];
         *           enemyStream.Read(data, 0, 1);
         *           if (data[0] == C_USER_CONNECTED)
         *           {
         *               this.Invoke(new MethodInvoker(() =>
         *               {
         *                   TurnIdent.Text = "Enemy's turn";
         *                   ExitBtn.Enabled = true;
         *               }));
         *               broadcastFlag = false;
         *               Task.Factory.StartNew(ListenerTCP);
         *           }
         *       }
         *   }
         *   tcpListener.Stop();
         * }
         */
        private void Interaction()
        {
            while (enemyConnected)
            {
                if (stream.DataAvailable)
                {
                    byte[] data = new byte[1];
                    stream.Read(data, 0, 1);
                    byte Type = data[0];
                    switch (Type)
                    {
                    case C_MESSAGE:
                        byte[] msg = new byte[2];
                        stream.Read(msg, 0, 1);
                        stream.Read(msg, 1, 1);
                        int    msgx     = msg[0];
                        int    msgy     = msg[1];
                        byte[] response = new byte[3];
                        this.Invoke(new MethodInvoker(() =>
                        {
                            Bitmap bmp   = new Bitmap(YourFleet.Image, YourFleet.Width, YourFleet.Height);
                            var graphics = Graphics.FromImage(bmp);
                            if ((shipMap[msgx, msgy] == true) && (shots[msgx, msgy] == false))
                            {
                                shots[msgx, msgy] = true;
                                response[0]       = C_HIT;
                                response[1]       = msg[0];
                                response[2]       = msg[1];
                                stream.Write(response, 0, 3);
                                graphics.FillRectangle(new SolidBrush(Color.Red), msgx * 37 + 2, msgy * 37 + 2, 33, 33);
                            }
                            else if (shots[msgx, msgy] == false)
                            {
                                shots[msgx, msgy] = true;
                                response[0]       = C_MISS;
                                response[1]       = msg[0];
                                response[2]       = msg[1];
                                stream.Write(response, 0, 3);
                                graphics.FillRectangle(new SolidBrush(Color.Black), msgx * 37 + 2, msgy * 37 + 2, 33, 33);
                                TurnIdent.Text     = "Your turn";
                                EnemyFleet.Enabled = true;
                            }
                            graphics.Dispose();
                            YourFleet.Image.Dispose();
                            YourFleet.Image = (Image)bmp.Clone();
                            bmp.Dispose();
                        }));
                        break;

                    case C_MISS:
                        byte[] missCoords = new byte[2];
                        stream.Read(missCoords, 0, 1);
                        stream.Read(missCoords, 1, 1);
                        int missCoordsX = missCoords[0];
                        int missCoordsY = missCoords[1];
                        this.Invoke(new MethodInvoker(() =>
                        {
                            Bitmap bmp   = new Bitmap(EnemyFleet.Image, EnemyFleet.Width, EnemyFleet.Height);
                            var graphics = Graphics.FromImage(bmp);
                            graphics.FillRectangle(new SolidBrush(Color.Black), missCoordsX * 37 + 2, missCoordsY * 37 + 2, 33, 33);
                            graphics.Dispose();
                            EnemyFleet.Image.Dispose();
                            EnemyFleet.Image = (Image)bmp.Clone();
                            bmp.Dispose();
                            TurnIdent.Text     = "Enemy's turn";
                            EnemyFleet.Enabled = false;
                        }));
                        break;

                    case C_HIT:
                        byte[] hitCoords = new byte[2];
                        stream.Read(hitCoords, 0, 1);
                        stream.Read(hitCoords, 1, 1);
                        int hitCoordsX = hitCoords[0];
                        int hitCoordsY = hitCoords[1];
                        this.Invoke(new MethodInvoker(() =>
                        {
                            Bitmap bmp   = new Bitmap(EnemyFleet.Image, EnemyFleet.Width, EnemyFleet.Height);
                            var graphics = Graphics.FromImage(bmp);
                            graphics.FillRectangle(new SolidBrush(Color.Red), hitCoordsX * 37 + 2, hitCoordsY * 37 + 2, 33, 33);
                            graphics.Dispose();
                            EnemyFleet.Image.Dispose();
                            EnemyFleet.Image = (Image)bmp.Clone();
                            bmp.Dispose();
                            deckNum--;
                            if (deckNum == 0)
                            {
                                byte[] victoryMsg = new byte[1];
                                victoryMsg[0]     = C_DEFEATED;
                                stream.Write(victoryMsg, 0, 1);
                                MessageBox.Show("You have defeated your enemy");
                                enemyConnected   = false;
                                connected        = false;
                                exitCheck        = false;
                                SyncInForm MForm = this.Owner as SyncInForm;
                                MForm.Close();
                                this.Close();
                            }
                        }));
                        break;

                    case C_DEFEATED:
                        enemyConnected = false;
                        connected      = false;
                        this.Invoke(new MethodInvoker(() =>
                        {
                            MessageBox.Show("You have been defeated");
                            exitCheck        = false;
                            SyncInForm MainF = this.Owner as SyncInForm;
                            MainF.Close();
                            this.Close();
                        }));
                        break;

                    case C_USER_DISCONNECTED:
                        enemyConnected = false;
                        connected      = false;
                        this.Invoke(new MethodInvoker(() =>
                        {
                            MessageBox.Show("Enemy disconnected");
                            exitCheck        = false;
                            SyncInForm MainF = this.Owner as SyncInForm;
                            MainF.Close();
                            this.Close();
                        }));
                        break;
                    }
                }
            }
        }