Help With Recursion In Minesweeper C++

Hi guys! Im a begginer :blush:

changed my code, wrote with recursion, but still another problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
 
#include "GameFunctions.hpp"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

const int height = 8;
const int width = 8;

struct Function
{
    char FieldOpen[height][width];
    char MineField[height][width];
    char BorderSymbol = '*';
    
    void ClearField()
    {
        for (int i = 0; i < height; ++i)
        {
            for (int j = 0; j < width; ++j)
            {
                MineField[i][j] = '0';
                FieldOpen[i][j] = BorderSymbol;
            }
        }
    }
    
    void CellNumbers(int x, int y)
    {
        if (x >= 0 && x < height)
        {
            if (y >= 0  && y < width)
            {
                if (MineField[x][y] != 'X')
                {
                    MineField[x][y]++;
                }
            }
        }
    }
    
    void SetOutMine()
    {
        srand(time_t(NULL));
        int QuantityMines = 10;
        
        for (int i = 0; i < QuantityMines; ++i)
        {
            int x = rand()% height;
            int y = rand()% width;
            MineField[x][y];
            
            if (MineField[x][y] == '0')
            {
                MineField[x][y] = 'X';
                CellNumbers(x - 1, y);
                CellNumbers(x - 1, y - 1);
                CellNumbers(x - 1, y + 1);
                CellNumbers(x + 1, y);
                CellNumbers(x + 1, y - 1);
                CellNumbers(x + 1, y + 1);
                CellNumbers(x, y - 1);
                CellNumbers(x, y + 1);
            }
        }
    }
    void ShowField()
    {
        cout << "  ";
        for (int i = 0; i < height; ++i)
        {
            cout << i << " ";
        }
        cout << endl;
        cout << "  _______________\n";
        for (int i = 0; i < height; ++i)
        {
            cout << (char)(i+65) << "|";
            for (int j = 0; j < width; ++j)
            {
                cout << FieldOpen[i][j] << " ";
            }
            cout << endl;
        }
    }
    void OpenMines()
    {
        for (int i = 0; i < height; ++i)
        {
            for (int j = 0; j < width; ++j)
            {
                if(MineField[i][j] == 'X')
                {
                    FieldOpen[i][j] = 'X';
                }
            }
        }
    }
    bool Reveal(int x, int y)
    {
        if (FieldOpen[x][y] == BorderSymbol && MineField[x][y] != 'X' && x >= 0 && x < height && y >= 0 && y < width)
        {
            FieldOpen[x][y] = MineField[x][y];
            if(MineField[x][y] == '0')
            {
                Reveal(x, y - 1);
                Reveal(x, y + 1);
                Reveal(x - 1, y - 1);
                Reveal(x - 1, y + 1);
                Reveal(x - 1, y);
                Reveal(x + 1, y + 1);
                Reveal(x + 1, y - 1);
                Reveal(x + 1, y);
            }
        }
        return true;
    }
};

void StartGame()
{
    Function function;
    
    function.ClearField();
    function.SetOutMine();
    int x;
    int y;
    do
    {
        function.ShowField();
        cout << "Please enter some strike cordinates ";
        cin >> x >> y;
        if (x > height && y > width)
        {
            cout << " \n \t Please enter again some strike cordinates, because you stike out of field! ";
            cin >> x >> y;
        }
    }
    while (function.Reveal(x,y));
    cout << "\n \t GAME OVER ";
}

Last edited on
Topic archived. No new replies allowed.