Ejemplo n.º 1
0
 private void ValidateConnection(Client firstPlayer, Client secondPlayer)
 {
     while (true)
     {
         if (!firstPlayer.IsConnected())
         {
             this.winner = 2;
             break;
         }
         if (!secondPlayer.IsConnected())
         {
             this.winner = 1;
             break;
         }
     }
 }
Ejemplo n.º 2
0
        private Client playGame(Client[] players)
        {
            SetLocations();
            Client firstPlayer  = players[0];
            Client secondPlayer = players[1];

            Thread.Sleep(5000);
            firstPlayer.SendName(secondPlayer.name);
            secondPlayer.SendName(firstPlayer.name);
            Thread.Sleep(2000);
            for (int i = 3; i > 0; i--)
            {
                firstPlayer.SendInt(i);
                secondPlayer.SendInt(i);
                Thread.Sleep(1000);
            }
            Thread server = new Thread(() =>
            {
                Server();
            });
            Thread firstPlayerLitsener = new Thread(() =>
            {
                LitsenToFirstPlayer(firstPlayer);
            });
            Thread secondPlayerLitsener = new Thread(() =>
            {
                LitsenToSecondPlayer(secondPlayer);
            });
            Thread validateConnection = new Thread(() =>
            {
                ValidateConnection(firstPlayer, secondPlayer);
            });

            server.Start();
            firstPlayerLitsener.Start();
            secondPlayerLitsener.Start();
            validateConnection.Start();

            while (true)
            {
                if (this.winner != 0)
                {
                    server.Abort();
                    firstPlayerLitsener.Abort();
                    secondPlayerLitsener.Abort();
                    validateConnection.Abort();

                    if (winner == 1)
                    {
                        if (!secondPlayer.IsConnected())
                        {
                            firstPlayer.SendServer(true, WhoWins.you, this.ballLeft, this.ballTop, this.secondPlayerRacketTop);
                        }
                        else
                        {
                            firstPlayer.SendServer(false, WhoWins.you, this.ballLeft, this.ballTop, this.secondPlayerRacketTop);
                            secondPlayer.SendServer(false, WhoWins.enemy, this.playgroundRight - this.ballLeft - this.ballWidth, this.ballTop, this.firstPlayerRacketTop);
                        }
                        return(firstPlayer);
                    }

                    else if (winner == 2)
                    {
                        if (!firstPlayer.IsConnected())
                        {
                            secondPlayer.SendServer(true, WhoWins.you, this.playgroundRight - this.ballLeft - this.ballWidth, this.ballTop, this.firstPlayerRacketTop);
                        }
                        else
                        {
                            firstPlayer.SendServer(false, WhoWins.enemy, this.ballLeft, this.ballTop, this.secondPlayerRacketTop);
                            secondPlayer.SendServer(false, WhoWins.you, this.playgroundRight - this.ballLeft - this.ballWidth, this.ballTop, this.firstPlayerRacketTop);
                        }
                        return(secondPlayer);
                    }
                }
            }
        }