Basic AI Screws Up Whole Program

I created a version of 2048, and I also want to create a basic AI that would play the game as long as it could. This is where I am running into issues. My AI seems to overwriting cells and screwing up the whole board. I have been looking for the error for a whole week and have not found it.

Would someone be able to point in the direction to get this code working?





The specific function that is doing this is my AI function(uncreative, but clear), and that is this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
xy AI(xy coordinates){
    int i,mostCells;
    mostCells = 0;
    
    InitalizeBackupBoard();
    //take a copy of the current board
    TransferBoard();
    
    for (i=0;i<4;i++){
        //push the board
        PushLoop(i);
        
        //find the number of empty cells
        coordinates = GetEmptyCell(coordinates);

        //compare the number of empty cells
        //if the current # of cells is larger than the last largest
        if (coordinates.emptyCells > mostCells){

            //make the current # of empty cells the largest number of cells
                //and make the direction it was pushed the optimal direction
            mostCells = coordinates.emptyCells;
            coordinates.pushDirection = i;
        }
        //bring the original board back
        RepeatTransfer();
    }
    return coordinates;
}


The whole code is longer, but all the functions that are mentioned in this loop are here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
struct xy{
    int emptyCells;
    int pushDirection;
    bool boardFull;
};

void PushUp(){
    int i,j,k;
    int sort;
    for (k=0;k<BOARD_SIZE;k++){
        for (int i=0; i < BOARD_SIZE; i++){
	        for (int j=1; j < BOARD_SIZE; j++){
                //if the value above the selected value is a zero
                if (board[j-1][k] == 0){
                    //switch them
                    sort = board[j-1][k];
                    board[j-1][k] = board[j][k];
                    board[j][k] = sort;
                }
	    }
        }
    }
}

void PushDown(){
    int i,j,k;
    int sort;
    for (k=0;k<BOARD_SIZE;k++){
        for (i = 0; i < BOARD_SIZE;i++){
            for (j = 0; j < BOARD_SIZE - 1; j++){
                //if the value below the selected value is a zero
                if (board[j+1][k] == 0){
                    //switch them
                    sort = board[j+1][k];
                    board[j+1][k] = board[j][k];
                    board[j][k] = sort;
                }
            }
        }
    }
}

void PushLeft(){
    int i,j,k;
    int sort;
    for (k=0;k<BOARD_SIZE;k++){
        for (int i=0; i < BOARD_SIZE; i++){
	        for (int j=1; j < BOARD_SIZE; j++){
                //if the value above the selected value is a zero
                if (board[k][j-1] == 0){
                    //switch them
                    sort = board[k][j-1];
                    board[k][j-1] = board[k][j];
                    board[k][j] = sort;
                }
	    }
        }
    }
}

void PushRight(){
    int i,j,k;
    int sort;
    for (k=0;k<BOARD_SIZE;k++){
        for (i = 0; i < BOARD_SIZE;i++){
            for (j = 0; j < BOARD_SIZE - 1; j++){
                //if the value below the selected value is a zero
                if (board[k][j+1] == 0){
                    //switch them
                    sort = board[k][j+1];
                    board[k][j+1] = board[k][j];
                    board[k][j] = sort;
                }
            }
        }
    }
}

int PushBoard(int push){
    int i,j,k;
    int sort;
    
    //if up
    if (push == 0){
        PushUp();
    }
    //if down
    else if (push == 1){
        PushDown();
    }
    //if left
     else if (push == 2){
        PushLeft();
    }
    //if right
    else if (push == 3){
        PushRight();
    }
}

void AddUp(){
    int i,j,k;
    for (k=0;k<BOARD_SIZE;k++){
        for (int i=0; i < BOARD_SIZE; i++){
	        for (int j=1; j < BOARD_SIZE; j++){
                if (board[j-1][k] == board[j][k] && board[j-1][k] != 0){
                    //add one to the value above and set the selected
                        //value to zero
                    board[j-1][k] = board[j-1][k] + 1;
                    board[j][k] = 0;
                }
            }
        }
    }
}

