예제 #1
0
 public virtual bool IsValidMove(BoardSquare[,] board, int fromRow, int fromCol, int toRow, int toCol, ChessColors turn, int turnNumber)
 {
     if (!(InRange(fromRow) && InRange(fromCol) && InRange(toRow) && InRange(toCol)))
         return false;
     else if (fromRow == toRow && fromCol == toCol)
         return false;
     else
         return true;
 }
예제 #2
0
        private void InitBoard()
        {
            toSq = null;
            fromSq = null;
            board = new BoardSquare[8, 8];

            int width = panelChessBoard.Width/8;
            int height = panelChessBoard.Height/8;

            for (int row=0; row < 8; row++)
                for (int col = 0; col < 8; col++)
                {
                    board[row, col] = new BoardSquare(row, col, width, height);
                    panelChessBoard.Controls.Add(board[row, col]);
                    board[row, col].Click += new System.EventHandler(Square_Click);
                }
            
            board[0, 0].ChessPiece = new Rook(ChessColors.Black);
            board[0, 7].ChessPiece = new Rook(ChessColors.Black);

            board[7, 0].ChessPiece = new Rook(ChessColors.White);
            board[7, 7].ChessPiece = new Rook(ChessColors.White);

            board[0, 2].ChessPiece = new Bishop(ChessColors.Black);
            board[0, 5].ChessPiece = new Bishop(ChessColors.Black);

            board[7, 2].ChessPiece = new Bishop(ChessColors.White);
            board[7, 5].ChessPiece = new Bishop(ChessColors.White);

            board[0, 1].ChessPiece = new Knight(ChessColors.Black);
            board[0, 6].ChessPiece = new Knight(ChessColors.Black);

            board[7, 1].ChessPiece = new Knight(ChessColors.White);
            board[7, 6].ChessPiece = new Knight(ChessColors.White);

            board[0, 3].ChessPiece = new King(ChessColors.Black);
            board[7, 4].ChessPiece = new King(ChessColors.White);

            board[0, 4].ChessPiece = new Queen(ChessColors.Black);
            board[7, 3].ChessPiece = new Queen(ChessColors.White);

            
            for (int i = 0; i <= 7; i++)
                board[1, i].ChessPiece = new Pawn(ChessColors.Black);
            for (int i = 0; i <= 7; i++)
                board[6, i].ChessPiece = new Pawn(ChessColors.White);

            turn = ChessColors.White;
            label2.Text = turn.ToString();
            label2.Refresh();
            //board[0, 0].Image = (new Icon(Properties.Resources.Black_Rook, 48, 48)).ToBitmap();
        }
예제 #3
0
파일: Pawn.cs 프로젝트: PratikDhanave/Chess
        public override bool IsValidMove(BoardSquare[,] board, int fromRow, int fromCol, int toRow, int toCol, ChessColors turn, int turnNumber)
        {
            if (!base.IsValidMove(board, fromRow, fromCol, toRow, toCol, turn, turnNumber))
                return false;
            if (turn == ChessColors.Black)
            {
                if (fromRow > toRow)
                    return false;
            }
            if (turn == ChessColors.White)
            {
                if (fromRow < toRow)
                    return false;
            }

            if (board[toRow, toCol].ChessPiece == null)
            {
                if (Math.Abs(fromRow - toRow) == 1 && fromCol == toCol)
                    return true;
                //en passant implemented here
                else if (Math.Abs(fromRow - toRow) == 1 && Math.Abs(toCol - fromCol) == 1)
                {
                    if (board[fromRow, toCol].ChessPiece.Name == ChessPieces.Pawn &&board[fromRow, toCol].ChessPiece.PieceColor != turn && board[fromRow, toCol].ChessPiece.LastMove == turnNumber - 1)
                        return true;
                    else
                        return false;
                }
                else if (Math.Abs(fromRow - toRow) == 2 && toCol == fromCol)
                {
                    if (board[fromRow,fromCol].ChessPiece.LastMove == 0)
                        return true;
                    else
                        return false;
                }
                else
                    return false;
            }
            else if (board[toRow, toCol].ChessPiece != null)
            {
                if (board[toRow, toCol].ChessPiece.PieceColor != turn && Math.Abs(fromRow - toRow) == 1 && Math.Abs(toCol-fromCol) == 1)
                    return true;
                else
                    return false;
            }
            return false;
            
                    
        }
