1

我开始编写视频扑克程序,但遇到了一些问题。

我有一个保持功能,如下所示:

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"; 
}

这个功能可以让我屏蔽文本,让玩家知道哪些牌被选中,哪些没有。我遇到的问题是第一个和最后一个“持有”如果被持有就不会被阻挡。

到目前为止,这是我调用该Hold函数的代码:

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;
            }
        }
    }
}
4

2 回答 2

1

你还没有展示Console::BackgroundColor()实际做了什么,所以很难确定。然而...

通常会cout 缓冲其输出直到稍后。因此,Console::BackgroundColor()可能会更改立即生效的内容,然后缓冲,然后在文本有机会发送到控制台cout << "HOLD"之前重置颜色。"HOLD"

也许您需要在更改颜色之前立即刷新输出:

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"; 
}
于 2011-12-06T18:27:33.673 回答
0

Stricto sensustd::cout和其他 C++ 标准流没有任何颜色或字体。

ANSI 转义码有一个标准,定义了如何在旧的基于字符的终端(以及当前的终端仿真器,如xterm和更新的克隆)上更改字体和颜色。

但是,如果您关心基于终端的 I/O,我建议您使用类似ncurses的库

于 2011-12-06T19:24:30.757 回答