示例#1
0
 public bool IsLegalPlayerBearOffMove(int fromIndex, int cubeNumber, Player player, GameBoard board)
 {
     if (player.Color.Equals("White"))
     {
         if (fromIndex + cubeNumber == 25)
         {
             return(true);
         }
         if (IsCheckerLeft(board, player, fromIndex, cubeNumber))
         {
             return(true);
         }
     }
     else
     {
         if (fromIndex - cubeNumber == 0)
         {
             return(true);
         }
         if (IsCheckerLeft(board, player, fromIndex, cubeNumber))
         {
             return(true);
         }
     }
     return(false);
 }
示例#2
0
        public IEnumerable <KeyValuePair <int, int> > AvailableMoves(GameBoard gameBoard, Dice dice, Player player)
        {
            List <KeyValuePair <int, int> > ListOfMoves = new List <KeyValuePair <int, int> >();

            if (player.Color.Equals("White"))
            {
                if (gameBoard.Fields[26].WhiteCheckers == 0)
                {
                    for (int i = 1; i < gameBoard.Fields.Length; i++)
                    {
                        if (!dice.RolledDouble)
                        {
                            if (i + dice.FirstCube <= 24 && dice.FirstCube != 0)
                            {
                                if (IsPlayerCanMove(gameBoard, i, player) && IsFieldAvailable(gameBoard, i, i + dice.FirstCube, dice.FirstCube, player))
                                {
                                    ListOfMoves.Add(new KeyValuePair <int, int>(i, i + dice.FirstCube));
                                }
                            }
                        }
                        if (i + dice.SecondCube <= 24 && dice.SecondCube != 0)
                        {
                            if (IsPlayerCanMove(gameBoard, i, player) && IsFieldAvailable(gameBoard, i, i + dice.SecondCube, dice.SecondCube, player))
                            {
                                ListOfMoves.Add(new KeyValuePair <int, int>(i, i + dice.SecondCube));
                            }
                        }
                    }
                    return(ListOfMoves);
                }
                else
                {
                    return(AvailableMovesFromCenter(gameBoard, dice, player));
                }
            }
            else
            {
                if (gameBoard.Fields[26].RedCheckers == 0)
                {
                    for (int i = 1; i < gameBoard.Fields.Length; i++)
                    {
                        if (!dice.RolledDouble)
                        {
                            if (i - dice.FirstCube >= 1 && dice.FirstCube != 0)
                            {
                                if (IsPlayerCanMove(gameBoard, i, player) && IsFieldAvailable(gameBoard, i, i - dice.FirstCube, dice.FirstCube, player))
                                {
                                    ListOfMoves.Add(new KeyValuePair <int, int>(i, i - dice.FirstCube));
                                }
                            }
                        }
                        if (i - dice.SecondCube >= 1 && dice.SecondCube != 0)
                        {
                            if (IsPlayerCanMove(gameBoard, i, player) && IsFieldAvailable(gameBoard, i, i - dice.SecondCube, dice.SecondCube, player))
                            {
                                ListOfMoves.Add(new KeyValuePair <int, int>(i, i - dice.SecondCube));
                            }
                        }
                    }
                    return(ListOfMoves);
                }
                else
                {
                    return(AvailableMovesFromCenter(gameBoard, dice, player));
                }
            }
        }
        public void DrawBoard(GameBoard board)
        {
            bool center  = true;
            bool outZone = true;

            Console.Clear();
            _fieldsToDraw = new Field[board.Fields.Length];

            for (int i = 0; i < board.Fields.Length; i++)
            {
                if (board.Fields[i].RedCheckers > 0)
                {
                    _fieldsToDraw[i]             = new Field();
                    _fieldsToDraw[i].RedCheckers = board.Fields[i].RedCheckers;
                }
                else if (board.Fields[i].WhiteCheckers > 0)
                {
                    _fieldsToDraw[i] = new Field();
                    _fieldsToDraw[i].WhiteCheckers = board.Fields[i].WhiteCheckers;
                }
                else
                {
                    _fieldsToDraw[i] = new Field();
                }
            }

            // Header
            for (int i = 13; i < 26; i++)
            {
                if (center && i == 19)
                {
                    Console.Write("| " + i + " ");
                    center = false;
                }
                else if (outZone && i == 25)
                {
                    Console.Write("| ");
                    Console.Write("25 | ");
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.Write("Checkers = " + board.Fields[i].WhiteCheckers + " ");
                    Console.ResetColor();
                    outZone = false;
                }
                else
                {
                    Console.Write(i + " ");
                }
            }

            //Upper field
            for (int i = 1; i < 6; i++)
            {
                Console.WriteLine("");
                center  = true;
                outZone = true;
                for (int j = 13; j < 26; j++)
                {
                    if (center && j == 19)
                    {
                        Console.Write("| ");
                        center = false;
                    }

                    if (outZone && j == 25)
                    {
                        Console.Write("| ");
                        outZone = false;
                    }

                    if (_fieldsToDraw[j].WhiteCheckers > 0)
                    {
                        Console.ForegroundColor = ConsoleColor.White;
                        Console.Write("x");
                        Console.ResetColor();
                        _fieldsToDraw[j].WhiteCheckers--;
                    }
                    else if (_fieldsToDraw[j].RedCheckers > 0)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Write("o");
                        Console.ResetColor();
                        _fieldsToDraw[j].RedCheckers--;
                    }
                    else
                    {
                        Console.Write(" ");
                    }
                    Console.Write("  ");
                }
            }
            Console.WriteLine("");

            //lower field
            for (int i = 5; i > 0; i--)
            {
                Console.WriteLine("");
                center  = true;
                outZone = true;

                for (int j = 12; j >= 0; j--)
                {
                    if (center && j == 6)
                    {
                        Console.Write("| ");
                        center = false;
                    }
                    if (outZone && j == 0)
                    {
                        Console.Write("| ");
                        outZone = false;
                    }
                    if (_fieldsToDraw[j].WhiteCheckers >= i)
                    {
                        Console.ForegroundColor = ConsoleColor.White;
                        Console.Write("x");
                        Console.ResetColor();
                        _fieldsToDraw[j].WhiteCheckers--;
                    }
                    else if (_fieldsToDraw[j].RedCheckers >= i)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Write("o");
                        Console.ResetColor();
                        _fieldsToDraw[j].RedCheckers--;
                    }
                    else
                    {
                        Console.Write(" ");
                    }
                    Console.Write("  ");
                }
            }
            Console.WriteLine("");

            //footer
            center  = true;
            outZone = true;
            for (int i = 12; i >= 0; i--)
            {
                if (center && i == 6)
                {
                    Console.Write("| ");
                    center = false;
                }
                else if (outZone && i == 0)
                {
                    Console.Write("| ");
                    Console.Write("00 | ");
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write("Checkers = " + board.Fields[i].RedCheckers + " ");
                    Console.ResetColor();
                    outZone = false;
                }
                if (i < 10 && i != 0)
                {
                    Console.Write("0" + i + " ");
                }
                else
                {
                    if (i != 0)
                    {
                        Console.Write(i + " ");
                    }
                }
            }
            Console.WriteLine("");


            if (board.Fields[26].WhiteCheckers > 0)
            {
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write("White ");
                Console.ResetColor();
                Console.ForegroundColor = ConsoleColor.DarkGreen;
                Console.WriteLine($"Checkers in Bar: {board.Fields[26].WhiteCheckers}");
                Console.ResetColor();
            }

            if (board.Fields[26].RedCheckers > 0)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Write("Red ");
                Console.ResetColor();
                Console.ForegroundColor = ConsoleColor.DarkGreen;
                Console.WriteLine($"Checkers in Bar: {board.Fields[26].RedCheckers}");
                Console.ResetColor();
            }
        }
 public void DrawWrongMove(Player player, GameBoard board)
 {
     DrawBoard(board);
     Console.WriteLine($"{player.Name} you can't move there!");
 }
        public int[] DrawAskToMove(Player player, Dice dice, GameBoard board)
        {
            DrawBoard(board);
            bool legalMoveFrom = false;
            bool legalMoveTo   = false;

            int[] fromTo = new int[2];

            if (dice.RolledDouble)
            {
                Console.WriteLine("Double!");
                Console.WriteLine($"Throw result: {dice.FirstCube} {dice.SecondCube}");
            }
            else
            {
                Console.WriteLine($"Throw result: {dice.FirstCube} {dice.SecondCube}");
            }

            Console.WriteLine($"{player.Color}: {player.Name} Make your move");
            Console.WriteLine($"Moves left: {player.Moves}");

            if (player.Color.Equals("White") && board.Fields[26].WhiteCheckers > 0 && player.IsMyTurn)
            {
                Console.WriteLine("Move from bar!");
            }
            else if (player.Color.Equals("Red") && board.Fields[26].RedCheckers > 0 && player.IsMyTurn)
            {
                Console.WriteLine("Move from bar!");
                fromTo[0] = 25;
            }
            else
            {
                Console.WriteLine("From:");
                while (!legalMoveFrom)
                {
                    if (!int.TryParse(Console.ReadLine(), out fromTo[0]))
                    {
                        Console.WriteLine("Must be a number!");
                    }
                    else if (fromTo[0] < 0 || fromTo[0] > 25)
                    {
                        Console.WriteLine("Field does not exist!");
                    }
                    else
                    {
                        legalMoveFrom = true;
                    }
                }
            }


            Console.WriteLine("To:");
            while (!legalMoveTo)
            {
                if (!int.TryParse(Console.ReadLine(), out fromTo[1]))
                {
                    Console.WriteLine("Must be a number!");
                }
                else if (fromTo[1] < 0 || fromTo[1] > 25)
                {
                    Console.WriteLine("Field does not exist!");
                }
                else
                {
                    legalMoveTo = true;
                }
            }
            return(fromTo);
        }