C + + to achieve a simple Minesweeper game (console version) _c language

Source: Internet
Author: User
Tags goto rand

C + + Novice Code, please forgive me.
C + + written in a simple console version of the minesweeper game. The player opens the box by entering the coordinates of the square.
is just a prototype, can let the player perform turn the box operation and judge wins and loses, has not added the Mark box, the game menu, the recording game time, the reopening one bureau and so on function.
Players to enter the coordinates of the way to open the box is only suitable for small "mined area", if the "minefield" large, with the coordinates will become very inconvenient.

Code fragment Minesweeper V1.1

#include <stdio.h> #include <Windows.h> #define YELLOW foreground_red | Foreground_green | Foreground_intensity #define CYAN Foreground_blue | Foreground_green | Foreground_intensity #define ORANGE foreground_red | Foreground_green | Foreground_intensity #define PURPLE foreground_red | Foreground_blue |
 
Foreground_intensity using namespace std;
const int STARTX = 30;
const int starty = 6; const int MAXX = 9;//wide const int maxy = 9;//mined area High const int bombnumber = 10;//Mine quantity class cube{Private:bool IFHAVEB omb;//Whether the box contains a bomb bool ifopen;//the box has not been opened by the player int nearbombnumber;//the number of boxes containing bombs around the block of 8 public:void Setopen () {//Will Ope
  Change the value of N to True ifopen = true;
  BOOL Getopen () {//Get Ifopen value return ifopen;
  } void Setnearbombnumber (int number) {//give Nearbombnumber assignment nearbombnumber = number;
  } void Havebomb () {//to place mine Ifhavebomb = true;
  BOOL Getifhavebomb () {//Get Ifhavebomb value return ifhavebomb; int Getnearbombnumber () {//Get Nearbombnumber value return nearbombnumber; } void Resetcube (bool Ifhavebomb = FALSE, bool Ifopen = false, int nearbombnumber = 0) {//Initialize member data Ifhavebomb = i
    Fhavebomb;
    Ifopen = Ifopen;
  Nearbombnumber = Nearbombnumber;
}
};
 
Cube Cube[maxx][maxy]; void GoTo (int x, int y);/position cursor void Setbomb (int bombnumber);//Generate Bombnumber bomb and put into a random box void show ()//Display mine array int checkands Etnearbombnumber (int x, int y);/Check the number of mines around the current box void Gamestart ();//Initialize game void Showxy ()//display minefield coordinates bool player (BOOL &life
//Player input coordinates turn on the box void message (bool life);//player game after the end of the output of the information void AutoOpen (int x,int y);/player opened the box for no thunder and around the box without thunder, automatically turn around the box without Thunder
  BOOL Ifwin ()//To determine whether the player has mine clearance success void Showbomb ()//show mine position int main () {Gamestart () after game end;
  Show ();
  BOOL life = true, Win = true;
  while (the player (life) &&!ifwin ()) {} message (Life && Ifwin ());
