public static int ComparePlayers(PlayerWrapper playerA, PlayerWrapper playerB) { string a = playerA.GetName(); string b = playerB.GetName(); int aL = a.Length; int bL = b.Length; for (int i = 0; i < Math.Min(aL, bL); i++) { if (char.IsUpper(a[i]) && char.IsLower(b[i])) //A < b { return(-1); } else if (char.IsLower(a[i]) && char.IsUpper(b[i])) //a > B { return(1); } else //if characters are the same case or one character isn't a letter { return(a[i].CompareTo(b[i])); } } return((aL > bL) ? 1 : -1); }
//Sets fields for Referee and gives each player a stone public string AssignPlayer() { if (_players_set == 0) { Console.Write("Assigning " + _player1.GetName() + ": "); _current_player = _player1; _players_set++; _player1.ReceiveStones("B"); Console.WriteLine("Successful"); return("B"); } else if (_players_set == 1) { Console.Write("Assigning " + _player2.GetName() + ": "); _players_set++; _player2.ReceiveStones("W"); Console.WriteLine("Successful"); return("W"); } throw new InvalidOperationException("Invalid call to AssignPlayer in Referee: Cannot assign more than two players"); }
/* * 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; } } } }
/* * 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; } } } }