0

我正在尝试创建一种“你击沉了我的战舰”游戏。到目前为止,我有两个阵列: 1. 棋盘(Table)——要在上面玩的表面 2. 船(Avion)——在这种情况下是一架飞机

我想要实现的是创建一个包含整个表面并将平原放在其中的第三个数组。

所以我有飞机:

0 0 1 0 0
1 1 1 1 1
0 0 1 0 0
0 0 1 0 0
0 1 1 1 0

和表面:

  1 2 3 4 5 6 7 8 8 10
A 0 0 0 0 0 0 0 0 0 0
B 0 0 0 0 0 0 0 0 0 0
C 0 0 0 0 0 0 0 0 0 0
D 0 0 0 0 0 0 0 0 0 0
E 0 0 0 0 0 0 0 0 0 0
F 0 0 0 0 0 0 0 0 0 0
G 0 0 0 0 0 0 0 0 0 0
H 0 0 0 0 0 0 0 0 0 0
I 0 0 0 0 0 0 0 0 0 0
J 0 0 0 0 0 0 0 0 0 0

我想要实现的是:

  1 2 3 4 5 6 7 8 8 10
A 0 0 1 0 0 0 0 0 0 0
B 1 1 1 1 1 0 0 0 0 0
C 0 0 1 0 0 0 0 0 0 0
D 0 0 1 0 0 0 0 0 0 0
E 0 1 1 1 0 0 0 0 0 0
F 0 0 0 0 0 0 0 0 0 0
G 0 0 0 0 0 0 0 0 0 0
H 0 0 0 0 0 0 0 0 0 0
I 0 0 0 0 0 0 0 0 0 0
J 0 0 0 0 0 0 0 0 0 0

