Beispiel #1
0
partial         void InitGlobals(GameState state)
        {
            RegisterMethod(
                state,
                "Abs",
                "AddToSet",
                "ChoosePiece",
                "Contains",
                "Count",
                "DebugBreak",
                "DebugPrint",
                "GetMatching",
                "ForEach",
                "IsEmpty",
                "Lose",
                "Match",
                "Max",
                "Min",
                "NextPlayer",
                "PieceAt",
                "Place",
                "RemovePiece",
                "Win"
                );
        }
Beispiel #2
0
 public object Call(GameState state, Context ctx, params object[] p)
 {
     var l = new List<object>();
     l.Add(ctx);
     l.AddRange(p);
     return method.Invoke(null, l.ToArray());
 }
Beispiel #3
0
 /// <summary>
 /// Clones an existing context, except for the associated game state
 /// </summary>
 /// <param name="oldContext"></param>
 /// <param name="newState"></param>
 protected Context(Context oldContext, GameState newState)
 {
     this.parent = oldContext.parent; // FIXME: only works if we don't save any contexts into state
     this.Game = oldContext.Game;
     this.GameState = newState;
     this.vars = new Dictionary<string, object>(oldContext.vars); // TODO: make sure there are only value types
 }
Beispiel #4
0
 public static GameState PerformMove(Game game, GameState state, string fromString, string toString)
 {
     GameState newState;
     if (fromString[0] == '#' && toString[0] == '#')
     {
         Coords from = ParseCoords(fromString.Substring(1));
         Coords to = ParseCoords(toString.Substring(1));
         if ((newState = game.TryMakeMove(state, from, to)) == null)
         {
             Console.WriteLine("Invalid move!");
             return state;
         }
         return newState;
     }
     else if (toString[0] == '#')
     {
         Coords to = ParseCoords(toString.Substring(1));
         Piece p = FindOffboardPiece(state, fromString);
         if (p == null)
         {
             Console.WriteLine("No such piece offboard!");
             return state;
         }
         else if ((newState = game.TryMakeMoveFromOffboard(state, p, to)) == null) {
             Console.WriteLine("Invalid move!");
             return state;
         }
         return newState;
     }
     else {
         Console.WriteLine("Invalid or unsupported move!");
         return state;
     }
 }
Beispiel #5
0
 public object GetVariable(GameState state, string name)
 {
     if (name == "x" || name == "y" || name == "z")
     {
         Coords c = GetPosition(state, state.CurrentPlayer);
         if (c == null) return 0;
         if (name == "x") return c[0];
         else if (name == "y") return c[1];
         else if (name == "z") return c[2];
         else return -1; // WTF?
     }
     else if (name == "Position")
     {
         return GetPosition(state, state.CurrentPlayer);
     }
     else if (name == "Owner")
     {
         return Owner;
     }
     else if (name == "Type")
     {
         return Type;
     }
     else
     {
         return null;
     }
 }
Beispiel #6
0
 internal void RunAction(GameState state, Context ctx)
 {
     if (action != null)
     {
         action.Run(state, ctx);
     }
 }
Beispiel #7
0
 private void RegisterMethod(GameState state, params string[] names)
 {
     foreach (var name in names)
     {
         state.GlobalContext.SetVariable(name, new PredefinedFunction(typeof(M).GetMethod(name)));
     }
 }
Beispiel #8
0
 public static Piece FindOffboardPiece(GameState state, string type)
 {
     foreach (Piece p in state.CurrentPlayer.GetOffboard(state))
     {
         if (p.Type == type) return p;
     }
     return null;
 }
Beispiel #9
0
 public virtual object GetVariable(GameState state, string name)
 {
     object ret;
     if (!vars.TryGetValue(name, out ret))
     {
         if (parent != null) return parent.GetVariable(state, name);
         else return null;
     }
     return ret;
 }
Beispiel #10
0
 internal Coords GetPosition(GameState state, Player asker)
 {
     foreach (var kvp in Game.GetPieces(state, asker))
     {
         if (this == kvp.Value)
         {
             return kvp.Key;
         }
     }
     return null; // Piece is not on the board
 }
