Exemple #1
0
    /** Add threats from from into into if they are not contained yet.
     *
     * @returns ArrayList<Threat> List of the added threats.
     */
    private ThreatList Merge(ThreatList into, ThreatList from)
    {
        ThreatList res = new ThreatList();

        foreach (Threat t in from)
        {
            // check for every t if it is already in into-list
            bool alreadyin = false;
            foreach (Threat ct in into)
            {
                alreadyin = true;
                if (ct.category == t.category && ct.fields.Count == t.fields.Count)
                {
                    for (int i = 0; i < ct.fields.Count; ++i)
                    {
                        alreadyin = alreadyin &&
                                    ((((Coordinate)(ct.fields[i])).X == ((Coordinate)(t.fields[i])).X) &&
                                     (((Coordinate)(ct.fields[i])).Y == ((Coordinate)(t.fields[i])).Y));
                    }
                }
                else
                {
                    alreadyin = false;
                }
            }
            if (alreadyin == false)
            {
                into.Add(t);
                res.Add(t);
            }
        }
        return(res);
    }
    public override object Clone()
    {
        ThreatList tl = new ThreatList ();

        for (int n = 0 ; n < Count ; ++n)
            tl.Add (((Threat) this[n]).Clone ());

        return (tl);
    }
    public override object Clone()
    {
        ThreatList tl = new ThreatList();

        for (int n = 0; n < Count; ++n)
        {
            tl.Add(((Threat)this[n]).Clone());
        }

        return(tl);
    }
 /** Add threats from from into into if they are not contained yet.
  *
  * @returns ArrayList<Threat> List of the added threats.
  */
 private ThreatList Merge(ThreatList into, ThreatList from)
 {
     ThreatList res = new ThreatList ();
     foreach (Threat t in from)
     {
         // check for every t if it is already in into-list
         bool alreadyin = false;
         foreach (Threat ct in into)
         {
             alreadyin = true;
             if (ct.category == t.category && ct.fields.Count == t.fields.Count)
             {
                 for (int i = 0; i < ct.fields.Count; ++i)
                 {
                     alreadyin = alreadyin &&
                         ( ( ((Coordinate)(ct.fields[i])).X == ((Coordinate)(t.fields[i])).X ) &&
                           ( ((Coordinate)(ct.fields[i])).Y == ((Coordinate)(t.fields[i])).Y ) );
                 }
             }
             else alreadyin = false;
         }
         if (alreadyin == false) {
             into.Add(t);
             res.Add(t);
         }
     }
     return res;
 }
    public void UpdateThreatLists(int[,] board, Coordinate move, int attacker)
    {
        // 0. Initialized the removelists
        ownremovedthreatlist = new ThreatList();
        oppremovedthreatlist = new ThreatList();
        ownaddedthreatlist = new ThreatList();
        oppaddedthreatlist = new ThreatList();

        // 1. Remove blocked threats from both lists.
        ThreatList lookup = new ThreatList();
        foreach (Threat t in oppthreatlist) {
            foreach (Coordinate c in t.fields) {
                if (c.X == move.X && c.Y == move.Y) {
                    oppremovedthreatlist.Add(t);
                    lookup.Add(t.cause);
                }
            }
        }
        foreach (Threat t in oppremovedthreatlist) oppthreatlist.Remove(t);
        foreach (Threat t in ownthreatlist) {
            foreach (Coordinate c in t.fields) {
                if (c.X == move.X && c.Y == move.Y) {
                    ownremovedthreatlist.Add(t);
                    lookup.Add(t.cause);
                }
            }
        }
        foreach (Threat t in ownremovedthreatlist) ownthreatlist.Remove(t);

        //Lookup if "causes" still cause threats
        foreach (Coordinate c in lookup)
        {
            if (board[c.X, c.Y] == 1)
            {
                ThreatList curthreats = searcher.investigate(board, c, 1);

                ownaddedthreatlist = Merge(ownthreatlist, curthreats);

            }
            if (board[c.X, c.Y] == -1)
            {
                ThreatList curthreats = searcher.investigate(board, c, -1);

                oppaddedthreatlist = Merge(oppthreatlist, curthreats);
            }
        }

        if (attacker == 1)
        {
            // We are attacking
            // 2. Add our threats we build to our list

            ThreatList curthreats = searcher.investigate(board, move, attacker, true);

            Merge(ownaddedthreatlist, Merge(ownthreatlist, curthreats));

        }
        else
        {
            // The opponent is attacking
            // 2. Add his threats to his list
            ThreatList curthreats = searcher.investigate(board, move, attacker, true);

            Merge(oppaddedthreatlist, Merge(oppthreatlist, curthreats));
        }
    }
    private ThreatList SearchBoards(Coordinate node)
    {
        ThreatList res = new ThreatList ();
        int turn = attacker;

        Item tmpval;
        int digit1 = 0;
        int digit2 = 0;
        int digit3 = 0;
        int digit4 = 0;
        for (int j = 8; j >= 0; --j)
        {
            if (j + node.X - 4 >= 0 && j + node.X - 4 < board.GetLength(0))
            {
                digit1 += board[node.X + j - 4, node.Y] * turn + 1;
                //			Console.WriteLine("Grabbing {0}/{1}", node.move.x + j - 4, node.move.y);
            }
            else digit1 += 3;
            if (j + node.Y - 4 >= 0 && j + node.Y - 4 < board.GetLength(1))
            {
                digit2 += board[node.X, node.Y + j - 4] * turn + 1;
                //			Console.WriteLine("Grabbing {0}/{1}", node.move.x , node.move.y + j -4);
            }
            else digit2 += 3;
            if (node.X + j - 4 >= 0 && node.X + j - 4 < board.GetLength(0) &&
                node.Y + j - 4 >= 0 && node.Y + j - 4 < board.GetLength(1) )
            {
                digit3 += board[node.X + j - 4, node.Y + j - 4] * turn + 1;
                //			Console.WriteLine("Grabbing {0}/{1}", node.move.x + j - 4, node.move.y + j - 4);
            }
            else digit3 += 3;
            if (node.X - j + 4 >= 0 && node.X - j + 4 < board.GetLength(0) &&
                node.Y + j - 4 >= 0 && node.Y + j - 4 < board.GetLength(1) )
            {
                digit4 += board[node.X - j + 4, node.Y + j - 4] * turn + 1;
                //			Console.WriteLine("Grabbing {0}/{1}", node.move.x - j + 4, node.move.y + j - 4);
            }
            else digit4 += 3;
            if (j != 0)
            {
                digit1 <<= 2;
                digit2 <<= 2;
                digit3 <<= 2;
                digit4 <<= 2;
            }
        }

        tmpval = table[digit1];

        if (tmpval != null && (tmpval.create == false || create == true))
        {
            //Console.WriteLine("horz category: {0}", tmpval.category);
            ArrayList moves = new ArrayList();
            foreach(int field in tmpval.fields)
            {
                moves.Add(new Coordinate(node.X + tmpval.offset + field, node.Y));
            }
            Threat t = new Threat(node, tmpval.category, moves, tmpval.create);
            res.Add(t);
        }
        tmpval = table[digit2];
        if (tmpval != null && (tmpval.create == false || create == true))
        {
            //Console.WriteLine("vert category: {0}", tmpval.category);
            //Console.WriteLine("Node: {0}", node);
            //Console.WriteLine("offset: {0}", tmpval.offset);
            ArrayList moves = new ArrayList();
            foreach(int field in tmpval.fields)
            {
                //Coordinate tmp = new Coordinate(node.X , node.Y + tmpval.offset + field);
                //Console.WriteLine("adding move: {0}", tmp);
                moves.Add(new Coordinate(node.X, node.Y + tmpval.offset + field));
            }
            Threat t = new Threat(node, tmpval.category, moves, tmpval.create);
            res.Add(t);
        }
        tmpval = table[digit3];
        if (tmpval != null && (tmpval.create == false || create == true))
        {
            //Console.WriteLine("diag 1 category: {0}", tmpval.category);
            ArrayList moves = new ArrayList();
            foreach(int field in tmpval.fields)
            {
                moves.Add(new Coordinate(node.X + tmpval.offset + field, node.Y + tmpval.offset + field));
            }
            Threat t = new Threat(node, tmpval.category, moves, tmpval.create);
            res.Add(t);
        }
        tmpval = table[digit4];
        if (tmpval != null && (tmpval.create == false || create == true))
        {
            //Console.WriteLine("diag 2 category: {0}", tmpval.category);
            //Console.WriteLine("Node: {0}", node);
            //Console.WriteLine("offset: {0}", tmpval.offset);
            ArrayList moves = new ArrayList();
            foreach(int field in tmpval.fields)
            {
                //Coordinate tmp = new Coordinate(node.X -(tmpval.offset + field), node.Y + tmpval.offset + field);
                //Console.WriteLine("adding move: {0}", tmp);
                moves.Add(new Coordinate(node.X -(tmpval.offset + field), node.Y + tmpval.offset + field));
            }
            Threat t = new Threat(node, tmpval.category, moves, tmpval.create);
            res.Add(t);
        }

        return res;
    }
Exemple #7
0
    public void UpdateThreatLists(int[,] board, Coordinate move, int attacker)
    {
        // 0. Initialized the removelists
        ownremovedthreatlist = new ThreatList();
        oppremovedthreatlist = new ThreatList();
        ownaddedthreatlist   = new ThreatList();
        oppaddedthreatlist   = new ThreatList();

        // 1. Remove blocked threats from both lists.
        ThreatList lookup = new ThreatList();

        foreach (Threat t in oppthreatlist)
        {
            foreach (Coordinate c in t.fields)
            {
                if (c.X == move.X && c.Y == move.Y)
                {
                    oppremovedthreatlist.Add(t);
                    lookup.Add(t.cause);
                }
            }
        }
        foreach (Threat t in oppremovedthreatlist)
        {
            oppthreatlist.Remove(t);
        }
        foreach (Threat t in ownthreatlist)
        {
            foreach (Coordinate c in t.fields)
            {
                if (c.X == move.X && c.Y == move.Y)
                {
                    ownremovedthreatlist.Add(t);
                    lookup.Add(t.cause);
                }
            }
        }
        foreach (Threat t in ownremovedthreatlist)
        {
            ownthreatlist.Remove(t);
        }

        //Lookup if "causes" still cause threats
        foreach (Coordinate c in lookup)
        {
            if (board[c.X, c.Y] == 1)
            {
                ThreatList curthreats = searcher.investigate(board, c, 1);

                ownaddedthreatlist = Merge(ownthreatlist, curthreats);
            }
            if (board[c.X, c.Y] == -1)
            {
                ThreatList curthreats = searcher.investigate(board, c, -1);

                oppaddedthreatlist = Merge(oppthreatlist, curthreats);
            }
        }

        if (attacker == 1)
        {
            // We are attacking
            // 2. Add our threats we build to our list

            ThreatList curthreats = searcher.investigate(board, move, attacker, true);

            Merge(ownaddedthreatlist, Merge(ownthreatlist, curthreats));
        }
        else
        {
            // The opponent is attacking
            // 2. Add his threats to his list
            ThreatList curthreats = searcher.investigate(board, move, attacker, true);

            Merge(oppaddedthreatlist, Merge(oppthreatlist, curthreats));
        }
    }
    private ThreatList SearchBoards(Coordinate node)
    {
        ThreatList res  = new ThreatList();
        int        turn = attacker;

        Item tmpval;
        int  digit1 = 0;
        int  digit2 = 0;
        int  digit3 = 0;
        int  digit4 = 0;

        for (int j = 8; j >= 0; --j)
        {
            if (j + node.X - 4 >= 0 && j + node.X - 4 < board.GetLength(0))
            {
                digit1 += board[node.X + j - 4, node.Y] * turn + 1;
                //			Console.WriteLine("Grabbing {0}/{1}", node.move.x + j - 4, node.move.y);
            }
            else
            {
                digit1 += 3;
            }
            if (j + node.Y - 4 >= 0 && j + node.Y - 4 < board.GetLength(1))
            {
                digit2 += board[node.X, node.Y + j - 4] * turn + 1;
                //			Console.WriteLine("Grabbing {0}/{1}", node.move.x , node.move.y + j -4);
            }
            else
            {
                digit2 += 3;
            }
            if (node.X + j - 4 >= 0 && node.X + j - 4 < board.GetLength(0) &&
                node.Y + j - 4 >= 0 && node.Y + j - 4 < board.GetLength(1))
            {
                digit3 += board[node.X + j - 4, node.Y + j - 4] * turn + 1;
                //			Console.WriteLine("Grabbing {0}/{1}", node.move.x + j - 4, node.move.y + j - 4);
            }
            else
            {
                digit3 += 3;
            }
            if (node.X - j + 4 >= 0 && node.X - j + 4 < board.GetLength(0) &&
                node.Y + j - 4 >= 0 && node.Y + j - 4 < board.GetLength(1))
            {
                digit4 += board[node.X - j + 4, node.Y + j - 4] * turn + 1;
                //			Console.WriteLine("Grabbing {0}/{1}", node.move.x - j + 4, node.move.y + j - 4);
            }
            else
            {
                digit4 += 3;
            }
            if (j != 0)
            {
                digit1 <<= 2;
                digit2 <<= 2;
                digit3 <<= 2;
                digit4 <<= 2;
            }
        }

        tmpval = table[digit1];

        if (tmpval != null && (tmpval.create == false || create == true))
        {
            //Console.WriteLine("horz category: {0}", tmpval.category);
            ArrayList moves = new ArrayList();
            foreach (int field in tmpval.fields)
            {
                moves.Add(new Coordinate(node.X + tmpval.offset + field, node.Y));
            }
            Threat t = new Threat(node, tmpval.category, moves, tmpval.create);
            res.Add(t);
        }
        tmpval = table[digit2];
        if (tmpval != null && (tmpval.create == false || create == true))
        {
            //Console.WriteLine("vert category: {0}", tmpval.category);
            //Console.WriteLine("Node: {0}", node);
            //Console.WriteLine("offset: {0}", tmpval.offset);
            ArrayList moves = new ArrayList();
            foreach (int field in tmpval.fields)
            {
                //Coordinate tmp = new Coordinate(node.X , node.Y + tmpval.offset + field);
                //Console.WriteLine("adding move: {0}", tmp);
                moves.Add(new Coordinate(node.X, node.Y + tmpval.offset + field));
            }
            Threat t = new Threat(node, tmpval.category, moves, tmpval.create);
            res.Add(t);
        }
        tmpval = table[digit3];
        if (tmpval != null && (tmpval.create == false || create == true))
        {
            //Console.WriteLine("diag 1 category: {0}", tmpval.category);
            ArrayList moves = new ArrayList();
            foreach (int field in tmpval.fields)
            {
                moves.Add(new Coordinate(node.X + tmpval.offset + field, node.Y + tmpval.offset + field));
            }
            Threat t = new Threat(node, tmpval.category, moves, tmpval.create);
            res.Add(t);
        }
        tmpval = table[digit4];
        if (tmpval != null && (tmpval.create == false || create == true))
        {
            //Console.WriteLine("diag 2 category: {0}", tmpval.category);
            //Console.WriteLine("Node: {0}", node);
            //Console.WriteLine("offset: {0}", tmpval.offset);
            ArrayList moves = new ArrayList();
            foreach (int field in tmpval.fields)
            {
                //Coordinate tmp = new Coordinate(node.X -(tmpval.offset + field), node.Y + tmpval.offset + field);
                //Console.WriteLine("adding move: {0}", tmp);
                moves.Add(new Coordinate(node.X - (tmpval.offset + field), node.Y + tmpval.offset + field));
            }
            Threat t = new Threat(node, tmpval.category, moves, tmpval.create);
            res.Add(t);
        }

        return(res);
    }