/// <summary>
        /// Action of scoring in a game of the tournament
        /// </summary>
        /// <param name="pPlayer">Player who scores</param>
        /// <param name="pGameID">Game ID which identifies the actual game</param>
        public void SetScore(PaddlePlayer pPlayer, string pGameID)
        {
            try
            {
                if (_games.Count > 0)
                {
                    var game = _games.FirstOrDefault(a => a.ID.Equals(pGameID));

                    if (game != null)
                    {
                        game.SetScore(pPlayer);

                        Console.WriteLine("Player [{0}] has scored at game ID [{1}]", pPlayer.Licence, game.ID);
                    }
                    else
                    {
                        Console.WriteLine("Game with ID [{0}] no longer exists.", pGameID);
                    }

                    game.Progress();
                }
                else
                {
                    Console.WriteLine("No games are scheduled.");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error in order to set a score: {0}", ex.Message);
            }
        }
        /// <summary>
        /// Start a new game into the tournament
        /// </summary>
        /// <param name="pVisitingPlayer">Visiting player</param>
        /// <param name="pHomePlayer">Home player</param>
        /// <returns>Game ID which identifies the actual game in the tournament</returns>
        public string StartGame(PaddlePlayer pVisitingPlayer, PaddlePlayer pHomePlayer)
        {
            var game = new GameField(pVisitingPlayer, pHomePlayer);

            if (!_games.Contains(game))
            {
                var guid = Guid.NewGuid();

                var gameID = Regex.Replace(Convert.ToBase64String(Guid.NewGuid().ToByteArray()), "[/+=]", string.Empty);;

                game.Start(gameID);

                _games.Add(game);

                Console.WriteLine("New game has started with ID [{0}][{1}][{2}]", game.ID, pVisitingPlayer.Licence, pHomePlayer.Licence);

                return(game.ID);
            }
            else
            {
                Console.WriteLine("The game has already started.");

                return(string.Empty);
            }
        }
        /// <summary>
        /// Get the current scoreboard for a game of the tournament
        /// </summary>
        /// <param name="pPlayer">Player to get the scoreboard</param>
        /// <param name="pGameID">Game ID which identifies the actual game</param>
        /// <returns>The current scoreboard for the game in progress</returns>
        public ScoreBoard GetScore(PaddlePlayer pPlayer, string pGameID)
        {
            var game = GetGame(pGameID);

            if (game != null && game.Players.Contains(pPlayer))
            {
                //Get the current playing set

                var current = game.GetCurrentSet();

                //Get total sets won

                var CountSets = 0;

                game.CurrentScore.TryGetValue(pPlayer.Licence, out CountSets);

                return(new ScoreBoard
                {
                    Player = pPlayer,
                    Set = current.Name,
                    SetsWon = CountSets,
                    Status = current.ScoreStatus,
                    Score = current.GetScore(pPlayer)
                });
            }
            else
            {
                Console.WriteLine("No scoreboard has been found for the details");

                return(null);
            }
        }
Exemple #4
0
        //***********************************************
        //Game Requirements:
        //***********************************************

        //Several matches can be play at a time.
        //In a game of paddle, a player begins with a score of zero (0). With each success, the player earns more points.
        //The points are earned in this sequence: 0, 15, 30, 40.
        //If a player has 40 and scores again, that player wins the game as long as the other player does not also have 40 points.
        //If both players reach 40 points it is referred to as a 'deuce.'
        //Scoring during deuce give a player advantage.
        //If the other player scores again, the score returns to deuce. If a player has advantage and scores again, that player wins the game.
        //The matches are played to the best of three sets.

        //Players must be able to score points of the different games.
        //The game must be able to be completed with a winner
        //The 'deuce' case should be handled
        //After a game has been won, a winner must be determined
        //The current score of either player should be available at any point during the game.

        static void Main(string[] args)
        {
            var player1 = new PaddlePlayer {
                Licence = "SJ125OL", Name = "Jalen"
            };

            var player2 = new PaddlePlayer {
                Licence = "MU345UX", Name = "Damien"
            };

            var player3 = new PaddlePlayer {
                Licence = "JA687OP", Name = "Calvin"
            };

            var game1Guid = Tournament.Instance.StartGame(player1, player2);

            Tournament.Instance.SetScore(player1, game1Guid);

            var game2Guid = Tournament.Instance.StartGame(player1, player3);

            //Game 2 exemplifies the deuce case

            Tournament.Instance.SetScore(player1, game2Guid);

            Tournament.Instance.SetScore(player1, game2Guid);

            Tournament.Instance.SetScore(player1, game2Guid);

            Tournament.Instance.SetScore(player3, game2Guid);

            Tournament.Instance.SetScore(player3, game2Guid);

            Tournament.Instance.SetScore(player3, game2Guid);

            Tournament.Instance.SetScore(player3, game2Guid);

            Tournament.Instance.SetScore(player1, game2Guid);

            Tournament.Instance.SetScore(player3, game2Guid);

            Tournament.Instance.SetScore(player3, game2Guid);

            var score = Tournament.Instance.GetScore(player3, game2Guid);

            if (score == null)
            {
                Console.WriteLine("Player isn't playing in the actual game [{0}][{1}].", player3.Licence, game2Guid);
            }
            else
            {
                Console.WriteLine("Scoreboard for game ID [{0}] --> [{1}][{2}][{3}][{4}]", game2Guid, score.Score, score.SetsWon,
                                  score.Set.ToString(), score.Player.Licence);
            }

            Console.ReadLine();
        }
Exemple #5
0
 /// <summary>
 /// Find given player is visiting or home in the game
 /// </summary>
 /// <param name="pPlayer">Player to intent to find</param>
 /// <returns>Value 0 is visiting and 1 is home</returns>
 public int FindPlayer(PaddlePlayer pPlayer)
 {
     if (Players.Contains(pPlayer))
     {
         return(Array.IndexOf(Players, pPlayer));
     }
     else
     {
         return(-1);
     }
 }
Exemple #6
0
        /// <summary>
        /// Get the current score of the set
        /// </summary>
        /// <param name="pPlayer">Player to access to the scoreboard</param>
        /// <returns>Current score of the set</returns>
        public int GetScore(PaddlePlayer pPlayer)
        {
            //Get the actual game involved for this set

            var game = Tournament.Instance.GetGame(GameID);

            if (game == null)
            {
                throw new ArgumentException("Game wasn't found at this moment");
            }

            var pos = game.FindPlayer(pPlayer);

            return(_scoreBoard[pos]);
        }
Exemple #7
0
        /// <summary>
        /// Action of score for the actual game
        /// </summary>
        /// <param name="pPlayer">Player who scores in the game</param>
        public void SetScore(PaddlePlayer pPlayer)
        {
            //Only able to score at playing sets.

            foreach (var set in _sets)
            {
                if (set.ScoreStatus == StatusName.Playing || set.ScoreStatus == StatusName.Deuce)
                {
                    if (set.Score(pPlayer))
                    {
                        Console.WriteLine("Player [{0}] has succesfully scored.", pPlayer.Licence);

                        break;
                    }
                }
            }
        }
Exemple #8
0
        /// <summary>
        /// Creates a gamefield for playing
        /// </summary>
        /// <param name="pVisitingPlayer">Visiting player</param>
        /// <param name="pHomePlayer">Home Player</param>
        public GameField(PaddlePlayer pVisitingPlayer, PaddlePlayer pHomePlayer)
        {
            Players = new PaddlePlayer[] { pVisitingPlayer, pHomePlayer };

            CurrentScore = new Dictionary <string, int>();
        }
Exemple #9
0
        /// <summary>
        /// Action of score for the actual set
        /// </summary>
        /// <param name="pPlayer">Player who scores in the game</param>
        /// <returns>Score is succesfully set</returns>
        public bool Score(PaddlePlayer pPlayer)
        {
            //Get the actual game involved for this set

            var game = Tournament.Instance.GetGame(GameID);

            if (game == null)
            {
                throw new ArgumentException("Game wasn't found at this moment");
            }

            var pos = game.FindPlayer(pPlayer);

            if (pos == -1)
            {
                throw new ArgumentException("Player isn't currently playing in the game.");
            }
            else
            {
                if (Winner == null)
                {
                    if (_scoreBoard[pos] == GameCommon.PointsToWin)
                    {
                        //Check opponent score and decide the victory of the set

                        int rival = (pos == 1) ? _scoreBoard[pos - 1] : _scoreBoard[pos + 1];

                        if (_scoreBoard[pos] > rival || (ScoreStatus == StatusName.Deuce && _advsDeuce[pos] == true))
                        {
                            ScoreStatus = StatusName.Finished;

                            Winner = game.Players[pos];

                            return(true);
                        }
                        else if (_scoreBoard[pos] == rival && _advsDeuce.All(a => a == false))
                        {
                            ScoreStatus = StatusName.Deuce;

                            _advsDeuce[pos] = true;

                            return(true);
                        }
                        else if (ScoreStatus == StatusName.Deuce)
                        {
                            _advsDeuce = new bool[] { false, false };

                            return(true);
                        }
                    }
                    else
                    {
                        for (int i = 0; i < GameCommon.PointsValue.Length; i++)
                        {
                            if (GameCommon.PointsValue[i] > _scoreBoard[pos])
                            {
                                _scoreBoard[pos] = GameCommon.PointsValue[i];

                                //keep increment 'till score is topped and no winner decided

                                return(true);
                            }
                        }
                    }
                }
            }

            return(false);
        }