Playing field algorithm

I get a dictionary from a bunch of game items with three properties:

Address = Location in the game section
Offset = Offset to the left of where the game is connected.
Width = width of the game fragment (one, two or three wide)

The game plan itself has 6 wide and 5 high (a total of 30 possible positions)

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

Each number is a corresponding address on the board.

I would like to be able to add all these parts to the collection, for example ObservableCollection<GameRow> GameRows , where

 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); } } } 

and

 public class GamePiece { public string Width { get; set; } public string Color { get; set; } public int Address {get; set} } 

I also need to create a game element for any unused area on the board, which is one width and color gray (other parts of the game should be yellow).

Here's what the game pieces look like:

One wide:
X (offset = 0)

Two Wide:
0X (offset = 1)

Three wide 0X0 (offset = 1)

X = What is called the address for this part
0 = empty space that it occupies.

I am trying to come up with a way to parse this dictionary of the parts of the game that I get in GameRows.

Example:

Let's say that I get three parts. {Address = 2, Width = 1, Offset = 0}, {Address = 12, Width = 2, Offset = 1}, {Address = 23, Width = 3, Offset = 1}

I know that address 2 will be in the first row and will take position 02, and the rest of the game for this board should be empty gameplay with a width of 1. The piece that is address 12 will be in the second row and occupy positions 11 and 12. Piece, which is address 23, will be in the 4th row and will occupy positions 22, 23, 24. In the ObservableCollection GameRows there will be five GameRow objects in it. The GameRow object for line 4 will contain 4 objects in the Row collection. The first 3 being empty will be {Width = 1, Color = Gray}, and the last part will be {Width = 3, Color = Yellow}

+4
source share
2 answers

In principle, create a board with empty or gray parts. Process the dictionary by removing the gray pieces that are no longer needed and add the yellow pieces. Then process the board line by line to return an array of lines. Unit tests are included for your test cases. Change ROWS and COLS to accommodate any size board.

  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); } } 
+2
source

I include Offset in the GamePiece class for short.

 public class GamePiece { public string Width { get; set; } public string Color { get; set; } public int Address {get; set} public int Offset {get; set } } 

I also leave the details of the CreateOccupiedPiece function listed below:

 var d = new Dictionary<int, string>(); int rowWidth; int curCol; int address; string value; GamePiece curPiece; for (int rowNum=1; rowNum<=6; rowNum++) { rowWidth = 0; curCol = 1; while (rowWidth < 6) && (curCol <=6) { address = 6*(RowNum-1) + (curCol) if (d.TryGetValue(address, out value)) { // CreateOccupiedPiece returns New Populated GamePiece curPiece = CreateOccupiedPiece(value); // Offset value will be tell us how many previous GamePieces to delete // as well as how much to adjust our rowWidth if (curPiece.Offset > 0) { list.RemoveRange((curCol - curPiece.Offset - 1), curPiece.Offset); rowWidth = rowWidth - curPiece.Offset; } } else { // Just add an empty space (gray, 1-width piece, at location) curPiece = new GamePiece("Gray", "1", address); } Row.Add(curPiece); curCol++; rowWidth = rowWidth + Convert.ToInt32(curPiece.Width); } } 
0
source

All Articles