Code
#define EGRID 9
2 #define EMINES 10 //easy grid size and # of mines
10
11 #define MINE -4
12 #define UNREV -3
13 #define CORFLAG -2
14 #define INCORFLAG -1
15
16 #define MOVECHECK 0
17 #define MOVEFLAG 1
18
19 #include <cstdlib>
20 #include <iostream>
21 #include <time.h>
22
23 using namespace::std;
24
25 void printGrid(int grid[][EGRID])
26 {
27 for(int i = 0; i < EGRID; i++)
28 {
29 for(int j = 0; j < EGRID; j++)
30 {
31 int currVal = grid[j][i];
32 if((currVal == CORFLAG) || (currVal == INCORFLAG))
33 cout << "F ";
34 /*else if(currVal == MINE) Debugging code
35 * cout << "M ";*/
36 else if((currVal == UNREV) || (currVal == MINE))
37 cout << "- ";
38 else cout << currVal << " ";
39 }
40
41 cout << endl;
42 }
43 }
44
45 void initializeGrid(int grid[][EGRID])
46 {
47 for(int i = 0; i < EGRID; i++)
48 {
49 for(int j = 0; j < EGRID; j++)
50 {
51 grid[i][j] = UNREV;
52 }
53 }
54
55 srand(time(NULL));
56 int numMines = 0;
57 while(numMines < EMINES)
58 {
59
60 int mineX = rand() % EGRID;
61 int mineY = rand() % EGRID;
62
63 if(grid[mineX][mineY] == UNREV)
64 {
65 grid[mineX][mineY] = MINE;
66 numMines++;
67 }
68 }
69 }
70
71 bool checkSquare(int X, int Y, int grid[][EGRID], int gridX, int gridY)
72 {
73
74 int numMines = 0;
75 for(int i = Y-1; i<= Y+1; i++)
76 {
77 for(int j = X-1; j <= X+1; j++)
78 {
79 if(j >= 0 && j < gridX && !(j == X && i == Y) && i >= 0 && i < gridY)
80 {
81 if((grid[j][i] == MINE) || (grid[j][i] == CORFLAG))
82 numMines++;
83 }
84 }
85 }
86
87 if(grid[X][Y] == MINE)
88 {
89 char buf = '.';
90 cout << "Oh no! You've lost! Enter any value to end." << endl;
91 cin >> buf;
92 return true;
93 }
94 else
95 {
96 grid[X][Y] = numMines;
97 printGrid(grid);
98 return false;
99 };
100 }
101
102 bool flagSquare(int X, int Y, int grid[][EGRID], int gridX, int gridY)
103 {
104 if(grid[X][Y] == MINE) grid[X][Y] = CORFLAG;
105 else
106 {
107 if(grid[X][Y] == CORFLAG) grid[X][Y] = MINE;
108 }
109 if(grid[X][Y] == UNREV) grid[X][Y] = INCORFLAG;
110 else
111 {
112 if(grid[X][Y] == INCORFLAG) grid[X][Y] = UNREV;
113 }
114
115 int flagCount = 0;
116 for(int i = 0; i < gridX; i++)
117 {
118 for(int j = 0; j < gridY; j++)
119 {
120 if(grid[j][i] == CORFLAG)
121 flagCount++;
122 }
}
124
125 if (flagCount == EMINES)
126 {
127 char buf = '.';
128 cout << "Congrats, you won! Enter any value to end." << endl;
129 cin >> buf;
130 return true;
131 }
132 else
133 {
134 printGrid(grid);
135 return false;
136 }
137 }
138
139 int main (int argc, char* argv[])
140 {
141 int gameGrid[EGRID][EGRID];
142 bool gameEnd = false;
143
144
145 initializeGrid(gameGrid);
146 printGrid(gameGrid);
147 while(gameEnd == false)
148 {
149 int xToMove = -1, yToMove = -1, moveType;
150 char tempChar = '.';
151
152 while(tempChar != 'F' && tempChar != 'f' && tempChar != 'c' && tempChar != 'C')
153 {
154 cout << "What is the move type you would like to make? Type F to add or remove a flag, or C to check a square." << endl;
155 cin >> tempChar;
156 if(tempChar != 'F' && tempChar != 'f' && tempChar != 'c' && tempChar != 'C')
157 cout << "Invalid move. Please try again." << endl;
158 }
159
160
161 while(xToMove<=0 || xToMove>EGRID)
162 {
163 cout << "What is the X coordinate of your desired move? (Value between 1 and " << EGRID << ")" << endl;
164 cin >> xToMove;
if(xToMove<0 || xToMove>EGRID)
166 cout << "Invalid coordinate. Please give a number between 1 and " << EGRID << "." << endl;
167 }
168
169
170 while(yToMove<=0 || yToMove>EGRID)
171 {
172 cout << "What is the Y coordinate of your desired move? (Value between 1 and " << EGRID << ")" << endl;
173 cin >> yToMove;
174 if(yToMove<0 || yToMove>EGRID)
175 cout << "Invalid coordinate. Please give a number between 1 and " << EGRID << "." << endl;
176 }
177
178 if((tempChar == 'f') || (tempChar == 'F'))
179 {moveType = MOVEFLAG;}
180 else moveType = MOVECHECK;
181
182 if(moveType == MOVEFLAG)
183 gameEnd = flagSquare(xToMove - 1, yToMove - 1, gameGrid, EGRID, EGRID);
184 else gameEnd = checkSquare(xToMove - 1, yToMove - 1, gameGrid, EGRID, EGRID);
185
186
187 }
188
189 }
So off to a decent start.... things I do not quite know how to do to.
1) I tried the argv argc, but was not successful.. because I'm just a fucking noob

2) I don't know how to check each box after entering a coordinate where it will find all the boxes around what is selected that is NOT a bomb (would I do this recursively?)
If compiled, this will:
1) Create a 10x10 grid of coordinates (supposed to be based on user input of rows/columns using a.out -r # -c # -m #)
2) Randomizes location and number of mines from 1-10
3) Asks user to input coordinates (works correctly) whether to Move there or Flag there (works correctly). I get a core dump if the coordinates are not within the grid, but I'll figure that out.
4) If hits a mine, game ends
5) If not, user wins.
Thats where I'm at. Any advice?
This post was edited by Rws on Mar 13 2016 01:08pm