我得到了一系列具有三个属性的游戏片段:
地址=游戏中的位置
偏移=从游戏块连接的左侧偏移
宽度=游戏棋子的宽度(一,二或三宽)
游戏板本身是6宽5高(总共30个可能的位置)
25 26 27 28 29 30
19 20 21 22 23 24
13 14 15 16 17 18
07 08 09 10 11 12
01 02 03 04 05 06
每个号码都是电路板上的相应地址.
我希望能够将所有这些片段添加到像ObservableCollection< GameRow>这样的集合中. GameRows在哪里
public class GameRow
{
public ObservableCollection<GamePiece> Row { get; set; }
public GameRow(ArrayList<GamePiece> gamePieces)
{
Row = new ObservableCollection<GamePiece>();
foreach (GamePiece gamePiece in gamePieces)
{
Row.Add(gamePiece);
}
}
}
和
public class GamePiece
{
public string Width { get; set; }
public string Color { get; set; }
public int Address {get; set}
}
我还需要为棋盘上任何未使用的区域创建一个游戏,它是单一宽度和灰色(其他游戏棋子应该是黄色).
这是游戏的样子:
一个广泛的:
X(偏移= 0)
两宽:
0X(偏移= 1)
三宽
0X0(偏移= 1)
X =报告的内容是该作品的地址
0 =它占据的空白空间.
我正试图想出一种方法来解析我收到GameRows的游戏作品字典.
例:
可以说我得到了三件. {地址= 2,宽度= 1,偏移= 0},{地址= 12,宽度= 2,偏移= 1},{地址= 23,宽度= 3,偏移= 1}
我知道地址2件将位于第一排并占据位置02,该板的其余游戏件应该是宽度为1的空白游戏件.地址12的件将位于第二行并采取位置11和12.位于地址23的部分将位于第4行并占据位置22,23,24.ObservableCollection GameRows将包含五个GameRow对象.第4行的GameRow对象将包含Row集合中的4个对象.前三个是空白部分将是{宽度= 1,颜色=灰色},最后一个将是{宽度= 3,颜色=黄色}
最佳答案 基本上用空的或灰色的碎片创建一块板.处理字典,删除不再需要的灰色片段并添加黄色片段.然后逐行处理板以返回行数组.包含测试用例的单元测试.修改ROWS和COLS以适应任何规模的板.
public class UnitTest1
{
int ROWS = 5;
int COLS = 6;
public class GamePiece
{
public int Width;
public string Color;
public int Address;
public int Offset;
}
public class GameRow
{
public List<GamePiece> Row = new List<GamePiece>();
}
private void initializeBoard(GamePiece[] board) {
for(int i = 1; i < board.Length; i++) board[i] = EmptyPiece(i);
}
private GamePiece EmptyPiece(int address)
{
return new GamePiece
{
Address = address,
Width = 1,
Color = "Gray",
Offset = 0
};
}
private int ComputeRow(int address)
{
return ((address - 1) / COLS);
}
private void processDictionary(GamePiece[] board, Dictionary<int,GamePiece> d)
{
foreach (var piece in d.Values)
{
for (int i = 0; i < piece.Width; i++)
{
board[piece.Address - piece.Offset + i] = null;
}
board[piece.Address] = piece;
}
}
private GameRow[] convertBoardtoRows(GamePiece[] board)
{
var rows = new GameRow[ROWS];
for (int i = 0; i < rows.Length; i++) rows[i] = new GameRow();
for (int i = 1; i < board.Length; i++)
{
if (board[i] != null)
{
rows[ComputeRow(i)].Row.Add(board[i]);
}
}
return rows;
}
public GameRow[] ProcessPieces(Dictionary<int, GamePiece> dictionary)
{
GamePiece[] board = new GamePiece[ROWS*COLS+1];
initializeBoard(board);
processDictionary(board, dictionary);
return convertBoardtoRows(board);
}
[TestMethod]
public void TestMethod1()
{
var dictionary = new Dictionary<int, GamePiece>();
dictionary.Add(1, new GamePiece { Address = 2, Width = 1, Offset = 0, Color = "Yellow" });
dictionary.Add(2, new GamePiece { Address = 12, Width = 2, Offset = 1, Color = "Yellow" });
dictionary.Add(3, new GamePiece { Address = 23, Width = 3, Offset = 1, Color = "Yellow" });
var rows = ProcessPieces(dictionary);
Assert.IsTrue(rows[0].Row.Count == 6);
Assert.IsTrue(rows[0].Row.Where(p => p.Color == "Yellow").Count() == 1);
Assert.IsTrue(rows[0].Row.Where(p => p.Color == "Gray").Count() == 5);
var piece = rows[0].Row.Where(p => p.Color == "Yellow").First();
Assert.IsTrue(piece.Address == 2);
Assert.IsTrue(rows[1].Row.Count == 5);
Assert.IsTrue(rows[1].Row.Where(p => p.Color == "Yellow").Count() == 1);
Assert.IsTrue(rows[1].Row.Where(p => p.Color == "Gray").Count() == 4);
Assert.IsTrue(rows[1].Row.Where(p => p.Address == 11).Count() == 0);
piece = rows[1].Row.Where(p => p.Color == "Yellow").First();
Assert.IsTrue(piece.Address == 12);
Assert.IsTrue(rows[2].Row.Count == 6);
Assert.IsTrue(rows[2].Row.Where(p => p.Color == "Yellow").Count() == 0);
Assert.IsTrue(rows[2].Row.Where(p => p.Color == "Gray").Count() == 6);
Assert.IsTrue(rows[3].Row.Count == 4);
Assert.IsTrue(rows[3].Row.Where(p => p.Color == "Yellow").Count() == 1);
Assert.IsTrue(rows[3].Row.Where(p => p.Color == "Gray").Count() == 3);
Assert.IsTrue(rows[4].Row.Count == 6);
Assert.IsTrue(rows[4].Row.Where(p => p.Color == "Yellow").Count() == 0);
Assert.IsTrue(rows[4].Row.Where(p => p.Color == "Gray").Count() == 6);
}
}