Demining straightforward codes c

1, header files, function declarations
MS.h

#ifndef GAME_H
#define GAME_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define Row 12 is
#define COL 12 is
#define COUNT 10 // chessboard the total number of mine
extern char show_mine [row] [col ]; // display array
extern char real_mine [row] [col ]; // array Bree
void muen (); // menu function
void init_mine (); // initialize an array function
void set_mine (); // Bray function
int count_mine (); // statistics around the number of mine
void print_player (); // print the players checkerboard
void print_mine (); // print designer checkerboard
int sweep_mine (); / / demining function
void open_mine (int x, int y ); // expansion function
int count_show_mine (); // determine the number of players remaining unknown region of the board
#endif // GAME_H_

2, the main function
MS.c

_CRT_SECURE_NO_WARNINGS #define
#include "MS.h"
void Game ()
{
int RET = 0;
init_mine (); // initialize the player board and board designers
set_mine (); // give designers the checkerboard Bray
print_player (); // Print player board
if (count_show_mine () == COUNT) // win situation step of
{
print_mine ();
printf ( "player wins \ the n-\ the n-!");
return;
}
the while (1) // loop Minesweeper
{
int ret = sweep_mine (); // mine clearance, mine stepped returns 1, no step on mine returned 2;
IF (count_show_mine () == cOUNT) // If a player chessboard '*' number is the number of mine, game victory
{
print_mine (); // print designer board
printf ( "player wins \ n \ n");
BREAK;
}
iF (RET) // determine whether or not to step on mine
{
printf ( "Ray was killed \ n \ n" );
print_mine (); // Print the array to see the distribution of designer Ray Ray array
break;
}
Print_player (); // print board player
}
}
int main ()
{
srand ((unsigned int) Time (NULL)); generating a random number generator //
int INPUT = 0;
Muen (); // menu
do
{
Scanf ( "% D", & iNPUT);
Switch (iNPUT)
{
Case. 1: Game ();
BREAK;
Case (0): Exit (. 1);
BREAK;
default:
the printf ( "input error, re-enter \ n") ;
BREAK;
}
Muen ();
the printf ( "contiue \ n-?");
} the while (. 1);
System ( "PAUSE");
return 0;
}

3; function body
twst.c