int counterAvion = 0;
        int counterTable = 0;

        int[,] Table = new int[10, 10];
        int[,] Avion = new int[5, 5];
        int[,] Combined = new int[10, 10];


        // Table[][]
        //rowA
        Table[0, 0] = 0; Table[0, 1] = 0; Table[0, 2] = 0; Table[0, 3] = 0; Table[0, 4] = 0;
        Table[0, 5] = 0; Table[0, 6] = 0; Table[0, 7] = 0; Table[0, 8] = 0; Table[0, 9] = 0;
        //rowB
        Table[1, 0] = 0; Table[1, 1] = 0; Table[1, 2] = 0; Table[1, 3] = 0; Table[1, 4] = 0;
        Table[1, 5] = 0; Table[1, 6] = 0; Table[1, 7] = 0; Table[1, 8] = 0; Table[1, 9] = 0;
        //rowC
        Table[2, 0] = 0; Table[2, 1] = 0; Table[2, 2] = 0; Table[2, 3] = 0; Table[2, 4] = 0;
        Table[2, 5] = 0; Table[2, 6] = 0; Table[2, 7] = 0; Table[2, 8] = 0; Table[2, 9] = 0;
        //rowD
        Table[3, 0] = 0; Table[3, 1] = 0; Table[3, 2] = 0; Table[3, 3] = 0; Table[3, 4] = 0;
        Table[3, 5] = 0; Table[3, 6] = 0; Table[3, 7] = 0; Table[3, 8] = 0; Table[3, 9] = 0;
        //rowE
        Table[4, 0] = 0; Table[4, 1] = 0; Table[4, 2] = 0; Table[4, 3] = 0; Table[4, 4] = 0;
        Table[4, 5] = 0; Table[4, 6] = 0; Table[4, 7] = 0; Table[4, 8] = 0; Table[4, 9] = 0;
        //rowF
        Table[5, 0] = 0; Table[5, 1] = 0; Table[5, 2] = 0; Table[5, 3] = 0; Table[5, 4] = 0;
        Table[5, 5] = 0; Table[5, 6] = 0; Table[5, 7] = 0; Table[5, 8] = 0; Table[5, 9] = 0;
        //rowG
        Table[6, 0] = 0; Table[6, 1] = 0; Table[6, 2] = 0; Table[6, 3] = 0; Table[6, 4] = 0;
        Table[6, 5] = 0; Table[6, 6] = 0; Table[6, 7] = 0; Table[6, 8] = 0; Table[6, 9] = 0;
        //rowH
        Table[7, 0] = 0; Table[7, 1] = 0; Table[7, 2] = 0; Table[7, 3] = 0; Table[7, 4] = 0;
        Table[7, 5] = 0; Table[7, 6] = 0; Table[7, 7] = 0; Table[7, 8] = 0; Table[7, 9] = 0;
        //rowI
        Table[8, 0] = 0; Table[8, 1] = 0; Table[8, 2] = 0; Table[8, 3] = 0; Table[8, 4] = 0;
        Table[8, 5] = 0; Table[8, 6] = 0; Table[8, 7] = 0; Table[8, 8] = 0; Table[8, 9] = 0;
        //rowJ
        Table[9, 0] = 0; Table[9, 1] = 0; Table[9, 2] = 0; Table[9, 3] = 0; Table[9, 4] = 0;
        Table[9, 5] = 0; Table[9, 6] = 0; Table[9, 7] = 0; Table[9, 8] = 0; Table[9, 9] = 0;

        // Avion[][]
        //head
        Avion[0, 0] = 0; Avion[0, 1] = 0; Avion[0, 2] = 1; Avion[0, 3] = 0; Avion[0, 4] = 0;
        //wings
        Avion[1, 0] = 1; Avion[1, 1] = 1; Avion[1, 2] = 1; Avion[1, 3] = 1; Avion[1, 4] = 1;
        //mid_1
        Avion[2, 0] = 0; Avion[2, 1] = 0; Avion[2, 2] = 1; Avion[2, 3] = 0; Avion[2, 4] = 0;
        //mid_2
        Avion[3, 0] = 0; Avion[3, 1] = 0; Avion[3, 2] = 1; Avion[3, 3] = 0; Avion[3, 4] = 0;
        //tail
        Avion[4, 0] = 0; Avion[4, 1] = 1; Avion[4, 2] = 1; Avion[4, 3] = 1; Avion[4, 4] = 0;



        // Output Avion[][]
        Console.WriteLine("\nAirplane:\n");
        foreach (var item in Avion)
        {
            counterAvion++;
            Console.Write(item.ToString() + "  ");
            if (counterAvion == 5)
            {
                Console.WriteLine("\n");
                counterAvion = 0;
            }
        }

        // Output Table[][]
        Console.WriteLine("\nTable:\n");
        Console.WriteLine("  1  2  3  4  5  6  7  8  9  10\n");
        int counterColumn = 1;
        foreach (var item in Table)
        {
            counterTable++;
            //Console.WriteLine(counterColumn);
            switch (counterColumn)
            {
                case 1:
                    Console.Write("A ");
                    break;
                case 12:
                    Console.Write("B ");
                    break;
                case 23:
                    Console.Write("C ");
                    break;
                case 34:
                    Console.Write("D ");
                    break;
                case 45:
                    Console.Write("E ");
                    break;
                case 56:
                    Console.Write("F ");
                    break;
                case 67:
                    Console.Write("G ");
                    break;
                case 78:
                    Console.Write("H ");
                    break;
                case 89:
                    Console.Write("I ");
                    break;
                case 100:
                    Console.Write("J ");
                    break;
                default:
                    break;
            }

            Console.Write(item.ToString() + "  ");
            if (counterTable == 10)
            {
                Console.WriteLine("\n");
                counterTable = 0;
                counterColumn++;
            }
            counterColumn++;
        }
Console.ReadLine();

我是 C# 的新手,到目前为止我很喜欢它,但它.. 嗯.. 与我很久以前玩的 BorlandC 不同。

那么,关于如何实现这一目标的任何想法?

我也知道到目前为止我的代码有点混乱,因此对此的任何输入表示赞赏。

4

2 回答 2

1

复制这些值并不是特别难。如果要创建第三个数组,则可以在开始时创建第一个数组的直接副本,然后对其进行修改。

public void Combine(int[,] first, int[,] second, int rowStart, int colStart)
{
    //TODO validate parameters, ensure arrays are all large enough, etc.

    for (int i = 0; i < second.GetLength(0); i++)
    {
        for (int j = 0; j < second.GetLength(1); j++)
        {
            first[colStart + i, rowStart + j] |= second[i, j];
        }
    }
}
于 2012-12-27T18:21:37.763 回答
0

假设 Airplane 的尺寸永远不会大于表面,您可以遍历 Airplane 并为每个索引将表面设置为相同的值。

for(int i = 0; i < Airplane.length(0); i++){
    for(int j = 0; j < Airplane.length(1); j++){         
        surface[i,j] = Airplane[i,j]

如果不是,您可以轻松计算出偏移量。

于 2012-12-27T18:16:39.577 回答