- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
您好,我对这项作业有疑问。我的程序是 C++ 中的内存匹配游戏,它可以运行,但是每次用户输入有效或无效响应后,我都需要让我的二维数组中的字符移动位置。他们停留在与初始化相同的位置。我尝试了多种想法,但没有得出结论。甚至一些可能有帮助的提示也会受到赞赏。
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<string>
using namespace std;
class Game //Memory Match Game
{
private:
char cards[4][4] = { { '@', '#', '$', '%' },
{'^', '&', '*', '+'},
{'@', '#', '$', '%'},
{'^', '&', '*', '+' } };
char deck[16] = { '@','#', '$', '%', '^','&', '*', '+' }; //not being used atm
int r1, r2, c1, c2;
public:
Game()
{
r1 = 0; r2 = 0; c1 = 0; c2 = 0;
}
void begin()
{
srand((unsigned)time(NULL));
for (int x = 0; x < 4; x++) //begin designating cards
{
for (int y = 0; y < 4; y++)
{
cards[rand() % x][rand() % y]; // <:::::[:::] FIX
cout << " " << cards[x][y] << " ";
}
cout << endl;
}
cout << " \n 1 2 3 4\t\n";
cout << " ";
for (int x = 0; x <= 8; x++)
{
cout << "_";
}
cout << endl;
for (int x = 0; x < 4; x++) //appropriate positioning
{
cout << x + 1 << " | ";
for (int y = 0; y < 4; y++)
{
cout << "? ";
}
cout << endl;
}
cout << endl;
input();
}
void input()
{
srand(time(0));
cout << "Please type in the row and column of choice.\n"; //begin user play
cin >> r1;
cin >> c1;
cout << "Please type in the row and column of choice\n";
cin >> r2;
cin >> c2;
r1--; r2--; c1--; c2--;
cout << " 1 2 3 4\n";
cout << " ";
for (int x = 0; x <= 8; x++)
{
cout << "_";
}
cout << endl;
for (int x = 0; x < 4; x++)
{
cout << x + 1 << " | ";
for (int y = 0; y < 4; y++)
{
if ((x == r1) && (y == c1))
{
cout << cards[x][y] << " ";
}
else if ((x == r2) && (y == c2))
{
cout << cards[x][y] << " ";
}
else cout << "? ";
}
cout << endl;
}
match();
}
void match()
{
if (cards[r1][c1] == cards[r2][c2])
{
cout << "Valid Match!\n";
input();
}
else
cout << "Invalid Match!\n";
input();
}
};
int main()
{
Game memoryMatch;
memoryMatch.begin();
system("pause");
}
最佳答案
“洗牌”的一种方法是创建一个列表
,其中包含数组的所有索引。从 list
中随机选择一个项目并将其存储在 vector
中。删除该项目并重复直到列表为空。然后,您在数组中有一个包含“混洗”索引的 vector
。不要直接修改数组,而是将索引的 vector
存储到数组中。
参见 std::list
和 std::vector
,以及删除方法。
https://www.sgi.com/tech/stl/List.html
https://www.sgi.com/tech/stl/Vector.html
根据下面的评论,我给了你一个替代方案,但没有解释为什么你的代码不起作用。对此,有几个原因。
void begin()
{
srand((unsigned)time(NULL));
for (int x = 0; x < 4; x++) //begin designating cards
{
for (int y = 0; y < 4; y++)
{
cards[rand() % x][rand() % y]; // <:::::[:::] FIX
cout << " " << cards[x][y] << " ";
}
cout << endl;
}
这不起作用的主要原因是因为您分配给数组中的随机 x 和 y 元素。您可以一次、多次或根本不分配一个插槽。您需要做的是为数组中的每个元素分配一个随机项。
更像这样的东西会更接近你想要的:
char shuffled_cards[4][4];
for (int y = 0 ; y < 4 ; ++y) {
for (int x = 0 ; x < 4 ; ++x) {
shuffled_cards[y][x] = cards[rand()%4][rand()%4];
}
}
要掌握的主要事情是您应该处理另一组卡片,在本例中为 shuffled_cards
并从您的卡片组中随机分配一张卡片。
这就是我一开始发布的洗牌想法的用武之地。
关于c++ - 内存配对游戏如何随机化数组中的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39989383/
我有一个服务器程序(适用于所有三个主要操作系统),它是用 C++ 创建的,它努力连接两个陌生人进行通信。我当前的模型似乎已经过时了,我只是想知道是否有更好的方法来为客户提供服务。 -服务器收到连接请求
我希望有人能告诉我 GoogleTV 如何与本地 Youtube 应用配对。 我感兴趣的是“第一屏”GoogleTV/SmartTV 功能。 据我目前所知,“第二屏”应用程序需要使用与配对服务(来自
感谢下面的提示 • 您需要一次遍历字符串一个字符(for 循环或 while 循环) 当您点击 ,这是您的结束标记 • 现在检查> 之前的字符。是/吗? • 是:查看堆栈顶部。该字符串与 之间的字符
我正在使用 Node.js 和 WebSocket 创建基本的一对一聊天。每次客户端连接时,都会向他们发送其 ID 以及 salt+id 的 MD5 哈希值。然后,他们需要与另一个客户配对。当它们配对
我刚刚开始深入研究 Racket 宏,并尝试制作一个简洁的宏定义宏。我想扩展这样的表达式: (macro id (param) replacement1 (params ...) re
我有一个 foreach 循环(看,它在下面),我得到了一些元素。将此数据存储到 MySQL 中时,我想选择每个“count[]”所属的类别(此处标记为“interaction[]”)。例如,如果第
是否有可能找到一种更好的/模块化的方式来为配对分配相同的颜色,而不是像我目前实现的那样对它们进行硬编码? 如果对象 fname 匹配,则分配相同的颜色。 以下是 javascript 对象的子集。 d
在我的 iPhone 应用程序中,我想与固件设备通信。在连接期间,他们可以要求提供配对 key ,但他们说他们没有提供输入 key 的规定。 在这种情况下,USP(用户智能手机)永远不知道输入的 ke
这个问题在这里已经有了答案: How to merge every two lines into one from the command line? (21 个回答) 关闭 6 年前。 使用一个简
function pair(str) { var dna = []; var dnaarr = []; for(var i = 0; i < str.length; i++) {
我是一个相对年轻的开发人员,我对一些事情感到困惑。 这是我的代码: function pairElement(str) { var arr = []; var pairs = [
我有一个脚本,可以从文本文件中读取并插入元素,以便可以对它们进行样式设置和显示。但是,我现在想在 DIV 中配对两个元素。这是代码: var lines = request.responseText.
.Spotify:hover img { display:block; position: absolute; z-index: 0; top:17%; lef
我正在努力使用摩托罗拉随 RAZR 提供的 BT 4.0 API。在 one of their documents它声明在连接和使用他们的框架之前使用 Android API 进行配对。根据他们的说明
谁能告诉我一次可以通过蓝牙将多少台设备与 iPhone 配对..需要帮助..直到现在我还没有确切的数字。 最佳答案 在 iOS 6.1.4 中,低功耗蓝牙设备的当前限制是 10 个同时连接(至少在 N
我正在创建一个扩展推送通知的 Android Wear 应用程序。当推送通知进来时,我的应用程序会从服务器下载大约 10 张图像,并在 watch 上显示这些额外的图像。这些图像特定于 android
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题?通过 editing this post 添加详细信息并澄清问题. 8年前关闭。 Improve this
我有一个类似于使用以下命令获得的数据表: dt <- data.table( time = 1:8, part = rep(c(1, 1, 2, 2), 2), type = rep(c(
为了回应有用的评论,我编辑了原始问题(我假设 for 循环和应用循环给出不同的结果)。 我正在使用 R 运行大量 2 组 t 检验,使用来自分隔表的输入。根据此处和其他地方的建议,我尝试了“for-l
是否可以将脚本(例如 MathJax)加载到 EpicEditor 预览 iFrame 中?我希望我的预览是正确的 Markdown,然后运行 javascript 来预览 MathJax 内容。
我是一名优秀的程序员,十分优秀!