C++

Jan 27, 2018 at 9:52pm
Coding for a rock paper scissors game

Function > bool isMoveGood(char move);

My Response >
void test_isMoveGood() {
if ( (char move == R) || (char move == r)|| (char move == P) || (char move == p) || (char move == S) || (char move == s) )
{
bool isMoveGood = true;
} else {
bool isMoveGood = false;
}

}

is this a correct approach?
Jan 27, 2018 at 10:15pm
You don't need the char before the move in the if statement you can just use "move" since you're passing the parameter. Since you're using a char put in since quotes like move == 'R'. Return true and false when the move is good, you don't set the function to true or false.
Jan 27, 2018 at 10:24pm
1
2
3
4
bool is_move_good(char move) { 
  move = std::toupper(move); // convert `move' to upper-case 
  return move == 'R' || move == 'P' || move == 'S';
} 
Jan 27, 2018 at 10:27pm
if I'm returning the move, where does bool come into the mix then?
the function bool isMoveGood(char move) was given to us and we must use that framework
Jan 27, 2018 at 10:30pm
move == 'R' || move == 'P' || move == 'S' is evaluated as a bool. You can name the function back to isMoveGood, mbozzi just chose to format it with underscore instead.

In English, it would read
"return true if either move equals R, OR move equals P, OR move equals S. False otherwise."


Similarly, the two codes are equivalent:
1
2
3
4
5
6
7
bool isTwo(int n)
{
    if (n == 2)
        return true;
    else
        return false;
}

1
2
3
4
bool isTwo(int n)
{
    return n == 2;
}
Last edited on Jan 27, 2018 at 10:39pm
Jan 28, 2018 at 6:00am
Jan 28, 2018 at 8:37am
Using a char is not the best choice for a choice.
I would prefer an enum.
 
enum Choice {PAPER, SCISSOR, ROCK};

For the complete code see
http://hobbyprogrammer.byethost13.com/downloads/cpp/rock_paper_Scissor/
Jan 29, 2018 at 9:56pm
there isnt even any code past a cin and cout statement if you make a 2-d lookup table of {draw,win,lose} ... just becomes cin input, cout result[input][computer_turn]
Last edited on Jan 29, 2018 at 9:57pm
Topic archived. No new replies allowed.