示例#1
0
    //Fonction récursive qui calcul la listes des plateaux possibles

    /*  private void getAllBoardPossible(int index, int end, Board board, Dictionary<int, Board> result)
     * {
     *    index = index % 30;
     *    if (index != end || result.Count == 0)
     *    {
     *        Vector2Int? v = board.findPieceByID(index);
     *        if (v.HasValue == false)
     *        {
     *            getAllBoardPossible(index + 1, end, board, result);
     *        }
     *        else
     *        {
     *            Piece piece = board.grille[v.Value.x, v.Value.y];
     *            List<Action> la;
     *            if (piece is Pion)
     *            {
     *                la = getAvalaibleMove((Pion)piece, board);
     *            }
     *            else
     *            {
     *                la = getAvalaibleMove((Tower)piece, board);
     *            }
     *
     *            foreach (Action ac in la)
     *            {
     *                Board tmp = ac.toBoard(board);
     *                int hash = tmp.GetHashCode();
     *                if (!result.ContainsKey(hash))
     *                {
     *                    result.Add(hash, tmp);
     *                    getAllBoardPossible(index + 1, end, tmp, result);
     *                }
     *            }
     *        }
     *    }
     * }*/
    private Dictionary <string, Prediction> getAllBoardPossible(
        int index, int end, Board board, bool start)
    {
        //Debug.Log(index + ":" + end);
        index = index % 30;
        // Cas d'arret
        if (index == end && start == false)
        {
            Dictionary <string, Prediction> dic = new Dictionary <string, Prediction>();

            Vector2Int vectmp = board.findPieceByID(index).Value;
            Prediction tmp    = new Prediction(board, board.grille[vectmp.x, vectmp.y].color);
            return(dic);
        }
        else // Recurtion
        {
            Vector2Int?v = board.findPieceByID(index);
            if (v == null || v.HasValue == false)
            {
                return(getAllBoardPossible(1 + index, end, board, false));
            }
            else
            {
                Piece         piece = board.grille[v.Value.x, v.Value.y];
                List <Action> la;
                if (piece is Pion)
                {
                    la = getAvalaibleMove((Pion)piece, board);
                }
                else
                {
                    la = getAvalaibleMove((Tower)piece, board);
                }
                //Debug.Log("nombre d'action pour " + piece.id + " : " + la.Count);

                // On parcour les actions possibles et on les réalisent
                Dictionary <string, Prediction> dic = new Dictionary <string, Prediction>();
                foreach (Action ac in la)
                {
                    Prediction tmp  = new Prediction(board, board.grille[ac.oldPosition.x, ac.oldPosition.y].color);
                    string     hash = tmp.GetHashString();

                    if (!dic.ContainsKey(hash))
                    {
                        dic.Add(hash, tmp);
                        Dictionary <string, Prediction> tmpdic = getAllBoardPossible(1 + index, end, tmp.board, false);
                        foreach (KeyValuePair <string, Prediction> entry in tmpdic)
                        {
                            if (!dic.ContainsKey(entry.Key))
                            {
                                dic.Add(entry.Key, entry.Value);
                            }
                        }
                    }
                }
                return(dic);
            }
        }
    }
