C + + implements minesweeper (coordinates)

Source: Internet
Author: User

Yesterday when observing the snake's code, saw how to implement the C + + code to achieve minesweeper, think it is very interesting, today then tried again

#include <ctime>#include<cstdlib>#include<iostream>using namespacestd;intmap[ A][ A];//in order to avoid the special processing of the boundary, we extend the surrounding boundary of the two-dimensional array 1intderection[3] = {0,1, -1};//Array of directionsintCalculate (intXinty) { intCounter =0;  for(inti =0; I <3; i++ )   for(intj =0; J <3; J + + )   if(map[x+derection[i]][y+derection[j]] = =9) Counter++;//count the number of mines around (x, y) centered returncounter;}voidGame (intXinty) {    if(Calculate (x, y) = =0) {Map[x][y]=0;  for(inti =0; I <3; i++ )  {                                  //simulation of the game process, if the point to a blank, then the system automatically scale out    for(intj =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]);//conditions are more, one is not allowed to two direction coordinates at the same time 0, otherwise  }                                                      //the second is that recursion cannot go out of bounds. Three is to ensure that the call is not returned.  } ElseMap[x][y]=Calculate (x, y);}voidprint () { for(inti =1; I <Ten; i++ ) {   for(intj =1; J <Ten; J + + )  {   if(Map[i][j] = =-1|| MAP[I][J] = =9) cout<<"#"; Elsecout<<Map[i][j]; } cout<<Endl;}}BOOLCheck () {intCounter =0;  for(inti =1; I <Ten; i++ )   for(intj =1; J <Ten; J + + )   if(Map[i][j]! =-1) Counter++; if(Counter = =Ten )  return true; Else  return false;}intMain () {intI, J, X, y;Charch; Srand (Time (0 ) );  Do{memset (map,-1,sizeof(map));//Initialize map All to-1, later with-1 to indicate areas not covered    for(i =0; I <Ten; ) {x= rand ()%9+1; Y= rand ()%9+1; if(Map[x][y]! =9) {Map[x][y]=9; I++; }  }    for(i =1; I <Ten; i++ )  {    for(j =1; J <Ten; J + +) cout<<"#"; cout<<"\ n"; } cout<<"\ n"; cout<<"Please enter a coordinate:";  while(Cin >> x >>y) {if(Map[x][y] = =9) {cout<<"GAME over"<< Endl;//Point in Thunder after the game is over and the position of the output Thunder     for(i =1; I <Ten; i++ )    {      for(j =1; J <Ten; 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;} 

Program:

The interface is a little rough, and it is through the coordinates of the demining, and until now do not understand how the program exactly how to enter the coordinates, but still thank the author it

C + + implements minesweeper (coordinates)

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.