Ejemplo n.º 1
0
        /// <summary>
        /// Start a new game or tiebreak if applicable
        /// </summary>
        public void StartNewGame()
        {
            //Check if another game is in progress
            if (this.currentGame() != null)
            {
                if (currentGame().State != TennisGame.StateEnum.sFinished)
                {
                    throw new Exception("Another game is already in progress");
                }
            }

            TennisGame newGame;

            if (ScoreContestant1 == ScoreContestant2 &&
                ScoreContestant1 == PartOf.TieBreakAtSameScoreOf &&
                (PartOf.TieBreakFinalSet || PartOf.Sets.Count != PartOf.BestOutOf)
                )
            {
                //The set has entered a tiebreak
                TennisTiebreak newTiebreak = new TennisTiebreak();
                newTiebreak.SetStartServer(StartServer);

                newGame               = newTiebreak;
                newGame.PartOf        = this;
                newGame.PointsPerGame = PartOf.TieBreakLength;
            }
            else
            {
                if (this.PartOf.Sets.Count == this.PartOf.BestOutOf && this.PartOf.Sets.Count > 1 && this.PartOf.FinalSetIsTiebreak)
                {
                    //The final set has started, and it is a tiebreak
                    TennisTiebreak newTiebreak = new TennisTiebreak();
                    newTiebreak.SetStartServer(StartServer);

                    newGame               = newTiebreak;
                    newGame.PartOf        = this;
                    newGame.PointsPerGame = this.PartOf.FinalSetTieBreakLength;
                }
                else
                {
                    //Start new regular game
                    newGame        = new TennisGame();
                    newGame.PartOf = this;
                }
            }

            if (Games.Count > 0)
            {
                newGame.Server = PartOf.GetNextServer().ContestantNr;
            }
            else
            {
                newGame.Server = StartServer;
            }
            newGame.Receiver = PartOf.GetReceiver().ContestantNr;
            newGame.Start();

            Games.Add(newGame);
        }
        private void ProcessSet(TennisSet set)
        {
            int Server     = 1;
            int Returner   = 2;
            int PointCount = 0;

            foreach (TennisGame game in set.Games)
            {
                Server     = game.Server;
                Returner   = 3 - Server;
                PointCount = 1;

                if (game.GetType() != typeof(TennisTiebreak))
                {
                    //Tiebreaks do not count for service games won or lost
                    if (game.Winner != 0)
                    {
                        IncrementItem(Statistics.ServiceGamesPlayed, Server);
                        IncrementItem(Statistics.ReturnGamesPlayed, Returner);

                        if (game.Winner == game.Server)
                        {
                            IncrementItem(Statistics.ServiceGamesWon, Server);
                        }
                        else
                        {
                            IncrementItem(Statistics.ReturnGamesWon, Returner);
                        }
                    }
                }

                foreach (TennisPoint point in game.Points)
                {
                    if (game.GetType() != typeof(TennisTiebreak))
                    {
                        ProcessPoint(point, point.Server, 3 - point.Server, 1);
                    }
                    else
                    {
                        TennisTiebreak tiebreak = (TennisTiebreak)game;
                        Server = tiebreak.GetStartServer();
                        if (((PointCount / 2) % 2) == 1)
                        {
                            Server = 3 - Server;
                        }
                        Returner = 3 - Server;

                        ProcessPoint(point, Server, Returner, 1);

                        PointCount++;
                    }
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Add a new point to this set
        /// </summary>
        /// <param name="newPoint"></param>
        public void Add(TennisPoint newPoint)
        {
            //Update statistics
            Statistics.Add(newPoint);

            //Start new game if necessary
            if (currentGame() != null)
            {
                if (currentGame().State == TennisGame.StateEnum.sFinished)
                {
                    StartNewGame();
                }
            }
            else
            {
                StartNewGame();
            }

            //Add this new point to the game
            if (currentGame().GetType() == typeof(TennisTiebreak))
            {
                TennisTiebreak currentTiebreak = (TennisTiebreak)currentGame();
                currentTiebreak.Add(newPoint);
            }
            else
            {
                currentGame().Add(newPoint);
            }


            //Check the score if the game is finished
            if (currentGame().State == TennisGame.StateEnum.sFinished)
            {
                RebuildScore();

                //Check if there is a winner for this set
                int GamesPerSet = PartOf.NumberGamesPerSet;

                if ((ScoreContestant1 >= GamesPerSet && ScoreContestant2 < ScoreContestant1 - 1) ||
                    (ScoreContestant2 >= GamesPerSet && ScoreContestant1 < ScoreContestant2 - 1) ||
                    (currentGame().GetType() == typeof(TennisTiebreak)))
                {
                    Duration.End();
                    Winner = currentGame().Winner;
                }

                if (Winner == 0)
                {
                    StartNewGame();
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Switch the server to start serving the match. Default is Contestant 1
        /// </summary>
        public void SwitchServer()
        {
            if (Points.Count > 0)
            {
                throw new Exception("Can't switch server when the match has started.");
            }

            TennisContestant newServer = GetNextServer();

            currentSet().StartServer = newServer.ContestantNr;
            currentGame().Server     = newServer.ContestantNr;
            currentGame().Receiver   = 3 - currentGame().Server;
            if (currentGame().GetType() == typeof(TennisTiebreak))
            {
                TennisTiebreak _tiebreak = (TennisTiebreak)currentGame();
                _tiebreak.SetStartServer(newServer.ContestantNr);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Check if the set is on match point and if so return the number of the player who has it
        /// </summary>
        /// <returns></returns>
        private int CheckSetPoint()
        {
            TennisGame currentGame   = this.currentGame();
            int        ScoreServer   = 0;
            int        ScoreReceiver = 0;

            if (currentGame.Server == 1)
            {
                ScoreServer   = currentSet().ScoreContestant1;
                ScoreReceiver = currentSet().ScoreContestant2;
            }
            else
            {
                ScoreReceiver = currentSet().ScoreContestant1;
                ScoreServer   = currentSet().ScoreContestant2;
            }

            //Regular game
            if (currentGame.GetType() != typeof(TennisTiebreak))
            {
                if (ScoreServer >= NumberGamesPerSet - 1 && ScoreReceiver < ScoreServer && currentGame.GamePoint())
                {
                    return(currentGame.Server);
                }
                if (ScoreReceiver >= NumberGamesPerSet - 1 && ScoreServer < ScoreReceiver && currentGame.BreakPoint())
                {
                    return(3 - currentGame.Server);
                }
            }
            else
            {
                //Tiebreak
                TennisTiebreak Tiebreak = (TennisTiebreak)currentGame;

                return(Tiebreak.SetPoint());
            }

            return(0);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Generates a match object from its XML representation.
        /// </summary>
        /// <param name="reader"></param>
        public void ReadXml(System.Xml.XmlReader reader)
        {
            if (reader.Name != "Set")
            {
                throw new Exception("Unexpected node encountered, Set expected");
            }

            String _ID               = reader.GetAttribute("ID");
            String _Winner           = reader.GetAttribute("Winner");
            String _StartServer      = reader.GetAttribute("StartServer");
            String _ScoreContestant1 = reader.GetAttribute("ScoreContestant1");
            String _ScoreContestant2 = reader.GetAttribute("ScoreContestant2");

            ID               = long.Parse(_ID);
            Winner           = int.Parse(_Winner);
            StartServer      = int.Parse(_StartServer);
            ScoreContestant1 = int.Parse(_ScoreContestant1);
            ScoreContestant2 = int.Parse(_ScoreContestant2);

            while (reader.Read() && !(reader.Name == "Set" && reader.NodeType == System.Xml.XmlNodeType.EndElement))
            {
                if (reader.NodeType == System.Xml.XmlNodeType.Element)
                {
                    switch (reader.Name)
                    {
                    case "Games":

                        while (reader.Read() && !(reader.Name == "Games" && reader.NodeType == System.Xml.XmlNodeType.EndElement))
                        {
                            if (reader.NodeType == System.Xml.XmlNodeType.Element)
                            {
                                switch (reader.Name)
                                {
                                case "Game":

                                    TennisGame newGame = new TennisGame();
                                    newGame.PartOf = this;
                                    newGame.ReadXml(reader);
                                    this.PartOf.Points.AddRange(newGame.Points);

                                    this.Games.Add(newGame);

                                    break;

                                case "Tiebreak":

                                    TennisTiebreak newTiebreak = new TennisTiebreak();
                                    newTiebreak.ReadXml(reader);
                                    newTiebreak.PartOf = this;
                                    this.PartOf.Points.AddRange(newTiebreak.Points);

                                    this.Games.Add(newTiebreak);

                                    break;
                                }
                            }
                        }

                        break;

                    case "Statistics":

                        Statistics.ReadXml(reader);

                        break;

                    case "Duration":

                        Duration.ReadXml(reader);

                        break;
                    }
                }
            }
        }