示例#2
0
    //Fonction récursive qui calcul la listes des plateaux possibles pour le tour du joueur
    private Dictionary <string, Prediction> getAllBoardPossibleMove(
        int ID, Board initBoard, Color color)
    {
        // Le int le tour actuel de la piece donc la profondeur
        Dictionary <int, Dictionary <string, Prediction> > metaDic = new Dictionary <int, Dictionary <string, Prediction> >();

        // On innitialise le premier plateau
        Dictionary <string, Prediction> init = new Dictionary <string, Prediction>();

        init.Add(
            initBoard.GetHashString(),
            new Prediction(
                initBoard, color
                )
            );

        metaDic.Add(0, init);
        List <Piece> pieces    = initBoard.getOrdonedListTeam(ID);
        int          end       = pieces.Count;
        int          iteration = 0;

        do
        {
            Dictionary <string, Prediction> currentDic;
            if (metaDic.ContainsKey(iteration))
            {
                currentDic = metaDic[iteration];
            }
            else // On descend dans les tours
            {
                iteration++;
                continue;
            }
            List <string> tmpListKey = new List <string>(currentDic.Keys);
            foreach (string Key in tmpListKey) // On test les possibilité possible au tour suivant du board
            {
                if (!currentDic.ContainsKey(Key))
                {
                    continue;
                }
                Prediction current = currentDic[Key];
                Board      board   = current.board;
                Vector2Int?v       = board.findPieceByID(pieces[iteration].id);
                if (v != null && v.HasValue != false)
                {
                    Piece         piece = board.grille[v.Value.x, v.Value.y];
                    List <Action> la;
                    if (piece is Pion)
                    {
                        la = getAvalaibleMove((Pion)piece, board);
                    }
                    else
                    {
                        la = getAvalaibleMove((Tower)piece, board);
                    }
                    // On filtrer pour diminuer le nombre de mouve possible
                    piece.refreshGoal(board);
                    List <Action> tmpLA = piece.goal.filter(board, la);

                    la = new List <Action>();
                    var rand = new Random();
                    for (int i = 0; i < Math.Min(2, tmpLA.Count); ++i)
                    {
                        la.Add(tmpLA[rand.Next(0, tmpLA.Count)]); // On prend que deux moves
                    }
                    //Debug.Log("nombre d'action pour " + piece.id + " : " + la.Count);

                    //On charge le dictionnaire de l'index suivant
                    int next = (iteration + 1) % pieces.Count;
                    Dictionary <string, Prediction> nextBoards;
                    if (metaDic.ContainsKey(next))
                    {
                        nextBoards = metaDic[next];
                    }
                    else
                    {
                        nextBoards = new Dictionary <string, Prediction>();
                    }
                    // On parcour les actions possibles et on les réalisent
                    foreach (Action ac in la)
                    {
                        //Prediction tmp = new Prediction(board, ac);
                        Prediction tmp  = current.extendPrediction(ac);
                        string     hash = tmp.GetHashString();
                        if (!nextBoards.ContainsKey(hash) &&
                            tmp.board.findPieceByID(ID) != null &&
                            tmp.board.findPieceByID(ID).HasValue)
                        {
                            nextBoards.Add(hash, tmp);
                        }
                    }
                    metaDic[next] = nextBoards;
                }
            }
            iteration += 1;
        } while (iteration < end);

        if (end < pieces.Count && metaDic.ContainsKey(end))
        {
            return(metaDic[end]);
        }
        else
        {
            return(metaDic[0]);
        }
    }
