Exemplo n.º 1
0
        private Vector ToTurnInput(GameState gs, DoubleHint hint)
        {
            double ratio  = 0.0;
            Vector vector = null;

            if (gs.CanDouble())
            {
                if (gs.GameType == GameType.Match)
                {
                    ratio = System.Math.Min(
                        (System.Math.Max(gs.Score(0), gs.Score(1)) + gs.Cube.Value) / (double)gs.MatchTo,
                        1.0);
                }

                if (gs.GameType == GameType.Money)
                {
                    ratio = System.Math.Min(gs.Cube.Value * gs.Stake / (double)gs.Limit, 1.0);
                }

                vector = new Vector(System.Math.Abs(System.Math.Min(hint.DoubleTakeEq, hint.DoublePassEq) - hint.NoDoubleEq),
                                    ratio, 100.0);
            }
            else
            {
                vector = new Vector(0.0, ratio, 0.0);
            }

            return(vector);
        }
Exemplo n.º 2
0
        public override int TimeOnTurnChanged(GameState gamestate, DoubleHint doubleHint, ResignHint resignHint)
        {
            if (random.Next(15) == 0)
            {
                return(300 + random.Next(800));
            }

            return(200);
        }
Exemplo n.º 3
0
        public override DoubleHint DoubleHint(GameState gamestate)
        {
            Stopwatch sw = Stopwatch.StartNew();

            DoubleHint hint = null;

            if (gamestate.CanDouble())
            {
                hint = gnubg.DoubleHint(gamestate);
            }

            Console.WriteLine("[Double Hint] " + sw.ElapsedMilliseconds + "ms.");

            return(hint);
        }
Exemplo n.º 4
0
        public NeuralThinker(IEnumerable <GameStateMoveAction> moves,
                             IEnumerable <GameStateDoubleAction> doubles,
                             IEnumerable <GameStateResignAction> resigns,
                             IEnumerable <GameStateTurnAction> turns)
        {
            GnuBgHintModule gnubg = new GnuBgHintModule(@"gnubg/ipoint.exe");

            gnubg.Initialize();

            foreach (GameStateMoveAction gsma in moves)
            {
                List <PlayHint> hints = gnubg.PlayHint(gsma.Original, 500);

                this.moves.Add(new KeyValuePair <GameStateAction, Vector>(gsma, ToMoveInput(gsma, hints)));
            }

            foreach (GameStateDoubleAction gsda in doubles)
            {
                DoubleResponseHint hint = gnubg.DoubleResponseHint(gsda.GameState);

                //Vector vector = new Vector(hint.TakeEq, hint.PassEq, System.Math.Abs(hint.PassEq - hint.TakeEq));
                this.doubles.Add(new KeyValuePair <GameStateAction, Vector>(gsda, ToDoubleInput(gsda.GameState, hint)));
            }

            foreach (GameStateResignAction gsra in resigns)
            {
                ResignResponseHint hint = gnubg.ResignResponseHint(gsra.GameState);

                Vector vector = new Vector();

                this.resigns.Add(new KeyValuePair <GameStateAction, Vector>(gsra, vector));
            }

            foreach (GameStateTurnAction gsta in turns)
            {
                DoubleHint hint = null;
                if (gsta.GameState.CanDouble())
                {
                    hint = gnubg.DoubleHint(gsta.GameState);
                }

                this.turns.Add(new KeyValuePair <GameStateAction, Vector>(gsta, ToTurnInput(gsta.GameState, hint)));
            }

            random.Next();
            random.Next();
        }
Exemplo n.º 5
0
        public override int TimeOnTurnChanged(GameState gamestate, DoubleHint doubleHint, ResignHint resignHint)
        {
            // Player has captured pieces, think less time.
            if (gamestate.Board.CapturedCount(gamestate.PlayerOnRoll) > 0)
            {
                return((int)(coefficient * Gaussian.Next(700, 250, 20000, 500, 2.0)));
            }

            if (!gamestate.CanDouble())
            {
                return((int)(coefficient * Gaussian.Next(400, 500, 20000, 1500, 2.0)));
            }

            //  Parameter   Values
            //  Mean        1000 ms
            //  Minimum     500 ms
            //  Maximum     20000 ms
            //  Deviation   2.0 (95.4% are within 2000 ms from mean)
            return((int)(coefficient * Gaussian.Next(1000, 1000, 20000, 1700, 2.0)));
        }
Exemplo n.º 6
0
        public override int TimeOnTurnChanged(GameState gamestate, DoubleHint doubleHint, ResignHint resignHint)
        {
            Vector v = ToTurnInput(gamestate, doubleHint);

            foreach (KeyValuePair <GameStateAction, Vector> gv in turns)
            {
                gv.Value.Distance = Vector.ComputeDistance(gv.Value, v);
            }

            turns.Sort(Compare);


            foreach (KeyValuePair <GameStateAction, Vector> gv in turns)
            {
                Console.WriteLine(gv.Key.Time + " " + gv.Value.Distance);
            }
            Console.WriteLine(turns.Count);

            return((int)turns[0].Key.Time);
        }
