Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <string.h>
- #include <time.h>
- #define Nmax 30
- /*
- N.W N N.E
- \ | /
- \ | /
- W----Cell----E
- / | \
- / | \
- S.W S S.E
- Cell-->Current Cell (X, Y)
- N --> North (X, Y-1)
- S --> South (X, Y+1)
- E --> East (X+1, Y)
- W --> West (X-1, Y)
- N.E--> North-East (X+1, Y-1)
- N.W--> North-West (X-1, Y-1)
- S.E--> South-East (X+1, Y+1)
- S.W--> South-West (X-1, Y+1)
- */
- struct node{
- char bomb;
- bool click;
- };
- typedef struct node cell;
- void Mines(cell **g, int n, int m);
- void Sweep(cell **g, int n, int x, int y);
- void Num(cell **g, int n);
- int Count(cell **g, int n, int x, int y);
- void Print(cell **g, int n);
- void PrintAns(cell **g, int n, bool cheat);
- int main(int argc, char *argv[]){
- int N = atoi(argv[1] );
- int M = atoi(argv[2] );
- srand(time(NULL) );
- if(N > Nmax){
- printf("error\n");
- return 0;
- }
- for(int j = 0; j < N; j++){
- for(int i = 0; i < N; i++)
- printf("? ");
- printf("\b\n");
- }
- printf("\n");
- cell **grid = calloc(N + 2, sizeof(cell *) );
- for(int i = 0; i < N + 2; i++)
- *(grid + i) = calloc(N + 2, sizeof(cell) );
- for(int i = 0; i < N + 1; i++) ( *(grid + 0) + i)->click = true;
- for(int i = 1; i < N + 2; i++) ( *(grid + N + 1) + i)->click = true;
- for(int j = 1; j < N + 2; j++) ( *(grid + j) + 0)->click = true;
- for(int j = 0; j < N + 1; j++) ( *(grid + N + 1) + j)->click = true;
- Mines(grid, N, M);
- Num(grid, N);
- int X, Y;
- const char C1[] = "eagle\0";
- const char C2[] = "eye\0";
- char Sx[7];
- char Sy[5];
- bool cheat = false;
- unsigned int W;
- while(scanf("%s%s", Sx, Sy) == 2){
- if( strcmp(Sx, C1) && strcmp(Sy, C2) ){
- X = atoi(Sx) + 1;
- Y = atoi(Sy) + 1;
- }else{
- if(cheat) system("clear");
- cheat = true;
- PrintAns(grid, N, cheat);
- continue;
- }
- if(X > N || Y > N){
- printf("error\n");
- break;
- }
- if( ( *(grid + Y) + X )->bomb == 'X' ){
- printf("You are dead!!\n");
- break;
- }
- if( ( *(grid + Y) + X)->click ){
- printf("You have clicked!!\n");
- continue;
- }
- Sweep(grid, N, X, Y);
- W = 0;
- for(int j = 1; j < N + 1; j++){
- for(int i = 1; i < N + 1; i++){
- if( !( *(grid + j) + i)->click )
- W++;
- }
- }
- if(W == M){
- printf("You win!!\n");
- break;
- }
- system("clear");
- PrintAns(grid, N, cheat);
- Print(grid, N);
- }
- free(grid);
- return 0;
- }
- void Mines(cell **g, int n, int m){
- int tempX, tempY;
- for(int i = 0; i < m; i++){
- tempX = rand() % n + 1;
- tempY = rand() % n + 1;
- if( ( *(g + tempY) + tempX )->bomb == 'X' )
- i--;
- else
- ( *(g + tempY) + tempX )->bomb = 'X';
- }
- }
- void Num(cell **g, int n){ // can be advanced
- for(int j = 1; j < n + 1; j++){
- for(int i = 1; i < n + 1; i++){
- if( ( *(g + j) + i)->bomb != 'X' )
- ( *(g + j) + i)->bomb = '0' + Count(g, n, i, j);
- }
- }
- }
- void Sweep(cell **g, int n, int x, int y){
- if( ( *(g + y) + x )->click == true)
- return;
- ( *(g + y) + x )->click = true;
- if( ( *(g + y) + x )->bomb != '0')
- return;
- Sweep(g, n, x, y - 1);
- Sweep(g, n, x + 1, y);
- Sweep(g, n, x, y + 1);
- Sweep(g, n, x - 1, y);
- Sweep(g, n, x + 1, y + 1);
- Sweep(g, n, x + 1, y - 1);
- Sweep(g, n, x - 1, y + 1);
- Sweep(g, n, x - 1, y - 1);
- /*
- for(int j = -1; j <= 1; j++){
- for(int i = -1; i <= 1; i++){
- Sweep(g, n, x + i, y + j);
- // note that there's a situation where i = 0, j = 0
- }
- }
- */
- return;
- }
- int Count(cell **g, int n, int x, int y){
- int cnt = 0;
- for(int j = -1; j <= 1; j++){
- for(int i = -1; i <= 1; i++){
- if( (*(g + y + j) + x + i)->bomb == 'X' ) cnt++;
- // note that there's a situation where i = 0, j = 0
- }
- }
- return cnt;
- }
- void Print(cell **g, int n){
- /*
- for(int j = 1; j < n + 1; j++){
- for(int i = 1; i < n + 1; i++)
- printf("%c ", ( *(g + j) + i)->bomb);
- printf("\b\n");
- }
- */
- for(int j = 1; j < n + 1; j++){
- for(int i = 1; i < n + 1; i++){
- if( ( *(g + j) + i)->click && ( *(g + j) + i)->bomb != 'X'){
- if( ( *(g + j) + i)->bomb == '0' )
- printf("- ");
- else
- printf("%c ", ( *(g + j) + i)->bomb);
- }else printf("? ");
- }
- printf("\b\n");
- }
- printf("\n");
- }
- void PrintAns(cell **g, int n, bool cheat){
- if(cheat){
- for(int j = 1; j < n + 1; j++){
- for(int i = 1; i < n + 1; i++)
- ( *(g + j) + i)->bomb == 'X' ? printf("* ") : printf("- ");
- printf("\b\n");
- }
- printf("\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement