private int checkGapSize(Cars thisCar) { var placeOfCarColumn = Grid.GetColumn(thisCar.body); var placeOfCarRow = Grid.GetRow(thisCar.body); #region LEFTtoRIGHT if (placeOfCarRow == LeftRightRow) { for (var searchPointColumn = placeOfCarColumn + 1; searchPointColumn < gr_mainGrid.ColumnDefinitions.Count; searchPointColumn++) { for (int j = 0; j < gr_mainGrid.Children.Count; j++) { UIElement uiE = gr_mainGrid.Children[j]; if (Grid.GetColumn(uiE) == searchPointColumn && Grid.GetRow(uiE) == placeOfCarRow) { var gapSize = Grid.GetColumn(uiE) - placeOfCarColumn - 8; return(Math.Abs(gapSize)); } } } return(8); } #endregion LEFTtoRIGHT #region RIGHTtoLEFT if (placeOfCarRow == RightLeftRow) { for (var searchPointColumn = placeOfCarColumn - 1; searchPointColumn >= 0; searchPointColumn--) { for (int j = 0; j < gr_mainGrid.Children.Count; j++) { UIElement uiE = gr_mainGrid.Children[j]; if (Grid.GetColumn(uiE) == searchPointColumn && Grid.GetRow(uiE) == placeOfCarRow) { var gapSize = placeOfCarColumn - Grid.GetColumn(uiE) - 8; return(gapSize); } } } return(8); } #endregion RIGHTtoLEFT #region TOPtoBOTTOM //second lane if (placeOfCarColumn == TopBottomColumn) { for (var searchPointRow = placeOfCarRow + 1; searchPointRow < gr_mainGrid.RowDefinitions.Count; searchPointRow++) { for (int j = 0; j < gr_mainGrid.Children.Count; j++) { UIElement uiE = gr_mainGrid.Children[j]; if (Grid.GetRow(uiE) == searchPointRow && Grid.GetColumn(uiE) == placeOfCarColumn) { var gapSize = Grid.GetRow(uiE) - placeOfCarRow - 8; return(gapSize); } } } return(8); } //first lane if (placeOfCarColumn == TopBottomColumn2) { for (var searchPointRow = placeOfCarRow + 1; searchPointRow < gr_mainGrid.RowDefinitions.Count; searchPointRow++) { for (int j = 0; j < gr_mainGrid.Children.Count; j++) { UIElement uiE = gr_mainGrid.Children[j]; if (Grid.GetRow(uiE) == searchPointRow && Grid.GetColumn(uiE) == placeOfCarColumn) { var gapSize = Grid.GetRow(uiE) - placeOfCarRow - 8; return(gapSize); } } } return(8); } //third lane if (placeOfCarColumn == TopBottomColumn3) { for (var searchPointRow = placeOfCarRow + 1; searchPointRow < gr_mainGrid.RowDefinitions.Count; searchPointRow++) { for (int j = 0; j < gr_mainGrid.Children.Count; j++) { UIElement uiE = gr_mainGrid.Children[j]; if (Grid.GetRow(uiE) == searchPointRow && Grid.GetColumn(uiE) == placeOfCarColumn) { var gapSize = Grid.GetRow(uiE) - placeOfCarRow - 8; return(gapSize); } } } return(8); } #endregion TOPtoBOTTOM #region BOTTOMtoTOP //lane 2 if (placeOfCarColumn == BottomTopColumn) { for (var searchPointRow = placeOfCarRow - 1; searchPointRow >= 0; searchPointRow--) { for (int j = 0; j < gr_mainGrid.Children.Count; j++) { UIElement uiE = gr_mainGrid.Children[j]; if (Grid.GetRow(uiE) == searchPointRow && Grid.GetColumn(uiE) == placeOfCarColumn) { var gapSize = placeOfCarRow - Grid.GetRow(uiE) - 8; return(gapSize); } } } return(8); } //lane 1 if (placeOfCarColumn == BottomTopColumn2) { for (var searchPointRow = placeOfCarRow - 1; searchPointRow >= 0; searchPointRow--) { for (int j = 0; j < gr_mainGrid.Children.Count; j++) { UIElement uiE = gr_mainGrid.Children[j]; if (Grid.GetRow(uiE) == searchPointRow && Grid.GetColumn(uiE) == placeOfCarColumn) { var gapSize = placeOfCarRow - Grid.GetRow(uiE) - 8; return(gapSize); } } } return(8); } //lane 3 if (placeOfCarColumn == BottomTopColumn3) { for (var searchPointRow = placeOfCarRow - 1; searchPointRow >= 0; searchPointRow--) { for (int j = 0; j < gr_mainGrid.Children.Count; j++) { UIElement uiE = gr_mainGrid.Children[j]; if (Grid.GetRow(uiE) == searchPointRow && Grid.GetColumn(uiE) == placeOfCarColumn) { var gapSize = placeOfCarRow - Grid.GetRow(uiE) - 8; return(gapSize); } } } return(8); } #endregion BOTTOMtoTOP return(8); }
public void spawnCars(int carsToSpawn) { if (checkSpawn() == true) { //cars left to right if (rnd.Next(0, 7) == 0) { Cars car = new Cars(LeftRightColumn, LeftRightRow); gr_mainGrid.Children.Add(car.body); carListLeftToRight.Add(car); } //cars right to left else if (rnd.Next(0, 7) == 1) { Cars car = new Cars(RightLeftColumn, RightLeftRow); gr_mainGrid.Children.Add(car.body); carListRightToLeft.Add(car); } //cars top to bottom 1 else if (rnd.Next(0, 7) == 2) { Cars car = new Cars(TopBottomColumn, TopBottomRow); gr_mainGrid.Children.Add(car.body); carListTopToBottom.Add(car); } //cars bottom to top 1 else if (rnd.Next(0, 7) == 3) { Cars car = new Cars(BottomTopColumn, BottomTopRow); gr_mainGrid.Children.Add(car.body); carListBottomToTop.Add(car); } //cars bottom to top 2 else if (rnd.Next(0, 7) == 4) { Cars car = new Cars(BottomTopColumn2, BottomTopRow); gr_mainGrid.Children.Add(car.body); carListBottomToTop.Add(car); } //cars top to bottom 2 else if (rnd.Next(0, 7) == 5) { Cars car = new Cars(TopBottomColumn2, TopBottomRow); gr_mainGrid.Children.Add(car.body); carListTopToBottom.Add(car); } //cars bottom to top 3 else if (rnd.Next(0, 7) == 6) { Cars car = new Cars(BottomTopColumn3, BottomTopRow); gr_mainGrid.Children.Add(car.body); carListBottomToTop.Add(car); } //cars top to bottom 3 else { Cars car = new Cars(TopBottomColumn3, TopBottomRow); gr_mainGrid.Children.Add(car.body); carListTopToBottom.Add(car); } } }