Ejemplo n.º 1
0
        private void aiAttack(Position attacked)
        {
            Position from = select.getPosition();
            Position to   = new Position();

            to.x = attacked.x - select.getPosition().x > 0 ? attacked.x + 1 : attacked.x - 1;
            to.y = attacked.y - select.getPosition().y > 0 ? attacked.y + 1 : attacked.y - 1;
            draughts.Remove(Arbiter.findDraught(attacked, draughts));
            mw.removeEllipse(attacked.x, attacked.y);
            bool isKing = select.isKing();

            select.moveTo(to);
            if (isKing != select.isKing())
            {
                mw.setKing(from.x, from.y);
            }
            mw.turnOn(from.x, from.y, to.x, to.y);
        }
Ejemplo n.º 2
0
 public bool endOfTurn()
 {
     if (isAttacking && Arbiter.needAttack(select, draughts))
     {
         return(false);
     }
     else
     {
         if (Arbiter.isVictory(isBlackTurn, draughts))
         {
             mw.victoryMessage();
             return(true);
         }
         isBlackTurn = !isBlackTurn;
         isAttacking = false;
         return(true);
     }
 }
Ejemplo n.º 3
0
 public bool wantMove(Position to)
 {
     if (attackers != null)
     {
         if (attackers.Contains(select) && Arbiter.isAttack(select, to, draughts))
         {
             return(true);
         }
         else
         {
             return(false);
         }
     }
     else
     {
         return(Arbiter.canMove(select, to, draughts));
     }
 }
Ejemplo n.º 4
0
        private int checkDraught(Position pos, List <Draught> list)
        {
            int points = POINTS_FOR_MOVE_WITHOUT_NEXT_EATEN;

            foreach (Draught dr in list)
            {
                if (dr.isBlack() != isBlack && Arbiter.canAttack(dr, pos, list))
                {
                    points -= POINTS_FOR_MOVE_WITHOUT_NEXT_EATEN;
                    break;
                }
            }
            if (isNear(pos, list))
            {
                points += POINTS_FOR_SAVE;
            }
            return(points);
        }
Ejemplo n.º 5
0
        public void moveTo(Position to)
        {
            Position from   = select.getPosition();
            bool     isKing = select.isKing();

            if (Arbiter.isAttack(select, to, draughts))
            {
                attack(to);
            }
            else
            {
                select.moveTo(to);
            }

            if (isKing != select.isKing())
            {
                mw.setKing(from.x, from.y);
            }
            mw.turnOn(from.x, from.y, to.x, to.y);
        }
Ejemplo n.º 6
0
        private int attack(Draught d, List <Draught> list)
        {
            List <Position> pos = d.getAttackMoves();

            foreach (Position p in pos)
            {
                if (Arbiter.canAttack(d, p, list))
                {
                    Position to = new Position();
                    to.x = p.x - d.getPosition().x > 0 ? p.x + 1 : p.x - 1;
                    to.y = p.y - d.getPosition().y > 0 ? p.y + 1 : p.y - 1;

                    List <Draught> newList = copyList(list);
                    newList.Remove(Arbiter.findDraught(p, newList));
                    Arbiter.findDraught(d.getPosition(), newList).moveTo(to);
                    return(POINTS_FOR_EAT + (Arbiter.findDraught(to, newList).isKing() != d.isKing()?POINTS_FOR_KING:0) + attack(Arbiter.findDraught(to, newList), newList));
                }
            }
            return(checkDraught(d.getPosition(), list));
        }
Ejemplo n.º 7
0
        public void turnAI()
        {
            Turn t = comp.doTurn(draughts);

            if (t.d.getPosition().x != -1)
            {
                select = t.d;
                if (t.isAttack)
                {
                    aiAttack(t.to);
                    while (Arbiter.getDraughtsNeedToMove(isBlackTurn, draughts) != null)
                    {
                        Turn tur = comp.doTurn(draughts);
                        if (tur.d.getPosition().x != -1)
                        {
                            select = tur.d;
                            aiAttack(tur.to);
                        }
                        else
                        {
                            mw.errorMessage();
                        }
                    }
                }
                else
                {
                    moveTo(t.to);
                }
                if (Arbiter.isVictory(isBlackTurn, draughts))
                {
                    mw.victoryMessage();
                }
            }
            else
            {
                mw.errorMessage();
            }

            isBlackTurn = !isBlackTurn;
        }