예제 #4
0
파일: Knight.cs 프로젝트: B-Rich/Chess
	public override bool IsValidMove(BoardSquare[,] board, int fromRow, int fromCol, int toRow, int toCol, ChessColors turn, int turnNumber)
        { 
        	if (!base.IsValidMove(board, fromRow, fromCol, toRow, toCol, turn, turnNumber))
                	return false;
        	if (Math.Abs(toRow-fromRow) > 2 || Math.Abs(toCol-fromCol) > 2 || toRow == fromRow || toCol == fromCol)
                	return false;
        	else if (Math.Abs(toRow-fromRow) == 2 && Math.Abs(toCol-fromCol) == 1)
		    {
            		if (board[toRow,toCol].ChessPiece == null)
                    {
                        
                        return true;

                    }	
			        else if(board[toRow,toCol].ChessPiece.PieceColor != turn)
                    {
                        
                        return true;

                    }
           	 	    else
                		return false;
		}	
        	else if(Math.Abs(toRow-fromRow) == 1 && Math.Abs(toCol-fromCol) == 2)
            {
			    if (board[toRow,toCol].ChessPiece == null)
                {
                    
                    return true;

                }
			    else if(board[toRow,toCol].ChessPiece.PieceColor != turn)
                {
                  
                    return true;

                }
           	 	else
                	return false;
		}
        	else 
	        	return false;
	


}
예제 #5
0
파일: Bishop.cs 프로젝트: B-Rich/Chess
        public override bool IsValidMove(BoardSquare[,] board, int fromRow, int fromCol, int toRow, int toCol, ChessColors turn, int turnNumber)
        {
            if (!base.IsValidMove(board, fromRow, fromCol, toRow, toCol, turn, turnNumber))
                return false;
            if (toCol == fromCol || toRow == fromRow)
                return false;
            if (Math.Abs(fromRow - toRow) == Math.Abs(fromCol - toCol))
            {
		
                for (int i = 1; i < Math.Abs(fromRow-toRow); i++)
                {
			        if (fromRow> toRow && fromCol>toCol)
			        {
				        if (board[fromRow-i,fromCol-i].ChessPiece != null)
					        return false;
			        }
			        if (fromRow> toRow && fromCol<toCol)
			        {
			            if (board[fromRow-i,fromCol+i].ChessPiece != null)
					        return false;
			        }
			        if (fromRow<toRow && fromCol<toCol)
			        {
			            if (board[fromRow+i,fromCol+i].ChessPiece != null)
			                return false;
			        }
			        if (fromRow< toRow && fromCol>toCol)
			        {   
				        if (board[fromRow+i,fromCol-i].ChessPiece != null)
					        return false;
			        }
                    
		        }
                if (board[toRow, toCol].ChessPiece == null)
                    return true;
                else if (board[toRow, toCol].ChessPiece.PieceColor != turn)
                    return true;
                else
                    return false;
               
            }
	        else
            	return false;
            //if absolute vaalue of (fromRow - toRow) == absolute value of (toRow - toCol) Math.Abs(----) 
        }
