You have created an infinite loop through the improper use of the decrement operators. Look at the following:
// switch numbers 2 and 1
board[j][i--] = 2;
board[j--][i - 2] = 1;
Both i and j control the inner and outer loops and are incremented on each pass of their respective loops. In these two operations, the code is trying to access the indexes that are 1 less than i or j. However, the --
operator actually permanently decrements these values. Let's say that when the code gets to these two lines that i=2 and j=2. After these two lines process, i =1 and j = 1. If you put a printf statement after these two lines, you'd see that with d=3, both i and j keep getting set to 1 and the loop runs forever.
Instead of using the --
operator which permanently alters the contents of a variable, you should use i-1
. This will be evaluated and the result will only be used for the array index without altering the value of i. It is a transient evaluation that will not reassign a new value to i. Same applies to j.
This addresses your question and this particular problem, but there are more issues that you will need to debug. For starters, the init() function is not initializing the array correctly. But I wouldn't want to deprive you of the fun of debugging it! ;-)
If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)