As
Dput has alluded, the conditional statements always evaluate to true or false, so this:
while(stop == false){
can be simplified to:
while(!stop){
You have a boolean variable here, so that is easy to understand. It also works for integers - when it's 0 it's false. This can be useful when one has a loop that decrements - so it ends when the variable gets to 0.
In your original program you had:
while(userinput != 'w' && userinput != 's' && userinput != 'a' && userinput != 'd')
You may not have got around to changing it yet - but it would be better using a switch. It's important to do error checking on user input, so we can't leave that out. Here is a switch inside a while:
http://www.cplusplus.com/forum/beginner/92070/#msg499037
Here's a slightly different version with some of the game logic in:
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
|
bool Quit = false;
while (!Quit) {
// get user input
switch (userinput) {
case 'w' :
if (row) { // row not zero
row--;
}
else {
//print error msg and let it loop again
}
break;
case 's' :
if (row!=7) {
//print error msg and let it loop again
}
break;
// etc for other options
case 'q' : // user wants to quit
Quit = true;
break;
default: // process errors
// doing nothing here makes it loop again
break;
}
}
|
One should use a
std::endl
at key points, because it flushes the buffer. Use
\n
as you have for the rest of the time.
@Dput
As I mentioned to the OP, I prefer Row & Col for variable names, rather than n & m, for the same reason I explained to the OP. Btw
n = n - 1
is more concisely written as
--n
or
n--
. The prefix version decrements the value before it is used.
Any way I hope you are all having good fun - I am, anyone would think it was the wet season at this end, so I have time to reply to you guys 8>D
EDIT:
I saw you earlier post about the problems 10's and 8's. The usual thing to do there is make them const variables, and just use the variable name from there on.
1 2 3 4 5 6 7 8 9 10
|
const int RowSize = 8;
const int ColSize = 10;
char board[RowSize ][ColSize ];
for(int Row =0; Row < RowSize ; ++Row) {
for(int Col=0; Col <ColSize ; ++Col){
board[Row][Col] = '.';
}
}
|