C++

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?
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.
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';
} 
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
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
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/
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
Registered users can post here. Sign in or register to post.