Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <time.h>
- #include <array>
- #define MAX_Y 24
- #define MAX_X 30
- #define MINE -1
- #define NONE 0
- #define FLAG 1
- #define QMRK 2
- #define HIDDEN 0
- #define VISIBLE 1
- using namespace std;
- enum GameState {
- WAITING,
- RUNNING,
- GAMEOVER,
- VICTORY
- };
- bool g_bUsingLives = true;
- char g_chGameState = WAITING;
- short g_chBoard_H = 9;
- short g_chBoard_W = 9;
- short g_nBoard_M = 10;
- short g_chBoard_tH = 9;
- short g_chBoard_tW = 9;
- short g_nBoard_tM = 10;
- short g_nPercentageC = 0;
- short g_nTotalFlagsC = 0;
- char g_chLivesLeftC = 0;
- char g_chMinesHitC = 0;
- //int g_nTimerStart = 0;
- //int g_nTimerStop = 0;
- class TileData {
- public:
- TileData() {
- State = HIDDEN;
- Type = 0;
- Marker = NONE;
- };
- char State;
- char Type;
- char Marker;
- void SetAll(char s, char t, char m) {
- switch (s) {
- case 'D': State = HIDDEN; break;
- case 'N': break;
- default : State = s; break;
- }
- switch (t) {
- case 'D': Type = 0; break;
- case 'N': break;
- default : Type = t; break;
- }
- switch (m) {
- case 'D': Marker = NONE; break;
- case 'N': break;
- default : Marker = m; break;
- }
- }
- };
- TileData* g_anBoard[MAX_Y][MAX_X];
- // incomplete
- void InitBoard() {
- for (short y = 0; y < MAX_Y; y++) {
- for (short x = 0; x < MAX_X; x++) {
- g_anBoard[y][x] = new TileData();
- }
- }
- }
- void InitGame(signed char, signed char);
- void OpenTile(signed char y, signed char x, bool bAddPercentage, char s, char t, char m) {
- if (g_chGameState == WAITING) InitGame(y, x);
- g_anBoard[y][x]->SetAll(s, t, m);
- if (bAddPercentage) g_nPercentageC++;
- }
- void ReadTile(signed char y, signed char x) {
- switch (g_anBoard[y][x]->Type) {
- case MINE:
- OpenTile(y, x, false, MINE, 'N', 'N');
- g_chMinesHitC++;
- if (g_bUsingLives && g_chLivesLeftC) g_chLivesLeftC--;
- else {
- g_chGameState = GAMEOVER;
- // g_nTimerStop = curtime;
- //UpdateMarkers();
- }
- break;
- case 0: // BLANK
- OpenTile(y, x, true, VISIBLE, 'N', 'N');
- // begin searching adjacent tiles to explore
- for (char sY = -1; sY < 2; sY++) {
- if (y + sY < 0 || y + sY >= g_chBoard_H) continue; // check vertical boundaries
- for (char sX = -1; sX < 2; sX++) {
- if (x + sX < 0 || x + sX >= g_chBoard_W) continue; // check horizontal boundaries
- if (g_anBoard[y + sY][x + sX]->State != HIDDEN) continue; // skip if it's not HIDDEN
- if (g_anBoard[y + sY][x + sX]->Marker == FLAG) continue; // skip if it has FLAG
- ReadTile(y + sY, x + sX); // recourse to explore
- }
- }
- break;
- default:
- OpenTile(y, x, true, VISIBLE, 'N', 'N');
- break;
- }
- if (g_nPercentageC == g_chBoard_H * g_chBoard_W - g_nBoard_M) {
- g_chGameState = VICTORY;
- // g_nTimerStop = curtime;
- //UpdateMarkers();
- }
- }
- void UpdateMarkers() {
- for (signed char y = 0; y < g_chBoard_H; y++) {
- for (signed char x = 0; x < g_chBoard_W; x++) {
- // incomplete
- }
- }
- }
- void InitGame(signed char y, signed char x) {
- short nMinesLeftC = g_nBoard_M;
- while (nMinesLeftC > 0) {
- signed char rY = rand() % g_chBoard_H;
- signed char rX = rand() % g_chBoard_W;
- if (rY >= y - 1 && rY <= y + 1 && rX >= x - 1 && rX <= x + 1) continue; // skip if the coord is within a 3x3 rectangle
- if (g_anBoard[rY][rX]->Type == MINE) continue; // skip if it's already a mine
- g_anBoard[rY][rX]->Type = MINE;
- nMinesLeftC--;
- }
- for (y = 0; y < g_chBoard_H; y++) {
- for (x = 0; x < g_chBoard_W; x++) {
- if (g_anBoard[y][x]->Type != MINE) continue;
- for (signed char sY = -1; sY < 2; sY++) {
- if (y + sY < 0 || y + sY >= g_chBoard_H) continue; // check vertical boundaries
- for (signed char sX = -1; sX < 2; sX++) {
- if (x + sX < 0 || x + sX >= g_chBoard_W) continue; // check horizontal boundaries
- if (g_anBoard[y + sY][x + sX]->Type == MINE) continue; // skip if it's a MINE
- g_anBoard[y + sY][x + sX]->Type++; // up the adjacent tile's value
- }
- }
- }
- }
- g_chGameState = RUNNING;
- // g_nTimerStart = curtime;
- }
- int main() {
- srand(time(nullptr));
- for (short y = 0; y < MAX_Y; y++) {
- for (short x = 0; x < MAX_X; x++) {
- g_anBoard[y][x] = new TileData();
- }
- }
- // incomplete
- for (short y = 0; y < MAX_Y; y++) {
- for (short x = 0; x < MAX_X; x++) {
- delete g_anBoard[y][x];
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment