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