Example #1
0
        public void onButtonPressed(object sender, EventArgs e) //Chess board button pressed
        {
            Button buttonClicked = (Button)sender;

            int locX = (int)Math.Floor(buttonClicked.Location.X / 50f); //Get pressed button x and y coords
            int locY = (int)Math.Floor(buttonClicked.Location.Y / 50f);

            if (selectedX == -1 && selectedY == -1) //First selection
            {
                if (chessBoardNodeArray[locX, locY].chessPiece != null && chessBoardNodeArray[locX, locY].chessPiece.pieceColor == moveColor)
                {
                    availableMoves.Clear();

                    selectedX = locX;
                    selectedY = locY;

                    chessBoardNodeArray[locX, locY].ChangeColor(ChessBoardNodeColor.Selected);
                    availableMoves = GetMoves();
                    ShowMoves(availableMoves);
                }
            }
            else //Pressed after seleceting a piece, move a piece
            {
                AvailableMove pressedMove = PressedOnAvailableMove(chessBoardNodeArray[locX, locY]);
                if (pressedMove.node != null)
                {
                    ChangePieceLocation(selectedX, selectedY, pressedMove);
                }

                SoftClearBoard();
            }
        }
Example #2
0
 protected bool AddMove(ChessBoardNode[,] board, int newX, int newY, List <AvailableMove> moveList, MoveType moveType = MoveType.MoveAndAttack)
 {
     if (newX < 8 && newX >= 0 && newY < 8 && newY >= 0)
     {
         AvailableMove newMove = new AvailableMove(board[newX, newY], moveType);
         if (board[newX, newY].chessPiece == null)
         {
             if (moveType == MoveType.OnlyAttack)
             {
                 return(false);
             }
             else
             {
                 moveList.Add(newMove);
                 return(true);
             }
         }
         else
         {
             if (board[newX, newY].chessPiece.pieceColor == pieceColor)
             {
                 return(false);
             }
             else if (moveType != MoveType.OnlyMove)
             {
                 moveList.Add(newMove);
                 return(false);
             }
             else
             {
                 return(false);
             }
         }
     }
     else
     {
         return(false);
     }
 }
Example #3
0
 private AvailableMove selectMove(List<AvailableMove> allMovesList, Piece[,] board, ref bool mate, string colour)
 {
     AvailableMove move = new AvailableMove();
     if (allMovesList.Count > 0)
     {
         var maxRating = allMovesList.Max(x => x.Rating);
         move = allMovesList.Where(x => x.Rating == maxRating).First();
     }
     else if (cf.isThreatened(uf.FindKing(colour, board), uf.OtherColour(colour), board))
     {
         df.Checkmate(colour);
         mate = true;
     }
     else
     {
         df.Stalemate();
         mate = true;
     }
     return move;
 }
Example #4
0
 private Piece[,] makeTheoreticalMove(AvailableMove move, Piece[,] board)
 {
     //clone board
     Piece[,] tmpBoard = new Piece[8, 8];
     for (int row = 0; row < 8; row++)
         for (int col = 0; col < 8; col++)
             tmpBoard[row, col] = new Piece(board[row, col]);
     //make theoretical move
     tmpBoard[move.Move[0], move.Move[1]] = new Piece(move.Piece);
     tmpBoard[tmpBoard[move.Move[0], move.Move[1]].Row, tmpBoard[move.Move[0], move.Move[1]].Col] = new Piece();
     tmpBoard[move.Move[0], move.Move[1]].Row = move.Move[0];
     tmpBoard[move.Move[0], move.Move[1]].Col = move.Move[1];
     return tmpBoard;
 }
Example #5
0
 private void makeMove(AvailableMove move, Piece[,] board, Piece[,] WhiteTaken, Piece[,] BlackTaken, int[] whiteTakenIndeces, int[] blackTakenIndeces)
 {
     mf.movePiece(move.Piece, board, WhiteTaken, BlackTaken, whiteTakenIndeces, blackTakenIndeces, new int[] { move.Piece.Row, move.Piece.Col }, move.Move[0], move.Move[1]);
 }
Example #6
0
        public void ChangePieceLocation(int fromX, int fromY, AvailableMove moveTo) //Move piece
        {
            switch (moveTo.moveType)
            {
            case MoveType.Castling:
                if (moveColor == ChessPieceColor.White) //White castles
                {
                    if (moveTo.node.locationX == 6)     //Short castles
                    {
                        moveTo.node.ChangePiece(chessBoardNodeArray[4, 0].chessPiece);
                        chessBoardNodeArray[fromX, fromY].ChangePiece(null);
                        moveTo.node.chessPiece.moved = true;

                        chessBoardNodeArray[5, 0].ChangePiece(chessBoardNodeArray[7, 0].chessPiece);
                        chessBoardNodeArray[7, 0].ChangePiece(null);
                        chessBoardNodeArray[5, 0].chessPiece.moved = true;
                    }
                    else     //Long castles
                    {
                        moveTo.node.ChangePiece(chessBoardNodeArray[4, 0].chessPiece);
                        chessBoardNodeArray[fromX, fromY].ChangePiece(null);
                        moveTo.node.chessPiece.moved = true;

                        chessBoardNodeArray[3, 0].ChangePiece(chessBoardNodeArray[0, 0].chessPiece);
                        chessBoardNodeArray[0, 0].ChangePiece(null);
                        chessBoardNodeArray[3, 0].chessPiece.moved = true;
                    }
                }
                else                                //Black castles
                {
                    if (moveTo.node.locationX == 6) //Short castles
                    {
                    }
                    else
                    {
                    }
                }
                break;

            default:
                Console.WriteLine(fromY + "ssss");
                Console.WriteLine(chessBoardNodeArray[fromX, fromY].chessPiece + "adad");
                moveTo.node.ChangePiece(chessBoardNodeArray[fromX, fromY].chessPiece);

                chessBoardNodeArray[fromX, fromY].ChangePiece(null);
                moveTo.node.chessPiece.moved = true;
                break;
            }

            //Change current moving team color
            if (moveColor == ChessPieceColor.White)
            {
                moveColor = ChessPieceColor.Black;
            }
            else
            {
                moveColor = ChessPieceColor.White;
            }
            MoveLabel.Text = moveColor.ToString();

            SoftClearBoard();
        }
Example #7
0
 public void AddChildren(MoveNode node, AvailableMove childMove, Piece[,] board)
 {
     MoveNode child = new MoveNode(childMove, board);
     child.Parent = node;
     node.Children.Add(child);
 }
Example #8
0
 public MoveNode(AvailableMove move, Piece[,] board)
 {
     Children = new List<MoveNode>();
     Move = move;
     Board = board;
 }
Example #9
0
 public MoveNode(Piece[,] board)
 {
     Move = new AvailableMove();
     Children = new List<MoveNode>();
     Board = board;
 }
Example #10
0
 public MoveNode()
 {
     Move = new AvailableMove();
     Children = new List<MoveNode>();
 }