Rules
remine.c
#include <stdio.h>#include <math.h>#include <ctype.h>#include <stdlib.h>#define MAX_MATRIX 20#define BOMB 1#define NO_BOMB 3#define BOMB_FOUND 2#define NO_BOMB_FOUND 4#define MODE_BOMBER 5#define MODE_POLICE 6void printr(int arr[][20], int mSize, int mRowHL, int mColHL, int mMode){ int iCol, iRow; // column number printf (" 0 "); for (iCol=0; iCol < mSize; iCol++) { printf (" %d ", iCol + 1); } printf ("\n"); for (iCol=0; iCol < mSize; iCol++) { // row number printf (" %d ", iCol + 1); for(iRow=0; iRow < mSize; iRow++) { // printf (" %d ", iRow); if (arr[iRow][iCol] == BOMB) { if (iRow == mRowHL && iCol == mColHL) { if (mMode == MODE_BOMBER) { printf("[X]"); } else if (mMode == MODE_POLICE) { printf("[0]"); } } else { if (mMode == MODE_BOMBER) { printf(" X "); } else if (mMode == MODE_POLICE) { printf(" 0 "); } } } else if (arr[iRow][iCol] == NO_BOMB) { if (iRow == mRowHL && iCol == mColHL) { printf("[0]"); } else { printf(" 0 "); } } else if (arr[iRow][iCol] == BOMB_FOUND) { if (iRow == mRowHL && iCol == mColHL) { printf("[X]"); } else { printf(" X "); } } else if (arr[iRow][iCol] == NO_BOMB_FOUND) { if (iRow == mRowHL && iCol == mColHL){ printf("[0]"); } else { printf(" 0 "); } } } printf("\n"); } printf("\n");}void initr(int arr[][20], int num_rows, int num_cols){ int iCol, iRow; for (iCol=0; iCol < num_rows; iCol++) { for(iRow=0; iRow < num_cols; iRow++) arr[iRow][iCol] = NO_BOMB; } }int main(){ int iField[MAX_MATRIX][MAX_MATRIX]; int iSize = 0; int iRow = 0; int iCol = 0; int iBombs = 0; int iBombsPlaced = 0; int i = 0; int iBombsToFind = 0; int iBombsFound = 0; int iGuesses = 0; char cRequest = 'y'; char cInnerRequest = 'y'; // ask for the matrix while (tolower(cRequest) == 'y') { printf("Enter the size of the field: "); scanf ("%d", &iSize); if (iSize < 5 || iSize > 20) { printf("The field size should be from 5 to 20. Input again (y/n)? "); getchar(); cRequest = getchar(); if (tolower(cRequest) != 'y') { printf("Program exited.\n"); return 0; } } else { // bai! Nalibog ko ani na part. uhm pwede man sad gud // ang iyang pasabot kai: iBombs = floor((double)(iSize * iSize) / 4); // 2 dimensional man gud ang array! or pwede sad ang iyang pasabot // kai deretso na 1/4th sa size. // iBombs = floor((double)iSize / 4); cRequest = 'n'; } } // initialize matrix /* initializtion of the array goes here, but since im using visual studio, can't do non constant array dimensions. GCC supports non constant array dimensions. put the initialization of the array here in that case. */ // this part zeroes out all elements of the array. initr(iField, iSize, iSize); // ask for the mines for (i = 0; i < iBombs; i++) { cRequest = 'y'; while (tolower(cRequest) == 'y') { int row, col; cRequest = 'y'; cInnerRequest = 'y'; while (tolower(cInnerRequest) == 'y') { cInnerRequest = 'y'; printf ("Bomb row: "); scanf ("%d", &row); printf (" column: "); scanf ("%d", &col); if (row > 0 && col > 0 && row <= iSize && col <= iSize) { cInnerRequest = 'n'; } else { printf ("The cell you entered is out of bounds. Enter again (y/n)? "); cInnerRequest = getchar(); cInnerRequest = getchar(); if (tolower(cInnerRequest) != 'y') { printf("Program exited.\n"); return 0; } } } system ("CLS"); if (iField[col-1][row-1] == BOMB) { printf ("This location already has a bomb.\n"); printr(iField, iSize, col-1, row-1, MODE_BOMBER); printf ("Put on another location (y/n)? "); getchar(); cRequest = getchar(); if (tolower(cRequest) != 'y') { printf("Program exited.\n"); return 0; } } else { iField[col-1][row-1] = BOMB; iBombsPlaced++; cRequest = 'n'; printf ("Bombs placed: %d of %d\n", iBombsPlaced, iBombs); printr(iField, iSize, col-1, row-1, MODE_BOMBER); } } } // clear screen printf ("Press any key to continue to Police's turn.\n"); getchar(); // gi tuyo ni xa na duha getchar(); // para mo work. system ("CLS"); iBombsToFind = floor((double)iBombs/2.0); // ask the police for the mines printf ("Police's Turn\n"); for (i = 0; i < iBombs; i++) { cRequest = 'y'; while (tolower(cRequest) == 'y') { int row, col; cRequest = 'y'; cInnerRequest = 'y'; while (tolower(cInnerRequest) == 'y') { cInnerRequest = 'y'; printf ("Bomb row: "); scanf ("%d", &row); printf (" column: "); scanf ("%d", &col); if (row > 0 && col > 0 && row <= iSize && col <= iSize) { cInnerRequest = 'n'; } else { printf ("The cell you entered is out of bounds. Enter again (y/n)? "); cInnerRequest = getchar(); cInnerRequest = getchar(); if (tolower(cInnerRequest) != 'y') { printf("Program exited.\n"); return 0; } } } system ("CLS"); if (iField[col-1][row-1] == BOMB) { iField[col-1][row-1] = BOMB_FOUND; iBombsFound++; iGuesses++; if (iBombsFound >= iBombsToFind) { printf ("Bombs found: %d of %d. Guesses: %d. You win!\n", iBombsFound, iBombs, iGuesses); printr(iField, iSize, col-1, row-1, MODE_BOMBER); printf ("Game over! Press enter to quit!\n"); getchar(); return 0; } else { printf ("Found: %d of %d. Guesses left: %d. To Find: %d.\n", iBombsFound, iBombs, iBombs - iGuesses, iBombsToFind - iBombsFound); printr(iField, iSize, col-1, row-1, MODE_POLICE); } } else if (iField[col-1][row-1] == BOMB_FOUND || iField[col-1][row-1] == NO_BOMB_FOUND) { printf ("You already checked this location.\n"); printr(iField, iSize, col-1, row-1, MODE_POLICE); // kung nakita na niya ani na location tapos na doble niya ug check // then it's up to you kung i record ni as an attemt to guess, or just ignore it. //iGuesses++; //if (iGuesses > iBombsFound) { // printf ("Bombs found: %d of %d. You lose!\n", iGuesses, iBombs); // printf ("Game over! Press enter to quit!\n"); // getchar(); // return 0; //} } else { iGuesses++; printf ("No bomb. Found: %d of %d. Guesses left: %d. To Find: %d.\n", iBombsFound, iBombs, iBombs - iGuesses, iBombsToFind - iBombsFound); iField[col-1][row-1] = NO_BOMB_FOUND; printr(iField, iSize, col-1, row-1, MODE_POLICE); if (iGuesses >= iBombs) { printf ("Found: %d of %d. Guesses: %d. You lose!\n", iBombsFound, iBombs, iGuesses); printf ("Game over! Press any key to quit!\n"); getchar(); return 0; } } } } return 0;}
from a friend.