English 中文(简体)
C#中卡游戏中的最佳卡片选择
原标题:Best card choice in card game in C#

问题在于按照这些规则在游戏的每一时刻选择最佳选择:

  • 你们只能拿到最左边或最右心。

  • 你们的反对者将首先从左边或最右心中选择最高卡。 如果它有tie,它就将取胜。 考虑到这一点并不总是最佳选择。

有时是无法赢得胜利的,但是,你必须显示,通过打击这种反对者,你能够增加最大的距离(或说是战略)。

例:

Cards:    1 2 4 2 8 4 3
Opponent: 3 4 2 2 = 11
Me:       1 8 4 = 13

在此,我选择了第二次,而不是4次,因此,我可以在8次发言。 因此,选择最高卡并不总是最佳的。

I ve been trying to implement this solution using recursion but I m not sure it s the best option. Any ideas on how to design this algorithm?

[EDIT] Thanks to @PengOne for it s generous help. This is the code im trying to implement, but unfortunately it s giving me errors. What should I fix in it? I m editing this as I progress.

static int cardGameValue(List<int> D, int myScore, int opponentScore)
{
    if (D.Count == 0) return myScore;
    else
    {
        if (D[0] <= D[D.Count - 1])
        {
            opponentScore += D[D.Count - 1];
            D.RemoveAt(D.Count - 1);
        }
        else
        {
            opponentScore += D[0];
            D.RemoveAt(0);
        }

        int left = cardGameValue(
                new List<int>(D.GetRange(1, D.Count - 1)),
                myScore + D[0],
                opponentScore);

        int right = cardGameValue(
                new List<int>(D.Take(D.Count - 2)),
                myScore + D[D.Count - 1],
                opponentScore);

        if (left >= right)
        { return left; }
        else
        { return right; }
    }
}
最佳回答

利用再入侵从最简单的案例中找到解决办法。

<代码>D为卡片阵列。 ∗∗∗∗∗ B 是你的反对者总卡。 <代码>S = A-B为游戏价值。 如果<代码>S>0,如果失去<代码>S<0和,请打上<>。 S=0

The easiest is to make two moves at once, your move followed by the opponent s determined move. There are two base cases to consider:

  • 如果length(D) = 0, 返回S。 游戏已经结束。

  • If length(D) == 1, return S + D[0]. You choose the remaining card, and the game ends.

在<代码>length(D) > 1时,对两种可能性进行评估。

  • Let L be the result of the game if you choose the left card followed by the opponent doing his deterministic move, i.e.

    L = D[0] - max(D[...],D[N-1]+kaGameValue(newD)

  • Let R be the result of the game if you choose the right card followed by the opponent doing his deterministic move, i.e.

    R = D[N-1] - max(D[0],D[N-2]) + cardGameValue(newD)

Choose the play corresponding to the larger number, i.e. take D[0] if L>=R, otherwise take D[N-1]. Here N = length(D).

问题回答

这部法典实际上已经最终生效。 感谢大家的支持。

    static int cardGameValue(List<int> D, int myScore, int opponentScore)
    {
        if (D.Count == 0) return myScore;
        else if (D.Count == 1)
        {
            opponentScore += D[0];
            return myScore;
        }
        else
        {
            if (D[0] <= D[D.Count - 1])
            {
                opponentScore += D[D.Count - 1];
                D.RemoveAt(D.Count - 1);
            }
            else
            {
                opponentScore += D[0];
                D.RemoveAt(0);
            }

            int left = cardGameValue(new List<int>(D.GetRange(1, D.Count - 1)), myScore + D[0], opponentScore);

            int right = cardGameValue(new List<int>(D.GetRange(0, D.Count - 1)), myScore + D[D.Count - 1], opponentScore);

            if (left >= right)
            {
                return left;
            }
            else
            {
                return right;
            }
        }
    }




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签