1

I am in my second C++ class and I have to make a Video Poker game for my final. 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;

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:

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

How can I change just the A or the S without changing anything else? 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?


@regmagik- It won't let me put code in the comments and have it still formatted: This is what I have now:

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". If I leave the L in it says identifier "L" is undefined.


@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.

#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;
}
kingcobra1986
  • 871
  • 1
  • 12
  • 35
  • You can look here: http://stackoverflow.com/questions/15459466/how-to-get-full-control-over-the-console or here: http://stackoverflow.com/questions/20019945/c-controls-for-a-game-running-in-windows-console – Amadeus Dec 06 '14 at 23:07
  • I wanted to take that "card" above and change just the A and the S without using clear screen. Also I should have mentioned above that I am using microsoft Visual Studio 2013 – kingcobra1986 Dec 06 '14 at 23:42

1 Answers1

1

Here is a sample code compiled in 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);
regmagik
  • 484
  • 3
  • 13
  • I am guessing I required #include . I am not really familiar with this code, could you explain it to me? – kingcobra1986 Dec 07 '14 at 00:51
  • For example How would I have a char variable displayed instead of "My Text". And should I do the card (the parts with the '-' and the '|') with normal cout and then have the spot where the two Chars have different coords? – kingcobra1986 Dec 07 '14 at 01:08
  • To output a single `char ch='@';` you can pass `&ch` instead of "My Text" – regmagik Dec 07 '14 at 01:34
  • It wasn't formatting well enough to put my code here in the comments, so I added my code and problems to the original question. – kingcobra1986 Dec 07 '14 at 01:50
  • The error message `saying that argument of type "char *" is incompatible with the parameter of type "LPCWSTR"` is an indication that your project settings has UNICODE; replace all declarations of `char` with `TCHAR` and wrap your string literals with TEXT macro: `const TCHAR* SUITS[] = { TEXT("H"), TEXT("C"), TEXT("D"), TEXT("S")}; ` – regmagik Dec 07 '14 at 02:08
  • Ok I got most of it to work except the cin character won't stay in one spot, how do I make it so when the user types in the character it is always in the same spot? – kingcobra1986 Dec 07 '14 at 03:15
  • You could disable echo for cin, and then display user input at specific coordinate. Disable cin echo example: HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); DWORD mode; GetConsoleMode(hStdin, &mode); mode &= ~ENABLE_ECHO_INPUT; SetConsoleMode(hStdin, mode ); – regmagik Dec 07 '14 at 04:23
  • Ill start another question on that part so that they don't say that we are going off the topic of this question. Thank you for your help! – kingcobra1986 Dec 07 '14 at 05:15
  • If you want to look at the new question it's here: http://stackoverflow.com/questions/27339936/how-to-make-the-cin-show-in-the-same-spot-each-time-for-c-console – kingcobra1986 Dec 07 '14 at 05:32
  • I have the code from earlier put in a function called change_character_display(char new_char, int xCoord, int yCoord). I was wondering if there was something i can put in this function that can convert char to TCHAR without going through all my code (I have 6 cpp files and 6 header files made already and am almost done with the whole thing). I cannot find anything on doing this. I am aware that it is not the right way to use both char and TCHAR. I will keep that in mind for my next project. – kingcobra1986 Dec 07 '14 at 12:21