예제 #6
0
        private void initField()
        {
            for (int x = 0; x < 8; x++)
            {
                for (int y = 0; y < 8; y++)
                {
                    field [x, y] = new BoardSquare(fieldStartSquare.transform.position + new Vector3(x * squareSize, 0, y * squareSize));
                }
            }

            //Pawns
            for (int x = 0; x < 8; x++)
            {
                field [x, 1].piece = Instantiate(pawn, field [x, 1].position, Quaternion.Euler(whiteRot)).GetComponent <ChessPiece> ();
                field [x, 1].piece.setMaterial(whiteMat);
                field [x, 6].piece = Instantiate(pawn, field [x, 6].position, Quaternion.Euler(blackRot)).GetComponent <ChessPiece> ();
                field [x, 6].piece.setMaterial(blackMat);
            }

            //Rooks
            field [0, 0].piece = Instantiate(rook, field [0, 0].position, Quaternion.identity).GetComponent <ChessPiece> ().init(whiteMat);
            field [7, 0].piece = Instantiate(rook, field [7, 0].position, Quaternion.identity).GetComponent <ChessPiece> ().init(whiteMat);
            field [0, 7].piece = Instantiate(rook, field [0, 7].position, Quaternion.identity).GetComponent <ChessPiece> ().init(blackMat);
            field [7, 7].piece = Instantiate(rook, field [7, 7].position, Quaternion.identity).GetComponent <ChessPiece> ().init(blackMat);

            //Knights
            field [1, 0].piece = Instantiate(knight, field [1, 0].position, Quaternion.identity).GetComponent <ChessPiece> ().init(whiteMat);
            field [6, 0].piece = Instantiate(knight, field [6, 0].position, Quaternion.identity).GetComponent <ChessPiece> ().init(whiteMat);
            field [1, 7].piece = Instantiate(knight, field [1, 7].position, Quaternion.identity).GetComponent <ChessPiece> ().init(blackMat);
            field [6, 7].piece = Instantiate(knight, field [6, 7].position, Quaternion.identity).GetComponent <ChessPiece> ().init(blackMat);

            //Bishops
            field [2, 0].piece = Instantiate(bishop, field [2, 0].position, Quaternion.Euler(whiteRot)).GetComponent <ChessPiece> ().init(whiteMat);
            field [5, 0].piece = Instantiate(bishop, field [5, 0].position, Quaternion.Euler(whiteRot)).GetComponent <ChessPiece> ().init(whiteMat);
            field [2, 7].piece = Instantiate(bishop, field [2, 7].position, Quaternion.Euler(blackRot)).GetComponent <ChessPiece> ().init(blackMat);
            field [5, 7].piece = Instantiate(bishop, field [5, 7].position, Quaternion.Euler(blackRot)).GetComponent <ChessPiece> ().init(blackMat);

            //Queens
            field [3, 0].piece = Instantiate(queen, field [3, 0].position, Quaternion.identity).GetComponent <ChessPiece> ().init(whiteMat);
            field [3, 7].piece = Instantiate(queen, field [3, 7].position, Quaternion.identity).GetComponent <ChessPiece> ().init(blackMat);

            //Kings
            field [4, 0].piece = Instantiate(king, field [4, 0].position, Quaternion.identity).GetComponent <ChessPiece> ().init(whiteMat);
            field [4, 7].piece = Instantiate(king, field [4, 7].position, Quaternion.identity).GetComponent <ChessPiece> ().init(blackMat);
        }
예제 #7
0
파일: Rook.cs 프로젝트: PratikDhanave/Chess
        public override bool IsValidMove(BoardSquare[,] board, int fromRow, int fromCol, int toRow, int toCol, ChessColors turn, int turnNumber)
        {
            if (!base.IsValidMove(board, fromRow, fromCol, toRow, toCol, turn, turnNumber))
                return false;
            if (fromRow != toRow && fromCol != toCol)
                return false;
            //horiz or vert?
            if (fromRow == toRow ) 
            {
                    if (Math.Abs(fromCol - toCol) == 1)
                    {
                        if (board[toRow, toCol].ChessPiece == null)
                            return true;
                        else if (board[toRow, toCol].ChessPiece.PieceColor != turn)
                            return true;
                        else
                            return false;
                    }


                    for (int i = 1; i <= Math.Abs(toCol - fromCol) - 1; i++)
                    {
                        if (fromCol > toCol)
                        {
                            if (board[fromRow, fromCol - i].ChessPiece != null)
                                return false;
                        }
                        if (fromCol < toCol)
                        {
                            if (board[fromRow, fromCol+i].ChessPiece != null)
                                return false;
                        }
                    }
                   
                }
                if (fromCol == toCol)
                {
                    if (Math.Abs(fromRow - toRow) == 1)
                    {
                        if (board[toRow, toCol].ChessPiece == null)
                            return true;
                        else if (board[toRow, toCol].ChessPiece.PieceColor != turn)
                            return true;
                        else
                            return false;
                    }
                    for (int i = 1; i <= Math.Abs(toRow - fromRow) - 1; i++)
                    {
                        if (fromRow > toRow)
                        {
                            if (board[fromRow-i, fromCol].ChessPiece != null)
                                return false;
                        }
                        if (fromCol < toCol)
                        {
                            if (board[fromRow+i, fromCol].ChessPiece != null)
                                return false;
                        }
                    }
                    
                }
                if (board[toRow, toCol].ChessPiece == null)
                    return true;
                else if (board[toRow, toCol].ChessPiece.PieceColor != turn)
                    return true;
                else
                    return false;
            //to Empty or OppColor
            //Math.Sign(----) +1, -1, 0
            
            
            //Castling
            //check?
            //1. Make move
            //2. Ask each opposing piece if it can move to king location
            //3. if all no --> commit , else --> undo
        }
