Today is the function of the game process, the basic algorithm has been explained before, today is to achieve basic functions
Add the header files you need to use earlier
#include <conio.h> //_kbhit () #include <stdlib.h>//rand (), Srand () #include <time.h> // Srand ((unsigned) time (NULL))
The process of the game is the mouse operation process, the realization is the mouse click Processing, because not WINAPI button click, so you have to calculate the coordinates to get the mouse action
void Mousegetxy (void) {m = Getmousemsg (); MouseX = m.x; Mousey = m.y;}
M is the structure of the mouse, preceded by a defined, specific structure to view the front or Easyx help
Then it is the mouse operation of the game process
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)/* The current mouse position is 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);/* LoseThe 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; } }}}}}
Switch case only two, left and right mouse button operation
Then is the game victory or game failure function, are relatively simple
void Gameover (void) {int I, j;for (i = 0; i <; i++) {for (j = 0; J < Ten; J + +) {if (Mine[i][j].num = = 1)//Show all mines {Draw Empty (i, J, 0, White); Setfillstyle (bs_solid); Setfillcolor (RED); FillEllipse (3 + J *, + + i *, +-J *), + i * 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"));}
At this point the game is completed, the basic functions are realized, we can also add more advanced features, such as limit the number of thunder, Countdown, etc., the next time to release the complete code
C Language New handwriting minesweeper Strategy 3