Stuck on put player in the board

Viewed 34

I am doing a game in C, I have a board with dimensions generated by the user, where the Xs are the randomly generated 'trees', I am trying to enter the players J1 and J2 who would be who are going to play, I want it in opposite corners, for example if the board is 9x9, J1 should be at 9a and J2 at 1i.

I tried putting tablero[i = TamTablero] [j = 0] = 'J1' in the code, but that didn't work. And the same for J2. I've been stuck here for quite a while.

Here is my code, where TamTablero means BoardSize:



int main() {

    int TamTablero=0;

    printf("Ingrese el area del bosque donde va a jugar: ");
    scanf("%d", &TamTablero);

    char tablero[TamTablero][TamTablero];

    int i,j;

    for(i = 0; i < TamTablero; i++)
       for(j = 0; j < TamTablero; j++)
           tablero[i][j] = '*';

    srand (time(NULL));

    int arboles = 0;

    while(arboles < TamTablero + 3){

       i = rand() % TamTablero;
       j = rand() % TamTablero;

       if( tablero[i][j] != 'X'){

            tablero[i][j] = 'X';
            arboles++;
       }
    }

    printf("\n   ");

    for (j=0; j<TamTablero; j++)

        printf(" %c", 'A' + j);

        printf("\n");

    for (i=0; i<TamTablero; i++){

        printf("%d |", i+1);

        for (j=0; j<TamTablero; j++){

            printf(" %c", tablero[i][j]);
       }

        printf(" \n");
   }

 return 0;

} ```
2 Answers

You can add the players when you're creating the board:

for(i = 0; i < TamTablero; i++) {
    for(j = 0; j < TamTablero; j++) {
        // Add J1
        if (i == 0 && j == 0)
            tablero[i][j] = '1';    // Player 1
        else if (i == TamTablero - 1 && j == TamTablero - 1)
            tablero[i][j] = '2';    // Player 2
        else
            tablero[i][j] = '*';    // void
    }
}

You have declared tablero as an array of characters, in particular single characters. If you want to do tablero[i][j] = "J1", you will have to declare tablero as an array of strings, or char * in C.

Related