C language new handwritten minesweeper source code

Source: Internet
Author: User

Release the source code today, due to write in a file very messy, so divided into three files written

Drawing and mouse functions graph.h

/* Drawing with mouse-related functions */#include <graphics.h> #include <easyx.h>void drawempty (int, int, int, int)//function void to initialize the empty lattice interface Drawsmile (void);//The function of the smiley face is void drawredflag (int, int);//Right-click the red flag function int MouseX, mousey;//mouse x, y coordinates mousemsg m;//mouse structure variable, Accept mouse messages void Drawsmile (void) {Setfillstyle (bs_solid); Setfillcolor (YELLOW); FillEllipse (90, 5, 110, 25);//Draw an ellipse (smiley) function, The parameter is the upper-left lower-right corner coordinate of the ellipse bounding rectangle Setfillstyle (bs_solid); Setfillcolor (BLACK);//Draw the eye fillellipse (x, x, y, n); FillEllipse (103, 13, 107, Bar (97, 20, 103, 21);//Draw Mouth}void drawredflag (int i, int j) {Setfillstyle (bs_solid); Setfillcolor (RED); Bar (8 + J * 20 , + i *, 8 + J * + 5, + i * + 5), SetColor (BLACK), line (8 + J *, + I *, 8 + J *), + i * 20 + 10 );} void Drawempty (int i, int j, int mode, int color)//Draw 16*16 's small lattice {setfillstyle (bs_solid); Setfillcolor (color); if (mode = = 0) {b AR (Ten + J * 20-8, + i * 20-8, + J * + 8, + + I * 20 + 8);} else if (mode = = 1) {Bar (ten + J * 20-7, + i * 20-7, + J * + 7, + + I * 20 + 7);}} void MousegetXY (void)//Get the position of the mouse {m = getmousemsg (); MouseX = m.x; Mousey = m.y;}

Auxiliary calculation function Auxiliary.h