示例#3
0
    //private Dictionary<string, Prediction> getAllBoardPossibleIte(
    //int start, int end, Board initBoard)
    //{
    //    int index = 0 + start;
    //    // Le int le tour actuel de la piece donc la profondeur
    //    Dictionary<int, Dictionary<string, Prediction>> metaDic = new Dictionary<int, Dictionary<string, Prediction>>();

    //    // On innitialise le premier plateau
    //    Dictionary<string, Prediction> init = new Dictionary<string, Prediction>();

    //    init.Add(
    //        initBoard.GetHashString(),
    //        new Prediction(
    //            initBoard,
    //            new Action(initBoard.findPieceByID(start).Value)
    //        )
    //    );

    //    metaDic.Add(index, init);

    //    do
    //    {
    //        Dictionary<string, Prediction> currentDic;
    //        if (metaDic.ContainsKey(index))
    //        {
    //            currentDic = metaDic[index];
    //        }
    //        else // On descend dans les tours
    //        {
    //            index = (index + 1) % 30;
    //            continue;
    //        }

    //        foreach (KeyValuePair<string, Prediction> current in currentDic) // On test les possibilité possible au tour suivant du board
    //        {
    //            Board board = current.Value.board;
    //            Vector2Int? v = board.findPieceByID(index);
    //            if (v != null && v.HasValue != false)
    //            {
    //                Piece piece = board.grille[v.Value.x, v.Value.y];
    //                List<Action> la;
    //                if (piece is Pion)
    //                {
    //                    la = getAvalaibleMove((Pion)piece, board);
    //                }
    //                else
    //                {
    //                    la = getAvalaibleMove((Tower)piece, board);
    //                }
    //                piece.refreshGoal(board);
    //                la = piece.goal.filter(board, la);
    //                //Debug.Log("nombre d'action pour " + piece.id + " : " + la.Count);

    //                //On charge le dictionnaire de l'index suivant
    //                int next = (index + 1) % 30;
    //                Dictionary<string, Prediction> nextBoards;
    //                if (metaDic.ContainsKey(next))
    //                {
    //                    nextBoards = metaDic[next];
    //                }
    //                else
    //                {
    //                    nextBoards = new Dictionary<string, Prediction>();
    //                }
    //                // On parcour les actions possibles et on les réalisent
    //                foreach (Action ac in la)
    //                {
    //                    Prediction tmp = new Prediction(board, ac);
    //                    string hash = tmp.GetHashString();
    //                    string symetric = tmp.GetHashStringSymetrie();
    //                    if (!nextBoards.ContainsKey(hash)
    //                        && !nextBoards.ContainsKey(symetric)
    //                        && tmp.board.findPieceByID(start) != null
    //                        && tmp.board.findPieceByID(start).HasValue)
    //                    {
    //                        nextBoards.Add(hash, tmp);
    //                    }
    //                }
    //                metaDic[next] = nextBoards;
    //            }
    //        }
    //        index = (index + 1) % 30;
    //    }
    //    while (index != end);
    //    if (metaDic.ContainsKey(end))
    //    {
    //        return metaDic[end];
    //    }
    //    else
    //    {
    //        return metaDic[start];
    //    }
    //}
    private Dictionary <string, Prediction> getAllBoardPossibleIte(
        int ID, int end, Board initBoard)
    {
        // Le int le tour actuel de la piece donc la profondeur
        Dictionary <int, Dictionary <string, Prediction> > metaDic = new Dictionary <int, Dictionary <string, Prediction> >();

        // On innitialise le premier plateau
        Dictionary <string, Prediction> init = new Dictionary <string, Prediction>();
        Vector2Int tmpvec = initBoard.findPieceByID(ID).Value;

        init.Add(
            initBoard.GetHashString(),
            new Prediction(
                initBoard, initBoard.grille[tmpvec.x, tmpvec.y].color
                )
            );

        metaDic.Add(0, init);
        List <Piece> pieces = initBoard.getOrdonedList(ID);

        end = (end > pieces.Count)? pieces.Count:end;
        int iteration = 0;

        do
        {
            Dictionary <string, Prediction> currentDic;
            if (metaDic.ContainsKey(iteration))
            {
                currentDic = metaDic[iteration];
            }
            else // On descend dans les tours
            {
                iteration++;
                continue;
            }

            foreach (KeyValuePair <string, Prediction> current in currentDic) // On test les possibilité possible au tour suivant du board
            {
                Board      board = current.Value.board;
                Vector2Int?v     = board.findPieceByID(pieces[iteration].id);
                if (v != null && v.HasValue != false)
                {
                    Piece         piece = board.grille[v.Value.x, v.Value.y];
                    List <Action> la;
                    if (piece is Pion)
                    {
                        la = getAvalaibleMove((Pion)piece, board);
                    }
                    else
                    {
                        la = getAvalaibleMove((Tower)piece, board);
                    }
                    //piece.refreshGoal(board);
                    //la = piece.goal.filter(board, la);
                    //Debug.Log("nombre d'action pour " + piece.id + " : " + la.Count);

                    //On charge le dictionnaire de l'index suivant
                    int next = (iteration + 1) % pieces.Count;
                    Dictionary <string, Prediction> nextBoards;
                    if (metaDic.ContainsKey(next))
                    {
                        nextBoards = metaDic[next];
                    }
                    else
                    {
                        nextBoards = new Dictionary <string, Prediction>();
                    }
                    // On parcour les actions possibles et on les réalisent
                    foreach (Action ac in la)
                    {
                        //Prediction tmp = new Prediction(board, ac);

                        Prediction tmp  = current.Value.extendPrediction(ac);
                        string     hash = tmp.GetHashString();
                        if (!nextBoards.ContainsKey(hash) &&
                            tmp.board.findPieceByID(ID) != null &&
                            tmp.board.findPieceByID(ID).HasValue)
                        {
                            nextBoards.Add(hash, tmp);
                        }
                    }
                    metaDic[next] = nextBoards;
                }
            }
            iteration += 1;
        } while (iteration < end);

        if (end < pieces.Count && metaDic.ContainsKey(end))
        {
            return(metaDic[end]);
        }
        else
        {
            return(metaDic[0]);
        }
    }