'Video Poker in C++
I'm starting to write a video poker program and I'm running into some issues.
I have a Hold function as you can see below:
void Game::Hold( bool& choice )
{
if( choice == true )
{
Console::BackgroundColor(Red);
Console::ForegroundColor(Black);
cout << "HOLD";
Console::BackgroundColor(Black);
Console::ForegroundColor(Red);
}
else
cout << "HOLD";
}
This functions allows me to block out the text so the player knows which cards are selected and which are not. The problem I'm having is that the first and last "Holds" wont block off if being held.
So far-this is my code that is calling the Hold
function:
void Game::Play( void )
{
Menu();
Console::Clear();
Deck nGame;
nGame.Shuffle();
Game Hand;
Card currentHand[ 5 ];
bool p_Hold[ 5 ] = { 0 , 0 , 0, 0, 0 };
for( int i = 0; i < 5; i++ )
currentHand[ i ] = nGame.Draw();
cout << "Type in which cards you would like to hold. Type \"d\" when done.\n\n";
char uChoice[ 5 ] = {};
for( int i = 0; i < 5; i++ )
{
if( uChoice[ i ] == 'd' )
break;
for( int i = 0; i < 5; i++ )
cout << " " << currentHand[ i ] << " ";
cout << endl;
for( int i = 0; i < 5; i++ )
{
cout << " ";
Hand.Hold( p_Hold[ i ] );
cout << " ";
}
cout << "\n\n\nWould you like to hold Card " << i + 1 << "? (1 = Yes/0 = No): ";
cin.get( uChoice[ i ] );
cin.clear();
cin.ignore( INT_MAX, '\n' );
cout << endl;
if( cin.good() )
{
for( int i = 0; i < 5; i++ )
{
if( uChoice[ i ] == '1' )
p_Hold[ i ] = true;
else
p_Hold[ i ] = false;
}
}
}
}
Solution 1:[1]
You haven't shown what Console::BackgroundColor()
actually does, so it's hard to be certain. However...
Normally cout
buffers its output until later. Because of this, the Console::BackgroundColor()
might be changing something that takes effect right away, then the cout << "HOLD"
is buffered, then you reset the color before the "HOLD"
text gets a chance to be sent to the console.
Perhaps you need to flush the output immediately before changing the color:
void Game::Hold( bool& choice )
{
if( choice == true )
{
cout.flush();
Console::BackgroundColor(Red);
Console::ForegroundColor(Black);
cout << "HOLD";
cout.flush();
Console::BackgroundColor(Black);
Console::ForegroundColor(Red);
}
else
cout << "HOLD";
}
Solution 2:[2]
Stricto sensu, std::cout
and other C++ standard streams don't have any colors or fonts.
There is a standard on ANSI escape codes defining how to change fonts & colors on old character based terminals (and current terminal emulators like xterm and newer clones).
But if you care about terminal based I/O, I do suggest using a library for that like ncurses
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Greg Hewgill |
Solution 2 | Basile Starynkevitch |