/* Auxiliary function */#include <conio.h>//_kbhit () struct{int num;//lattice Current state, 1 for Thunder, 0 for no thunder, or already showing number int roundnum;//statistic lattice around the number of mines int flag;//Right-click to display the red flag, 0 means no, 1 means there are}mine[10][10];int minenum;//statistics processed TCHAR randminenum[100];//Display the string of numbers */count the number of mines around each lattice * /int minestatistics (int i, int j) {int nnum = 0;if (i = = 0 && j = 0)//upper left corner lattice statistics {if (Mine[0][1].num = 1) nnum++;if ( Mine[1][0].num = = 1) nnum++;if (mine[1][1].num = = 1) nnum++;} ElseIf (i = = 0 && j = 9)//The statistics of the upper right corner lattice {if (mine[0][8].num = = 1) nnum++;if (mine[1][9].num = = 1) nnum++;if (MINE[1][8].N Um = = 1) nnum++;} ElseIf (i = = 9 && J = = 0)//left bottom grid statistics {if (Mine[8][0].num = = 1) nnum++;if (mine[9][1].num = = 1) nnum++;if (MINE[8][1].N Um = = 1) nnum++;} ElseIf (i = = 9 && j = 9)//Right bottom corner of the grid statistics {if (Mine[9][8].num = = 1) nnum++;if (mine[8][9].num = = 1) nnum++;if (MINE[8][8].N Um = = 1) nnum++;} else if (j = = 0)//The statistics of the first column on the left {if (mine[i][j + 1].num = = 1) nnum++;if (mine[i + 1][j].num = 1) nnum++;if (mine[i-1][j].num = = 1) nnum++;if (mine[i-1][j + 1].num = 1) nnum++;if (MiNe[i + 1][j + 1].num = = 1) nnum++;} else if (j = = 9)//The statistic {if (Mine[i][j-1].num = 1) nnum++;if (mine[i + 1][j].num = 1) of the first column on the right is nnum++;if (mine[i-1][j].num = = 1) nnum++;if (mine[i-1][j-1].num = = 1) nnum++;if (mine[i + 1][j-1].num = 1) nnum++;}  else if (i = = 0)//The statistics of the first row lattice {if (mine[i + 1][j].num = = 1) nnum++;if (mine[i][j-1].num = = 1) nnum++;if (mine[i][j + 1].num = = 1) nnum++;if (mine[i + 1][j-1].num = = 1) nnum++;if (mine[i + 1][j + 1].num = = 1) nnum++;} else if (i = = 9)//The last row of the grid statistics {if (Mine[i-1][j].num = = 1) nnum++;if (mine[i][j-1].num = = 1) nnum++;if (mine[i][j + 1].num = = 1) nnum++;if (mine[i-1][j-1].num = = 1) nnum++;if (mine[i-1][j + 1].num = 1) nnum++;} else//General Lattice Statistics {if (Mine[i-1][j].num = = 1) nnum++;if (mine[i-1][j + 1].num = 1) nnum++;if (mine[i][j + 1].num = 1) nnum++ if (mine[i + 1][j + 1].num = = 1) nnum++;if (mine[i + 1][j].num = 1) nnum++;if (mine[i + 1][j-1].num = 1) nnum++;if (Mine I [j-1].num = 1] nnum++;if (mine[i-1][j-1].num = = 1) nnum++;} Return nnum;//around the latticeTotal number of LEI number of statistical results returned}void showwhite (int i, int j)//show the blank portion of the mined-out area {if (Mine[i][j].flag = = 1 | | Mine[i][j].num = = 0)//If there is a red flag or if the lattice has been processed, no judgment will be made on the lattice return;minenum--;//shows a number or a space in the lattice is more processing a lattice, when all the lattice has been processed to indicate victory if (mine[i][ J].roundnum = = 0 && mine[i][j].num! = 1)//Display space {Drawempty (I, J, 1, White); Mine[i][j].num = 0;} ElseIf (Mine[i][j].roundnum! = 0)//output Thunder number {Drawempty (i, J, 1, White); _stprintf_s (Randminenum, _t ("%d"), Mine[i][j]. Roundnum); SetColor (RED); Outtextxy (6 + J *, + + i *, randminenum); Mine[i][j].num = 0;//has been output the number of the grid with 0 means that the grid return has been used;} /*8 display all blank lattices recursively */if (i! = 0 && Mine[i-1][j].num! = 1) showwhite (I-1, j); if (i! = 0 && J! = 9 &&am P Mine[i-1][j + 1].num! = 1) showwhite (i-1, J + 1); if (j! = 9 && mine[i][j + 1].num! = 1) Showwhite (i, j + 1); if ( J! = 9 && I! = 9 && mine[i + 1][j + 1].num! = 1) showwhite (i + 1, j + 1); if (i! = 9 && mine[i + 1][) J].num! = 1) showwhite (i + 1, j); if (i! = 9 && J! = 0 && mine[i + 1][j-1].num! = 1) Showwhite (i + 1, j-1); if (j! = 0 && Mine[i][j-1].num! = 1) showwhite (i, j-1); if (i! = 0 && J! = 0 &A mp;& Mine[i-1][j-1].num! = 1) showwhite (i-1, j-1);}

Main function Main.cpp

#include <stdio.h> #include "graph.h" #include "auxiliary.h" #include <stdlib.h>//rand (), Srand () #include <time.h>//srand ((unsigned) time (NULL)) void game (void);//main program void gamebegin (void);//game start void GamePlay (void) ;//game Execution process void gameover (void);//Game over void Gamewin (void);//game win int play = 0;//whether the first time to play the game flag int flag = 1;//The game fails to restart after the flag int AGAIN = 0;//Game restarts in the middle of the flag int main (void) {Initgraph ($, closegraph, showconsole);//Initialize game interface and size game ();//Run Game ();// Close the graphical interface}void game (void) {while (1) {if (FLAG = = 1) {gamebegin ();//Draw out the game interface and determine whether to play and restart gameplay ();//The function if (AGAIN = = 1) of the game procedure { AGAIN = 0;continue;}} FLAG = 0;if (m.umsg = = wm_lbuttondown)//left mouse button pressed event {mousegetxy (); if (MouseX > && mousex<110 && Mouse Y>5 && Mousey <) {FLAG = 1;continue;}} if (_kbhit ())//Determine if there is a key to exit {break;}} void Gamebegin (void) {int i, j;cleardevice (); PLAY = 1;minenum = 0;setfillstyle (bs_solid);//Here the usage and TC differ setfillcolor (white); bar (0, 0, 200, 230); Set the background area for (i = 0; i < i++)//plot each mined area (small) {for (j = 0; J < 10; J + +) {Drawempty (i, J, 0, Lightgray);}} Drawsmile ();//Draw the middle of the smiley face Srand ((unsigned) time (NULL)),//The number of random number of different seed numbers for (i = 0; i <; i++) {for (j = 0; J <; J + +) {M Ine[i][j].num = rand ()% 8;//random number generation range 0-7if (mine[i][j].num = = 1) {minenum++;} Else{mine[i][j].num = 2;} printf ("%3d", mine[i][j].num); Mine[i][j].flag = 0;} printf ("\ n"); _stprintf_s (Randminenum, _t ("%d"), minenum);//Convert Minrnum to String type SetBkColor (white); SetColor (RED);  Settextstyle (0, _t ("0")); Outtextxy (2, 2, randminenum);} Minenum = 100-minenum;} void GamePlay (void)/* Game Procedure */{int I, j, Num = 0;/*num is used to receive statistical functions to return the number of mines around a lattice */for (i = 0; i <; i++) for (j = 0; j<10; J + +) Mine[i][j].roundnum = Minestatistics (i, j);/* Count the number of mines around each lattice */while (!_kbhit ()) {m = getmousemsg (); switch (m.umsg) {   Case wm_lbuttondown:{Mousegetxy (); if (mousex>90 && mousex<110 && mousey>5 && mousey<25)/* * Re-come */{MessageBox (NULL, TE   XT ("Re-start Success"), TEXT ("YES"), MB_OK);   AGAIN = 1;   Return } if (Mousex>0 && mousex<200 && mousey>30 && mousey < 230)/* Current mouse position within the grid range */{j = (MouseX)/20;/*x coordinates */   i = (MouseY-30)/20;/*y coordinates */if (Mine[i][j].flag = = 1)/* If the lattice has a red flag the left key is invalid */continue;   if (mine[i][j].num! = 0)/* If the lattice has not been processed */{if (Mine[i][j].num = = 1)//* Mouse pressed grid is mine */{Gameover ();/* Game failed */break;   } else/* the mouse pressed grid is not mine */{Num = Minestatistics (i, j);   if (Num = = 0)/* No mines around, use a recursive algorithm to display the blank lattice */Showwhite (i, j);   else/* presses the grid around a mine */{_stprintf_s (Randminenum, _t ("%d"), Num);/* Outputs the number of mines around the current grid */Drawempty (I, J, 1, White);   SetColor (RED);   Outtextxy (6 + J *, + + i *, randminenum);   minenum--;   } Mine[i][j].num = 0;/* The number of mines around the lattice to 0 indicates that the lattice has been used */if (Minenum < 1)/* won */{Gamewin ();   Break   }}}}}case wm_rbuttondown:{mousegetxy (); if (MouseX > 0 && mousex<200 && mousey>30 && mousey < 230)/* Current mouse position within the grid range */{j = ( MouseX)/20;/*x coordinates */i = (MouseY-30)/20;/*y coordinates *///messagebox (NULL, TEXT ("Right-click Test"), Text ("YES"), MB_OK);   if (Mine[i][j].flag = = 0 && mine[i][j].num! = 0)/* Originally no red flag now displays the red flag */{Drawredflag (i, j);   Mine[i][j].flag = 1;   } else if (Mine[i][j].flag = = 1)/* has a red flag and then right-click on the red flag disappears */{Drawempty (i, J, 0, Lightgray);   Mine[i][j].flag = 0; }}}}}}void Gameover (void) {int I, j;for (i = 0; i <; i++) {for (j = 0; J <; J + +) {if (Mine[i][j].num = = 1)//Display All Mines {Drawempty (I, J, 0, White); Setfillstyle (bs_solid); Setfillcolor (RED); FillEllipse (3 + J *), + i *, + + J * 20, SetBkColor (white), SetColor (RED), Settextstyle (0, _t ("Arial")), Outtextxy (2, 2, _t ("Lose Again"));}}} void Gamewin (void) {SetBkColor (white); SetColor (RED); Settextstyle (0, _t ("Arial")); Outtextxy (2, 2, _t ("You Won"));}
More features are being perfected, to be continued!!

C language new handwritten minesweeper source code

Related Article

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.