Exemplo n.º 1
0
        /*********************************************************************
        * IsCheckMate
        * Loop through the board and for each piece we check if they can kill
        * the king if they can we end and return true.
        *********************************************************************/
        public bool IsCheckMate()
        {
            for (int r = 0; r < 8; r++)
            {
                for (int c = 0; c < 8; c++)
                {
                    Piece piece = board[r, c].GetPiece();
                    if (piece != null && piece.GetTeam() != turn && piece.GetPieceType() == "King")
                    {
                        Piece king   = board[r, c].GetPiece();
                        Piece killer = null;
                        king.CalcPossMoves(board);
                        bool Check            = false;
                        int  checkCount       = 0;
                        int  vulnerableChecks = 0;
                        if (IsCheck(r, c, ref killer))
                        {
                            Check = true;
                            checkCount++;
                            if (IsKillable(killer.GetRow(), killer.GetColumn(), turn))
                            {
                                vulnerableChecks++;
                            }
                        }
                        if (Check)
                        {
                            for (int i = 0; i < king.GetPossMoves().Count; i++)
                            {
                                if (IsCheck(king.GetPossMoves()[i].row, king.GetPossMoves()[i].col, ref killer))
                                {
                                    checkCount++;
                                    if (IsKillable(killer.GetRow(), killer.GetColumn(), turn))
                                    {
                                        vulnerableChecks++;
                                    }
                                }
                            }
                        }
                        if (checkCount > 0 && checkCount == king.GetPossMoves().Count)
                        {
                            checkText.Text = "CHECKMATE";
                            return(true);
                        }
                        else if (checkCount == 0 || checkCount == vulnerableChecks)
                        {
                            checkText.Text = "";
                        }
                        else if (Check)
                        {
                            MessageBox.Show("king is checked");
                            checkText.Text = "CHECK";
                        }
                    }
                }
            }

            return(false);
        }
Exemplo n.º 2
0
 /*********************************************************************
 * IsOtherTeam
 * Parameters
 *   piece - the piece we are checking
 * Check to see if the piece is an enemy piece or not
 *********************************************************************/
 public bool IsOtherTeam(Piece piece)
 {
     if (piece != null)
     {
         if (piece.GetTeam() != this.team)
         {
             return(true);
         }
     }
     return(false);
 }
Exemplo n.º 3
0
 /*********************************************************************
 * IsCheck
 * Loop through the board and for each piece we check if they can kill
 * the king if they can we end and return true.
 *********************************************************************/
 public bool IsCheck(int row, int col, ref Piece killer)
 {
     for (int r = 0; r < 8; r++)
     {
         for (int c = 0; c < 8; c++)
         {
             killer = board[r, c].GetPiece();
             if (killer != null && killer.GetTeam() == turn)
             {
                 killer.CalcPossMoves(board);
                 if (killer.IsPossible(row, col))
                 {
                     return(true);
                 }
             }
         }
     }
     return(false);
 }
Exemplo n.º 4
0
        private void SetForegroundColor(string color, Piece piece = null)
        {
            color = color.ToLower();
            bool doesNotMatch = false;

            switch (color)
            {
            case "white":
                Console.ForegroundColor = ConsoleColor.White;
                break;

            case "black":
                Console.ForegroundColor = ConsoleColor.Black;
                break;

            case "darkblue":
                Console.ForegroundColor = ConsoleColor.DarkBlue;
                break;

            case "darkgreen":
                Console.ForegroundColor = ConsoleColor.DarkGreen;
                break;

            case "darkcyan":
                Console.ForegroundColor = ConsoleColor.DarkCyan;
                break;

            case "darkred":
                Console.ForegroundColor = ConsoleColor.DarkRed;
                break;

            case "darkmagenta":
                Console.ForegroundColor = ConsoleColor.DarkMagenta;
                break;

            case "darkyellow":
                Console.ForegroundColor = ConsoleColor.DarkYellow;
                break;

            case "darkgray":
                Console.ForegroundColor = ConsoleColor.DarkGray;
                break;

            case "gray":
                Console.ForegroundColor = ConsoleColor.Gray;
                break;

            case  "blue":
                Console.ForegroundColor = ConsoleColor.Blue;
                break;

            case "green":
                Console.ForegroundColor = ConsoleColor.Green;
                break;

            case "cyan":
                Console.ForegroundColor = ConsoleColor.Cyan;
                break;

            case "red":
                Console.ForegroundColor = ConsoleColor.Red;
                break;

            case "magenta":
                Console.ForegroundColor = ConsoleColor.Magenta;
                break;

            case "yellow":
                Console.ForegroundColor = ConsoleColor.Yellow;
                break;

            default:
                doesNotMatch = true;
                break;
            }

            if (doesNotMatch)
            {
                if (piece != null)
                {
                    switch (piece.GetTeam())
                    {
                    case 1:
                        Console.ForegroundColor = ConsoleColor.Red;
                        break;

                    case 2:
                        Console.ForegroundColor = ConsoleColor.Blue;
                        break;
                    }
                }
            }
        }