private static int DoHumanMove(ConnectFourBoard board, ConnectFourPiece human) { var location = -1; var legalMoves = new HashSet <int>(board.GetLegalMoves()); var availableMoves = string.Join(",", board.GetLegalMoves().Select(m => $"{m}")); while (!legalMoves.Contains(location)) { Console.WriteLine($"please, enter where to put next {human} [{availableMoves}]"); location = int.TryParse(new string(Console.ReadKey().KeyChar, 1), out var loc) ? loc : -1; } return(location); }
public static void Main(string[] args) { Console.WriteLine("Hello, World!"); var positions = Enumerable.Range(1, ConnectFourBoard.CellAmount).Select(_ => (ConnectFourPiece)ConnectFourPiece.Empty).ToArray(); var board = new ConnectFourBoard(positions, (ConnectFourPiece)ConnectFourPiece.Black); var(human, computer) = SelectSide(); var moveNumber = 0; while (board.GetLegalMoves().Any()) { Console.Clear(); Console.WriteLine($"step#{moveNumber}"); Console.WriteLine(board.ToString()); var location = board.GetTurn() == human ? DoHumanMove(board, human) : DoComputerMove(board); Console.WriteLine($"the move is to put {board.GetTurn()} into column {location}"); moveNumber++; board = board.Move(location) as ConnectFourBoard; } PrintResult(moveNumber, board, human); }