Pegz Game C# - 帮助入门

时间:2012-03-28 00:47:56

标签: c# panel

我的任务是在C#中制作一个基本的Pegz游戏作为学校的作业。规则可以在这里找到:http://tinyurl.com/6wro5xc。我不是一步一步地寻找如何建立游戏,而是如何开始。我是C#应用程序开发的新手,但有C#web开发的背景。

到目前为止,我有一个流量控制布局面板,其中包含一个5x5网格的常规面板。在每个常规面板中,我有一个包含挂钉图像的图片框。所有面板都已满,只有一个仍为空。有关示例,请参见附图。为了跟踪5x5面板的网格,我想我会使用一个二维BitArray来保存网格中面板的坐标,如果存在一个挂钩则为true,如果不存在则为false。

我的第一个问题是如何将网格绑定到数组。换句话说:我怎么知道数组的[0] [0]代表网格的左上角?我有数组BitArray[,] board = new BitArray[5,5];的以下构造函数我知道我首先必须循环遍历数组并将除了一个值之外的所有值分配给true,但是我将需要在数组中获取该数据并应用它到网格。如何将该数组绑定到网格的概念是我坚持的部分。

我感谢任何帮助。

enter image description here

2 个答案:

答案 0 :(得分:2)

毫无疑问,这个想法比这个更好,但是这里有一些东西可以让你开始......

不是创建一个位数组,而是创建一个您创建的类的数组来表示一个点,该点还包含设置/取消设置该点的方法。然后你可以做像MyGrid [x] [y] .Set();。

这样的事情

您也可以考虑使用集合而不是数组。除了需要它们的低级或遗留类型操作之外,我很少再使用数组。

答案 1 :(得分:0)

使用Dyanmic数组比MultiDimensional数组快得多,但如果它更容易使用后者并且您知道值。假设你知道框中每个整数的值,看起来你有一个[5] [5]二维数组。在某处使用.NET和C ++可以很好地启动:

#include <iostream>
#include <vector>

using namespace std;

int main()
{

vector <int> DynArrNums(25);

DynArrNums[0] = //Put any value here which you know for the rules of your game as a number;
    DynArrNums[1] = 1001;
    DynArrNums[2] = 1002;//Put any value here which you know for the rules of your game as a number;
    DynArrNums[3] = 1003;//Put any value here which you know for the rules of your game as a number;
    DynArrNums[4] = 1004;//Put any value here which you know for the rules of your game as a number;
    DynArrNums[5] = 1005;//Put any value here which you know for the rules of your game as a number;
    DynArrNums[6] = 1006;//Put any value here which you know for the rules of your game as a number;
    DynArrNums[7] = 1007;//Put any value here which you know for the rules of your game as a number;
    DynArrNums[8] = 1008;//Put any value here which you know for the rules of your game as a number;
    DynArrNums[9] = 1009;//Put any value here which you know for the rules of your game as a number;
    DynArrNums[10] = 1010;//Put any value here which you know for the rules of your game as a number;
    DynArrNums[11] = 1011;//Put any value here which you know for the rules of your game as a number;
    DynArrNums[12] = 1012;//Put any value here which you know for the rules of your game as a number;
    DynArrNums[13] = 1013;//Put any value here which you know for the rules of your game as a number;
    DynArrNums[14] = 1014;//Put any value here which you know for the rules of your game as a number;
    DynArrNums[15] = 1015;//Put any value here which you know for the rules of your game as a number;
    DynArrNums[16] = 1016;//Put any value here which you know for the rules of your game as a number;
    DynArrNums[17] = 1017;//Put any value here which you know for the rules of your game as a number;
    DynArrNums[18] = 1018;//Put any value here which you know for the rules of your game as a number;
    DynArrNums[19] = 1019;//Put any value here which you know for the rules of your game as a number;
    DynArrNums[20] = 1020;//Put any value here which you know for the rules of your game as a number;
    DynArrNums[21] = 1021;//Put any value here which you know for the rules of your game as a number;
    DynArrNums[22] = 1022;//Put any value here which you know for the rules of your game as a number;
    DynArrNums[23] = 1023;//Put any value here which you know for the rules of your game as a number;
    DynArrNums[24] = 1024;//Put any value here which you know for the rules of your game as a number;

    cout << "Enter another number for the array" << endl;
    int AnotherNum = 0;
    cin >> AnotherNum;
    DynArrNums.push_back(AnotherNum);

    cout << "Number of integers in array: " << DynArrNums.size() << endl;
    cout << "Last element in array: ";
    cout << DynArrNums[DynArrNums.size() - 1] << endl;

return 0;
}

记住第一个数组存储为0,这就是为什么最后需要减1的原因。我只是尝试了它并且它有效:)

但是动态数组并不是特别需要知道你缺少的整数是数组中的中间面板,它只需要知道你还要添加一个。当然,这使它可以为您的游戏定制,您可以放任何价值。在这种情况下,它将它添加到结尾,您可以通过数组中的序列调用任何值。

要查找数字[0],第一个面板[1]是下一个面板。 [6]因此将是第二行的第一个小组[11]将是第3行中的第一个小组,依此类推。

最后,DynArrNums [25]并不意味着你有25个填充框。它只是意味着你的面板上有25个盒子,代码知道它们是一个缺失的,这就是为什么它最后要求它。

祝你好运。