private void piece_MouseDown(object sender, MouseEventArgs e) { //FROM if (!bGameStarted) return; CurrentPiece = null; ReplacedPiece = null; DestinationTile = null; var img = ((PictureBox)sender).Image; if (img == null) return; CurrentPiece = (GamePiece)sender; if ((CurrentPiece.PieceState == GridEntry.BLACKPAWN && _GameTurn.GameState == GameState.BLACKTURN || CurrentPiece.PieceState == GridEntry.WHITEPAWN && _GameTurn.GameState == GameState.WHITETURN || CurrentPiece.PieceState == GridEntry.BLACKKING && _GameTurn.GameState == GameState.BLACKTURN || CurrentPiece.PieceState == GridEntry.WHITEKING && _GameTurn.GameState == GameState.WHITETURN) == false) return; if (DoDragDrop(img, DragDropEffects.Move) == DragDropEffects.Move) { var currentMover = _GameTurn.GameState; _GameTurn.GameState = GameState.INMOVE; if (IsLegalMove(CurrentPiece, ReplacedPiece, DestinationTile) == false) { _GameTurn.GameState = currentMover; return; } if (DestinationTile != null && ReplacedPiece == null && CurrentPiece != null) { MovePiece(CurrentPiece, DestinationTile.BoardLocation, currentMover); return; } MessageBox.Show("Shouldn't Be here", "Mouse Down", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
//Events private void gridCell_DragEnter(object sender, DragEventArgs e) { DestinationTile = (TilePiece)sender; if (e.Data.GetDataPresent(DataFormats.Bitmap)) e.Effect = DragDropEffects.Move; }
private bool IsCaptureMove(GamePiece currentPiece, TilePiece destinationTile) { int row = currentPiece.BoardLocation.Row - destinationTile.BoardLocation.Row; int column = currentPiece.BoardLocation.Column - destinationTile.BoardLocation.Column; if (Math.Abs(row) > 2 || Math.Abs(column) > 2) return false; bool IsCapture = false; BoardLocation captureLoc = new BoardLocation(); if (currentPiece.PieceState == GridEntry.WHITEPAWN) { var land = new BoardLocation { Row = currentPiece.BoardLocation.Row + 2, Column = currentPiece.BoardLocation.Column - 2 }; if (destinationTile.BoardLocation.Row == land.Row && destinationTile.BoardLocation.Column == land.Column) { GridEntry cellState = (GridEntry)Enum.Parse(typeof(GridEntry), _GameTurn.BOARDARRAY[currentPiece.BoardLocation.Row + 1, currentPiece.BoardLocation.Column - 1].ToString()); IsCapture = cellState == GridEntry.BLACKPAWN || cellState == GridEntry.BLACKKING; captureLoc = new BoardLocation() { Row = currentPiece.BoardLocation.Row + 1, Column = currentPiece.BoardLocation.Column - 1 }; } land = new BoardLocation { Row = currentPiece.BoardLocation.Row + 2, Column = currentPiece.BoardLocation.Column + 2 }; if (destinationTile.BoardLocation.Row == land.Row && destinationTile.BoardLocation.Column == land.Column) { GridEntry cellState = (GridEntry)Enum.Parse(typeof(GridEntry), _GameTurn.BOARDARRAY[currentPiece.BoardLocation.Row + 1, currentPiece.BoardLocation.Column + 1].ToString()); IsCapture = cellState == GridEntry.BLACKPAWN || cellState == GridEntry.BLACKKING; captureLoc = new BoardLocation() { Row = currentPiece.BoardLocation.Row + 1, Column = currentPiece.BoardLocation.Column + 1 }; } } else if (currentPiece.PieceState == GridEntry.BLACKPAWN) { var land = new BoardLocation { Row = currentPiece.BoardLocation.Row - 2, Column = currentPiece.BoardLocation.Column - 2 }; if (destinationTile.BoardLocation.Row == land.Row && destinationTile.BoardLocation.Column == land.Column) { GridEntry cellState = (GridEntry)Enum.Parse(typeof(GridEntry), _GameTurn.BOARDARRAY[currentPiece.BoardLocation.Row - 1, currentPiece.BoardLocation.Column - 1].ToString()); IsCapture = cellState == GridEntry.WHITEPAWN || cellState == GridEntry.WHITEKING; captureLoc = new BoardLocation() { Row = currentPiece.BoardLocation.Row - 1, Column = currentPiece.BoardLocation.Column - 1 }; } land = new BoardLocation { Row = currentPiece.BoardLocation.Row - 2, Column = currentPiece.BoardLocation.Column + 2 }; if (destinationTile.BoardLocation.Row == land.Row && destinationTile.BoardLocation.Column == land.Column) { GridEntry cellState = (GridEntry)Enum.Parse(typeof(GridEntry), _GameTurn.BOARDARRAY[currentPiece.BoardLocation.Row - 1, currentPiece.BoardLocation.Column + 1].ToString()); IsCapture = cellState == GridEntry.WHITEPAWN || cellState == GridEntry.WHITEKING; captureLoc = new BoardLocation() { Row = currentPiece.BoardLocation.Row - 1, Column = currentPiece.BoardLocation.Column + 1 }; } } else if (currentPiece.IsPromoted) { if (currentPiece.PieceState == GridEntry.WHITEKING) { var land = new BoardLocation { Row = currentPiece.BoardLocation.Row - 2, Column = currentPiece.BoardLocation.Column - 2 }; if (destinationTile.BoardLocation.Row == land.Row && destinationTile.BoardLocation.Column == land.Column) { GridEntry cellState = (GridEntry)Enum.Parse(typeof(GridEntry), _GameTurn.BOARDARRAY[currentPiece.BoardLocation.Row - 1, currentPiece.BoardLocation.Column - 1].ToString()); IsCapture = cellState == GridEntry.BLACKPAWN || cellState == GridEntry.BLACKKING; captureLoc = new BoardLocation() { Row = currentPiece.BoardLocation.Row - 1, Column = currentPiece.BoardLocation.Column - 1 }; } land = new BoardLocation { Row = currentPiece.BoardLocation.Row - 2, Column = currentPiece.BoardLocation.Column + 2 }; if (destinationTile.BoardLocation.Row == land.Row && destinationTile.BoardLocation.Column == land.Column) { GridEntry cellState = (GridEntry)Enum.Parse(typeof(GridEntry), _GameTurn.BOARDARRAY[currentPiece.BoardLocation.Row - 1, currentPiece.BoardLocation.Column + 1].ToString()); IsCapture = cellState == GridEntry.BLACKPAWN || cellState == GridEntry.BLACKKING; captureLoc = new BoardLocation() { Row = currentPiece.BoardLocation.Row - 1, Column = currentPiece.BoardLocation.Column + 1 }; } land = new BoardLocation { Row = currentPiece.BoardLocation.Row + 2, Column = currentPiece.BoardLocation.Column + 2 }; if (destinationTile.BoardLocation.Row == land.Row && destinationTile.BoardLocation.Column == land.Column) { GridEntry cellState = (GridEntry)Enum.Parse(typeof(GridEntry), _GameTurn.BOARDARRAY[currentPiece.BoardLocation.Row + 1, currentPiece.BoardLocation.Column + 1].ToString()); IsCapture = cellState == GridEntry.BLACKPAWN || cellState == GridEntry.BLACKKING; captureLoc = new BoardLocation() { Row = currentPiece.BoardLocation.Row + 1, Column = currentPiece.BoardLocation.Column + 1 }; } land = new BoardLocation { Row = currentPiece.BoardLocation.Row + 2, Column = currentPiece.BoardLocation.Column - 2 }; if (destinationTile.BoardLocation.Row == land.Row && destinationTile.BoardLocation.Column == land.Column) { GridEntry cellState = (GridEntry)Enum.Parse(typeof(GridEntry), _GameTurn.BOARDARRAY[currentPiece.BoardLocation.Row + 1, currentPiece.BoardLocation.Column - 1].ToString()); IsCapture = cellState == GridEntry.BLACKPAWN || cellState == GridEntry.BLACKKING; captureLoc = new BoardLocation() { Row = currentPiece.BoardLocation.Row + 1, Column = currentPiece.BoardLocation.Column - 1 }; } } else if (currentPiece.PieceState == GridEntry.BLACKKING) { var land = new BoardLocation { Row = currentPiece.BoardLocation.Row - 2, Column = currentPiece.BoardLocation.Column - 2 }; if (destinationTile.BoardLocation.Row == land.Row && destinationTile.BoardLocation.Column == land.Column) { GridEntry cellState = (GridEntry)Enum.Parse(typeof(GridEntry), _GameTurn.BOARDARRAY[currentPiece.BoardLocation.Row - 1, currentPiece.BoardLocation.Column - 1].ToString()); IsCapture = cellState == GridEntry.WHITEPAWN || cellState == GridEntry.WHITEKING; captureLoc = new BoardLocation() { Row = currentPiece.BoardLocation.Row - 1, Column = currentPiece.BoardLocation.Column - 1 }; } land = new BoardLocation { Row = currentPiece.BoardLocation.Row - 2, Column = currentPiece.BoardLocation.Column + 2 }; if (destinationTile.BoardLocation.Row == land.Row && destinationTile.BoardLocation.Column == land.Column) { GridEntry cellState = (GridEntry)Enum.Parse(typeof(GridEntry), _GameTurn.BOARDARRAY[currentPiece.BoardLocation.Row - 1, currentPiece.BoardLocation.Column + 1].ToString()); IsCapture = cellState == GridEntry.WHITEPAWN || cellState == GridEntry.WHITEKING; captureLoc = new BoardLocation() { Row = currentPiece.BoardLocation.Row - 1, Column = currentPiece.BoardLocation.Column + 1 }; } land = new BoardLocation { Row = currentPiece.BoardLocation.Row + 2, Column = currentPiece.BoardLocation.Column + 2 }; if (destinationTile.BoardLocation.Row == land.Row && destinationTile.BoardLocation.Column == land.Column) { GridEntry cellState = (GridEntry)Enum.Parse(typeof(GridEntry), _GameTurn.BOARDARRAY[currentPiece.BoardLocation.Row + 1, currentPiece.BoardLocation.Column + 1].ToString()); IsCapture = cellState == GridEntry.WHITEPAWN || cellState == GridEntry.WHITEKING; captureLoc = new BoardLocation() { Row = currentPiece.BoardLocation.Row + 1, Column = currentPiece.BoardLocation.Column + 1 }; } land = new BoardLocation { Row = currentPiece.BoardLocation.Row + 2, Column = currentPiece.BoardLocation.Column - 2 }; if (destinationTile.BoardLocation.Row == land.Row && destinationTile.BoardLocation.Column == land.Column) { GridEntry cellState = (GridEntry)Enum.Parse(typeof(GridEntry), _GameTurn.BOARDARRAY[currentPiece.BoardLocation.Row + 1, currentPiece.BoardLocation.Column - 1].ToString()); IsCapture = cellState == GridEntry.WHITEPAWN || cellState == GridEntry.WHITEKING; captureLoc = new BoardLocation() { Row = currentPiece.BoardLocation.Row + 1, Column = currentPiece.BoardLocation.Column - 1 }; } } } if (IsCapture) { this.Controls.Remove(FindPiece(captureLoc) as Control); _GameTurn.BOARDARRAY[captureLoc.Row, captureLoc.Column] = (int)GridEntry.EMPTY; } return IsCapture; }
//Methods private void InitBoard() { int iInd = 0; int iIndex = 64; int iWhite = 1; int iBlack = 1; for (int row = 0; row < BOARDSIZE; row++) { for (int col = 0; col < BOARDSIZE; col++) { TilePiece gridCell = new TilePiece(TILESIZE); gridCell.MouseDown += gameCell_MouseDown; gridCell.BoardLocation = new BoardLocation { Row = row, Column = col }; gridCell.Location = new Point(LEFTMARGIN + TILESIZE * col, TOPMARGIN + TILESIZE * row); //mBox.Text += (TOPMARGIN + TILESIZE * row).ToString() + ", " + (5 + TILESIZE * col) + Environment.NewLine; gridCell.BackColor = (row % 2 == 0 && col % 2 == 0) || (row % 2 == 1 && col % 2 == 1) ? Color.White : Color.Black; gridCell.DragDrop += gridCell_DragDrop; gridCell.DragEnter += gridCell_DragEnter; this.Controls.Add(gridCell); this.Controls.SetChildIndex(gridCell, ++iInd); if (row > 4 && gridCell.BackColor == Color.Black) { _GameTurn.BOARDARRAY[row, col] = (int)GridEntry.BLACKPAWN; GamePiece piece = new GamePiece() { BoardLocation = new BoardLocation { Row = row, Column = col } }; piece.MouseDown += piece_MouseDown; piece.DragEnter += piece_DragEnter; piece.DragDrop += piece_DragDrop; piece.PieceState = GridEntry.BLACKPAWN; piece.Name = "BLACKPIECE_" + iWhite++; piece.Image = GetImage("Checkers.Assets.BlackPiece.png"); piece.Location = new Point((LEFTMARGIN + TILESIZE * col) + 10, (TOPMARGIN + TILESIZE * row) + 10); this.Controls.Add(piece); this.Controls.SetChildIndex(piece, ++iIndex); piece.BringToFront(); } else if (row < 3 && gridCell.BackColor == Color.Black) { _GameTurn.BOARDARRAY[row, col] = (int)GridEntry.WHITEPAWN; GamePiece piece = new GamePiece() { BoardLocation = new BoardLocation { Row = row, Column = col } }; piece.MouseDown += piece_MouseDown; piece.DragEnter += piece_DragEnter; piece.DragDrop += piece_DragDrop; piece.PieceState = GridEntry.WHITEPAWN; piece.Name = "WHITEPIECE_" + iBlack++; piece.Image = GetImage("Checkers.Assets.WhitePiece.png"); piece.Location = new Point((LEFTMARGIN + TILESIZE * col) + 10, (TOPMARGIN + TILESIZE * row) + 10); this.Controls.Add(piece); this.Controls.SetChildIndex(piece, ++iIndex); piece.BringToFront(); } else { if (gridCell.BackColor == Color.Black) { _GameTurn.BOARDARRAY[row, col] = (int)GridEntry.EMPTY; } else { _GameTurn.BOARDARRAY[row, col] = (int)GridEntry.NULL; } } } } }
private bool IsLegalMove(GamePiece currentPiece, GamePiece replacedPiece, TilePiece tileToMove) { /*Black pieces can only move down unless promoted, and * * *Pieces can only be on the black tiles *Can't Land on any piece... * */ bool IsLegal = false; if (tileToMove == null) return IsLegal; if (tileToMove.BackColor == Color.White) return false; if (ReplacedPiece != null && CurrentPiece != ReplacedPiece) { return false; } bool IsCapture = IsCaptureMove(currentPiece, DestinationTile); if (DestinationTile != null && ReplacedPiece == null && CurrentPiece != null) { if (currentPiece.PieceState == GridEntry.WHITEPAWN) { if ((currentPiece.BoardLocation.Row + 1 == DestinationTile.BoardLocation.Row && ((currentPiece.BoardLocation.Column == DestinationTile.BoardLocation.Column + 1 || currentPiece.BoardLocation.Column == DestinationTile.BoardLocation.Column - 1) && DestinationTile.BoardLocation.Column >= 0 && DestinationTile.BoardLocation.Column < BOARDSIZE)) || IsCapture) { IsLegal = true; } } else if (currentPiece.PieceState == GridEntry.BLACKPAWN) { if (currentPiece.BoardLocation.Row - 1 == DestinationTile.BoardLocation.Row && ((currentPiece.BoardLocation.Column == DestinationTile.BoardLocation.Column + 1 || currentPiece.BoardLocation.Column == DestinationTile.BoardLocation.Column - 1) && DestinationTile.BoardLocation.Column >= 0 && DestinationTile.BoardLocation.Column < BOARDSIZE) || IsCapture) { IsLegal = true; } } else if (currentPiece.IsPromoted) { if ((Math.Abs(currentPiece.BoardLocation.Row - DestinationTile.BoardLocation.Row) == 1 && Math.Abs(currentPiece.BoardLocation.Column - DestinationTile.BoardLocation.Column) == 1) || IsCapture) { IsLegal = true; } } } return IsLegal; }