c# 如何检查件是否已订购?

c# how to check if pieces are ordered?

图1.

properly ordered 2 pieces there can be 3 also

图2.

if empty space is between pieces its wrong order

我制作了检查所有棋子是否在基地或目标中的方法,如果是的话return是正确的,现在我需要另一种方法。

如果像图1那样,我需要把投掷次数改成3次,如果像图2那样,我只能投1次。

我有 4 个 goalPositions 和 4 个 piecePositions,需要检查 54-51 个路径位置的棋子是否正确排序(路径是 55 个字段 0-54 的数组),如果是 return true如果不是 return 错误。

我是 C# 的新手,直到现在还没有机会使用订单检查。

我试图用 3 个整数列表 goalPositions(填充了 51、52、53、54 个路径位置)、piecePositions(用 getPosition() 方法填充了棋子位置)和 piecesOnGoal(保留用于计数棋子)在目标位置上)。但运气不好。

我将添加一些代码。 Player 的一部分 class 使用该列表和方法检查球门或基地中的棋子

class Player    {
    protected PieceSet[] pieces;
    Color color;
    int numberOfThrows;
    Dice dice;
    public List<int> goalPositions;
    public List<int> piecePositions;
    public List<int> piecesOnGoal;


    public enum Color
    {
        Yellow, Green, Blue, Red
    }


    public Player(Color color)
    {
        int[] path = new int[55];
        this.color = color;
        dice = new Dice();
        numberOfThrows = 3;
        switch (color)
        {
            case Color.Yellow:
                path = BoardHelper.getYellowPath();
                break;
            case Color.Green:
                path = BoardHelper.getGreenPath();
                break;
            case Color.Blue:
                path = BoardHelper.getBluePath();
                break;
            case Color.Red:
                path = BoardHelper.getRedPath();
                break;
        }
        pieces = new PieceSet[4];
        pieces[0] = new PieceSet(path, 0);
        pieces[1] = new PieceSet(path, 1);
        pieces[2] = new PieceSet(path, 2);
        pieces[3] = new PieceSet(path, 3);


        piecePositions = new List<int>(4);
        piecePositions.Add(pieces[0].getPosition());
        piecePositions.Add(pieces[1].getPosition());
        piecePositions.Add(pieces[2].getPosition());
        piecePositions.Add(pieces[3].getPosition());


        goalPositions = new List<int>(4);
        goalPositions.Add(51);
        goalPositions.Add(52);
        goalPositions.Add(53);
        goalPositions.Add(54);


       piecesOnGoal =new List<int>();
    }

    public bool isAllPiecesInBaseOrGoal()
    {
        if ((pieces[0].getPosition() < 4 || pieces[0].getPosition() > 50) &&
           (pieces[1].getPosition() < 4 || pieces[1].getPosition() > 50) &&
           (pieces[2].getPosition() < 4 || pieces[2].getPosition() > 50) &&
           (pieces[3].getPosition() < 4 || pieces[3].getPosition() > 50))
            return true;
        else
            return false;
    }

这就是我想解决问题的方法:检查 goalPositions 是否包含 piecePositions。如果是,将该位置添加到 piecesOnGoal。现在我需要以某种方式检查这些 piecesOnGoal 是否已订购。如果是,则 return 为真。如果不是,则为假。

我愿意接受任何建议。

public bool isAllPiecesAreOrderedInGoal()
{            
    for (int i = 0; i < 4; i++)
    {
        if (goalPositions.Contains(piecePositions[i])) 
        {
            piecesOnGoal.Add(piecePositions[i]);
        }                               
    }
}

感谢任何帮助。

我会建议一种方法来检查是否有任何可能的移动。如果本垒或球门外有棋子,或者球门内有棋子前面有空位,则可以移动:

bool IsMovePossible()
{
    // check every piece
    for(int iPiece = 0; iPiece < 4; ++iPiece)
    {
        // if it is outside of home or goal, there is a possible move
        if(piecePositions[iPiece] > 3 && piecePositions[iPiece] < goalPositions.First())
            return true;
        // if it is in the goal, check the next position
        if(piecePositions[iPiece] >= goalPositions.First() 
              && piecePositions[iPiece] < goalPositions.Last() 
              && !piecePositions.Any(p => p == piecePositions[iPiece] + 1))
            return true;
    }
    // we have found no piece with a possible move
    return false;
}

然后,根据此方法的 return 值决定用户掷骰子的频率。请注意,如果您维护倒置地图(即,对于每个位置,存储哪个部分在该位置上),最后一次检查(piecePositions.Any)可以更有效地完成。不过检查四件应该没问题。