void AddDown(){
    int i,j,k;
    for (k=0;k<BOARD_SIZE;k++){
        for (int i=0; i < BOARD_SIZE; i++){
	        for (int j=1; j < BOARD_SIZE; j++){
                if (board[j+1][k] == board[j][k] && board[j+1][k] != 0){
                    //add one to the value below and set the selected
                        //value to zero
                    board[j+1][k] = board[j+1][k] + 1;
                    board[j][k] = 0;
                }
            }
        }
    }
}

void AddLeft(){
    int i,j,k;
    for (k=0;k<BOARD_SIZE;k++){
        for (int i=0; i < BOARD_SIZE; i++){
	        for (int j=1; j < BOARD_SIZE; j++){
                if (board[k][j-1] == board[k][j] && board[k][j-1] != 0){
                    //add one to the value above and set the selected
                        //value to zero
                    board[k][j-1] = board[k][j-1] + 1;
                    board[k][j] = 0;
                }
            }
        }
    }
}

void AddRight(){
    int i,j,k;
    for (k=0;k<BOARD_SIZE;k++){
        for (int i=0; i < BOARD_SIZE; i++){
	        for (int j=1; j < BOARD_SIZE; j++){
                if (board[k][j+1] == board[k][j] && board[k][j+1] != 0){
                    //add one to the value below and set the selected
                        //value to zero
                    board[k][j+1] = board[k][j+1] + 1;
                    board[k][j] = 0;
                }
            }
        }
    }
}

void AddBoard(int push){
    
    //if up
    if (push == 0){
        AddUp();
    }
    //if down
    else if (push == 1){
        AddDown();
    }
    //if left
    else if (push == 2){
        AddLeft();
    }
    //if right
    else if (push == 3){
        AddRight();
    }
}

void PushLoop(int push){
    PushBoard(push);
    AddBoard(push);
    PushBoard(push);
}
void TransferBoard(){
    int x,y;
    //make every cell of the backup the same as the original
    for (x=0;x<BOARD_SIZE;x++){
        for (y=0;y<BOARD_SIZE;y++){
            backupBoard[x][y] = board[x][y];
        }
    }
}

void RepeatTransfer(){
    int x,y;
    //put the original board back
    for (x=0;x<BOARD_SIZE;x++){
        for (y=0;y<BOARD_SIZE;y++){
            board[x][y] = backupBoard[x][y];
        }
    }
}

xy GetEmptyCell(xy coordinates){
    int x,y;
    //go through each cell, and it it is empty, add one to the empty cell count
    for (x=0;x<BOARD_SIZE;x++){
        for (y=0;y<BOARD_SIZE;y++){
            if (board[x][y] == 0){
                coordinates.emptyCells++;
            }
        }
    }
    return coordinates;
}
Last edited on
It would be nicer to read your code if you had used comments... Anyway, I'll just talk general stuff:

Did you test play it yourself BEFORE adding in the AI? i.e. Are you functions such as PushLeft() and AddDown() all working correctly?

Also, the AI should never modify the board. If you want it to do any kind of analysis, pass it a copy of the board and it can manipulate that and then in the end, the AI can call which function it wants to do on the board. This way, even if your AI makes some kind of screw up, it only calls a function that is maybe not optimal according to its checks, rather than screwing up the game.
Hey Mats,

I did test the program before adding the AI, and it worked quite well using a rand() function to generate the push direction.

Thanks for the tip about the AI, I was trying to cut down on the number of lines by placing a copy of he board in a backup and using preexisting functions to determine the best move -- using the TransferBoard() and RepeatTransfer():
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void TransferBoard(){
    int x,y;
    for (x=0;x<BOARD_SIZE;x++){
        for (y=0;y<BOARD_SIZE;y++){
            backupBoard[x][y] = board[x][y];
        }
    }
}

void RepeatTransfer(){
    int x,y;
    for (x=0;x<BOARD_SIZE;x++){
        for (y=0;y<BOARD_SIZE;y++){
            board[x][y] = backupBoard[x][y];
        }
    }
}
Hi OP, can I be TFG and ask you to drop the full code into something like pastebin? Something this large is just easier to stack trace sometimes.


There are no compiler errors, but if you want to run through the code with a compiler, then I need to give you the complete program, which is here:

https://docs.google.com/document/d/1l0_AFd81j98qeQfd0fkhpC7Q0oCKO8gaelkTmjdyw9w/edit?usp=sharing
Topic archived. No new replies allowed.