//Recursive method for obtaining the winner of a tournament.
 //if you call this method with a tournament Node, you can obtain the winner of that Node in specific.
 public Player winnerTournament(TournamentNode actualNode)
 {
     if (actualNode.isSon())
         return actualNode._player;
     else
         return winnerTournamentAux(actualNode._leftNode, actualNode._rightNode)._player;
 }
 public void setNodes(string match)
 {
     string[] args = match.Split('"');
     Player left = new Player(args[1], args[3]);
     Player right = new Player(args[5], args[7]);
     TournamentNode node = new TournamentNode(left, right);
     _winner = node.winnerTournament(node);
 }
        //Method that retrieves the winner of a single match.
        //the nodes that recieves most be sons
        private TournamentNode winnerMatch(TournamentNode left, TournamentNode right)
        {
            string playLeft = left._player.getPlay();
            string playRight = right._player.getPlay();
          
            if (leftwins(playLeft, playRight))
                return left;
            else
                return right;

        }
  //Auxiliar method of winnerTournament used for the recursion of the method in both Nodes of the tree
 private TournamentNode winnerTournamentAux(TournamentNode tournamentNodeleft, TournamentNode tournamentNoderight)
  {
     //end statement, when the Nodes are Sons
      if (tournamentNodeleft.isSon())
          return winnerMatch(tournamentNodeleft, tournamentNoderight);
      else
      {
          //Obtains the winner of the games in the left and right Nodes and obtains the champion
          TournamentNode leftwinner = winnerTournamentAux(tournamentNodeleft._leftNode, tournamentNodeleft._rightNode);
          TournamentNode rightwinner = winnerTournamentAux(tournamentNoderight._leftNode, tournamentNoderight._rightNode);
          return winnerTournamentAux(leftwinner, rightwinner);
      }
  }
        /*Constructor that recieves the path of a .txt file to obtain the winner and second place of the tournament specified in 
          the file.  */
        public Championship(string path)
        {
            using (StreamReader sr = new StreamReader(path))
            {
                 Stack list = new Stack();
                while (sr.Peek() >= 0) 
                {
                    String line = sr.ReadLine();
                    line.Trim();
                    if (line.Equals("["))
                    { 
                        TournamentNode node = new TournamentNode();
                        list.Push(node);
                    }
                    else if (line.Equals("]") || line.Equals("],"))
                    {
                        TournamentNode right = (TournamentNode) list.Pop();
                        TournamentNode left = (TournamentNode) list.Pop();
                        TournamentNode node = (TournamentNode)list.Pop();
                        node.setSons(left, right);
                        list.Push(node);
                    }
                    else
                    {
                      string[] args =  line.Split('"');
                      Player left = new Player(args[1].Trim().ToUpper(), args[3].Trim().ToUpper());
                      Player right = new Player(args[5].Trim().ToUpper(), args[7].Trim().ToUpper());
                      TournamentNode node = new TournamentNode(left, right);
                      list.Push(node);
                    }
                }

                _tournament = (TournamentNode)list.Pop();

            }

        }
 public TournamentNode(Player left, Player right)
 {
     _leftNode = new TournamentNode(left);
     _rightNode = new TournamentNode(right);
 }
 //Set of the Left and Right Nodes.
 internal void setSons(TournamentNode left, TournamentNode right)
 {
     _leftNode = left;
     _rightNode = right;
 }