【问题标题】:Memory Match Game ++ How to randomize chars in an arrayMemory Match Game ++ 如何随机化数组中的字符
【发布时间】:2016-10-12 02:00:33
【问题描述】:

您好,我对这项作业有疑问。我的程序是 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");
 }

【问题讨论】:

  • 你尝试过哪些“多种想法”?
  • 为什么从不使用using namespace std;system(); functions
  • 我在课堂上被教导如何使用这些功能,我通读了链接,我从不知道从长远来看它们有多不方便 o-o @amanuel2。编辑:谢谢 m8
  • @paddy 我已经尝试在我的 begin() 函数的第一个 for 循环中操作 cards[x][y] 2D 数组,但没有成功。
  • @RoninDaMerc 你试过std::vector 吗? :) 。从长远来看非常方便,如果您可以使用库的话!

标签: c++ arrays random char


【解决方案1】:

“洗牌”卡片的一种方法是创建一个list,其中包含数组的所有索引。从list 中随机选择一个项目并将其存储在vector 中。删除该项目并重复,直到列表为空。然后,您在数组中有一个 vector 的“洗牌”索引。不要直接修改数组,而是将vector 的索引存储到数组中。

参见std::liststd::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,并从您的卡片组中随机分配一张卡片。

这就是我一开始发布的洗牌想法的用武之地。

【讨论】:

  • OP 在问为什么他的答案不起作用,而不是他问题的替代方法。建议应始终作为评论而不是答案。
  • 根据您的评论编辑。
  • OP现在可能想看看答案!
  • @MarcD 我明白你在做什么,你添加的新数组,它基本上只是从牌组中随机卡(我原来的二维数组)。我试过了,它仍然没有改变字符的顺序。我觉得我可以对此进行调整,但必须等到明天,谢谢,从这里的每个人那里学到了很多
猜你喜欢
  • 2016-01-04
  • 2014-11-30
  • 2011-08-17
  • 1970-01-01
  • 1970-01-01
  • 2014-02-09
  • 1970-01-01
  • 2020-10-04
  • 2015-09-07
相关资源
最近更新 更多