There are two algorithms to determine whether a game can win or lose:
1. scan the entire board to see if there are five links in four directions. This algorithm is used to find the source code of many five games on the Internet. This means that each piece will scan the 19x19 board, which is complex, inefficient, and slightly code.
2. For each next word, the child starts to scan its four directions (for example, from the child's (X-4, y) coordinate to scan horizontally) Whether there are five links. This algorithm is commonly used and does not involve more complex data structures.
In addition, when declaring the position of a checkerboard, you can declare a checkerboard (4 + 19 + 4) × (4 + 19 + 4, let the pawnpiece offset (4, 4) coordinates.
The source code of algorithm 2 is as follows:
Static void IfWin (int x, int y, int color) {TCHAR win [20]; int a, B; if (stone [x] [y] = 1) wcscpy_s (win, _ T ("! "); Elsewcscpy_s (win, _ T (" White game victory! "); For (a = X-4; a <= x + 4; a ++) // determine if (stone [a] [y] = color & stone [a + 1] [y] = color & stone [a + 2] [y] = color & stone [a + 3] [y] = color & stone [a + 4] [y] = color) {MessageBoxW (Xqwl. hWnd, win, TEXT (""), MB_ OK); return ;}for (B = y-4; B <= y + 4; B ++) // determine the vertical if (stone [x] [B] = color & stone [x] [B + 1] = color & stone [x] [B + 2] = color & stone [x] [B + 3] = color & stone [x] [B + 4] = color) {MessageBoxW (Xqwl. hWnd, win, TEXT (""), MB_ OK); return;} for (a = X-4, B = y-4; a <= x + 4; a ++, B ++) // determine the right oblique if (stone [a] [B] = color & stone [a + 1] [B + 1] = color & stone [a + 2] [B + 2] = color & stone [a + 3] [B + 3] = color & stone [a + 4] [B + 4] = color) {MessageBoxW (Xqwl. hWnd, win, TEXT (""), MB_ OK); return ;}for (a = X-4, B = y + 4; a <= x + 4; a ++, B --) // determine the left oblique if (stone [a] [B] = color & stone [a + 1] [b-1] = color & stone [a + 2] [B-2] = color & stone [a + 3] [B-3] = color & stone [a + 4] [B-4] = color) {MessageBoxW (Xqwl. hWnd, win, TEXT (""), MB_ OK); return ;}}
The above source code compiler is VS2010.