C ++ mine clearance code

Source: Internet
Author: User
# Include <ctime> # include <cstdlib> # include <iostream> using namespace STD; int map [12] [12]; // to avoid special processing of boundaries, therefore, the boundary around the two-dimensional array is extended by 1int Derection [3] = {0, 1,-1}; // direction array int calculate (int x, int y) {int counter = 0; For (INT I = 0; I <3; I ++) for (Int J = 0; j <3; j ++) if (Map [x + Derection [I] [Y + Derection [J] = 9) Counter ++; // count (x, y) return counter;} void game (int x, int y) {If (calculate (X, y) = 0) {map [x] [Y] = 0; For (INT I = 0; I <3; I ++) {// simulate the game process. If the point is blank, the system automatically scales out for (Int J = 0; j <3; j ++) if (x + Derection [I] <= 9 & Y + Derection [J] <= 9 & x + Derection [I]> = 1 & Y + Derection [J ]> = 1 &&! (Derection [I] = 0 & Derection [J] = 0) & map [x + Derection [I] [Y + Derection [J] =-1) Game (x + Derection [I], Y + Derection [J]); // There are many conditions. First, you cannot set the coordinates of both directions to 0 at the same time; otherwise, you can call them recursively !} // Second, recursion cannot exit. Third, ensure that no call is returned .} Elsemap [x] [Y] = calculate (x, y);} void print () {for (INT I = 1; I <10; I ++) {for (Int J = 1; j <10; j ++) {If (Map [I] [J] =-1 | map [I] [J] = 9) cout <"#"; elsecout <map [I] [J];} cout <Endl ;}} bool check () {int counter = 0; For (INT I = 1; I <10; I ++) for (Int J = 1; j <10; j ++) if (Map [I] [J]! =-1) Counter ++; If (counter = 10) return true; elsereturn false;} int main () {int I, j, X, Y; char ch; srand (time (0); do {memset (MAP,-1, sizeof (MAP); // Initialize all maps to-1, in the future, use-1 to indicate the areas not involved (I = 0; I <10;) {x = rand () % 9 + 1; y = rand () % 9 + 1; if (Map [x] [Y]! = 9) {map [x] [Y] = 9; I ++ ;}}for (I = 1; I <10; I ++) {for (j = 1; j <10; j ++) cout <"#"; cout <"\ n" ;}cout <"\ n "; cout <"Please enter a coordinate:"; while (CIN> x> Y) {If (Map [x] [Y] = 9) {cout <"game over" <Endl; // After, the game ends and the position of the thunder is output for (I = 1; I <10; I ++) {for (j = 1; j <10; j ++) {If (Map [I] [J] = 9) cout <"@"; elsecout <"#" ;}cout <Endl ;}break;} game (x, y); print (); If (check ()) {cout <"you win" <Endl; break;} cout <"\ n";} cout <"do you want to play again, if true enter Y, or enter N "<Endl; CIN> CH; cout <" \ n ";}while (CH = 'y '); return 0 ;}

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.