Exemple #1
0
 private void RemoveIllegalMoves(MoveSet ms)
 {
     ms.moves.RemoveAll(a => a.numTaken < ms.minScore);
 }
Exemple #2
0
 public List<Move> GetAllAllowedMoves(int minPiecesToTake = 0)
 {
     MoveSet ms = new MoveSet();
     ms.minScore = minPiecesToTake;
     
     for (int i = 0; i < 50; i++) 
         if (pieces1D[i].color == currentColor)
             pieces1D[i].GetAllMoves (this, ms);
     
     RemoveIllegalMoves(ms);
     
     if (ms.minScore > 0 && ms.moves.Count == 0)
         throw new Exception("Invalid minScore value");
     
     return ms.moves;
 }
Exemple #3
0
        public bool GetAllMoves(Board b, MoveSet moveset, TakingMove tm = null, int depth = 0, bool needClone = false)
        {
            if (color == Color.None)
                return false;
            bool childNeedsClone = false;
            bool foundNewMove = false;

            //int direction = color == Color.White ? 1 : -1;
            int maxSteps = type == Type.Dam ? 10 : 1;
            for (int yDirection = -1; yDirection <= 1; yDirection += 2)
            {
                for (int xDirection = -1; xDirection <= 1; xDirection += 2)
                {
                    for (int i = 1; i < maxSteps+1; i++)
                    {
                        int targetX = x + i * xDirection;
                        int targetY = y + i * yDirection;
                        if (targetX < 0 || targetX > 9 || targetY < 0 || targetY > 9)
                            break;

                        if (b.pieces [targetX, targetY].color == this.color) { // a piece of our own army
                            break;
                        }
                        else if (b.pieces [targetX, targetY].color == Color.None)
                        {
                            // normal move, need to check y direction
                            if ((type == Type.Dam || (yDirection == (color == Color.White ? 1 : -1))) &&
                                tm == null && moveset.minScore == 0)
                            {
                                moveset.moves.Add (new Move(x, y, targetX, targetY)); // normal move, not taking anything
                            }
                        }
                        else // pieces of of opponent
                        {
                            if (b.pieces[targetX, targetY].taken || b.pieces[targetX, targetY] == this ||
                                targetX +xDirection < 0 || targetX + xDirection > 9 ||
                                targetY + yDirection < 0 || targetY + yDirection > 9)
                                break;
                            if (b.pieces [x + (i+1) * xDirection, y + (i+1) * yDirection].color != Color.None)
                                break;

                            foundNewMove = true;

                            // we can take it!
                            Color c = this.color;
                            this.color = Color.None;

                            int victimX = targetX;
                            int victimY = targetY;
                            bool z = b.pieces[victimX, victimY].taken;
                            // mark it as taken so we cantb.pieces[victimX, victimY].taken jump over it again
                            b.pieces[victimX, victimY].taken = true;

                            // We need to find moves depth-first, or our taken scheme will not work
                            do
                            {
                                targetX += xDirection;
                                targetY += yDirection;
                                if (targetX < 0 || targetX > 9 || targetY < 0 || targetY > 9 ||
                                                b.pieces [targetX,targetY].color != Color.None)
                                    break;
                                if (tm == null)
                                {
                                    tm = new TakingMove(x, y);
                                }
                                else if (needClone)
                                {
                                    tm = tm.Clone(depth);
                                }

            #if DEBUG
                                if (b.pieces[victimX,victimY].color == Color.None)
                                    throw new Exception();
            #endif
                                tm.AddHop(targetX,targetY, victimX, victimY, b.pieces [victimX, victimY].type);

                                b.pieces [targetX,targetY].color = c;
                                //TODO: type!!!
                                b.pieces[targetX, targetY].type = type;
                                childNeedsClone |= b.pieces [targetX,targetY].GetAllMoves(b, moveset, tm, depth + 1, childNeedsClone);
                                b.pieces [targetX,targetY].color = Color.None;
                                needClone = true;

                            }
                            while (++i < maxSteps);

            #if DEBUG
                            if (b.pieces[victimX, victimY].taken == false)
                                throw new Exception();
            #endif
                            b.pieces[victimX, victimY].taken = false;
                            this.color = c;
                        }
                    }
                }
            }
            if (!foundNewMove && tm != null && tm.numTaken >= moveset.minScore)
            {
                moveset.moves.Add(tm);
                moveset.minScore = tm.numTaken;
            }
            return foundNewMove;
        }