Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Basically create a board with the empty, or Gray, pieces. Process the dictionary, removing the gray pieces that are no longer needed and adding the yellow pieces. Then process the board row by row to return an array of rows. Unit tests are included for your test cases. Modify ROWS and COLS to accomodate any size board.</p> <pre><code> 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&lt;GamePiece&gt; Row = new List&lt;GamePiece&gt;(); } private void initializeBoard(GamePiece[] board) { for(int i = 1; i &lt; 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&lt;int,GamePiece&gt; d) { foreach (var piece in d.Values) { for (int i = 0; i &lt; 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 &lt; rows.Length; i++) rows[i] = new GameRow(); for (int i = 1; i &lt; board.Length; i++) { if (board[i] != null) { rows[ComputeRow(i)].Row.Add(board[i]); } } return rows; } public GameRow[] ProcessPieces(Dictionary&lt;int, GamePiece&gt; dictionary) { GamePiece[] board = new GamePiece[ROWS*COLS+1]; initializeBoard(board); processDictionary(board, dictionary); return convertBoardtoRows(board); } [TestMethod] public void TestMethod1() { var dictionary = new Dictionary&lt;int, GamePiece&gt;(); 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 =&gt; p.Color == "Yellow").Count() == 1); Assert.IsTrue(rows[0].Row.Where(p =&gt; p.Color == "Gray").Count() == 5); var piece = rows[0].Row.Where(p =&gt; p.Color == "Yellow").First(); Assert.IsTrue(piece.Address == 2); Assert.IsTrue(rows[1].Row.Count == 5); Assert.IsTrue(rows[1].Row.Where(p =&gt; p.Color == "Yellow").Count() == 1); Assert.IsTrue(rows[1].Row.Where(p =&gt; p.Color == "Gray").Count() == 4); Assert.IsTrue(rows[1].Row.Where(p =&gt; p.Address == 11).Count() == 0); piece = rows[1].Row.Where(p =&gt; p.Color == "Yellow").First(); Assert.IsTrue(piece.Address == 12); Assert.IsTrue(rows[2].Row.Count == 6); Assert.IsTrue(rows[2].Row.Where(p =&gt; p.Color == "Yellow").Count() == 0); Assert.IsTrue(rows[2].Row.Where(p =&gt; p.Color == "Gray").Count() == 6); Assert.IsTrue(rows[3].Row.Count == 4); Assert.IsTrue(rows[3].Row.Where(p =&gt; p.Color == "Yellow").Count() == 1); Assert.IsTrue(rows[3].Row.Where(p =&gt; p.Color == "Gray").Count() == 3); Assert.IsTrue(rows[4].Row.Count == 6); Assert.IsTrue(rows[4].Row.Where(p =&gt; p.Color == "Yellow").Count() == 0); Assert.IsTrue(rows[4].Row.Where(p =&gt; p.Color == "Gray").Count() == 6); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload