方法返回两个值(彼此重叠)

时间:2015-02-28 13:08:25

标签: c# minesweeper

static int CountMinesLeft(string[,] board, string[,] visibilityArray, int count)
{
    if (visibilityArray.Length == 81)
    {
        count = 10;
    }
    else if (visibilityArray.Length == 256)
    {
        count = 40;
    }
    else if (visibilityArray.Length == 480) 
    {
        count = 99;
    }

    for (int i = 0; i < board.GetLength(0); i++)
    {
        for (int p = 0; p < board.GetLength(1); p++)
        {

            if (visibilityArray[i, p] == "2")
            {
               count--;
            }

        }
    }
    return count;
}
    static void PrintMinesLeft(string[,] board, string[,] visibilityArray)
{

    Console.SetCursorPosition(11, 0);
    Console.WriteLine(CountMinesLeft(board, visibilityArray, 0));
}

这是扫雷游戏的一部分。

在这种方法中我应该做矿井计数器。如果难度为1(81),2(256),3(480),则矿数应为10,40,99。这里的问题是它返回两个值(10和count--,40和count-- ,99和count--)我只需要从循环中减去一个。

编辑:通过多个值,我的意思是在输出上有例如10,并且在其上面减去了10。这是图片http://s22.postimg.org/3vp1ujo71/8shit.png

2 个答案:

答案 0 :(得分:1)

你应该尝试ref或out modificators:

static int CountMinesLeft(string[,] board, string[,] visibilityArray, ref int anotherCount)
{
    if (visibilityArray.Length == 81)
    {
        anotherCount = 10;
    }
    else if (visibilityArray.Length == 256)
    {
        anotherCount = 40;
    }
    else if (visibilityArray.Length == 480) 
    {
        anotherCount = 99;
    }

    int count = anotherCount;
    for (int i = 0; i < board.GetLength(0); i++)
    {
        for (int p = 0; p < board.GetLength(1); p++)
        {

            if (visibilityArray[i, p] == "2")
            {
               count--;
            }
        }
    }
    return count;

}

调用方法:

int anotherCount = 0;
int count = CountMinesLeft(board, visibilityArray, ref anotherCount);

所以,在你有&#34;计数&#34;和&#34; anotherCount&#34;

答案 1 :(得分:0)

如果您尝试从方法中返回多个值,可以使用Tuple

另请查看此question