Reputation: 143
I'm trying to check the score and outputting who ever wins. Black > 0, White < 0, and a tie being == 0. What should I do to see if GetValue(board) == 0 without calling my function again or using another variable?
GetValue(board) > 0 ? cout << "Black wins" : cout << "White wins";
Upvotes: 0
Views: 86
Reputation: 141618
std::string win_message(int const &x)
{
if ( x == 0 ) return "Tie";
if ( x < 0 ) return "Black wins";
return "White wins";
}
// ...
cout << win_message( GetValue(board) );
Upvotes: 1
Reputation: 2288
Why don't you want to use a variable ? If you do, you can use a compound ternary operator :
int val = GetValue(board);
cout << val == 0 ? "Tie" : (val < 0 ? "White wins" : "Black wins");
Edit: But THAT isn't one line, is it? The REAL one liner, courtesy of lambda functions.
It also assumes that GetValue
returns an int. And requires a using namespace std
for conciseness.
cout << vector<string>({"White wins", "Tie", "Black Wins"})[([](int x){return(0<x)-(x<0)+1;}(GetValue(board)))];
(Also don't actually use that)
Upvotes: 2
Reputation: 3191
If you want to output the score with one function call you can do something like:
cout << msg[ GetValue(board) + 1] << endl;
Where:
msg[0] = "White Wins";
msg[1] = "Tie";
msg[2] = "Black Wins";
This assumes that GetValue
returns -1, 0, or 1;
Upvotes: 1