【发布时间】:2014-12-06 22:49:55
【问题描述】:
我正在上第二节 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]
我的实际代码将只显示一个带有 char 的框来显示面部和一个 char 来显示西装,打印时它看起来像这样:
---------
| |
| A | (A is for Ace)
| |
| S | (S is for Spades)
| |
---------
如何只更改 A 或 S 而无需更改其他任何内容? 或者使用上面的代码,如何只更改 FACES 字符串和 SUITS 字符串而不更改其上方或下方的文本?
@regmagik- 它不会让我将代码放入 cmets 并使其仍然格式化: 这就是我现在拥有的:
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;
}
我仍然收到一条错误消息,指出“char *”类型的参数与“LPCWSTR”类型的参数不兼容。如果我将 L 留在其中,则表示标识符“L”未定义。
@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;
}
【问题讨论】:
-
我想拿上面的那个“卡片”,只改变A和S而不使用清除屏幕。另外我应该在上面提到我正在使用 microsoft Visual Studio 2013
标签: c++ string console character