示例#1
0
        protected override ValoreMossa GetValoreMossa(IGioco gioco, int depth)
        {
            ValoreMossa resultMove = null;

            if (depth <= _max)
            {
                ITavola tavola   = gioco.Tavola;
                Lato    latoStep = gioco.ProssimoTurno;
                int?    beta     = null;
                for (int i = tavola.Buche.Count - 2; i >= 0; --i)
                {
                    IBuca p = tavola.Buche[i];
                    if (p.Lato == latoStep && i != 6 && i != 13 && p.Semi > 0 && !gioco.Finito)
                    {
                        IGioco giocoClone = gioco.Clone();
                        giocoClone.Muovi(i);
                        int valoreAttuale = ValoreAttuale(giocoClone);

                        if (depth == _max - 1 && beta.HasValue && latoStep == LatoPlayer && valoreAttuale < beta.Value)
                        {
                            continue;
                        }
                        else
                        {
                            bool        cambioTurno = gioco.ProssimoTurno != giocoClone.ProssimoTurno;
                            ValoreMossa valoreProssimaMossa;
                            if (!giocoClone.Finito)
                            {
                                valoreProssimaMossa = ValoreProssimaMossa(depth, giocoClone, cambioTurno);
                            }
                            else
                            {
                                valoreProssimaMossa = null;
                            }
                            ValoreMossa valore = new ValoreMossa(valoreProssimaMossa, i, valoreAttuale);
                            if (null == resultMove) //serve per il taglio
                            {
                                resultMove = valore;
                            }
                            else
                            {
                                resultMove = AggiornoValore(resultMove, valore, latoStep);
                                if (depth == _max - 1 && latoStep == LatoPlayer)
                                {
                                    beta = resultMove.Valore;
                                }
                            }
                        }
                    }
                }
            }
            return(resultMove);
        }
示例#2
0
        protected override ValoreMossa MaxValue(IGioco gioco, int depth, int alfa, int beta)
        {
            ValoreMossa mossa = PatternMatching(gioco);

            if (mossa != null)
            {
                return(mossa);
            }
            else
            {
                return(base.MaxValue(gioco, depth, alfa, beta));
            }
        }
示例#3
0
        protected override ValoreMossa GetValoreMossa(IGioco gioco, int depth)
        {
            ValoreMossa mossa = PatternMatching(gioco);

            if (mossa != null)
            {
                return(mossa);
            }
            else
            {
                return(base.GetValoreMossa(gioco, depth));
            }
        }
示例#4
0
        protected override ValoreMossa GetValoreMossa(IGioco gioco, int depth)
        {
            ValoreMossa resultMove = null;
            if (depth <= _max)
            {
                ITavola tavola = gioco.Tavola;
                Lato latoStep = gioco.ProssimoTurno;
                int? beta = null;
                for (int i = tavola.Buche.Count - 2; i >= 0; --i)
                {
                    IBuca p = tavola.Buche[i];
                    if (p.Lato == latoStep && i != 6 && i != 13 && p.Semi > 0 && !gioco.Finito)
                    {
                        IGioco giocoClone = gioco.Clone();
                        giocoClone.Muovi(i);
                        int valoreAttuale = ValoreAttuale(giocoClone);

                        if (depth == _max - 1 && beta.HasValue && latoStep == LatoPlayer && valoreAttuale < beta.Value)
                        {
                            continue;
                        }
                        else
                        {
                            bool cambioTurno = gioco.ProssimoTurno != giocoClone.ProssimoTurno;
                            ValoreMossa valoreProssimaMossa;
                            if (!giocoClone.Finito)
                            {
                                valoreProssimaMossa = ValoreProssimaMossa(depth, giocoClone, cambioTurno);
                            }
                            else
                            {
                                valoreProssimaMossa = null;
                            }
                            ValoreMossa valore = new ValoreMossa(valoreProssimaMossa, i, valoreAttuale);
                            if (null == resultMove) //serve per il taglio
                            {
                                resultMove = valore;
                            }
                            else
                            {
                                resultMove = AggiornoValore(resultMove, valore, latoStep);
                                if (depth == _max - 1 && latoStep == LatoPlayer)
                                    beta = resultMove.Valore;
                            }
                        }
                    }
                }
            }
            return resultMove;
        }
