如何更改之前打印到屏幕的C ++控制台中的字符

时间:2014-12-06 22:49:55

标签: c++ string console character

我在第二个C ++课程中,我必须为我的决赛制作一个视频扑克游戏。 我希望将我的游戏板打印到屏幕上并留在屏幕上,同时只更改角色是唯一要改变的东西,同时保持游戏板的其余部分在同一个地方停留在屏幕上而不清除屏幕和重新打印一切。这场比赛我的最后一堂课更多,我使用了清晰的屏幕,它似乎闪现了很多,因为每次更改都会重新打印整个屏幕。而不是把我的所有代码放在这里,以便其他人不为我做我的项目,我写了下面的代码作为我的问题的一个例子:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

const char * SUITS[] = { "HEARTS", "CLUBS", "DIAMONDS", "SPADES" };
const char * FACES[] = { "ACE", "TWO", "THREE", "FOUR", "FIVE", "SIX", 
            "SEVEN", "EIGHT", "NINE", "TEN", "JACK", "QUEEN", "KING" };

int main() {
    char q = 'a';
    do {
        srand(time(0));

        cout << "This is the card: " << endl;
        cout << FACES[rand() % 13] << " of " << SUITS[rand() % 4] << endl;
        cout << "Press any key to get another card, or q to quit: ";
        cin >> q;

    } while (q != 'q');
    return 0;
}

我怎样才能使它在打印时唯一改变的是这条线:FACES [rand()%13]&lt;&lt; &#34; &#34; &LT;&LT; SUITS [rand()%4]&lt;&lt; ENDL;

我的实际代码只会显示一个带有字符的框以显示面部,而一个字符用于显示打印时的外观:

  ---------
 |         |
 |    A    |   (A is for Ace)
 |         |
 |    S    |   (S is for Spades)
 |         |
  ---------

如何更改A或S而不更改其他内容? 或者使用上面的代码,如何更改FACES字符串和SUITS字符串而不更改其上方或下方的文本?


@ regmagik-它不允许我在评论中放入代码并让它仍然格式化: 这就是我现在所拥有的:

const char SUITS[] = { 'H', 'C', 'D', 'S'};
const char FACES[] = { 'A','2','3','4','5','6','7','8','9','T','J','Q','K'};

int main() {
    DWORD dw;
    COORD here;
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    if (hStdOut == INVALID_HANDLE_VALUE)
    {
        printf("Invalid handle");
    }
    here.X = 10;
    here.Y = 10;

    //suit random num
    char suit_char = SUITS[rand() % 4];
    char face_char = FACES[rand() % 13];

    WriteConsoleOutputCharacter(hStdOut, &suit_char, 7, here, &dw);

    return 0;
}

我仍然得到一个错误,说明类型&#34; char *&#34;与&#34; LPCWSTR&#34;类型的参数不兼容。如果我把L留在里面,那就说标识符&#34; L&#34;未定义。


@ regmagik-这是我的更新代码,它运行良好,但我希望用户的角色形式保留在一个位置。

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <Windows.h>
#include <string>

using namespace std;

const TCHAR SUITS[] = { 'H', 'C', 'D', 'S' };
const TCHAR FACES[] = { 'A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K' };
const TCHAR SPACE = ' ';
const int number_of_cards = 5;

int main() {
    char * line_of_stars = "*****************************************************\n";
    char * line_of_spaces = "*                                                   *\n";
    char * top_of_cards = "*  *******   *******   *******   *******   *******  *\n";
    char * card_sides = "* *       * *       * *       * *       * *       * *\n";
    char quit = 'q';
    //Display Screen
    cout << line_of_spaces << top_of_cards << card_sides << card_sides << card_sides
         << card_sides << card_sides << top_of_cards << line_of_spaces << line_of_stars;
    cout << "\nPress 'q' to quit or any other key to get new cards: ";

    DWORD dw;
    COORD suitCoord;
    COORD faceCoord;
    COORD nextMove;
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    if (hStdOut == INVALID_HANDLE_VALUE){ 
        printf("Invalid handle"); 
    }
    nextMove.X = 53;
    nextMove.Y = 11;
    do {
        for (int i = 0; i < number_of_cards; i++){

            int startX = 6 + (i * 10);
            int startY = 3;

            //Set coords for the i-nth card
            suitCoord.X = (startX);
            suitCoord.Y = (startY);
            faceCoord.X = (startX);
            faceCoord.Y = (startY + 2);
            //suit random num
            int rand1 = rand() % 4;
            TCHAR suit_char = SUITS[rand1];
            int rand2 = rand() % 13;
            TCHAR face_char = FACES[rand2];
            //Print to screen
            WriteConsoleOutputCharacter(hStdOut, &suit_char, 1, suitCoord, &dw);
            WriteConsoleOutputCharacter(hStdOut, &face_char, 1, faceCoord, &dw);
        }
        // Cover Last input with a space
        WriteConsoleOutputCharacter(hStdOut, &SPACE, 1, nextMove, &dw);
        cin.clear();
        cin >> quit;
        cout << "\b\b";
    } while (!(quit == 'q' || quit == 'Q'));

    return 0;
}

1 个答案:

答案 0 :(得分:1)

以下是在Visual Studio 2013中编译的示例代码:

DWORD dw;
COORD here;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (hStdOut == INVALID_HANDLE_VALUE)
{
    printf("Invalid handle");
}
here.X = 10;
here.Y = 10;
WriteConsoleOutputCharacter(hStdOut, L"My Text", 7, here, &dw);