Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # include <stdio.h>
- # include <stdlib.h>
- # include <time.h>
- # define N 5
- # define M 10
- void OpenSquareContainsZero(char board[ ][N], char flags[ ][N],int i,int j);
- int CountBombsLeft(char board[][N]);
- int CheckOpenedSquares(char flags[][N]);
- int CheckOpenedBomb(char board[][N], char flags[][N]);
- void InitBoards(char board[][N], char flags[][N]);
- int IsMine(char board[ ][N], int i, int j);
- void SetFlag(char flags[ ][N],int i,int j);
- void OpenSquare(char board[ ][N], char flags[ ][N],int i,int j);
- void PrintBoard(char board[ ][N],char flags[ ][N]);
- int CountOpenSquares(char flags[ ][N]);
- int CountBombsAround(char board[ ][N], int i, int j);
- void OpenSquareContainsZero(char board[ ][N], char flags[ ][N],int i,int j) // The function open a square if it is circled by 0 bombs, according to the rules
- {
- if(i<0 || i>N-1 || j<0 || j>N-1 || flags[i][j] == 'F' || CountBombsAround(board, i, j) != 0) return; // Breakpoint of the recursion
- else // For each square which circles the current square([i][j]) if the circling square is closed, it opens it, and do the same recursion for the circling square
- {
- flags[i][j] = '1'; // If the program entered here, it means that the 'If' above did not interrupt to the program, so make this square open
- if(i-1>=0 && i-1<=N-1 && j-1>=0 && j-1<=N-1 && flags[i-1][j-1] == '0') // For instance, if the row and the column of this square is legal, and the square is closed, then open it and make recursion for the square.
- {
- flags[i-1][j-1] = '1';
- OpenSquareContainsZero(board, flags, i-1, j-1);
- }
- if(i-1>=0 && i-1<=N-1 && j>=0 && j<=N-1 && flags[i-1][j] == '0')
- {
- flags[i-1][j] = '1';
- OpenSquareContainsZero(board, flags, i-1, j);
- }
- if(i-1>=0 && i-1<=N-1 && j+1>=0 && j+1<=N-1 && flags[i-1][j+1] == '0')
- {
- flags[i-1][j+1] = '1';
- OpenSquareContainsZero(board, flags, i-1, j+1);
- }
- if(i>=0 && i<=N-1 && j-1>=0 && j-1<=N-1 && flags[i][j-1] == '0')
- {
- flags[i][j-1] = '1';
- OpenSquareContainsZero(board, flags, i, j-1);
- }
- if(i>=0 && i<=N-1 && j+1>=0 && j+1<=N-1 && flags[i][j+1] == '0')
- {
- flags[i][j+1] = '1';
- OpenSquareContainsZero(board, flags, i, j+1);
- }
- if(i+1>=0 && i+1<=N-1 && j-1>=0 && j-1<=N-1 && flags[i+1][j-1] == '0')
- {
- flags[i+1][j-1] = '1';
- OpenSquareContainsZero(board, flags, i+1, j-1);
- }
- if(i+1>=0 && i+1<=N-1 && j>=0 && j<=N-1 && flags[i+1][j] == '0')
- {
- flags[i+1][j] = '1';
- OpenSquareContainsZero(board, flags, i+1, j);
- }
- if(i+1>=0 && i+1<=N-1 && j+1>=0 && j+1<=N-1 && flags[i+1][j+1] == '0')
- {
- flags[i+1][j+1] = '1';
- OpenSquareContainsZero(board, flags, i+1, j+1);
- }
- }
- }
- void InitBoards(char board[][N], char flags[][N]) // Initialization the boards
- {
- srand(time(NULL)); // Function to make the random to work well
- int rowpos, colpos, i, j;
- for(i=0; i<N; i++) // This loop makes the whole flag board as closed (All the squares are closed)
- {
- for(j=0; j<N; j++)
- {
- flags[i][j] = '0';
- }
- }
- for(i=0; i<M; i++) // The loop puts bombs in the board
- {
- rowpos = rand()%N; // Random a row position
- colpos = rand()%N; // Random a column position
- while(IsMine(board, rowpos, colpos) == 1) // If the program did random to square that already has bomb, it would make another random
- {
- rowpos = rand()%N;
- colpos = rand()%N;
- }
- board[rowpos][colpos] = 'X'; // After the program found a square that did not have already bomb it puts a bomb there
- }
- for(i=0; i<N; i++)
- {
- for(j=0; j<N; j++)
- {
- if(IsMine(board, i, j) == 0) board[i][j] = (char)(((int)'0')+CountBombsAround(board, i,j)); // If the square does not contain bomb, the square shows the number of the bomb that circle it
- }
- }
- }
- int IsMine(char board[ ][N], int i, int j) // The function checks if the square [i][j] contains bomb --> return 1, else return 0.
- {
- if(board[i][j] == 'X') return 1;
- return 0;
- }
- void SetFlag(char flags[ ][N],int i,int j) // If the square [i][j] has already 'F' the function make it closed. Otherwise the function mark the square as flag
- {
- if(flags[i][j] == '1')printf("This Square is already open.\n");
- else if(flags[i][j] == 'F') flags[i][j] = '0';
- else
- flags[i][j] = 'F';
- }
- void OpenSquare(char board[ ][N], char flags[ ][N],int i,int j) // The function opens square according the rules of the game
- {
- if(flags[i][j] == '1') printf("This square is already open.\n"); // If the user wants to open opened square then the message will show
- else if(board[i][j] == 'X' && flags[i][j] != 'F') // If the user chose to open a square which included bomb then...
- {
- flags[i][j] = '1'; // Make the square opened (a message of loss shows in the main...)
- PrintBoard(board, flags); // It prints the board of the game
- }
- else if(CountBombsAround(board, i, j) >= 1 && CountBombsAround(board, i, j) <= 8 && flags[i][j] != 'F') flags[i][j] = '1'; // If the square shows number in [1,8] then open it as usual
- else if(CountBombsAround(board, i, j) == 0) OpenSquareContainsZero(board, flags, i, j); // If the square shows '0' then open it according to the rules of the called function..
- else if(flags[i][j] == 'F') // If the user wants to open a square which he marked it as a bomb then....
- {
- int choice;
- printf("This square is flagged as a mine. Are you sure you want to open it?\n");
- printf("Enter your choice: 1. Yes 2. No\n");
- scanf("%d", &choice);
- if(choice == 1) flags[i][j] = '0'; // If the user wants to open it the program will make the square as close
- }
- }
- void PrintBoard(char board[ ][N],char flags[ ][N]) // The function prints the board
- {
- int i, j;
- printf(" 01234\n");
- printf(" -----\n");
- for(i=0; i<N; i++)
- {
- printf("%d%c",i, ':');
- for(j=0; j<N; j++)
- {
- if(board[i][j] == 'X')printf("X");
- if(flags[i][j] == '0' && board[i][j] != 'X')printf("%c", 178); // If the square is closed then it prints the char 178
- else if(flags[i][j] == 'F')printf("F"); // If the square marked as a bomb by the user, it prints the square as 'F'
- else if(flags[i][j] == '1') // If the square is opened...
- {
- if(IsMine(board, i, j) == 1)printf("X"); // If the square includes bomb it prints 'X'
- else
- printf("%d", CountBombsAround(board, i, j)); // Otherwise it prints the number of the bombs that circle the square
- }
- }
- printf("\n");
- }
- }
- int CountOpenSquares(char flags[ ][N]) // The function returns the number of the opened squares
- {
- int i, j, count = 0;
- for(i=0; i<N; i++)
- {
- for(j=0; j<N; j++)
- {
- if(flags[i][j] == '1')count++; // The function go all over the flags board and raise the count in 1 if the square is opened
- }
- }
- return count;
- }
- int CountBombsAround(char board[ ][N], int i, int j) // The function counts the number of the bombs that circle the square i,j
- {
- int count = 0;
- if(i == 0 && j == 0) // The case that the bomb in the left top corner of the board
- {
- if(board[i][j+1] == 'X')count++;
- if(board[i+1][j] == 'X')count++;
- if(board[i+1][j+1] == 'X')count++;
- }
- else if(i == N-1 && j == N-1) // The case that the square in the right bottom corner of the board
- {
- if(board[i-1][j] == 'X')count++;
- if(board[i][j-1] == 'X')count++;
- if(board[i-1][j-1] == 'X')count++;
- }
- else if(i == N-1 && j == 0) // The case that the square in the left bottom corner of the board
- {
- if(board[i][j+1] == 'X')count++;
- if(board[i-1][j] == 'X')count++;
- if(board[i-1][j+1] == 'X')count++;
- }
- else if(i == 0 && j == N-1) // The case that the square in the right top corner of the board
- {
- if(board[i][j-1] == 'X')count++;
- if(board[i+1][j] == 'X')count++;
- if(board[i+1][j-1] == 'X')count++;
- }
- else if(j == 0) // The case that the square is not in any corner, but the square is in the left edge of the board
- {
- if(board[i-1][j] == 'X')count++;
- if(board[i+1][j] == 'X')count++;
- if(board[i][j+1] == 'X')count++;
- if(board[i+1][j+1] == 'X')count++;
- if(board[i-1][j+1] == 'X')count++;
- }
- else if(j == N-1) // The case that the square is not in any corner, but the square is in the right edge of the board
- {
- if(board[i-1][j] == 'X')count++;
- if(board[i+1][j] == 'X')count++;
- if(board[i][j-1] == 'X')count++;
- if(board[i-1][j-1] == 'X')count++;
- if(board[i+1][j-1] == 'X')count++;
- }
- else if(i == 0) // The case that the square is not in any corner, but the square is in the top edge of the board
- {
- if(board[i][j-1] == 'X')count++;
- if(board[i][j+1] == 'X')count++;
- if(board[i+1][j] == 'X')count++;
- if(board[i+1][j-1] == 'X')count++;
- if(board[i+1][j+1] == 'X')count++;
- }
- else if(i == N-1) // The case that the square is not in any corner, but the square is in the bottom edge of the board
- {
- if(board[i][j-1] == 'X')count++;
- if(board[i][j+1] == 'X')count++;
- if(board[i-1][j] == 'X')count++;
- if(board[i-1][j-1] == 'X')count++;
- if(board[i-1][j+1] == 'X')count++;
- }
- else // The case that the square is not in any edge or corner of the board
- {
- if(board[i-1][j-1] == 'X')count++;
- if(board[i-1][j] == 'X')count++;
- if(board[i-1][j+1] == 'X')count++;
- if(board[i][j-1] == 'X')count++;
- if(board[i][j+1] == 'X')count++;
- if(board[i+1][j-1] == 'X')count++;
- if(board[i+1][j] == 'X')count++;
- if(board[i+1][j+1] == 'X')count++;
- }
- return count;
- }
- int CheckOpenedBomb(char board[][N], char flags[][N]) // The function checks if any square opened by the user and included bomb
- {
- int i, j;
- for(i=0; i<N; i++)
- {
- for(j=0; j<N; j++)
- {
- if(IsMine(board, i, j) == 1 && flags[i][j] == '1') return 1; // If any square in the board was opened and it included bomb then return 1
- }
- }
- return 0; // If the loop 'for' did not break by the 'return' it means the user did not open bomb so return 0
- }
- int CountBombsLeft(char flags[][N]) // The function counts the number of the bombs that left, by the mark of the flags
- {
- int i, j, count = M; // Count is M (Number of the bombs)
- for(i=0; i<N; i++)
- {
- for(j=0; j<N; j++)
- {
- if(flags[i][j] == 'F') count--; // If any square contains Flag (it means the user flagged the square) then count--...
- }
- }
- return count;
- }
- int main()
- {
- int choice, rowchoice, colchoice;
- char board[N][N]; // Creating board NxN
- char flags[N][N]; // Creating board NxN
- InitBoards(board, flags); // initialization the boards
- while(CheckOpenedBomb(board, flags) != 1 && CountOpenSquares(flags) != ((N*N)-M)) // The loop will stop once one of those cases will happen
- {
- PrintBoard(board,flags); // Each loop it prints the board
- printf("There are %d mines left.\nWhat would you like to do now:\n", CountBombsLeft(flags));
- printf("1. Open a new square.\n2. Flag a square as a mine.\n");
- scanf("%d", &choice); // Receive from the user the choice
- if(choice == 2) // The case the user will choose to flag a square as a mine
- {
- printf("Please insert row and column number: ");
- scanf("%d", &rowchoice); // Receive the row of the flag from the user
- scanf("%d", &colchoice); // Receive the column of the flag from the user
- SetFlag(flags, rowchoice, colchoice); // Put a flag into the square the user chose
- }
- if(choice == 1) // The case the user will choose to open any square
- {
- printf("Please insert row and column number: ");
- scanf("%d", &rowchoice); // Receive the row of the square that the user wants to open
- scanf("%d", &colchoice); // Receive the column of the square that the user wants to open
- OpenSquare(board, flags, rowchoice, colchoice); // The program will open the square of the user according to the rules of the game
- }
- }
- if(CountOpenSquares(flags) == ((N*N)-M))
- {
- PrintBoard(board,flags);
- printf("Congratulations!! You have won!");
- }
- else if(CheckOpenedBomb(board, flags) == 1) printf("Boom!!! You have lost.");
- // If the loop just stopped for any reason there are 2 cases which could caused that:
- // 1. The user opened a bomb, so the program will inform him he lost by the message above.
- // 2. The user won the game because he opened the whole squares but the bombs --> the sentence above will be informed to the user.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement