Пример #1
0
        /**
         * Think about the next move and return it.
         *
         * @param prevMoves The moves just made, or nil if none (first move).
         * @return A triple containing: the move, an explanation (or null) and a taunt (or null).
         */
        public Tuple<GdlExpression, string, string> Play(GdlList prevMoves)
        {
            // Construct list of previous moves
            GroundFact[] previousMoves = ParsePreviousMoves(prevMoves);

            if (previousMoves.Length > 0)
                UpdateCurrentState(previousMoves);

            Tuple<Term, string, string> move = MoveThink();

            // Convert the Term to a GdlExpression

            GdlExpression moveGdl;

            if (move == null || move.Item1 == null)
            {
                Logger.Error(GameId + ": move returned by moveThink was null");
                moveGdl = new GdlAtom(SymbolTable, Parser.TokNil);
                move = new Tuple<Term, string, string>(null, "", "");
            }
            else
            {
                // the top-level element returned is list of all elements in the parse;
                // in this case, we have just one, the move.
                moveGdl = Parser.Parse(move.Item1.ToString())[0];
            }

            var m = new Tuple<GdlExpression, string, string>(moveGdl, move.Item2, move.Item3);

            return m;
        }
Пример #2
0
        /// <summary>
        /// Starts a new game. Takes the information from the start message
        /// and gives it to the gamer factory, which creates an appropriate
        /// gamer for the game.
        /// see stanfordlogic.Gamer
        /// </summary>
        /// <param name="gameId">The identifier of the game being started.</param>
        /// <param name="role">The role I am playing in the new game.</param>
        /// <param name="description">The description (rules) of the game.</param>
        /// <param name="startClock">The time given to think about the game.</param>
        /// <param name="playClock">The time given to make a move.</param>
        /// <returns>The Gamer instance created to play the game.</returns>
        public static IGamer NewGame(String gameId, GdlAtom role, GdlList description, int startClock, int playClock)
        {
            lock (typeof(GameManager))
            {
                if (_gamerFactory == null)
                {
                    Logger.Error("No gamer factory set!");
                    return null;
                }

                // Make sure this game isn't already active:
                if (Games.Contains(gameId))
                {
                    Logger.Error("Game already active: " + gameId);
                    return null;
                }

                Logger.Info("");
                Logger.Info("-----------------------------------------------");
                Logger.Info("NEW GAME! " + gameId);
                Logger.Info("");
                Logger.Info("    My role : " + role);
                Logger.Info("Start clock : " + startClock);
                Logger.Info(" Play clock : " + playClock);
                Logger.Info("");

                // put in a temporary game:
                Games[gameId] = null;
            }

            IGamer g = _gamerFactory.MakeGamer(gameId, role, description, startClock, playClock);

            lock (typeof(GameManager))
            {
                Games[gameId] = g;
            }

            return g;
        }
Пример #3
0
        protected override void Execute()
        {
            if (_content.Size != 3)
                throw new Exception("PLAY request should have exactly three arguments, not " + _content.Size);

            GdlExpression prevExp = _content[2];
            GdlList prevMoves;

            var prevMovesStr = new StringBuilder();

            if (prevExp is GdlList)
            {
                prevMoves = (GdlList)_content[2];
                prevMovesStr.Append(" Previous moves: ");

                foreach (GdlExpression exp in prevMoves)
                {
                    prevMovesStr.Append(exp);
                    prevMovesStr.Append("  ");
                }
            }
            else
            {
                // make sure it's an atom containing NIL
                var prevAtom = prevExp as GdlAtom;
                if (prevAtom == null || prevAtom.GetToken() != GameContainer.Parser.TokNil)
                    throw new Exception("PLAY request doesn't have LIST and doesn't have NIL atom as prev-moves!");
                prevMoves = null; // empty prev moves
            }

            IGamer game = GameManager.GetGame(GameId);
            if (game == null)
            {
                Logger.Error("No game found for play request ID: " + GameId);
                Finish();
                return;
            }

            Logger.Info(GameId + ": Beginning move think." + prevMovesStr);

            Tuple<GdlExpression, String, String> next;

            try
            {
                next = game.Play(prevMoves);
            }
            catch (Exception e)
            {
                Logger.Debug(GameId + ": Exception while processing 'game.play':" + e.Message);

                Logger.Debug(GameId + ": " + e.StackTrace);

                var nil = new GdlAtom(GameContainer.SymbolTable, GameContainer.Parser.TokNil);
                next = new Tuple<GdlExpression, string, string>(nil, "exception", "Something bad happened");
            }

            String moveStr = next.Item1.ToString();
            Logger.Info(GameId + ": End of move think. Making move: " + moveStr);

            var answer = new StringBuilder(128);

            answer.Append(moveStr);

            // is there an explanation?
            if (next.Item2 != null)
            {
                answer.Append(" (explanation \"");
                answer.Append(Util.EscapeChars(next.Item2));
                answer.Append("\")");
            }

            SendAnswer(answer.ToString());
            Finish();
        }
Пример #4
0
        private void ExamineTopLevelAtom(GdlAtom atom)
        {
            int token = atom.GetToken();

            // This probably never happens. . . so make a note of it
            Console.WriteLine("WE GOT A TOP LEVEL ATOM!! " + _parser.SymbolTable[token]);

            //TODO: Make sure this symbol isn't a function/object symbol already.
            //if (IsFunctionSymbol(token) || IsObjectSymbol(token))
            //    throw new Exception(string.Format("Symbol '{0}' ({1}) already exists, but not as a relation symbol!", token, _parser.SymbolTable[token]));

            // It's a top level atom, so it has to be a relation symbol
            AddRelationSymbol(token, 0);
        }
Пример #5
0
        private void ExamineAtomTerm(GdlAtom atom)
        {
            // if it's a variable, do nothing with it.
            /*if ( atom instanceof GdlVariable )
                return;*/

            int token = atom.GetToken();

            //TODO: This term must be an object constant. Make sure it is.
            //if (IsFunctionSymbol(token) || IsRelationSymbol(token))
            //    throw new Exception(string.Format("Symbol '{0}' ({1}) already exists, but not as an object symbol!", token, _parser.SymbolTable[token]));

            // Add it to our list of object symbols
            AddObjectSymbol(token);
        }
Пример #6
0
        private Expression ExamineAtomRelation(GdlAtom atom)
        {
            int relName = atom.GetToken();

            //TODO: Make sure this symbol isn't a function/object symbol already.
            //if (IsFunctionSymbol(relName) || IsObjectSymbol(relName))
            //    throw new Exception(string.Format("Symbol '{0}' ({1}) already exists, but not as a relation symbol!", relName, _parser.SymbolTable[relName]));

            // Add to relation name to our list of relation symbols
            AddRelationSymbol(relName, 0);

            return GroundFact.FromExpression(atom);
        }