Mine (mine clearance game)

Source: Internet
Author: User

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/202001C60-0.gif "/> mine !, This game should have been played by a vast majority of people. It is very high, but if you know how the thunder in the game is arranged, it will surely be even higher. Let's take a look at how such a magical Ray is distributed? There are several key points in the process:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/2020013345-1.gif "/> take the random number as the position of the thunder, so that the location of the Thunder can be randomly distributed;

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/2020013345-1.gif "/> random number should not be repeated, to avoid reducing the number of thunder;

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/2020013345-1.gif "/> determine the number that should be displayed outside the ray

Namespace prj mine {class Program {static void Main (string [] args) {// create an object that generates a Random number to r Random r = new Random (); // set the edge length of the minefield to int length = 10; // create an index array element as an index ), int [] ary = new int [length * length]; // sets the number of Ray int count = 9; // initializes the index array, and the value of each element must be different [] for (int I = 0; I <length * length; I ++) {ary [I] = I ;} // set the range of values from the index array to change) int size = length * length; // create a location array, the element indicates the location of a mine. int [] locations = new int [count]; for (int j = 0; j <count; j ++) {// generate a random number index int index = r. next (size); // index the index array to retrieve the value int val = ary [index] at the corresponding position; // put the value retrieved from the index array into the array of mine locations [j] = val; // the value in the index array has been taken as the location of a mine, so the range should be reduced by 1 size --; // Overwrite for (int I = index; I <size-1; I ++) {ary [I] = ary [I + 1] ;}// create a minefield array and put Ray 9) in int [,] mines = new int [length, length]; // converts a one-dimensional array into a two-dimensional array for (int I = 0; I <count; I ++) {int x = locations [I]/length; int y = locations [I] % length; mines [x, y] = 9 ;} // display the number (int I = 0; I <mines. getLength (0); I ++) {for (int j = 0; j <mines. getLength (1); j ++) {// call the GetMineCount function if the grid is not ray 9 to obtain the number it should display if (mines [I, j] = 9) {continue;} mines [I, j] = GetMineCount (mines, I, j );}} // print the two-dimensional array for (int I = 0; I <mines. getLength (0); I ++) {for (int j = 0; j <mines. getLength (1); j ++) {Console. write ("{0} \ t", mines [I, j]);} Console. writeLine () ;}} private static int GetMineCount (int [,] mines, int RowIndex, int ColumIndex) {// int count = 0; // assume that the grid displays 0 int topRowIndex = RowIndex-1; int bottomRowIndex = RowIndex + 1; int leftCoumIndex = ColumIndex-1; int rightColumIndex = ColumIndex + 1; // upper left // The condition for grid existence is true if (topRowIndex> = 0 & leftCoumIndex> = 0) {// determines whether the grid in this position is mine, if the positive left side of the grid is a ray, add the number 1 if (mines [topRowIndex, leftCoumIndex] = 9) {count ++ ;}} // forward. // The condition for grid existence is true. if (topRowIndex> = 0) {// you can determine whether the grid in this position is mine, if the positive left side of the grid is a ray, add the number 1 if (mines [topRowIndex, ColumIndex] = 9) {count ++ ;}} // upper right // The condition for grid existence is true if (topRowIndex> = 0 & rightColumIndex <mines. getLength (1) {// determines whether the grid in this position is mine. if the grid on the left is mine, add 1 if (mines [topRowIndex, rightColumIndex] = 9) {count ++ ;}// bottom left // the condition that the grid exists is true if (bottomRowIndex <mines. getLength (0) & amp; leftCoumIndex & gt; = 0) {// determines whether the grid in this position is thunder, if the positive left side of the grid is a ray, add the number 1 if (mines [bottomRowIndex, leftCoumIndex] = 9) {count ++ ;}} // forward bottom // if (bottomRowIndex <mines. getLength (0) {// determines whether the grid in this position is thunder. if the grid on the left is thunder, add 1 if (mines [bottomRowIndex, columIndex] = 9) {count ++ ;}/// the bottom right // condition for the existence of the grid is true if (bottomRowIndex <mines. getLength (0) & rightColumIndex <mines. getLength (1) {// determines whether the grid in this position is thunder. if the grid on the left is thunder, add 1 if (mines [bottomRowIndex, rightColumIndex] = 9) {count ++ ;}// the condition for the existence of the positive left // grid is true if (leftCoumIndex> = 0) {// determine whether the grid in this position is mine. if the grid on the left is mine, add 1 if (mines [RowIndex, leftCoumIndex] = 9) {count ++ ;}/// the condition that the grid exists is true if (rightColumIndex <mines. getLength (1) {// determines whether the grid in this position is thunder. if the grid on the right is thunder, add 1 if (mines [RowIndex, rightColumIndex] = 9) {count ++ ;}// return the number returned by the grid: return count ;}}}

The running result is as follows: 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/2020011P5-4.gif "/>

650) this. width = 650; "src =" http://img1.51cto.com/attachment/201308/172649436.png "title =" Capture. PNG "/>


650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/2020013B7-6.gif "/> Ajax Girl, come on! 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/2020013B7-6.gif "/>

This article is from the "Ajax girl" blog!

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.