예제 #1
0
        public static GameDto ToDto(this Game game)
        {
            var   cells      = new List <CellDto>();
            var   cellId     = 0;
            ulong score      = 0;
            var   isFinished = false;

            for (var x = 0; x < game.Board.GetLength(0); x++)
            {
                for (var y = 0; y < game.Board.GetLength(1); y++)
                {
                    var value       = game.Board[x, y];
                    var stringValue = value == 0 ? "" : value.ToString();
                    var colorType   = "tile-" + stringValue;
                    cells.Add(new CellDto(cellId.ToString(), new Vec(x, y), colorType, stringValue, 0));
                    cellId += 1;
                    score  += value;

                    if (value == 2048)
                    {
                        isFinished = true;
                    }
                }
            }

            var gameDto = new GameDto(cells.ToArray(),
                                      true, false,
                                      4, 4,
                                      Guid.Empty,
                                      isFinished, (int)score);

            return(gameDto);
        }
예제 #2
0
        public static bool IsFinished(this GameDto game)
        {
            if (game == null)
            {
                throw new ArgumentNullException("Wrong game parameter.");
            }

            return(CellsWithType(game, CellType.Target).All(cell => cell.BlockType == BlockType.Box));
        }
예제 #3
0
        public GameDto ToDto(Guid id)
        {
            var cells = new List <CellDto>();

            for (var i = 0; i < Width; i++)
            {
                for (var j = 0; j < Height; j++)
                {
                    if (Field[i, j] != null)
                    {
                        cells.Add(new CellDto(Field[i, j].Id, new VectorDto(i, j), Field[i, j].Value.ToString(), 0));
                    }
                }
            }

            var gameDto = new GameDto(cells.ToArray(), true, true, Width, Height, id, IsFinished, 0);

            return(gameDto);
        }
예제 #4
0
        public static void MovePlayer(this GameDto game, Vec movement)
        {
            CellDto fromCell = game.GetPlayer();

            if (fromCell != null)
            {
                Vec     newPosition = fromCell.Pos + movement;
                CellDto toCell      = game.GetCellByPosition(newPosition);
                if (toCell != null)
                {
                    switch (toCell.BlockType)
                    {
                    case BlockType.Empty:
                        toCell.UpdateBlockType(BlockType.Player);
                        fromCell.UpdateBlockType(BlockType.Empty);
                        toCell.Id   = fromCell.Id;
                        fromCell.Id = (next++).ToString();
                        break;

                    case BlockType.Box:
                        Vec     nextPosition = newPosition + movement;
                        CellDto nextCell     = game.GetCellByPosition(nextPosition);
                        if (nextCell != null && nextCell.BlockType == BlockType.Empty)
                        {
                            nextCell.UpdateBlockType(BlockType.Box);
                            toCell.UpdateBlockType(BlockType.Player);
                            fromCell.UpdateBlockType(BlockType.Empty);
                            nextCell.Id = toCell.Id;
                            toCell.Id   = fromCell.Id;
                            fromCell.Id = (next++).ToString();
                        }
                        break;
                    }
                }
            }
        }
예제 #5
0
 public static IEnumerable <CellDto> CellsWithType(this GameDto game, CellType cellType)
 {
     return(game.Cells.Where(cell => cell.CellType == cellType));
 }
예제 #6
0
 public static IEnumerable <CellDto> CellsWithBlock(this GameDto game, BlockType blockType)
 {
     return(game.Cells.Where(cell => cell.BlockType == blockType));
 }
예제 #7
0
 public static IEnumerable <CellDto> EmptyCells(this GameDto game)
 {
     return(CellsWithBlock(game, BlockType.Empty));
 }
예제 #8
0
 public static CellDto GetCellByPosition(this GameDto game, Vec position)
 {
     return(game.Cells.FirstOrDefault((CellDto cell) => cell.Pos == position));
 }
예제 #9
0
 public static CellDto GetPlayer(this GameDto game)
 {
     return(game.Cells.FirstOrDefault(cell => cell.BlockType == BlockType.Player));
 }