/// --------------------------Initialization Functions From------------------------- /* * initializeButtonsComponent is a method that initialize the buttons components. */ private void initializeButtonsComponent() { // Coordinates for the graphic location: int xCoordinate = (int)eSizeAndLocations.ButtonXStartPosition; int yCoordinate = (int)eSizeAndLocations.ButtonYStartPosition; for (byte row = 0; row < r_BoardSize; row++) { for (byte column = 0; column < r_BoardSize; column++) { buttonSquares[row, column] = new ButtonSquare(row, column); buttonSquares[row, column].LocationInBoard = new Coordinates(column, row); // Check if the current buttonSquare is black or white if (((column % 2) == 0 && (row % 2) == 0) || ((column % 2) != 0 && (row % 2) != 0)) { buttonSquares[row, column].BackColor = System.Drawing.Color.Black; buttonSquares[row, column].Enabled = false; } else { buttonSquares[row, column].BackColor = System.Drawing.Color.White; } this.buttonSquares[row, column].Location = new System.Drawing.Point(xCoordinate, yCoordinate); this.buttonSquares[row, column].Size = new System.Drawing.Size((int)eSizeAndLocations.ButtonSize, (int)eSizeAndLocations.ButtonSize); this.buttonSquares[row, column].FlatStyle = System.Windows.Forms.FlatStyle.Flat; xCoordinate += (int)eSizeAndLocations.ButtonSize; } yCoordinate += (int)eSizeAndLocations.ButtonSize; xCoordinate = (int)eSizeAndLocations.ButtonXStartPosition; } }
/* * doPlayerMove is a method that responsible to the execute the player move according to * clicking on the destination button on game board. */ private void doPlayerMove(ButtonSquare i_DestinationButtonSquare) { if (m_FormCheckersGame.IsAnyPieceSelected()) { Coordinates to = i_DestinationButtonSquare.LocationInBoard; Coordinates from = m_FormCheckersGame.CurrentSelectedPiece.BelowSquare.LocationInBoard; executePlayerMove(from, to); } }
/// -----------------------------Remove & Select function------------------------------ /* * removePieceOnSquare is a method that recieve row and column of square below piece that * need to be remove, in addition recieve collection of pieces and remove the relevant piece * from the collection. */ private void removePieceOnSquare(int i_SquareBelowPieceRow, int i_SquareBelowPieceColmen, PictureBoxCheckersPiece[] i_Pieces) { ButtonSquare belowButtonSquare = buttonSquares[i_SquareBelowPieceRow, i_SquareBelowPieceColmen]; // scan array and locate the piece whose cell coordinates are received in function: for (int i = 0; i < r_NumberOfPiecesInOneSide; i++) { if (i_Pieces[i] != null) { // Check if the current piece it what we looking for if (i_Pieces[i].BelowSquare == belowButtonSquare) { Controls.Remove(i_Pieces[i]); i_Pieces[i] = null; break; } } } }
/// ----------------------------------Other Functions--------------------------------- /// /* * getPieceByCoordinates is a method that return the piece that is at the given coordinate. */ private PictureBoxCheckersPiece getPieceByCoordinates(int i_SquareBelowPieceRow, int i_SquareBelowPieceColmen, PictureBoxCheckersPiece[] i_Pieces) { ButtonSquare belowButtonSquare = buttonSquares[i_SquareBelowPieceRow, i_SquareBelowPieceColmen]; PictureBoxCheckersPiece pieceToSelect = null; foreach (PictureBoxCheckersPiece piece in i_Pieces) { if (piece != null) { if (piece.BelowSquare == belowButtonSquare) { pieceToSelect = piece; break; } } } return(pieceToSelect); }
/* * FormCheckersGame constructor for initilize the checker game form */ public FormCheckersGame(byte i_CheckersBoardSize) { r_BoardSize = i_CheckersBoardSize; r_NumberOfPiecesInOneSide = (byte)((r_BoardSize / 2) * ((r_BoardSize / 2) - 1)); buttonSquares = new ButtonSquare[r_BoardSize, r_BoardSize]; pictureBoxCheckersPieceWhitePieces = new PictureBoxCheckersPiece[r_NumberOfPiecesInOneSide]; pictureBoxCheckersPieceBlackPieces = new PictureBoxCheckersPiece[r_NumberOfPiecesInOneSide]; InitializeComponent(); initializePiecesComponent(); initializeButtonsComponent(); initializeLocationOfPieces(); resizeClient(); addControlsButtonSquareToForm(); ucPlayer1Information.IsMyTurn = true; EmphasizeCurrentPlayer(); addGroupPictureToUIPlayersInformation(); }