d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Minesweeper C++ Question > Using Command Line Arguments
Prev12
Add Reply New Topic New Poll
Member
Posts: 3,476
Joined: Jul 20 2015
Gold: 651.00
Mar 12 2016 03:31pm
when you run the program it will look like: ./minesweeper -r 10 -c 10 -m 20

that means there are 10 rows, 10 columns and 20 mines

the int argc will be the number of arguments.

the char* argv will be the actual arguments

and since you can do the command line argument in any order, just make sure you have the correct amount of arguments (rows, columns, mines).

You will need to parse out each string in argv to determine which number goes with rows, columns, and mines.

indexes for argv start at 1.

so in the above example argv would be:
argv[0] = nothing
argv[1] = "-r 10"
argv[2] = "-c 10"
argv[3] = "-m 20"

just split each string by a space and assign your rows, columns, mines variables to the correct number.
Member
Posts: 89,702
Joined: Jul 14 2008
Gold: 165,885.69
Mar 12 2016 04:25pm
Quote (spt_94 @ Mar 12 2016 01:31pm)
when you run the program it will look like: ./minesweeper -r 10 -c 10 -m 20

that means there are 10 rows, 10 columns and 20 mines

the int argc will be the number of arguments.

the char* argv will be the actual arguments

and since you can do the command line argument in any order, just make sure you have the correct amount of arguments (rows, columns, mines).

You will need to parse out each string in argv to determine which number goes with rows, columns, and mines.

indexes for argv start at 1.

so in the above example argv would be:
argv[0] = nothing
argv[1] = "-r 10"
argv[2] = "-c 10"
argv[3] = "-m 20"

just split each string by a space and assign your rows, columns, mines variables to the correct number.


So for user input for the number of cols, rows, mines, would it be

int input;
argv[3] = "-m[input]"

Little confused about that

btw heres the link to the assignment outline
http://classes.engr.oregonstate.edu/eecs/winter2016/cs161-001/assign/assign6.pdf

This post was edited by Rws on Mar 12 2016 04:28pm
Member
Posts: 12,786
Joined: May 17 2013
Gold: 4,010.00
Mar 12 2016 06:24pm
Quote (Rws @ 12 Mar 2016 23:25)
So for user input for the number of cols, rows, mines, would it be

int input;
argv[3] = "-m[input]"

Little confused about that

btw heres the link to the assignment outline
http://classes.engr.oregonstate.edu/eecs/winter2016/cs161-001/assign/assign6.pdf


I'm not sure what you mean.

The user would input (when starting the program)

./minesweeper followed by: -r 10 -c 10 -m 20
Since arguments are seperated by the dash, we end up with this array (ignore A[0]):

A[1] = -r 10
A[2] = -c 10
A[3] = -m 20

Then you would just need to parse whatever comes after -r, -c and -m as an int (checking if it's not one of course) and then use said integers for the variables in your program.
Member
Posts: 89,702
Joined: Jul 14 2008
Gold: 165,885.69
Mar 12 2016 06:28pm
Quote (Klexmoo @ Mar 12 2016 04:24pm)
I'm not sure what you mean.

The user would input (when starting the program)

./minesweeper followed by:-r 10 -c 10 -m 20
Since arguments are seperated by the dash, we end up with this array (ignore A[0]):

A[1] = -r 10
A[2] = -c 10
A[3] = -m 20

Then you would just need to parse whatever comes after -r, -c and -m as an int (checking if it's not one of course) and then use said integers for the variables in your program.


Oh gotcha :lol: I understand now.

Thanks for following up.
Member
Posts: 89,702
Joined: Jul 14 2008
Gold: 165,885.69
Mar 13 2016 01:07pm
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 :lol:
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
Member
Posts: 29,350
Joined: Mar 27 2008
Gold: 504.69
Mar 13 2016 04:51pm
Why not use getopt to parse the arguments?
Member
Posts: 89,702
Joined: Jul 14 2008
Gold: 165,885.69
Mar 13 2016 05:05pm
Quote (ROM @ Mar 13 2016 02:51pm)
Why not use getopt to parse the arguments?


I dont know what that is, hasn't been taught in lecture.
Go Back To Programming & Development Topic List
Prev12
Add Reply New Topic New Poll