/// Ask and return the desired board size. private static int GetBoardSize() { while (true) { Console.Write("Choose board size (default is 8): "); if (int.TryParse(Console.ReadLine(), out var boardSize)) { return(Math.Max(4, Math.Min(boardSize, 8))); } ColorPrint.Error("give a valid number..."); } }
/// Return move chosen by a human player. private Move GetHumanMove(List <Move> moves) { while (true) { var square = GetSquare(); // check if given square is one of the possible moves if (moves.Exists(x => square.Equals(x.Square))) { return(moves.Find(x => square.Equals(x.Square))); } ColorPrint.Error($"can't place a {_disk.Name()} disk in square {square}!", 2); } }
/// Ask human player for square coordinates. private static Square GetSquare() { while (true) { try { Console.Write(" Give disk position (x,y): "); var coords = Console.ReadLine(); if (string.IsNullOrEmpty(coords) || coords.Length != 3 || coords[1] != ',') { throw new FormatException(); } var x = int.Parse(coords.Substring(0, 1)); var y = int.Parse(coords.Substring(2, 1)); return(new Square(x, y)); } catch (FormatException) { ColorPrint.Error("give coordinates in the form (x,y)!", 2); } } }