return 0;
  } void GoTo (int x, int y) {//positioning cursor COORD COORD = {X,y};
SetConsoleCursorPosition (GetStdHandle (std_output_handle), coord);
 
}void Setbomb (int bombnumber = bombnumber) {//Generate Bombnumber bombs and put in random squares srand ((unsigned) getcurrenttime ());
    while (bombnumber--) {int x = MAXX + 1, y = Maxy + 1;
      while ((x >= MAXX | | y >= maxy) | | Cube[x][y].getifhavebomb () = = True) {x = rand ()% MAXX;
    y = rand ()% Maxy;
  } cube[x][y].havebomb ();
  } void Show () {//Display mine array system ("CLS");
  Showxy ();
  Setconsoletextattribute (GetStdHandle (std_output_handle), cyan);
    for (int i = 0;i < maxy;i++) {GoTo (startx, starty + i); for (int j = 0;j < maxx;j++) {if (cube[j][i].getopen () = = True) {if (Cube[j][i].getifhavebomb () = = Fals
          e) {if (cube[j][i].getnearbombnumber () = = 0) {//Dug up the box to show the number of squares around the box with thunder, if 0 will show space printf ("");
          else {printf ("%d", Cube[j][i].getnearbombnumber ());
    } else {printf ("x");//There is a thunder box that is dug up to show X} else {printf ("");//Open box with display} }}} VoiD showxy () {//Display Axis Setconsoletextattribute (GetStdHandle (std_output_handle), cyan);
  GoTo (STARTX-3, Starty + MAXY/2);
  printf ("Y");
  GoTo (StartX + MAXX, STARTY-2);
  printf ("X");
  Setconsoletextattribute (GetStdHandle (Std_output_handle), yellow);
    for (int i = 0;i < maxy;i++) {GoTo (STARTX-1, starty + i);
  printf ("%d", I);
    for (int i = 0;i < 2 * maxx;i + + 2) {GoTo (startx + i + 1, STARTY-1);
  printf ("%d", I/2);
   
  an int checkandsetnearbombnumber (int x, int y) {//Check the number of mines around the current box int num = 0;
  if (cube[x][y].getifhavebomb () = = True) {//If the square has a landmine, do not have to judge a few thunder return 0 around it; else {///two loops around the current square 8 squares sweep for (int i =-1; I <= 1; i++) {for (int j =-1; J <= 1; j +) {in
        T NX = x + i;
        int ny = y + j; if (!) ( NY = y && nx = x) && (NX >= 0 && NX <= MAXX-1) && (NY >= 0 &&am P NY <= MAXY-1)) {if (Cube[nx][ny].getifhaveboMB ()) {num++;
  {}}} cube[x][y].setnearbombnumber (num);//Set the number of mines near the box return 0; } void Gamestart () {//initializing game for (int i = 0;i < maxy;i++) {for (int j = 0;j < maxx;j++) {Cube[j]
    [I].resetcube ();
  } setbomb ();
    for (int i = 0;i < maxy;i++) {for (int j = 0;j < maxx;j++) {Checkandsetnearbombnumber (J, I);
  } BOOL Player (bool &life) {//player input coordinates open square int x, y;
  GoTo (STARTX-3, Starty + Maxy + 1);
  printf ("Please input coordinates (X,Y), X and y separated by spaces");
  GoTo (StartX + MAXX/2, Starty + Maxy + 2);
  scanf ("%d%d", &x, &y); if ((x < 0) | | (x > MAXX-1) | | (Y < 0) | | (Y > MAXY-1))
    {///When the player enters the coordinates out of range show ();
    GoTo (STARTX-3, Starty + Maxy + 3);
    printf ("The coordinates do not exist, please re-enter the coordinates");
  GoTo (StartX + MAXX/2, Starty + Maxy + 2);
    else if (cube[x][y].getifhavebomb () = = True) {//Cube[x][y].setopen () when the player opens a square with a mine;
    Show ();
    Life = false; return false;
  else if (cube[x][y].getopen () = = False) {//When the player opens the box without Thunder if (cube[x][y].getnearbombnumber () = 0) {Auto
      Open (x, y);
      Cube[x][y].setopen ();
    Show ();
      else {cube[x][y].setopen ();
    Show ();
    } else if (cube[x][y].getopen () = = True) {//Show () when the player enters the coordinates of the open square;
    GoTo (startx, Starty + Maxy + 3);
    printf ("The box has been dug up, please enter the coordinates again");
  GoTo (StartX + MAXX/2, Starty + Maxy + 2);
  } ifwin ();
return true;
    } void Message (Boolean result) {if (result = = = True) {////////Showbomb ();
    Setconsoletextattribute (GetStdHandle (Std_output_handle), yellow);
    GoTo (STARTX-1, Starty + Maxy + 1); printf ("Congratulations, you've won!")
    ");
  GoTo (startx, Starty + Maxy + 2);
    else {////The information Showbomb () when the player fails;
    Setconsoletextattribute (GetStdHandle (std_output_handle), PURPLE);
    GoTo (STARTX-1, Starty + Maxy + 1);
    printf ("xx You stepped on the Mine" xx);
  GoTo (startx, Starty + Maxy + 2); } void AutoOpen (int x, int y) {//player opened box for no thunder and no thunder Aroundbox, automatically turn around the square for the Lightning (int i =-1; I <= 1; i++) {for (int j =-1; J <= 1; j +) {int NX = x + i;
      int ny = y + j; if (!) (  NY = y && nx = x) && (NX >= 0 && NX <= MAXX-1) && (NY >= 0 &&
          NY <= MAXY-1) && cube[nx][ny].getopen () = = False) {if (cube[nx][ny].getnearbombnumber () = 0) {
          Cube[nx][ny].setopen ();
        AutoOpen (NX, NY);
        else {cube[nx][ny].setopen ();
  BOOL Ifwin () {//To determine whether the player has successfully reached the game end condition int num = 0;
        for (int i = 0;i < maxx;i++) {for (int j = 0;j < maxy;j++) {if (Cube[j][i].getopen () = False) {
      num++;
  }} if (num = = Bombnumber) {return true;
  else {return false;  } void Showbomb () {//Show mine location for after game (int i = 0;i < maxy;i++) {for (int j = 0;j < maxx;j++) {if (Cube[j][i].getifhavebomb () = = True) {Cube[j][i].setopen();
}} show (); }

Effect Chart:

The above is the entire content of this article, I hope to help you learn.

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.