#define _CRT_SECURE_NO_WARNINGS
#include"MS.h"
char show_mine[row][col] = { 0 };
char real_mine[row][col] = { 0 };
void muen()
{
printf(“\n ");
printf("1.play 0.exit
\n");
printf("
** \n”);
}
void init_mine()//初始化两个棋盘
{
int i = 0;
int j = 0;
for (int i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
show_mine[i][j] = ‘’;
real_mine[i][j] = ‘0’;
}
}
printf("\n");
}
void print_player()//打印玩家棋盘
{
int i = 0;
int j = 0;
the printf ( "0");
for (I =. 1; I <Row -. 1; I ++)
{
the printf ( "% 3D", I); // print abscissa (0-10)
}
the printf ( "\ n-");
for (i = 1; i < row - 1; i ++) // Print vertical scale (1-10)
{
the printf ( "% 2D", I);
for (J =. 1; J <COL -. 1; J ++)
{
printf ( "% 3c", show_mine [i] [j]); // player checkerboard array
}
the printf ( "\ n-");
}
}
void print_mine () // print board designer
{
int I = 0;
int J 0 =;
the printf ( "0");
for (I =. 1; I <Row -. 1; I ++)
{
the printf ( "% 3D", I); // print abscissa (0-10)
}
the printf ( "\ n- ");
for (I =. 1; I <Row - 2; I ++) // Print vertical scale (1-10)
{
the printf ("% 2D ", I);
for (J =. 1; J <COL -. 1; J ++)
{
the printf ( "%. 3C", real_mine [I] [J]);
}
the printf ( "\ n-");
}
the printf ( "\ n-");
}
void set_mine () // board designers to mine
{
int X = 0;
int Y = 0;
int COUNT = COUNT; // Number of mine
while (count) // out of the loop after Reb
{
int X = RAND () 10 + 1%; // generates a random number from 1 to 10, in the array subscript range of 1 to 10 Neibu Lei
int y = rand ()% 10 + 1; // generates a random number from 1 to 10, in an array Nei Bulei index in the range of 1 to 10
if (real_mine [x] [y ] == '0') // find a place not mine mine
{
real_mine [X] [Y] = '. 1';
Count -;
}
}
}
int count_mine (int X, Y int) // eight regions surrounding detecting the number of mine
{
int COUNT = 0;
IF (real_mine [X -. 1] [Y -. 1] == '. 1')
COUNT ++;
IF (real_mine [X -. 1] [Y] == '. 1')
COUNT ++;
IF (real_mine [X -. 1] [Y +. 1] == '. 1')
COUNT ++;
IF (real_mine [X] [Y -. 1] == '. 1')
COUNT ++;
IF (real_mine [X] [Y +. 1] == '. 1')
COUNT ++;
IF (real_mine [X +. 1] [Y -. 1] == '. 1')
COUNT ++ ;
IF (real_mine [X +. 1] [Y] == '. 1')
COUNT ++;
IF (real_mine [X +. 1] [Y +. 1] == '. 1')
COUNT ++;
return COUNT;
}
int sweep_mine () / / demining function returns a stepped mine, have not stepped Ray returns 0
{
int X = 0;
int Y = 0;
int COUNT = 0;
the printf ( "input coordinates demining");
scanf_s ( "% D% D", & x, & y); // only inputs 1 to 10
iF ((X> =. 1 && X <= 10) && (Y> =. 1 && Y <= 10)) // determine whether the input position error, input error re-enter
{
IF (real_mine [X] [Y] == '0') is not stepped on mine //
{
char count_mine = CH (X, Y);
show_mine [X] [Y] = CH + '0'; // number ASCII value and numeric characters corresponding to the ASCII value of the phase difference 48, i.e., '0' ASCII values
open_mine (X, Y);
IF (count_show_mine () == COUNT) // number of remaining unknown region is determined, the number of Ray player wins the number
{
print_mine ();
the printf ( "player wins!");
return 0;
}
}
the else IF (real_mine [X] [Y] == '. 1') // stepped Ray
{
return. 1;
}
}
the else
{
the printf ( "input error re-enter");
}
return 0; // no stepped mine
}
void open_mine (int X, Y int) // expansion function around the coordinates
{
IF (real_mine [X -. 1] [Y --1] == '0')
{
show_mine [x - 1] [y - 1] = count_mine (x - 1, y - 1) + '0'; // display the number of coordinates around mine
}
IF (real_mine [X -. 1] [Y] == ' 0 ')
{
show_mine [X -. 1] [Y] = count_mine (X -. 1, Y) +' 0 '; // display the number of coordinates around mine
}
IF (real_mine [X -. 1] [Y +. 1] = = '0')
{
show_mine [X -. 1] [Y +. 1] = count_mine (X -. 1,. 1 + Y) + '0'; // display the number of coordinates around mine
}
IF (real_mine [X] [Y -. 1] == '0')
{
show_mine [X] [Y -. 1] = count_mine (X, Y -. 1) + '0'; // display the number of coordinates around mine
}
IF (real_mine [X] [Y . 1 +] == '0')
{
show_mine [X] [Y +. 1] = count_mine (X, Y +. 1) + '0'; // display the number of coordinates around mine
}
IF (real_mine [X +. 1] [Y -. 1] == '0')
{
show_mine [X +. 1] [Y -. 1] = count_mine (. 1 X +, Y -. 1) + '0'; // display the number of coordinates around the mine
}
IF (real_mine [. 1 + X] [Y] == '0')
{
show_mine [. 1 + X] [Y] = count_mine (. 1 + X, Y) + '0'; // display the number of coordinates around the mine
}
IF (real_mine [X +. 1] [Y +. 1] == '0')
{
show_mine [X +. 1] [Y +. 1] = count_mine (. 1 + X, + Y. 1) + '0'; // the number of display coordinates around mine
}
}
int count_show_mine () // determine the remaining unknown region number, the number is the number Ray player wins
{
int COUNT = 0;
int I = 0;
int J = 0;
for (I = . 1; I <= Row - 2; I ++)
{
for (J =. 1; J <= COL - 2; J ++)
{
IF (show_mine [I] [J] == '
')
{
COUNT ++;
}
}
}
return COUNT ;
}

Guess you like

Origin blog.csdn.net/belongHWL/article/details/90262819