-2

I'm sure many of you are tired of seeing tic tac toe questions, but I'd appreciate any insight as to why this part doesn't function as intended.

The rest of the code works great, but right here, the computer doesn't make its move, instead it just re-displays the board and goes back to the user's move.

I verified that the user and computer marks are carried over to the function, but the selection process isn't working. It defaults to the for loop, continuing to the other functions from there, but it still doesn't assign the computer's mark.

I already have code elsewhere that makes sure the board isn't full before being sent over to the next move.

Thank you

void cmove(char umark, char cmark)
{
    int i=0;
    if((board[4] == NULL))
    {
        board[4] == cmark;
        DisplayBoard();
        cwin(umark , cmark);
    }
    if((board[0] != NULL) && (board[1] != NULL) && (board[2] != NULL) && (board[3] != NULL) && (board[4] == umark) && (board[5] != NULL) &&
       (board[6] != NULL) && (board[7] != NULL) && (board[8] != NULL))
       {
           board[0]= cmark;
           DisplayBoard();
           cwin(umark ,cmark);
       }
    for(i=0; i<9; i++)
    {
        if(board[i] == NULL)
        {
            board[i] == cmark;
            DisplayBoard();
            cwin(umark, cmark);
            i=9;
        }
    }
}
Jens
  • 69,818
  • 15
  • 125
  • 179
C. NGL
  • 1
  • 1
  • Have you already tried debugging it to find out which code paths are actually executed? If the application behaves differently than expected, you should first try to debug your program and find out why it does so. – maja Sep 03 '16 at 19:12
  • 2
    What are all those `NULL`s? Is your board an array of pointers? Please make sure you have full compiler warnings enabled, might turn up something else. – Weather Vane Sep 03 '16 at 19:14
  • 2
    `board[4] == cmark;` is a mistake, which the compiler should have warned you of. Also `board[i] == cmark;`. Please change `==` to `=`. – Weather Vane Sep 03 '16 at 19:15
  • Oh man, can't believe I put double, I've been coding too long. – C. NGL Sep 03 '16 at 19:20
  • The NULL is checking if is assigned there yet. Using this elsewhere to make sure the user selects an empty slot works. – C. NGL Sep 03 '16 at 19:21
  • 1
    @C.NGL `NULL` is a pointer value, not an `int` or `char` value (you didn't show what `board` is).. – Weather Vane Sep 03 '16 at 19:24
  • 1
    Based on the assignments (if they get fixed as others have suggested), `board` is probably `char board[10]` (e.g.). So, most of your (e.g) `board[3] != NULL` in your `if`s should be `board[3] != 0` The `==` vs `=` with `-Wall` would have been flagged as "statement with no effect" – Craig Estey Sep 03 '16 at 19:26
  • "I've been coding too long." But not long enough to routinely enable all compiler warnings. – Weather Vane Sep 03 '16 at 19:28
  • As in I'm tired and making stupid mistakes. I am still a noob – C. NGL Sep 04 '16 at 01:13

2 Answers2

1

This is a comparison and not an assignment:

 board[i] == cmark;

You have a similar problem in the first conditional block board[4] == cmark;

Turn on all the warnings your compiler can muster to catch this sort of mistake.

LBes
  • 3,366
  • 1
  • 32
  • 66
P-Nuts
  • 879
  • 5
  • 6
0
board[4] == cmark;

This is comparison and has no effect. Your intent was to assign instead:

board[4] = cmark;
4pie0
  • 29,204
  • 9
  • 82
  • 118