1

I want to change my enum code with related values and want to get same output like before change. But i am getting different output. Codes before change were showed in comment lines. How and why it can be? I give the values to get same output. I can't understand why didn't i get the same output? (Random number not a problem. After change, it is not printing the board, just printing first user value not continue, not getting an error just keeping to look like the ss)

//enum {_,X,O}
enum {X,O,_};
//int user = 1;
//int comp = 2;
int user = 0;
int comp = 1;
void initBoard(int board[]){
    int n_pieces_user; //number of pieces for user
    int n_pieces_comp; //number of pieces for computer
    int  n_pieces_sum;
    int chosen_player;
    int position;

    printf("Input number of pieces for user and computer respectively: ");
    scanf("%d %d", &n_pieces_user, &n_pieces_comp);
    int n_pieces_sum = n_pieces_user + n_pieces_comp;
    int arr_pos[n_pieces_sum];
    printf("Press 1 to choose Player1, press 2 to Player2 (Player1 plays first!): ");
    while(1){
        scanf("%d",&chosen_player);
        if(chosen_player == 1){ //User plays first
            srand((unsigned)time(&t)); //initializes random number generator
            for(i=0; i<n_pieces_sum; i++){
                position = rand()%SIZE;
                for(k=0; k<i; k++){
                  //while((board[position]==1) || (board[position]==2)){
                    while((board[position] == 0) || (board[position] == 1)){
                        position = rand()%SIZE;
                        arr_pos[i] = position;
                    }
                    arr_pos[i] = position;
                }
                if(i<n_pieces_user){
                    arr_pos[i] = position;
                    printf("\nUser's random initial piece position: %d", arr_pos[i]);
                    board[position] = 0;
                }
                if(i>=n_pieces_user && i<n_pieces_sum){
                    arr_pos[i] = position;
                    printf("\nComputer's random initial piece position: %d", arr_pos[i]);
                    board[position] = 1;
                }
            }
            turn = comp;
            break;
        }
        else if(chosen_player == 2){ //Computer plays first
            srand((unsigned)time(&t)); //initializes random number generator
            for(i=0; i<n_pieces_sum; i++){
                position = rand()%SIZE;
                for(k=0; k<i; k++){
                  //while((board[position]==1) || (board[position]==2)){
                    while((board[position]==0) || (board[position]==1)){
                        position = rand()%SIZE;
                        arr_pos[i] = position;
                    }
                    arr_pos[i] = position;
                }
                if(i<n_pieces_comp){
                    arr_pos[i] = position;
                    printf("\nComputer's random initial piece position: %d", arr_pos[i]);
                    board[position] = 0;
                }
                if(i>=n_pieces_comp && i<n_pieces_sum){
                    arr_pos[i] = position;
                    printf("\nUser's random initial piece position: %d", arr_pos[i]);
                    board[position] = 1;
                }
            }
            turn = user;
            break;
        }else
            printf("Choose correct input!\n");
    }
    printf("\n");
}
void printBoard(const int board[]){
    char symbol[] = { 'X','O','_' };
    //char symbol[] = { '_','X','O' };
    printf("\n              BOARD\n\n");
    for(i=0; i<SIZE; i++) {
        if(i != 0 && i%7 == 0)
            printf("\n\n");
        printf("  %c  ",symbol[board[i]]);
    }
    printf("\n\n");
}

First output: enter image description here

Output after change: enter image description here

LunaAquila
  • 45
  • 4
  • 1
    Beside of the problem, your usage of `srand()` is suspicious. See [c - srand() — why call it only once? - Stack Overflow](https://stackoverflow.com/questions/7343833/srand-why-call-it-only-once) – MikeCAT Dec 25 '20 at 23:27
  • Random number is used, so the result need not be the same. – MikeCAT Dec 25 '20 at 23:30
  • @MikeCAT (1) It was working at first and tried to write before while(1) and nothing changed. (2) In second input, get out of for loop and not printing 5 of each user's random initial position. Not getting an error, just terminal keeps looking like the ss. – LunaAquila Dec 25 '20 at 23:31
  • @LunaAquila A random number seeded with the current time will be different between runs, no surprise there. – dxiv Dec 25 '20 at 23:34
  • 1
    @dxiv The issue isn't that the random piece is different. The problem they're having is that it's not selecting 5 pieces for the computer and player, it just selects one piece and hangs. – Barmar Dec 25 '20 at 23:36
  • @dxiv this is not what i am talking about. – LunaAquila Dec 25 '20 at 23:37
  • @Barmar Right, but there is nothing in the question to support the statement that the difference between the "*before*" and "*after*" outputs is the code change. The first and very obvious difference is that the "*before*" and "*after*" are running with different inputs, which *is* because of `rand`. – dxiv Dec 25 '20 at 23:39
  • You're probably getting stuck in the `while((board[position] == 0) || (board[position] == 1))` loop. – Barmar Dec 25 '20 at 23:41
  • Please post a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) including callers of the functions. – MikeCAT Dec 25 '20 at 23:41
  • @Barmar I just edited sorry, i forgot to write that line. It was `while((board[position] == 1) || (board[position] == 2))` at first and ran properly. – LunaAquila Dec 25 '20 at 23:47
  • How do you initialize the `board` array? – Barmar Dec 25 '20 at 23:49
  • Why aren't you using the `enum` values anywhere? – Barmar Dec 25 '20 at 23:50
  • @Barmar `#define SIZE 49 int board[SIZE];` – LunaAquila Dec 25 '20 at 23:50
  • 1
    If it's a global variable, it's initialized to all `0` values. So the loop will never find an element without `0` or `1` in it. – Barmar Dec 25 '20 at 23:52
  • 3
    You need to initialize it: `for (int i = 0; i < SIZE; i++) { board[i] = _; }` – Barmar Dec 25 '20 at 23:53

0 Answers0