[C Language] algorithm and source code for determining the victory and defeat of five games

Source: Internet
Author: User

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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.