How to place pieces on the board?

081

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:

  1. Function placePiece:

    • Takes three parameters: row, col, and piece.
    • Checks if the specified position is valid (within bounds).
    • Assigns the piece value to the specified position on the chessboard.
  2. Function printBoard:

    • Loops through the chessboard array and prints the current state of the board.
  3. Main Function:

    • Calls placePiece to place pieces at specific coordinates.
    • Calls printBoard to display the current state of the chessboard.

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!

0 Comments

no data
Be the first to share your comment!