Beispiel #11
0
 object ICallable.Call(GameState state, Context ctx, params object[] args)
 {
     if (argList.Length != args.Length) throw new InvalidGameException("Invalid parameter count!");
     Context local = new Context(ctx);
     for (int i = 0; i < argList.Length; i++)
     {
         local.SetVariable(argList[i], args[i]);
     }
     body.Run(state, local);
     return local.GetVariable(state, "_Return");
 }
Beispiel #12
0
 public MultiParentContext(GameState g, params IReadContext[] parents)
     : base(g)
 {
     this.parents = new IWriteContext[parents.Length];
     for (int i = 0; i < parents.Length; i++)
     {
         var p = parents[i];
         if (p is IWriteContext) this.parents[i] = (IWriteContext)p;
         else { this.parents[i] = new ReadonlyContextWrapper(p); }
     }
 }
Beispiel #13
0
 public override object GetVariable(GameState state, string name)
 {
     object ret = base.GetVariable(state, name);
     if (ret != null) return ret;
     foreach (var c in parents)
     {
         ret = c.GetVariable(state, name);
         if (ret != null) return ret;
     }
     return null;
 }
Beispiel #14
0
 private GameState(GameState gs)
 {
     this.game = gs.game;
     this.CurrentPlayerID = gs.CurrentPlayerID;
     this.Board = new Dictionary<Coords, Piece>(gs.Board);
     this.OverrideNextPlayer = gs.OverrideNextPlayer;
     this.GlobalContext = (Game.GlobalContext)gs.GlobalContext.Clone(this);
     foreach (var player in game.EnumeratePlayers())
     {
         offboard.Add(player, new HashSet<Piece>(gs.GetOffboard(player)));
     }
 }
Beispiel #15
0
 public static void PrintBoard(Game game, GameState state)
 {
     Console.WriteLine("Board size: {0}", game.Size);
     Console.WriteLine("Player Count: {0}", game.PlayerCount);
     Console.WriteLine("Current player: {0}", state.CurrentPlayer);
     Console.WriteLine();
     Console.WriteLine("Onboard pieces:");
     foreach (KeyValuePair<Coords, Piece> kvp in game.GetPieces(state))
     {
         Console.WriteLine("  {0} - {1}", kvp.Key, kvp.Value);
     }
     Console.WriteLine("Offboard pieces for current player:");
     Console.WriteLine("  " + string.Join(", ", state.CurrentPlayer.GetOffboard(state)));
 }
Beispiel #16
0
 public object GetVariable(GameState state, string name)
 {
     switch (name)
     {
         case "label":
             return Label;
         case "from":
             return Game.Transform(From, state.CurrentPlayer);
         case "to":
             return Game.Transform(To, state.CurrentPlayer);
         default:
             return null;
     }
 }
Beispiel #17
0
 public bool Equals(GameState other)
 {
     // States are considered equal, if the same pieces are at the same position
     if (Board.Count != other.Board.Count) return false;
     foreach (var kvp in Board)
     {
         Piece value;
         if (!other.Board.TryGetValue(kvp.Key, out value)) return false;
         if (!value.Equals(kvp.Value)) return false;
     }
     // TODO: compare offboard pieces
     return true;
 }
Beispiel #18
0
 object IReadContext.GetVariable(GameState state, string name)
 {
     switch (name)
     {
         case "Self":
             return this;
         default:
             return null;
     }
 }
 public object GetVariable(GameState state, string name)
 {
     return readContext.GetVariable(state, name);
 }
Beispiel #20
0
 public Coords GetPosition(GameState state)
 {
     return GetPosition(state, null);
 }
Beispiel #21
0
        public bool IsValidPlace(GameState state, Coords c)
        {
            if (c.IsPlaceHolder) throw new ArgumentOutOfRangeException("Non-placeholder coords needed.");

            for (int i = 0; i < c.Dimension; i++)
            {
                if (c[i] < 1 || c[i] > Size[i]) return false;
            }
            return IsValidByRules(state, c);
        }
Beispiel #22
0
 private bool TenPredicate(GameState state, Coords coords)
 {
     return coords[1] == 10;
 }
Beispiel #23
0
        private Piece SelectPiece(GameState state, IEnumerable<Piece> pieces)
        {
            selectState = SelectState.Special;
            this.mustSelectFrom = pieces;

            offBoard.ClearHighlight();
            gamePanel.ClearHighlight();
            foreach (var p in mustSelectFrom)
            {
                offBoard.AddHighlight(p);
                gamePanel.AddHighlight(p);
            }
            offBoard.InvalidateVisual();
            gamePanel.InvalidateVisual();

            selectionFrame = new DispatcherFrame();
            Dispatcher.PushFrame(selectionFrame);
            selectionFrame = null;

            selectState = SelectState.From;
            this.mustSelectFrom = null;

            return selectedPiece;
        }
Beispiel #24
0
        private void MakeMove()
        {
            if (fromOffboard != null && toCoord != null)
            {
                // From offboard to board
                var newGameState = game.TryMakeMoveFromOffboard(gameState, FromOffboard, toCoord);
                if (newGameState != null) gameState = newGameState;

                offBoard.Refresh();
                gamePanel.InvalidateVisual();
            }
            else if (fromCoord != null && toCoord != null)
            {
                // From coords to coords
                var newGameState = game.TryMakeMove(gameState, FromCoord, toCoord);
                if (newGameState != null) gameState = newGameState;

                offBoard.Refresh();
                gamePanel.InvalidateVisual();
            }

            if (game.GameOver)
            {
                if (game.PlayerCount == 1)
                {
                    if (game.Winners.Count() == 1)
                    {
                        System.Windows.MessageBox.Show("You won!");
                    }
                    else
                    {
                        System.Windows.MessageBox.Show("You lost!");
                    }
                }
                else
                {
                    if (game.Winners.Count() == 0)
                    {
                        System.Windows.MessageBox.Show("Game over!\nThe game ended in a tie!");
                    }
                    else if (game.Winners.Count() == 1)
                    {
                        System.Windows.MessageBox.Show("Game over!\nThe winner is: " + game.Winners.First().ToString());
                    }
                    else
                    {
                        System.Windows.MessageBox.Show("Game over!\nThe winners are: " + string.Join(", ", game.Winners));
                    }
                }
                NewGame();
            }
            selectState = SelectState.From;
            FromCoord = null;
            FromOffboard = null;
            toCoord = null;
            currentPlayerLabel.Content = gameState.CurrentPlayer.ToString();
        }
Beispiel #25
0
        private void NewGame()
        {
            if (rulebook == null) return;
            GameState state;
            this.game = new Game(rulebook, out state);
            gameState = state;
            game.SetSelectPieceFunction(SelectPiece);

            var cache = new ImageCache(game, gamename);
            SetCoordTransformation(game);

            gamePanel.Game = game;
            gamePanel.GetGameState = () => gameState;
            gamePanel.ImageCache = cache;

            offBoard.Game = game;
            offBoard.GetGameState = () => gameState;
            offBoard.ImageCache = cache;

            gamePanel.InvalidateVisual();

            offBoard.Refresh();

            currentPlayerLabel.Content = gameState.CurrentPlayer.ToString();
        }
Beispiel #26
0
 public IEnumerable<Coords> EnumerateCoords(GameState state, Player asker)
 {
     var coords = new List<Coords>();
     PossibleCoordsToArray(state, new int[0], 0, asker, coords);
     return coords;
 }
Beispiel #27
0
 private bool TargetPredicate(GameState state, Coords coords)
 {
     return Coords.Match(coords, new Coords(5, 7));
 }
Beispiel #28
0
 public IEnumerable<KeyValuePair<Coords, Piece>> GetPieces(GameState state, Player asker)
 {
     var newDict = state.Board.Select(kvp => new KeyValuePair<Coords, Piece>(Transformation(asker, kvp.Key), kvp.Value));
     return newDict;
 }
Beispiel #29
0
 private bool TruePredicate(GameState state, Coords coords)
 {
     return true;
 }
Beispiel #30
0
 public IEnumerable<Piece> GetPiecesWithoutCoords(GameState state)
 {
     return state.Board.Values.AsEnumerable();
 }