Ejemplo n.º 8
0
 private void freeMoves()
 {
     foreach (Draught d in draughts)
     {
         if (d.isBlack() == isBlack)
         {
             foreach (Position p in d.getFreeMoves())
             {
                 if (Arbiter.findDraught(p, draughts) == null)
                 {
                     List <Draught> newList = copyList(draughts);
                     Arbiter.findDraught(d.getPosition(), newList).moveTo(p);
                     int points = checkDraught(p, newList);
                     if (d.isKing() != Arbiter.findDraught(p, newList).isKing())
                     {
                         points += POINTS_FOR_KING;
                     }
                     sortedMoves.Add(new Turn(d, false, p), points);
                 }
             }
         }
     }
 }
Ejemplo n.º 9
0
 public bool notMoves(bool isBlack)
 {
     foreach (Draught d in draughts)
     {
         if (d.isBlack() == isBlack)
         {
             foreach (Position p in d.getFreeMoves())
             {
                 if (Arbiter.canMove(d, p, draughts))
                 {
                     return(false);
                 }
             }
             foreach (Position p in d.getAttackMoves())
             {
                 if (Arbiter.canAttack(d, p, draughts))
                 {
                     return(false);
                 }
             }
         }
     }
     return(true);
 }
Ejemplo n.º 10
0
        void item3_Click(object sender, RoutedEventArgs e)
        {
            System.IO.StreamReader fileR;
            try { fileR = new System.IO.StreamReader("hello.txt"); }
            catch (System.IO.FileNotFoundException) { errorMessage(); return; }
            catch (System.IO.DirectoryNotFoundException) { errorMessage(); return; }
            List <Draught> list = new List <Draught>();

            while (!fileR.EndOfStream)
            {
                String   s = fileR.ReadLine();
                bool     isKing;
                bool     isBlack;
                Position pos;
                if (s.Contains("true"))
                {
                    isKing = true;
                }
                else if (s.Contains("false"))
                {
                    isKing = false;
                }
                else
                {
                    errorMessage(); fileR.Close(); return;
                }
                if (s.Contains("black"))
                {
                    isBlack = true;
                }
                else if (s.Contains("white"))
                {
                    isBlack = false;
                }
                else
                {
                    errorMessage(); fileR.Close(); return;
                }

                int st  = s.LastIndexOf("(");
                int end = s.LastIndexOf(")");
                if (end - st != 4 || s[st + 2] != ',')
                {
                    errorMessage(); fileR.Close(); return;
                }
                int x, y;
                try
                {
                    x = int.Parse(s[st + 1].ToString());
                    y = int.Parse(s[st + 3].ToString());
                    if (x > 7 || y > 7)
                    {
                        errorMessage(); fileR.Close(); return;
                    }
                }
                catch (System.ArgumentNullException) { errorMessage(); fileR.Close(); return; }
                catch (System.FormatException) { errorMessage(); fileR.Close(); return; }
                catch (System.OverflowException) { errorMessage(); fileR.Close(); return; }
                pos = new Position(x, y);
                if (Arbiter.findDraught(pos, list) != null)
                {
                    errorMessage(); fileR.Close(); return;
                }
                list.Add(new Draught(isBlack, isKing, pos));
            }
            fileR.Close();
            clear();
            isSelected = false;
            initChessmate(blockDark, blockWhite);
            setEllipseFromFile(list, draughtWhite, draughtBlack, kingWhite, kingBlack);
            t = new Table(this, list);
        }
Ejemplo n.º 11
0
 public void startOfTurn(Position pos)
 {
     attackers = Arbiter.getDraughtsNeedToMove(isBlackTurn, draughts);
     select    = Arbiter.findDraught(pos, draughts);
 }