예제 #8
0
파일: Queen.cs 프로젝트: B-Rich/Chess
        public override bool IsValidMove(BoardSquare[,] board, int fromRow, int fromCol, int toRow, int toCol, ChessColors turn, int turnNumber)
        {
            if (!base.IsValidMove(board, fromRow, fromCol, toRow, toCol, turn, turnNumber))
                return false;
            if (fromRow != toRow && fromCol != toCol && Math.Abs(fromRow - toRow) != Math.Abs(fromCol - toCol))
                return false;
            if (fromRow == toRow)
            {
                if (Math.Abs(fromCol - toCol) == 1)
                {
                    if (board[toRow, toCol].ChessPiece == null)
                        return true;
                    else if (board[toRow, toCol].ChessPiece.PieceColor != turn)
                        return true;
                    else
                        return false;
                }


                for (int i = 1; i <= Math.Abs(toCol - fromCol) - 1; i++)
                {
                    if (fromCol > toCol)
                    {
                        if (board[fromRow, fromCol - i].ChessPiece != null)
                            return false;
                    }
                    if (fromCol < toCol)
                    {
                        if (board[fromRow, fromCol + i].ChessPiece != null)
                            return false;
                    }
                }

            }
            if (fromCol == toCol)
            {
                if (Math.Abs(fromRow - toRow) == 1)
                {
                    if (board[toRow, toCol].ChessPiece == null)
                        return true;
                    else if (board[toRow, toCol].ChessPiece.PieceColor != turn)
                        return true;
                    else
                        return false;
                }
                for (int i = 1; i <= Math.Abs(toRow - fromRow) - 1; i++)
                {
                    if (fromRow > toRow)
                    {
                        if (board[fromRow - i, fromCol].ChessPiece != null)
                            return false;
                    }
                    if (fromRow < toRow)
                    {
                        if (board[fromRow + i, fromCol].ChessPiece != null)
                            return false;
                    }
                }

            }
            if (board[toRow, toCol].ChessPiece == null)
                return true;
            else if (board[toRow, toCol].ChessPiece.PieceColor != turn)
                return true;
            
            if (Math.Abs(fromRow - toRow) == Math.Abs(fromCol - toCol))
            {

                for (int i = 1; i < Math.Abs(fromRow - toRow); i++)
                {
                    if (fromRow > toRow && fromCol > toCol)
                    {
                        if (board[fromRow - i, fromCol - i].ChessPiece != null)
                            return false;
                    }
                    if (fromRow > toRow && fromCol < toCol)
                    {
                        if (board[fromRow - i, fromCol + i].ChessPiece != null)
                            return false;
                    }
                    if (fromRow < toRow && fromCol < toCol)
                    {
                        if (board[fromRow + i, fromCol + i].ChessPiece != null)
                            return false;
                    }
                    if (fromRow < toRow && fromCol > toCol)
                    {
                        if (board[fromRow + i, fromCol - i].ChessPiece != null)
                            return false;
                    }

                }
                if (board[toRow, toCol].ChessPiece == null)
                    return true;
                else if (board[toRow, toCol].ChessPiece.PieceColor != turn)
                    return true;
                else
                    return false;

            }
            else
                return false;
        }
예제 #9
0
        private void Square_Click(object sender, EventArgs e)
        {
            BoardSquare sq = (BoardSquare)sender;

            //MessageBox.Show( string.Format("[{0},{1}]", sq.Row, sq.Col ) );
            if (sq.ChessPiece == null || sq.ChessPiece.PieceColor != turn)
            {
                // Deselect the previous selection?
                if (toSq != null && toSq != sq)
                    toSq.IsSelected = false;

                if (!sq.IsSelected)
                {
                    sq.IsSelected = true;
                    toSq = sq;
                }
                else
                {
                    sq.IsSelected = false;
                    toSq = null;
                }
            }
            else if (sq.ChessPiece.PieceColor == turn)
            {
                if (fromSq != null && fromSq != sq)
                    fromSq.IsSelected = false;

                if (!sq.IsSelected)
                {
                    sq.IsSelected = true;
                    fromSq = sq;
                }
                else
                {
                    sq.IsSelected = false;
                    fromSq = null;
                }
            }
        }