Exemplo n.º 1
0
    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);
    }
Exemplo n.º 2
0
 private static void PrintResult(int moveNumber, ConnectFourBoard board, ConnectFourPiece human)
 {
     Console.Clear();
     Console.WriteLine($"After {moveNumber} steps");
     Console.WriteLine(board.ToString());
     if (board.IsWin())
     {
         var winnerName = board.GetTurn() == human ? "computer" : "human";
         Console.WriteLine($"{winnerName} wins!");
     }
     else
     {
         Console.WriteLine("It's a draw!");
     }
 }
 private int GetMatchingCount(Segment segment, ConnectFourPiece piece)
 {
     return(segment.Locations.Count(l => _positions[l] == piece));
 }
 private bool AreAllTheSameFill(Segment segment, ConnectFourPiece piece)
 {
     return(GetMatchingCount(segment, piece) == segment.Locations.Count);
 }
 public ConnectFourBoard(ConnectFourPiece[] positions, ConnectFourPiece turn)
 {
     _positions = positions;
     _turn      = turn ?? throw new ArgumentNullException(nameof(turn));
 }
 //the very first turn game state
 public ConnectFourBoard()
 {
     _positions = Enumerable.Range(1, CellAmount).Select(_ => (ConnectFourPiece)ConnectFourPiece.Empty).ToArray(); //the field is empty
     _turn      = ConnectFourPiece.Black as ConnectFourPiece;                                                      //first player puts Black pieces
 }