/// <summary> /// recieve selected cell /// check if the selectected cell has got a piece /// </summary> /// <param name="cell"></param> /// <returns></returns> public bool SelectCellForMove(BoardCell cell) { bool result = false; if (SelectedCell != null) { SelectedCell.UnSelect(); } if (cell.CurrentPiece != null) { cell.Select(); SelectedCell = cell; } //if source cell has got a piece and destination cell is empty else if (SelectedCell != null && cell.CurrentPiece == null) { Move move = new Move(SelectedCell, cell); result = CompleteMove(move); } return(result); }
public Move(BoardCell SourceCell, BoardCell DestinationCell) { this.SourceCell = SourceCell; this.DestinationCell = DestinationCell; Piece = SourceCell.CurrentPiece; }
public PossibleMoves(Player player, Board board) { //look at each active piece for a player and and add to list of All moves foreach (BoardPiece piece in player.ActivePieces) { BoardCell sourceCell = board.content[piece.Y, piece.X]; int possibleX; int possibleY; //Jump possibleX = piece.X + 2; possibleY = piece.Y + 2; if (possibleX < 8 && possibleY < 8) { BoardCell destinationCell = board.content[possibleY, possibleX]; if (destinationCell.CurrentPiece == null) { Move move = new Move(sourceCell, destinationCell); ALLMoves.Add(move); } } possibleX = piece.X + 2; possibleY = piece.Y - 2; if (possibleX < 8 && possibleY >= 0) { BoardCell destinationCell = board.content[possibleY, possibleX]; if (destinationCell.CurrentPiece == null) { Move move = new Move(sourceCell, destinationCell); ALLMoves.Add(move); } } possibleX = piece.X - 2; possibleY = piece.Y + 2; if (possibleX >= 0 && possibleY < 8) { BoardCell destinationCell = board.content[possibleY, possibleX]; if (destinationCell.CurrentPiece == null) { Move move = new Move(sourceCell, destinationCell); ALLMoves.Add(move); } } possibleX = piece.X - 2; possibleY = piece.Y - 2; if (possibleX >= 0 && possibleY >= 0) { BoardCell destinationCell = board.content[possibleY, possibleX]; if (destinationCell.CurrentPiece == null) { Move move = new Move(sourceCell, destinationCell); ALLMoves.Add(move); } } //1 step possibleX = piece.X + 1; possibleY = piece.Y + 1; if (possibleX < 8 && possibleY < 8) { BoardCell destinationCell = board.content[possibleY, possibleX]; if (destinationCell.CurrentPiece == null) { Move move = new Move(sourceCell, destinationCell); ALLMoves.Add(move); } } possibleX = piece.X + 1; possibleY = piece.Y - 1; if (possibleX < 8 && possibleY >= 0) { BoardCell destinationCell = board.content[possibleY, possibleX]; if (destinationCell.CurrentPiece == null) { Move move = new Move(sourceCell, destinationCell); ALLMoves.Add(move); } } possibleX = piece.X - 1; possibleY = piece.Y + 1; if (possibleX >= 0 && possibleY < 8) { BoardCell destinationCell = board.content[possibleY, possibleX]; if (destinationCell.CurrentPiece == null) { Move move = new Move(sourceCell, destinationCell); ALLMoves.Add(move); } } possibleX = piece.X - 1; possibleY = piece.Y - 1; if (possibleX >= 0 && possibleY >= 0) { BoardCell destinationCell = board.content[possibleY, possibleX]; if (destinationCell.CurrentPiece == null) { Move move = new Move(sourceCell, destinationCell); ALLMoves.Add(move); } } } }
/// <summary> /// draw board using 2D array with alternate black and white cells /// and then populate with pieces on the black cell /// </summary> public void drawBoard() { content = new BoardCell[8, 8]; int cellColor = 1; // 0 for white and 1 for black string pieceColor = null; bool WhiteCell = false; for (int i = 0; i < 8; i++)// to generate alternate cell colours { if (cellColor == 0) { cellColor = 1; } else { cellColor = 0; } for (int j = 0; j < 8; j++) { if (j % 2 == cellColor) { WhiteCell = true; } else { WhiteCell = false; } // to generate board pieces BoardPiece Piece = null; //it will only generate pieces for black cells //for first 3 rows and last 3 rows and where cellcolour is black if ((i < 3 || i > 4) && WhiteCell == false) { //for first 3 rows of the board player1 pieces if (i < 3) { //get a colour from player1 to set piece colour for player1 pieceColor = game.Player1.Color; } else { pieceColor = game.Player2.Color; } } else { pieceColor = null; } if (string.IsNullOrEmpty(pieceColor) == false) { if (i < 3) { Piece = new BoardPiece(game.Player1);//create a player1 piece } else { Piece = new BoardPiece(game.Player2);// create a player2 piece } } else { Piece = null; } BoardCell Cell; if (WhiteCell)//if WhiteCell= false that means white board cells { Cell = new BoardCell(i, j, BoardCell.WHITE_CELL, Piece); } else { Cell = new BoardCell(i, j, BoardCell.BLACK_CELL, Piece); } drawBoardElements(Cell); //draw cell on the grid content[i, j] = Cell; if (pieceColor == game.Player1.Color) { game.Player1.ActivePieces.Push(Piece);//add to list of active pieces for player1 } else if (pieceColor == game.Player2.Color) { game.Player2.ActivePieces.Push(Piece);//add to list of active pieces for player2 } } } }