简体   繁体   English

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

[英]How to change a character in C++ console that was printed to the screen earlier

I am in my second C++ class and I have to make a Video Poker game for my final. 我在第二堂C ++课中,必须为决赛做一个视频扑克游戏。 I want to have my game board printed to the screen and stay on the screen while only the characters that change are the only thing to change, while keeping the rest of the game board stays on the screen in the same place without clearing the screen and re-printing everything. 我希望将游戏板打印到屏幕上并停留在屏幕上,而只有更改的字符才是唯一更改,同时将其余游戏板保留在屏幕上的同一位置而不清除屏幕,并且重新打印所有内容。 The game I made more my final last class, I used the clear screen and it appeared to flash a lot because it was reprinting the entire screen each time a change was made. 在最后一堂课上,我做了更多的游戏,我使用了清晰的屏幕,但它似乎经常闪烁,因为每次进行更改时,它都会重新打印整个屏幕。 Instead of putting all of my code on here and so that someone else doesn't do my project for me I wrote the following code for an example of my question: 而不是将我所有的代码都放在这里,这样别人不会为我做我的项目,而是为下面的问题示例编写了以下代码:

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

How can I make it so that the only thing that changes above is this line when printed: FACES[rand() % 13] << " of " << SUITS[rand() % 4] << endl; 我该如何做才能使上面唯一改变的是打印时的这一行:FACES [rand()%13] <<“ of << SUITS [rand()%4] <<” endl;

My actual code will just display a box with an char to show the face and a char to show the suit it will look like this when printed: 我的实际代码将只显示一个带有char的框,以显示脸部,并以char的形式显示西装,在打印时将如下所示:

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

How can I change just the A or the S without changing anything else? 如何仅更改A或S而不更改其他任何内容? Or using the code above, How can I change just the FACES string and the SUITS string without changing the text above or below it? 还是使用上面的代码,如何更改FACES字符串和SUITS字符串而不更改上面或下面的文本?


@regmagik- It won't let me put code in the comments and have it still formatted: This is what I have now: @ 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;
}

I'm still getting an error saying that argument of type "char *" is incompatible with the parameter of type "LPCWSTR". 我仍然收到错误消息,说“ char *”类型的参数与“ LPCWSTR”类型的参数不兼容。 If I leave the L in it says identifier "L" is undefined. 如果我将L留在其中,表示标识符“ L”未定义。


@regmagik- Here is my updated code, it is working well but I would like for the character form the user to stay in one spot. @ 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;
}

Here is a sample code compiled in Visual Studio 2013: 以下是在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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM