To place pieces on the chessboard represented by the chessboard array, you can assign values to specific indices in the array. Here's a simple example of how to do this:
Example Code
#include <stdio.h>
#define N 15
int chessboard[N + 1][N + 1] = { 0 };
// Function to place a piece on the board
void placePiece(int row, int col, int piece) {
if (row >= 0 && row <= N && col >= 0 && col <= N) {
chessboard[row][col] = piece; // Place the piece at the specified position
} else {
printf("Invalid position!\n");
}
}
// Function to print the chessboard
void printBoard() {
for (int i = 0; i <= N; i++) {
for (int j = 0; j <= N; j++) {
printf("%d ", chessboard[i][j]);
}
printf("\n");
}
}
int main() {
// Place pieces on the board
placePiece(0, 0, 1); // Place piece '1' at (0, 0)
placePiece(0, 1, 2); // Place piece '2' at (0, 1)
// Print the chessboard
printBoard();
return 0;
}
Explanation:
-
Function
placePiece:- Takes three parameters:
row,col, andpiece. - Checks if the specified position is valid (within bounds).
- Assigns the
piecevalue to the specified position on the chessboard.
- Takes three parameters:
-
Function
printBoard:- Loops through the
chessboardarray and prints the current state of the board.
- Loops through the
-
Main Function:
- Calls
placePieceto place pieces at specific coordinates. - Calls
printBoardto display the current state of the chessboard.
- Calls
Usage:
You can modify the placePiece function calls in main to place different pieces at various positions on the board. The pieces can be represented by different integers (e.g., 1 for a pawn, 2 for a knight, etc.).
Feel free to ask if you have more questions or need further examples!