示例#5
0
        private ValoreMossa MinValue(IGioco gioco, int depth, int alfa, int beta)
        {
            ValoreMossa resultMove = null;

            if (depth <= _max && !gioco.Finito)
            {
                ITavola tavola   = gioco.Tavola;
                Lato    latoStep = gioco.ProssimoTurno;

                int[] heuristc = _heuristicFunction.HeuristicFunction(gioco.Clone());
                for (int j = 0; j < heuristc.Length; ++j)
                {
                    int i = heuristc[j];
                    OperationCount++;
                    IGioco giocoClone = gioco.Clone();
                    giocoClone.Muovi(i);
                    bool        cambioTurno         = gioco.ProssimoTurno != giocoClone.ProssimoTurno;
                    ValoreMossa valoreProssimaMossa = null;
                    if (!giocoClone.Finito)
                    {
                        if (!cambioTurno)
                        {
                            valoreProssimaMossa = MinValue(giocoClone, depth, alfa, beta);
                        }
                        else
                        {
                            valoreProssimaMossa = MaxValue(giocoClone, depth + 1, alfa, beta);
                        }
                    }

                    ValoreMossa valore = new ValoreMossa(valoreProssimaMossa, i, ValoreAttuale(giocoClone));
                    if (null == resultMove)//inizializzazione
                    {
                        resultMove = valore;
                    }
                    else
                    {
                        resultMove = AggiornoValore(resultMove, valore, latoStep);
                    }
                    if (resultMove.Valore < alfa)
                    {
                        return(resultMove);
                    }
                    beta = resultMove.Valore < beta ? resultMove.Valore : beta;
                }
            }
            return(resultMove);
        }
示例#6
0
        private ValoreMossa MinValue(IGioco gioco, int depth)
        {
            ValoreMossa resultMove = null;

            if (depth <= _max && !gioco.Finito)
            {
                ITavola tavola   = gioco.Tavola;
                Lato    latoStep = gioco.ProssimoTurno;


                for (int i = tavola.Buche.Count - 2; i >= 0; --i)
                {
                    IBuca p = tavola.Buche[i];
                    if (p.Lato == latoStep && i != 6 && i != 13 && p.Semi > 0 && !gioco.Finito)
                    {
                        OperationCount++;

                        IGioco giocoClone = gioco.Clone();
                        giocoClone.Muovi(i);
                        bool        cambioTurno         = gioco.ProssimoTurno != giocoClone.ProssimoTurno;
                        ValoreMossa valoreProssimaMossa = null;
                        if (!giocoClone.Finito)
                        {
                            if (!cambioTurno)
                            {
                                valoreProssimaMossa = MinValue(giocoClone.Clone(), depth);
                            }
                            else
                            {
                                valoreProssimaMossa = MaxValue(giocoClone.Clone(), depth + 1);
                            }
                        }

                        ValoreMossa valore = new ValoreMossa(valoreProssimaMossa, i, ValoreAttuale(giocoClone));
                        if (null == resultMove)//inizializzazione
                        {
                            resultMove = valore;
                        }
                        else
                        {
                            resultMove = AggiornoValore(resultMove, valore, latoStep);
                        }
                    }
                }
            }
            return(resultMove);
        }
示例#7
0
文件: AbsAIPlayer.cs 项目: nickxbs/IA
 protected ValoreMossa AggiornoValore(ValoreMossa resultMove, ValoreMossa m, Lato latoStep)
 {
     if (latoStep == LatoPlayer)
     {
         if (m.Valore >= resultMove.Valore) //MAX: tengo sempre la migliore che poi ritorno
         {
             resultMove = m;
         }
     }
     else
     {
         if (m.Valore <= resultMove.Valore) //MIN: tengo sempre la migliore che poi ritorno
         {
             resultMove = m;
         }
     }
     return(resultMove);
 }
示例#8
0
文件: AbsAIPlayer.cs 项目: nickxbs/IA
 protected ValoreMossa AggiornoValore(ValoreMossa resultMove, ValoreMossa m, Lato latoStep)
 {
     if (latoStep == LatoPlayer)
     {
         if (m.Valore >= resultMove.Valore) //MAX: tengo sempre la migliore che poi ritorno
         {
             resultMove = m;
         }
     }
     else
     {
         if (m.Valore <= resultMove.Valore) //MIN: tengo sempre la migliore che poi ritorno
         {
             resultMove = m;
         }
     }
     return resultMove;
 }