Exemplo n.º 7
0
 public override int TimeOnTurnChanged(GameState gamestate, DoubleHint doubleHint, ResignHint resignHint)
 {
     return(random.Next(500, 1000));
 }
Exemplo n.º 8
0
 public abstract int TimeOnTurnChanged(GameState gamestate, DoubleHint doubleHint, ResignHint resignHint);
Exemplo n.º 9
0
 public override int TimeOnTurnChanged(GameState gamestate, DoubleHint doubleHint, ResignHint resignHint)
 {
     return(0);
 }
Exemplo n.º 10
0
        private void HandleAI()
        {
            if (currentGameState.PlayerOnTurn == 1)
            {
                if (currentGameState.HasOffer)
                {
                }
                else
                {
                    if (currentGameState.DiceRolled)
                    {
                        Thread.Sleep(thinker.TimeOnDiceRolled(currentGameState));

                        List <PlayHint> hints = gnubg.PlayHint(currentGameState, 500);

                        if (hints != null)
                        {
                            TimedPlay play = thinker.TimedPlayOnRoll(currentGameState, hints, GR.Gambling.Backgammon.Venue.VenueUndoMethod.UndoLast);

                            Stack <Move> oppMadeMoves = new Stack <Move>();
                            //currentGameState.Board.MakePlay(1, hints[0].Play);
                            foreach (TimedMove move in play)
                            {
                                Thread.Sleep(move.WaitBefore);

                                if (move.IsUndo)
                                {
                                    Move m = oppMadeMoves.Pop();
                                    currentGameState.Board.UndoMove(1, m);
                                }
                                else
                                {
                                    oppMadeMoves.Push(move);
                                    currentGameState.Board.MakeMove(1, move);
                                }

                                Render();
                                this.Refresh();

                                //this.Invalidate();

                                Thread.Sleep(move.WaitAfter);
                            }
                        }

                        if (hints == null)
                        {
                            Thread.Sleep(1000);
                        }

                        Thread.Sleep(500);

                        currentGameState.ChangeTurn();
                    }
                    else
                    {
                        DoubleHint doubleHint = gnubg.DoubleHint(currentGameState);
                        Thread.Sleep(thinker.TimeOnTurnChanged(currentGameState, doubleHint, null));

                        if (currentGameState.CanDouble())
                        {
                            if (doubleHint.Action == DoubleAction.Double)
                            {
                                watch = Stopwatch.StartNew();

                                DialogResult res = MessageBox.Show("Your opponent doubles..", "Double offer", MessageBoxButtons.YesNo);

                                watch.Stop();

                                GameState gs = currentGameState.Clone();
                                gs.Double();
                                GameStateDoubleAction gsda = new GameStateDoubleAction(gs, watch.ElapsedMilliseconds, (res == DialogResult.Yes) ? DoubleResponse.Take : DoubleResponse.Pass);
                                doubles.Add(gsda);

                                textBoxLog.Text          += "Double response added " + gs.OfferType.ToString() + " " + watch.ElapsedMilliseconds + "ms." + Environment.NewLine;
                                textBoxLog.SelectionStart = textBoxLog.Text.Length;
                                textBoxLog.ScrollToCaret();

                                if (res == DialogResult.Yes)
                                {
                                    currentGameState.SetCube(currentGameState.Cube.Value * 2, 0);

                                    Render();
                                    this.Refresh();
                                }
                                else
                                {
                                    status = GameStatus.GameOver;
                                }

                                UpdateControls();

                                return;
                            }
                        }

                        if (!resignOfferMade)
                        {
                            ResignHint resignHint = gnubg.ResignHint(currentGameState, false);
                            if (resignHint.Value != ResignValue.None)
                            {
                                watch = Stopwatch.StartNew();
                                DialogResult res = MessageBox.Show("Your opponent wants to resign for " + resignHint.Value.ToString(), "Resign offer", MessageBoxButtons.YesNo);
                                watch.Stop();

                                GameState gs = currentGameState.Clone();
                                gs.Resign(resignHint.Value);
                                GameStateResignAction gsra = new GameStateResignAction(gs, watch.ElapsedMilliseconds, (res == DialogResult.Yes) ? ResignResponse.Accept : ResignResponse.Reject);
                                resigns.Add(gsra);

                                textBoxLog.Text          += "Resign response added " + gs.OfferType.ToString() + " " + watch.ElapsedMilliseconds + "ms." + Environment.NewLine;
                                textBoxLog.SelectionStart = textBoxLog.Text.Length;
                                textBoxLog.ScrollToCaret();

                                if (res == DialogResult.Yes)
                                {
                                    status = GameStatus.GameOver;

                                    Render();
                                    this.Refresh();
                                }
                                else
                                {
                                    resignOfferMade = true;
                                }

                                UpdateControls();

                                return;
                            }
                        }

                        // Roll
                        currentGameState.SetDice(random.Next(1, 7), random.Next(1, 7));
                    }
                }

                UpdateControls();
            }
        }