Writing mine-clearing game with Java--algorithm thinking

Source: Internet
Author: User
Tags array
Algorithm?? Author: Aaron2004





??? Minesweeper is a fairly classic game. He provided a very friendly interface.





??? Here's how to explain my mine-clearance program ideas. First we randomly put Ray on the minefield, which can be achieved with random class. When there is no thunder, the place is clicked and a number is displayed indicating how many mines are around it, how does this happen? We can see the whole minefield as a two-dimensional array a[?i [j], such as mined areas:





????????????? 11.12? 13? 14 15
16? 17 18

???????????? 21-22-23-24-25-26-27-28


?????????????? 31.32? 33? 34 35
36? 37 38

????????????? 41.42? 43? 44 45
46? 47 48

?????????????? 51.52? 53? 54 55
56? 57 58

???? We can find that a[I [j] are surrounded by the following relationships:





?????????? A[I–1] [j–1]????????? A[?I–1] [j]???????????? a[i–1] [j + 1]





a[????????????? a[I [j–1]?????????????? A[?i] [j]???????????????????? a[I] [j + 1]





a[????????????? A[?i + 1] [j-1]?????????? A[?i + 1 [j]????????????? a[i + 1][j + 1]





???????????? Thus, the detection can be started clockwise from the upper-left corner of a[I [j]. Of course, if you go beyond the boundaries, you need to use the constraints to judge!





???????????? The mine-clearance process will also automatically start a minefield that has been identified as not mined. If the a[3][4] is marked as a mine, then a[2][4],a[2][5],a[3][3],a[3][5],a[4][3],a[4][4],a[4][5] will be launched, spilling over into an indeterminate minefield. This is also the key to implementation. We can set the elements of an array to a class object, and the classes they belong to have an event: when it is expanded, check that the number of mines around it is equal to the number of lightning markers around it, and if it is equal, expand the surrounding minefields. Such a new minefield unfolded and triggered the event, so that it was handed down and spread to a minefield that could not be unfolded. It is believed that after understanding the above two key points, the mine area this class writes completely (if to add whether has the Thunder mark, expands the marking, the periphery Thunder number and so on, double clicks, right and left click mouse event and so on), realizes the demining procedure should be very simple one thing.











?











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.