Ejemplo n.º 1
0
        public ImageCache(Game g, string gamename)
            : this(g)
        {
            try
            {
                BitmapImage img = new BitmapImage(new Uri(System.IO.Directory.GetCurrentDirectory() + @"\Images\" + gamename + @"\bg.png"));
                pieces.Add("bg", img);
            }
            catch (System.IO.IOException e)
            {
                // Don't worry about it, we'll draw a checkerboard instead
            }

            for (int i = 0; i < g.PlayerCount; i++)
            {
                int player = i + 1;
                foreach (string type in g.PieceTypes)
                {
                    if (type == "piece") continue;
                    string pieceName = type + player.ToString();
                    string fallbackName = "piece" + player.ToString();
                    try
                    {
                        BitmapImage img = new BitmapImage(new Uri(System.IO.Directory.GetCurrentDirectory() + @"\Images\" + gamename + @"\" + pieceName + ".png"));
                        pieces.Add(pieceName, img);
                    }
                    catch (System.IO.IOException e)
                    {
                        // We'll use a fallback image named "pieceX" where X is the player
                        pieces.Add(pieceName, pieces[fallbackName]);
                    }
                }
            }
        }
Ejemplo n.º 2
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;
     }
 }
Ejemplo n.º 3
0
 internal MoveDefinition(string pieceType, string label, Coords from, Coords to, Game game)
 {
     this.PieceType = pieceType;
     this.Label = label;
     this.From = from;
     this.To = to;
     this.Game = game;
 }
Ejemplo n.º 4
0
 public MoveRule(string piece, CoordExpr from, CoordExpr to, bool targetEmpty, string label, Expression condition, Statement action, Game g)
 {
     PieceType = piece;
     From = from;
     To = to;
     TargetMustBeEmpty = targetEmpty;
     Label = label;
     Condition = condition;
     this.action = action;
 }
Ejemplo n.º 5
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)));
     }
 }
Ejemplo n.º 6
0
 private ImageCache(Game g)
 {
     // Load default images
     pieces.Clear();
     for (int i = 0; i < g.PlayerCount; i++)
     {
         int player = i + 1;
         string pieceName = "piece" + player.ToString();
         BitmapImage img = new BitmapImage(new Uri(System.IO.Directory.GetCurrentDirectory() + @"\Images\" + pieceName + ".png"));
         pieces.Add(pieceName, img);
     }
 }
Ejemplo n.º 7
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)));
 }
Ejemplo n.º 8
0
 private void SetCoordTransformation(Game game)
 {
     if (game.Size.Dimension == 3)
     {
         // Suppose mills
         gamePanel.ICT = new CoordTransformer(
             new Coords[] {
                 new Coords(1,1,1), new Coords(2,1,1), new Coords(3,1,1),
                 new Coords(1,2,1),                    new Coords(3,2,1),
                 new Coords(1,3,1), new Coords(2,3,1), new Coords(3,3,1),
                 new Coords(1,1,2), new Coords(2,1,2), new Coords(3,1,2),
                 new Coords(1,2,2),                    new Coords(3,2,2),
                 new Coords(1,3,2), new Coords(2,3,2), new Coords(3,3,2),
                 new Coords(1,1,3), new Coords(2,1,3), new Coords(3,1,3),
                 new Coords(1,2,3),                    new Coords(3,2,3),
                 new Coords(1,3,3), new Coords(2,3,3), new Coords(3,3,3),
             },
             new Coords[] {
                 new Coords(3,3), new Coords(5,3), new Coords(7,3),
                 new Coords(3,5),                  new Coords(7,5),
                 new Coords(3,7), new Coords(5,7), new Coords(7,7),
                 new Coords(2,2), new Coords(5,2), new Coords(8,2),
                 new Coords(2,5),                  new Coords(8,5),
                 new Coords(2,8), new Coords(5,8), new Coords(8,8),
                 new Coords(1,1), new Coords(5,1), new Coords(9,1),
                 new Coords(1,5),                  new Coords(9,5),
                 new Coords(1,9), new Coords(5,9), new Coords(9,9),
             }
             );
     }
     else
     {
         gamePanel.ICT = new IdentityTransformer();
     }
 }
Ejemplo n.º 9
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();
        }
Ejemplo n.º 10
0
 internal GameState(Game game)
 {
     this.game = game;
     this.Board = new Dictionary<Coords, Piece>();
 }
Ejemplo n.º 11
0
 internal Piece(string type, Player owner, Game game)
 {
     this.Type = type;
     this.Owner = owner;
     this.Game = game;
 }
Ejemplo n.º 12
0
        static void Main(string[] args)
        {
            try
            {
                if (args.Length != 1)
                {
                    PrintUsage();
                    return;
                }

                string fileName = System.IO.Path.Combine("Games", args[0] + ".game");
                if (!System.IO.File.Exists(fileName))
                {
                    Console.WriteLine("{0} does not exists!", fileName);
                    PrintUsage();
                    return;
                }

                GameState state;
                Game game = new Game(System.IO.File.ReadAllText(fileName), out state);

                game.SetSelectPieceFunction(PieceChooser);

                Console.WriteLine("Game loaded successfully");

                PrintHelp();

                while (!game.GameOver)
                {
                    Console.WriteLine();
                    Console.Write("> ");
                    string line = Console.ReadLine().Trim();
                    if (line.Length == 0)
                    {
                        Console.WriteLine("?");
                        continue;
                    }
                    string[] command = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                    switch (command[0])
                    {
                        case "?":
                        case "h":
                            PrintHelp();
                            break;
                        case "b":
                            PrintBoard(game, state);
                            break;
                        case "l":
                            PrintMoves(game, state);
                            break;
                        case "m":
                            if (command.Length != 3) goto default;
                            GameState newState = PerformMove(game, state, command[1], command[2]);
                            if (newState != state)
                            {
                                state = newState;
                                Console.WriteLine("OK! Next player: {0}", state.CurrentPlayer);
                            }
                            break;
                        case "q":
                            Console.WriteLine("Bye!");
                            return;
                        default:
                            Console.WriteLine("Invalid command! Type ? for help.");
                            break;
                    }
                }

                if (game.PlayerCount == 1)
                {
                    if (game.Winners.Count() == 1)
                    {
                        Console.WriteLine("You won!");
                    }
                    else
                    {
                        Console.WriteLine("You lost!");
                    }
                }
                else
                {
                    if (game.Winners.Count() == 0)
                    {
                        Console.WriteLine("Tie!");
                    }
                    else if (game.Winners.Count() == 1)
                    {
                        Console.WriteLine("The winner is {0}", game.Winners.First());
                    }
                    else
                    {
                        Console.WriteLine("The winners are {0}", string.Join(", ", game.Winners));
                    }
                }
                Console.WriteLine("Press return to quit.");
                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine("An error occured! Details follow:");
                Console.WriteLine(e);
            }
        }
Ejemplo n.º 13
0
 static void PrintMoves(Game game, GameState state)
 {
     Console.WriteLine("Possible moves:");
     foreach (var rule in game.EnumeratePossibleMoves(state))
     {
         Console.WriteLine("  " + rule.ToString());
     }
 }