示例#9
0
        protected override ValoreMossa GetValoreMossa(IGioco gioco, int depth)
        {
            ValoreMossa resultMove = null;

            if (depth <= _max)
            {
                ITavola tavola   = gioco.Tavola;
                Lato    latoStep = gioco.ProssimoTurno;

                for (int i = tavola.Buche.Count - 2; i >= 0; --i)
                {
                    IBuca p = tavola.Buche[i];
                    if (p.Lato == latoStep && i != 6 && i != 13 && p.Semi > 0 && !gioco.Finito)
                    {
                        OperationCount++;
                        IGioco giocoClone = gioco.Clone();
                        giocoClone.Muovi(i);
                        int         valoreAttuale = ValoreAttuale(giocoClone);
                        ValoreMossa valoreProssimaMossa;
                        if (!giocoClone.Finito)
                        {
                            bool cambioTurno = gioco.ProssimoTurno != giocoClone.ProssimoTurno;
                            valoreProssimaMossa = ValoreProssimaMossa(depth, giocoClone.Clone(), cambioTurno);
                        }
                        else
                        {
                            valoreProssimaMossa = null;
                        }
                        ValoreMossa m = new ValoreMossa(valoreProssimaMossa, i, valoreAttuale);

                        if (null == resultMove)//inizializzazione
                        {
                            resultMove = m;
                        }
                        else
                        {
                            resultMove = AggiornoValore(resultMove, m, latoStep);
                        }
                    }
                }
            }
            return(resultMove);
        }
示例#10
0
 private ValoreMossa PatternMatching(IGioco gioco)
 {
     IList<IBuca> buche = gioco.Tavola.Buche;
     if (buche[0].Semi == 3 && buche[1].Semi == 3 && buche[2].Semi == 3 && buche[3].Semi == 3 && buche[4].Semi == 3 && buche[5].Semi == 3)
     {
         //ValoreMossa vm2 = new ValoreMossa(null, 5, 2);
         //return new ValoreMossa(vm2, 3, 2);
         return new ValoreMossa(null, 5, 1);
     }
     if (buche[0].Semi == 4 && buche[1].Semi == 4 && buche[2].Semi == 3 && buche[3].Semi == 3 && buche[4].Semi == 3 && buche[5].Semi == 0 && buche[6].Semi == 1)
     {
         ValoreMossa vm3 = new ValoreMossa(null, 1, 2);
         ValoreMossa vm2 = new ValoreMossa(vm3, 5, 2);
         return new ValoreMossa(vm2, 3, 2);
     }
     if (buche[0].Semi == 0 && buche[1].Semi == 0 && buche[2].Semi == 4 && buche[3].Semi == 1 && buche[4].Semi == 5 && buche[5].Semi == 0 && buche[6].Semi == 8 && buche[13].Semi == 8)
     {
         return new ValoreMossa(null, 4, 2);
     }
     return null;
 }
示例#11
0
        private ValoreMossa PatternMatching(IGioco gioco)
        {
            IList <IBuca> buche = gioco.Tavola.Buche;

            if (buche[0].Semi == 3 && buche[1].Semi == 3 && buche[2].Semi == 3 && buche[3].Semi == 3 && buche[4].Semi == 3 && buche[5].Semi == 3)
            {
                //ValoreMossa vm2 = new ValoreMossa(null, 5, 2);
                //return new ValoreMossa(vm2, 3, 2);
                return(new ValoreMossa(null, 5, 1));
            }
            if (buche[0].Semi == 4 && buche[1].Semi == 4 && buche[2].Semi == 3 && buche[3].Semi == 3 && buche[4].Semi == 3 && buche[5].Semi == 0 && buche[6].Semi == 1)
            {
                ValoreMossa vm3 = new ValoreMossa(null, 1, 2);
                ValoreMossa vm2 = new ValoreMossa(vm3, 5, 2);
                return(new ValoreMossa(vm2, 3, 2));
            }
            if (buche[0].Semi == 0 && buche[1].Semi == 0 && buche[2].Semi == 4 && buche[3].Semi == 1 && buche[4].Semi == 5 && buche[5].Semi == 0 && buche[6].Semi == 8 && buche[13].Semi == 8)
            {
                return(new ValoreMossa(null, 4, 2));
            }
            return(null);
        }
示例#12
0
        public override int Elaborazione(IGioco gioco)
        {
            if (mossaMultipla == null || mossaMultipla.Count == 0)
            {
                ValoreMossa val = GetValoreMossa(gioco.Clone(), 0);
                mossaMultipla = new List <int>();
                ValoreMossa valTmp = val.ProssimaMossa;
                while (valTmp != null && valTmp.Lato == val.Lato)
                {
                    mossaMultipla.Add(valTmp.Mossa);
                    valTmp = valTmp.ProssimaMossa;
                }

                _valoreMassimo = val.Valore;
                return(val.Mossa);
            }
            else
            {
                int mossa = mossaMultipla[0];
                mossaMultipla.RemoveAt(0);
                return(mossa);
            }
        }
示例#13
0
文件: MinMax.cs 项目: nickxbs/IA
 protected ValoreMossa MaxValue(IGioco gioco, int depth)
 {
     ValoreMossa resultMove = null;
     if (depth <= _max && !gioco.Finito)
     {
         ITavola tavola = gioco.Tavola;
         Lato latoStep = gioco.ProssimoTurno;
         int[] heuristc = _heuristicFunction.HeuristicFunction(gioco.Clone());
         for (int j = 0; j < heuristc.Length; ++j)
         {
             int i = heuristc[j];
             OperationCount++;
             IGioco giocoClone = gioco.Clone();
             giocoClone.Muovi(i);
             bool cambioTurno = gioco.ProssimoTurno != giocoClone.ProssimoTurno;
             ValoreMossa valoreProssimaMossa = null;
             if (!giocoClone.Finito)
             {
                 if (!cambioTurno)
                     valoreProssimaMossa = MaxValue(giocoClone.Clone(), depth);
                 else
                     valoreProssimaMossa = MinValue(giocoClone.Clone(), depth + 1);
             }
             ValoreMossa valore = new ValoreMossa(valoreProssimaMossa, i, ValoreAttuale(giocoClone));
             if (null == resultMove)//inizializzazione
             {
                 resultMove = valore;
             }
             else
             {
                 resultMove = AggiornoValore(resultMove, valore, latoStep);
             }
         }
     }
     return resultMove;
 }
示例#14
0
文件: AbsAIPlayer.cs 项目: nickxbs/IA
 public ValoreMossa(ValoreMossa nextMove, int i, int score)
 {
     _nextMove = nextMove;
     _i        = i;
     _score    = score;
 }
示例#15
0
文件: AbsAIPlayer.cs 项目: nickxbs/IA
 public ValoreMossa(ValoreMossa nextMove, int i, int score)
 {
     _nextMove = nextMove;
     _i = i;
     _score = score;
 }
示例#16
0
文件: MinMax.cs 项目: nickxbs/IA
        private ValoreMossa MinValue(IGioco gioco, int depth)
        {
            ValoreMossa resultMove = null;
            if (depth <= _max && !gioco.Finito)
            {

                ITavola tavola = gioco.Tavola;
                Lato latoStep = gioco.ProssimoTurno;

                for (int i = tavola.Buche.Count - 2; i >= 0; --i)
                {
                    IBuca p = tavola.Buche[i];
                    if (p.Lato == latoStep && i != 6 && i != 13 && p.Semi > 0 && !gioco.Finito)
                    {
                        OperationCount++;

                        IGioco giocoClone = gioco.Clone();
                        giocoClone.Muovi(i);
                        bool cambioTurno = gioco.ProssimoTurno != giocoClone.ProssimoTurno;
                        ValoreMossa valoreProssimaMossa = null;
                        if (!giocoClone.Finito)
                        {
                            if (!cambioTurno)
                                valoreProssimaMossa = MinValue(giocoClone.Clone(), depth);
                            else
                                valoreProssimaMossa = MaxValue(giocoClone.Clone(), depth + 1);
                        }

                        ValoreMossa valore = new ValoreMossa(valoreProssimaMossa, i, ValoreAttuale(giocoClone));
                        if (null == resultMove)//inizializzazione
                        {
                            resultMove = valore;
                        }
                        else
                        {
                            resultMove = AggiornoValore(resultMove, valore, latoStep);
                        }

                    }
                }
            }
            return resultMove;
        }