Searched, couldn't find it.. I'm looking to simplify this:
if ((menuSelection != 'A') || (menuSelection != 'B') || (menuSelection != 'C') || (menuSelection != 'D') || (menuSelection != 'X'))
Can anyone help?
Thanks in advance..
Andrew
Printable View
Searched, couldn't find it.. I'm looking to simplify this:
if ((menuSelection != 'A') || (menuSelection != 'B') || (menuSelection != 'C') || (menuSelection != 'D') || (menuSelection != 'X'))
Can anyone help?
Thanks in advance..
Andrew
If it's continuous:
if (menuSelection >= 'A' && menuSelection <= 'X')
it's not :D but thanks. Anyone else?
oops.. sorry folks, it should be:
if ((menuSelection != 'A') && (menuSelection != 'B') && (menuSelection != 'C') && (menuSelection != 'D') && (menuSelection != 'X'))
you could put it into a small function:
this kind of function is good if you are using the same menu more than once and need to frequently validate the inputCode:bool checkInput( const char& input )
{
//perform if statement to make sure a valid character is entered.
return true;
}
Code:
bool
is_true(char ch) {
switch(tolower(ch)) {
case 'a': case 'x': case 'b' : case 't':
return true;
}
return false;
}
Thanks Guys...
Here's a doozy of a compound boolean statement (its movement checking for a knight in my chess game):
Code:if((x2 == (x1 - 2) && y2 == (y1 -1)) || (x2 == (x1 - 2) && y2 == (y1 + 1)) ||
(x2 == (x1 -1) && y2 == (y1-2)) || (x2 == (x1 - 1) && y2 == (y1 + 2)) ||
(x2 == (x1 + 1) && y2 == (y1 -2)) || (x2 == (x1 + 1) && y2 == (y1 + 2)) ||
(x2 == (x1 + 2) && y2 == (y1 - 1)) || (x2 == (x1 + 2) && y2 == (y1 + 1)))
.... perhaps you should implement it differently then :)
hey, for my simple chess program it works. What would you suggest? :)Quote:
Originally posted by BMJ
.... perhaps you should implement it differently then :)
use a case statement, have switches for 'A' 'B' 'C' 'D' and 'X', and have the code under the
if ((menuSelection != 'A') || (menuSelection != 'B') || (menuSelection != 'C') || (menuSelection != 'D') || (menuSelection != 'X'))
be exectued in the default: thingy instead
Thats diferent than a compound if statement, though. What you've just proposed is a replacement for multiple if statements.Quote:
Originally posted by moi
use a case statement, have switches for 'A' 'B' 'C' 'D' and 'X', and have the code under the
if ((menuSelection != 'A') || (menuSelection != 'B') || (menuSelection != 'C') || (menuSelection != 'D') || (menuSelection != 'X'))
be exectued in the default: thingy instead
i made the assumption (wrongly i guess) that he would want to be processing entry for the valid keys at the same time that he elimiates bad entriesQuote:
Originally posted by Captain Penguin
Thats diferent than a compound if statement, though. What you've just proposed is a replacement for multiple if statements.
Hey whaddya know, that IS what he was doing. I sort of ignored his main post and concentrated on the phrase of the subject, "Testing multiple conditions". :)Quote:
Originally posted by moi
i made the assumption (wrongly i guess) that he would want to be processing entry for the valid keys at the same time that he elimiates bad entries
Arrays come to mind...
Arrays? Not sure what you're meaning is here.Quote:
Originally posted by Sebastiani
Arrays come to mind...
My program is very simple.. a class consisting of an 8x8 char array with '_''s as blank spaces and letters for pieces. The class has a Move() function which takes 4 arguments, x1, y1, x2, and y2. x1,y1 is the starting piece and x2,y2 is the destination. The function checks the coordinates to make sure it is a valid move, and in the if() statement I posted above, those are when the move is valid for a knight.
well i have also implemented the game in C... I used an array of 64 to make it simpler.. 8 x 8 has a problem of manuplating validity..
this is how i dod it.. conside there is an array of array[64].. now consider that the king is in the array[40] position.. no to get a valid move i check the move made like this...
consider i = 40;
now valid moves are
array[i+1];
array[i-1];
array[i-8];
array[i+8];
array[i-9];
array[i-7];
array[i+9];
array[i+7];
that is |'''''''|''''''''|'''''''''|
'''''''''''''x'''''''''''''''' |'''''''|''''''''|'''''''''|
''''''''''''''''''''''''''''''
where x is the king.. But this algorithm has a problem.. now conside that the king is in the 8th A8 then the position b1 also become a valid move.. i check this inside the loop... so instead of considering the blocks in this order
1 2 3 4 5 6 7 8
A ------------------------------>--------->
B--------------------------->----------->
i made it as
1 2 3 4 5 6 7 8
A ------------------------------>--------->|
B ---<---------------------<-------------<-|
so that problem is eliminated.. and for example if an component is above to move to a valid position say array[40]. then i check if there is any other pawns in that block.. i stored the position of all the pieces in an array called component[64]; so this holds the entire state of the chess board...
so if any doubts u r free to ask questions.... My game is not yet ready.. i am still workong on it.. i have problems in pecial moves like castling etc etc.. and my game is a 2 player game.. so no AI involved,, i am planin to implement one later.. for this i have thought of a pointing system where i can back track 2 levels(not that intlegent program but still...) and then allocating points to the white and black side.. but i will have to improve this...