Exemplo n.º 1
0
        /*
         * Simulates a game between two players
         * Returns a list off victors
         * Players can be local or remote
         * If a remote player sends illegal data or disconnects, the other player is declared victor
         */
        public List <string> RefereeGame()
        {
            while (true)
            {
                try
                {
                    string next_move = _current_player.MakeAMove(GetBoardHistory());
                    if (next_move == "pass")
                    {
                        Pass();
                    }
                    else
                    {
                        //maybe this function shouldn't be in referee... need to check if this is a valid point b/c no wrappeer
                        ValidationMethods.ValidatePoint(next_move, _size);
                        Play(next_move);
                    }
                    //Don't Update current player!!! already updated in Pass() and Play()
                    //_current_player = _current_player == _player1 ? _player2 : _player1;
                }
                catch (RefereeException)
                {
                    List <PlayerWrapper> victors = GetVictors();
                    List <string>        names   = new List <string>();
                    foreach (PlayerWrapper victor in victors)
                    {
                        names.Add(victor.GetName());
                    }

                    return(names);
                }
                catch (Exception e)
                {
                    if (e is JsonSerializationException || e is ArgumentException || e is SocketException || e is WrapperException || e is JsonReaderException)
                    {
                        List <string> names = new List <string>();
                        if (_current_player == _player1)
                        {
                            names.Add(_player2.GetName());
                        }
                        else
                        {
                            names.Add(_player1.GetName());
                        }
                        return(names);
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
Exemplo n.º 2
0
        /*
         * Simulates a game between two players
         * Assumes players have already been registered
         * call ReceiveStones on each player
         * Returns a list of victors
         * Players can be local or remote
         * If a remote player sends illegal data or disconnects, the other player is declared victor
         */
        public List <string> RefereeGame(string name1, string name2, out bool has_cheater)
        {
            try
            {
                AssignPlayer(); //Assign player 1
            }
            catch (Exception e)
            {
                if (e is JsonSerializationException || e is ArgumentException || e is SocketException || e is WrapperException || e is JsonReaderException)
                {
                    Console.WriteLine("Failed");
                    List <string> victor = new List <string>();
                    victor.Add(name2);
                    has_cheater = true;
                    return(victor);
                }
                else
                {
                    throw;
                }
            }

            try
            {
                AssignPlayer(); //Assign player 2
            }
            catch (Exception e)
            {
                if (e is JsonSerializationException || e is ArgumentException || e is SocketException || e is WrapperException || e is JsonReaderException)
                {
                    Console.WriteLine("Failed");
                    List <string> victor = new List <string>();
                    victor.Add(name1);
                    has_cheater = true;
                    return(victor);
                }
                else
                {
                    throw;
                }
            }

            while (true)
            {
                try
                {
                    Console.Write("Asking player " + _current_player.GetName() + " for a move: ");
                    string next_move = _current_player.MakeAMove(GetBoardHistory());
                    if (next_move == "pass")
                    {
                        Console.WriteLine("pass");
                        Pass();
                    }
                    else
                    {
                        Console.Write(next_move + ", ");
                        Play(next_move);
                        Console.WriteLine("played successfully");
                    }
                    //Don't Update current player!!! already updated in Pass() and Play()
                    //_current_player = _current_player == _player1 ? _player2 : _player1;
                }
                catch (RefereeException)
                {
                    Console.WriteLine("GAME ENDED");
                    List <PlayerWrapper> victors = GetVictors();
                    List <string>        names   = new List <string>();
                    foreach (PlayerWrapper victor in victors)
                    {
                        names.Add(victor.GetName());
                    }
                    has_cheater = false;
                    return(names);
                }
                catch (Exception e)
                {
                    if (e is JsonSerializationException || e is ArgumentException || e is SocketException || e is WrapperException || e is JsonReaderException)
                    {
                        Console.WriteLine("FAILED");
                        Console.WriteLine("GAME ENDED");
                        List <string> names = new List <string>();
                        if (_current_player == _player1)
                        {
                            names.Add(_player2.GetName());
                        }
                        else
                        {
                            names.Add(_player1.GetName());
                        }
                        has_cheater = true;
                        return(names);
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }