Хороший способ сделать это в современном подобном языку C OO?

исходный репозиторий был недавно обновлен, чтобы использовать новый код:

from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials

...

5
задан John Munsch 26 May 2009 в 14:09
поделиться

7 ответов

Вперед, битовые маски гаджетов. Используйте 4-битную маску для каждой плитки, указав, на какой стороне находится стена.

A B C D

Бит A указывает стену наверху, B - справа, C - внизу, D - слева. Определите константы, которые помогут вам логически пересечься с маской, т.е.

if (tile.Walls & (WALL_LEFT | WALL_RIGHT))
  // Do stuff

Для поиска изображения эта 4-битная маска дает 16 возможностей. Используйте его как индекс в «массиве» изображений, чтобы вы могли напрямую найти правильное изображение без каких-либо усилий.

15
ответ дан 18 December 2019 в 07:31
поделиться

I suggest creating a bit flag enum like the following.

[Flags]
public enum WallLocations
{
    None = 0,
    Left = 1,
    Right = 2,
    Top = 4,
    Bottom = 8
}

Then you can use a dictionary to map from the wall locations to the images.

Dictionary<WallLocations, Image> map = new Dictionary<WallLocations, Image>();

map.Add(WallLocations.None, image0000);
map.Add(WallLocations.Left, image1000);
map.Add(WallLocations.Right, image0100);
map.Add(WallLocations.Top, image0010);
map.Add(WallLocations.Bottom, image0001);
map.Add(WallLocations.Left | WallLocations.Right, image1100);
// ....

At least in C# you could also extend the enum definition with all 16 cases.

[Flags]
public enum WallLocations
{
    None = 0,

    Left = 1,
    Right = 2,
    Top = 4,
    Bottom = 8,

    LeftAndRight = Left | Right,
    LeftAndTop = Left | Top,
    LeftAndBottom = Left | Bottom,
    RightAndTop = Right | Top,
    RightAndBottom = Left | Bottom,
    TopAndBottom = Top | Bottom,

    AllExceptLeft = Right | Top | Bottom,
    AllExceptRight = Left | Top | Bottom,
    AllExceptTop = Left | Right | Bottom,
    AllExceptBottom = Left | Right | Top,

    All = Left | Right | Top | Bottom
}
2
ответ дан 18 December 2019 в 07:31
поделиться

Do the tile objects have any other properties? If not (or if you can factor out those), you could make the tile objects themselves into an enumeration of 16 constants with a Tile.getImage() method that returns a fixed image passed to the constructor. This is known as the Flyweight pattern:

class Tile {
    public final boolean isWallAtTop;
    public final boolean isWallAtRight;
    public final boolean isWallAtLeft;
    public final boolean isWallAtBottom;
    public final Image image;

    private Tile(boolean top, boolean right, boolean left, 
                 boolean bottom, Image image)
    {
        this.isWallAtTop = top;
        this.isWallAtRight = right;
        this.isWallAtLeft = left;
        this.isWallAtBottom = bottom;
        this.image = image;
    }

    public static final Tile WALLS_ALL_AROUND = 
        new Tile(true, true, true, true, new Image("allWalls.png"))
    // more constants here, plus other methods that work with
    // only the wall data
}

In Java, you could even implement this as a "real" enum.

For a map that consists of tiles, you could either have a simple 2-dimensional array of Tile references, or if you need other data for individual tiles, have another SpecificTile class that contains the "other data" and a reference to one of the Tile objects above.

4
ответ дан 18 December 2019 в 07:31
поделиться

Can't you do something with flag enums ?

0
ответ дан 18 December 2019 в 07:31
поделиться

You should put the wall configuration into a single variable and build a mapping of this variable to the tile images.

0
ответ дан 18 December 2019 в 07:31
поделиться

Вместо того, чтобы делать WALLS_ALL_AROUND изображением, сделайте его объектом, который включает в себя изображение, а также любую другую логику, необходимую для обработки разрешенных движений и прочего. В объекте Местоположение вы можете просто установить свойство стены на это WALLS_ALL_AROUND , которое вы можете запросить для изображения и иметь дело с другими вашими стенами. логика.

Так как вам нужно только шестнадцать экземпляров этих (неизменяемых) объектов для каждого набора изображений (каждый из них, вероятно, является одноэлементным подклассом Tile , если у вас есть только один набор изображений), вы также сохранить память.

0
ответ дан 18 December 2019 в 07:31
поделиться

Приличное решение OO, вероятно, будет включать шаблон стратегии . Одна из возможных реализаций - иметь класс WallConfiguration и WallFactory для их создания. Тогда Tile будет содержать WallConfiguration. Реализация (стиль C ++) будет выглядеть примерно так:

0
ответ дан 18 December 2019 в 07:31
поделиться
Другие вопросы по тегам:

Похожие вопросы: