Example #1
0
 /// Print current score for both players.
 public void PrintScore()
 {
     var(black, white) = PlayerScores();
     Console.WriteLine("\n" + this);
     Console.Write($"Score: {ColorPrint.Get(black, Disk.Black.DiskColor())} | " +
                   $"{ColorPrint.Get(white, Disk.White.DiskColor())}\n");
 }
Example #2
0
        /// Print available move coordinates and resulting points gained.
        public void PrintMoves(IReadOnlyCollection <Move> moves)
        {
            ColorPrint.Write($"  Possible moves ({moves.Count}):\n", Color.Yellow);
            // convert board from Disk enums to strings
            var boardStr = new List <string>(_board.Count);

            boardStr.AddRange(_board.Select(disk => disk.BoardChar()));
            foreach (var move in moves)
            {
                Console.WriteLine($"  {move}");
                var(x, y) = move.Square;
                boardStr[y * _size + x] = ColorPrint.Get(move.Value, Color.Yellow);
            }
            // print board with move positions
            Console.Write("   ");
            foreach (var i in _indices)
            {
                Console.Write($" {i}");
            }
            foreach (var y in _indices)
            {
                Console.Write($"\n  {y}");
                foreach (var x in _indices)
                {
                    Console.Write($" {boardStr[y * _size + x]}");
                }
            }
            Console.WriteLine("");
        }
Example #3
0
 public static string BoardChar(this Disk disk)
 {
     if (disk == Disk.Empty)
     {
         return("_");
     }
     return(ColorPrint.Get(disk == Disk.White ? "W" : "B", disk.DiskColor()));
 }
Example #4
0
 /// 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...");
     }
 }
Example #5
0
 /// 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);
     }
 }
Example #6
0
        public static void Main(string[] args)
        {
            ColorPrint.Write("OTHELLO GAME - C#\n", Color.Green);
            // try to read board size from command line args
            if (args.Length == 0 || !int.TryParse(args[0], out var boardSize))
            {
                boardSize = GetBoardSize();
            }
            else
            {
                Console.WriteLine($"Using board size: {boardSize}");
            }

            var game = new Othello(boardSize);

            game.Play();
        }
Example #7
0
 /// 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);
         }
     }
 }
Example #8
0
        /// Print ending status and winner info.
        private void PrintResult()
        {
            Console.WriteLine("\n================================");
            ColorPrint.Write("The game is finished!\n", Color.Green);
            Console.WriteLine("Result:".Pastel(Color.Silver));
            PrintStatus();
            Console.WriteLine("");

            var winner = _board.Result();

            if (winner == Disk.Empty)
            {
                Console.WriteLine("The game ended in a tie...");
            }
            else
            {
                Console.WriteLine($"The winner is {winner.Name()}!");
            }
        }
Example #9
0
        /// Play one round as this player.
        public void PlayOneMove(Board board)
        {
            Console.WriteLine($"Turn: {_disk.Name()}");
            var moves = board.PossibleMoves(_disk);

            if (moves.Any())
            {
                _canPlay = true;
                if (_human && _showHelpers)
                {
                    board.PrintMoves(moves);
                }
                var chosenMove = _human ? GetHumanMove(moves) : GetComputerMove(moves);
                board.PlaceDisc(chosenMove);
                board.PrintScore();
                ++_roundsPlayed;
            }
            else
            {
                _canPlay = false;
                ColorPrint.Write("  No moves available...", Color.Yellow);
            }
        }
Example #10
0
 public static string Name(this Disk disk)
 {
     return(ColorPrint.Get(disk.ToString().ToUpper(), disk.DiskColor()));
 }