Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <string.h>
- void InitGame(void);
- void PlaceBomb(void);
- int Count(int, int);
- int GetMine(int, int);
- void DrawField(void);
- void InitChar(void);
- void RestartGame(void);
- int Mines[9][9] = {{0}};
- char Draw[9][9] = {{0}};
- int TotalMines = 0;
- int HiddenCells = 0;
- int MarkedCells = 0;
- int RowIn = 0;
- int ColIn = 0;
- char Mark = 0;
- int Win = 0;
- int main()
- {
- srand(time(NULL)); //inits the RNG
- InitGame();
- while (69)
- {
- printf("Bombs: %d Marked: %d\n", TotalMines, MarkedCells);
- printf(" 1 2 3 4 5 6 7 8 9\n");
- DrawField();
- printf("Your move: ");
- scanf("%1d%1d%1c", &RowIn, &ColIn, &Mark);
- RowIn--;
- ColIn--;
- if (Mark == '-')
- {
- if (Draw[RowIn][ColIn] == 'B')
- Draw[RowIn][ColIn] = '-';
- else
- Draw[RowIn][ColIn] = 'B';
- MarkedCells++;
- }
- if (Mark == '+')
- {
- if (Mines[RowIn][ColIn] != 0)
- {
- Draw[RowIn][ColIn] = 'X';
- printf("Row: %d, Col: %d \n", RowIn, ColIn);
- printf("BOOM. \n");
- RestartGame();
- }
- else if (Count(RowIn, ColIn))
- {
- Draw[RowIn][ColIn] = Count(RowIn, ColIn);
- printf("Count: %d \n", Draw[RowIn][ColIn]);
- }
- else
- Draw[RowIn][ColIn] = '0';
- }
- Win = 0;
- for (int i = 0; i < 9; i++)
- {
- for (int j = 0; j < 9; j++)
- {
- if (Draw[i][j] != '-' && !Mines[i][j])
- {
- Win++;
- }
- }
- }
- if (Win >= (81 - TotalMines))
- {
- printf("You won! Now the ladies will love you!");
- RestartGame();
- }
- }
- return 0;
- }
- void InitGame(void) //this function resets the game;
- {
- int i = 0;
- memset(Mines, 0, sizeof Mines); //sets the whole array of Mines to 0
- MarkedCells = 0;
- HiddenCells = 0;
- InitChar();
- printf("*************************\n");
- printf("* M I N E S W E E P E R *\n");
- printf("*************************\n");
- printf("Enter number of bombs (1-80): ");
- scanf("%d", &TotalMines);
- for (i = 0; i < TotalMines; i++)
- {
- PlaceBomb();
- }
- }
- void PlaceBomb(void)
- {
- int randX = 0;
- int randY = 0;
- int i = 0;
- int j = 0;
- while (2)
- {
- randX = rand() % 9;
- randY = rand() % 9;
- if (!Mines[randX][randY])
- {
- Mines[randX][randY] = 1;
- break;
- }
- }
- }
- int Count(int x, int y) //bit of a yikes to read, also improvable through recursion to work like MS Minesweeper; not declared in the example though
- {
- if (x == 0 && y == 0)
- return GetMine(x, y + 1) + GetMine(x + 1, y) + GetMine(x + 1, y + 1);
- if (x == 0)
- return GetMine(x, y - 1) + GetMine(x, y + 1) + GetMine(x + 1, y - 1) + GetMine(x + 1, y) + GetMine(x + 1, y + 1);
- if (y == 0)
- return GetMine(x - 1, y) + GetMine(x - 1, y + 1) + GetMine(x, y + 1) + GetMine(x + 1, y) + GetMine(x + 1, y + 1);
- if (x == 8 && y == 8)
- return GetMine(x - 1, y - 1) + GetMine(x - 1, y) + GetMine(x, y - 1);
- if (x == 8)
- return GetMine(x - 1, y - 1) + GetMine(x - 1, y) + GetMine(x - 1, y + 1) + GetMine(x, y - 1) + GetMine(x, y + 1);
- if (y == 8)
- return GetMine(x - 1, y - 1) + GetMine(x - 1, y) + GetMine(x, y - 1) + GetMine(x + 1, y - 1) + GetMine(x + 1, y);
- return GetMine(x - 1, y - 1) + GetMine(x - 1, y) + GetMine(x - 1, y + 1) + GetMine(x, y - 1) + GetMine(x, y + 1) + GetMine(x + 1, y - 1) + GetMine(x + 1, y) + GetMine(x + 1, y + 1);
- }
- int GetMine(int x, int y)
- {
- return Mines[x][y];
- }
- void DrawField(void)
- {
- for (int i = 0; i < 9; ++i)
- {
- printf("%d ", i + 1);
- for (int j = 0; j < 9; ++j)
- {
- if (Draw[i][j] < 10)
- printf("%d ", Draw[i][j]); //using this to make Count() work; Stupid Chars!
- else
- printf("%c ", Draw[i][j]);
- }
- printf("\n");
- }
- }
- void InitChar(void) //sets the Array that's being drawn to default
- {
- for (int i = 0; i < 9; i++)
- {
- for (int j = 0; j < 9; j++)
- {
- Draw[i][j] = '-';
- }
- }
- }
- void RestartGame(void)
- {
- char Reset;
- printf("Do you want to play again (y/n)?");
- scanf(" %c", &Reset);
- if (Reset == 'y')
- {
- system("CLS"); //clears the screen, loses Linux Compat though
- InitGame();
- return;
- }
- else
- {
- printf("Coward!");
- exit(0); //ends the program without having to break out of the main loop
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment