private void InitTetriminoArray() { if (RotateableArray.GetLength(0) != RotateableArray.GetLength(1)) { throw new System.Exception("Rotateable array should be square"); } var temp = RotateableArray; while (MultiArrayHelper.AllInColumnFalse(temp, 0)) { temp = MultiArrayHelper.TrimColumn(0, temp); DeductedLeftRows++; } while (MultiArrayHelper.AllInRowFalse(temp, 0)) { temp = MultiArrayHelper.TrimRow(0, temp); } while (MultiArrayHelper.AllInColumnFalse(temp, temp.GetLength(1) - 1)) { temp = MultiArrayHelper.TrimColumn(temp.GetLength(1) - 1, temp); } while (MultiArrayHelper.AllInRowFalse(temp, temp.GetLength(0) - 1)) { temp = MultiArrayHelper.TrimRow(temp.GetLength(0) - 1, temp); } TetriminoArray = temp; }
private static Tetrimino DetectNextBlock(Bitmap data, int stepX, int stepY, int xStart, int yStart, Graphics graphicsPanel2, int vakjeNummer) { bool[,] tetriminoArray = new bool[4, 4]; for (int y = 0; y < 2; y++) { for (int x = 0; x < 4; x++) { var pixelX = xStart + stepX * x; var pixelY = yStart + stepY * y; int dingetje = 5; var pixels = new List <Color> { data.GetPixel(pixelX, pixelY), data.GetPixel(pixelX - stepX / dingetje, pixelY - stepY / dingetje), data.GetPixel(pixelX + stepX / dingetje, pixelY - stepY / dingetje), data.GetPixel(pixelX - stepX / dingetje, pixelY + stepY / dingetje), data.GetPixel(pixelX + stepX / dingetje, pixelY + stepY / dingetje) }; var darkest = Color.FromArgb(pixels.Min(t => t.R), pixels.Min(t => t.G), pixels.Min(t => t.B)); var avg = darkest.R + darkest.G + darkest.B / 3; float hue = darkest.GetHue(); float saturation = darkest.GetSaturation(); float lightness = darkest.GetBrightness(); //if (darkest.R < BlackColorTreshhold && darkest.G < BlackColorTreshhold && darkest.B < BlackColorTreshhold) //{ // darkest = Color.Black; //} //else //{ // darkest = Color.Red; //} if (lightness < 0.3f) { darkest = Color.Black; } else { darkest = Color.Red; tetriminoArray[y + 1, x] = true; } var br = new SolidBrush(darkest); graphicsPanel2.FillRectangle(br, x * TetrisConstants.BlockSize, y * TetrisConstants.BlockSize + vakjeNummer * (TetrisConstants.BlockSize * 3), TetrisConstants.BlockSize, TetrisConstants.BlockSize); } } if (tetriminoArray[0, 0] == false && tetriminoArray[0, 1] == false && tetriminoArray[0, 2] == false && tetriminoArray[0, 3] == false && tetriminoArray[0, 3] == false && tetriminoArray[1, 3] == false && tetriminoArray[2, 3] == false && tetriminoArray[3, 3] == false) { tetriminoArray = MultiArrayHelper.TrimArray(0, 3, tetriminoArray); } int rowToCheck = tetriminoArray.GetLength(0) - 1; if (tetriminoArray[0, 0] == false && tetriminoArray[rowToCheck, 1] == false && tetriminoArray[rowToCheck, 2] == false && tetriminoArray[1, 0] == false && tetriminoArray[2, 0] == false) { tetriminoArray = MultiArrayHelper.TrimArray(rowToCheck, 0, tetriminoArray); } // if (tetriminoArray[1, 0] == false && // tetriminoArray[1, 1] == false && // tetriminoArray[1, 2] == false && // tetriminoArray[1, 3] == false) //{ // tetriminoArray = new bool[1, 4] { { tetriminoArray[0, 0], tetriminoArray[0, 1], tetriminoArray[0, 2], tetriminoArray[0, 3] } }; //} //if (tetriminoArray[0, 3] == false && tetriminoArray[1, 3] == false) //{ // tetriminoArray = new bool[2, 3] { { tetriminoArray[0, 0], tetriminoArray[0, 1], tetriminoArray[0, 2] }, { tetriminoArray[1, 0], tetriminoArray[1, 1], tetriminoArray[1, 2] } }; //} //if (tetriminoArray[0, 0] == false && tetriminoArray[1, 0] == false) //{ // tetriminoArray = new bool[2, 2] { { tetriminoArray[0, 1], tetriminoArray[0, 2] }, { tetriminoArray[1, 1], tetriminoArray[1, 2] } }; //} if (MultiArrayHelper.IsValidTetrino(tetriminoArray)) { return(new Tetrimino(tetriminoArray)); } else { return(null); } }
internal static bool IsTwoGraphSameMatrix(Graph graph1, Graph graph2) { if (graph1.graphMatrix.Rank != 2 && graph1.graphMatrix.Rank != graph2.graphMatrix.Rank || graph1.graphMatrix.GetLength(0) != graph1.graphMatrix.GetLength(1) || graph2.graphMatrix.GetLength(0) != graph2.graphMatrix.GetLength(1)) { return(false); } var graph1Size = graph1.graphMatrix.GetLength(0); var graph2Size = graph2.graphMatrix.GetLength(0); var graph1MatrixTemp = graph1.graphMatrix.Clone() as int[, ]; var graph2MatrixTemp = graph2.graphMatrix.Clone() as int[, ]; //Process matrix and put every node index into dictionary list by their connection count. //So, <5, [0,3,4]> means nodes 0,3 and 4 have 5 connections each. //After grouping them by their connection number, we will sort the matrix and permutate them. var graph1IndexListPerCountDic = new SortedDictionary <int, List <int> >(); //<count>, <list of indices that has same number of connections> for (int i = 0; i < graph1Size; i++) { var countOfIndex = 0; for (int j = 0; j < graph1Size; j++) { if (i == j) { continue; } if (graph1MatrixTemp[i, j] > 0) { countOfIndex++; } } if (countOfIndex <= 0) { continue; } if (!graph1IndexListPerCountDic.ContainsKey(countOfIndex)) { graph1IndexListPerCountDic[countOfIndex] = new List <int>(); } graph1IndexListPerCountDic[countOfIndex].Add(i); } if (graph1IndexListPerCountDic.Count == 0) { return(false); } var graph2IndexListPerCountDic = new SortedDictionary <int, List <int> >(); //<count>, <list of indices that has same number of connections> for (int i = 0; i < graph2Size; i++) { var countOfIndex = 0; for (int j = 0; j < graph2Size; j++) { if (i == j) { continue; } if (graph2MatrixTemp[i, j] > 0) { countOfIndex++; } } if (countOfIndex <= 0) { continue; } if (!graph2IndexListPerCountDic.ContainsKey(countOfIndex)) { graph2IndexListPerCountDic[countOfIndex] = new List <int>(); } graph2IndexListPerCountDic[countOfIndex].Add(i); } if (graph2IndexListPerCountDic.Count == 0) { return(false); } //Check if every count has same number of indices in each graph //eg. graph1 has 3 nodes with 2 connection and 2 nodes with 1 connection. graph2 has 2 nodes with 2 connection and 2 nodes with 1 connection. Then they are definitely not equal. foreach (KeyValuePair <int, List <int> > graph1IndexListPerCount in graph1IndexListPerCountDic) { if (!graph2IndexListPerCountDic.ContainsKey(graph1IndexListPerCount.Key) || graph2IndexListPerCountDic[graph1IndexListPerCount.Key].Count != graph1IndexListPerCount.Value.Count) { return(false); } } //Sort matrices so that they have less connections from lower indeces // // <not sorted> // 0 1 2 3 4 5 // 0 0 1 0 1 1 1 // 1 1 0 1 0 1 0 // 2 0 1 0 0 0 0 // 3 1 0 0 0 1 0 // 4 1 1 0 1 0 0 // 5 1 0 0 0 0 0 // </not sorted> // // <sorted> // 2 3 5 4 1 0 // // 2 0 0 0 1 1 0 // 3 0 0 0 1 1 0 // 5 0 0 0 1 1 1 // 4 1 1 1 0 0 1 // 1 1 1 1 0 0 1 // 0 0 0 1 1 1 0 // </sorted> // var graph1CountDicKeys = graph1IndexListPerCountDic.Keys.ToArray(); var graph2CountDicKeys = graph2IndexListPerCountDic.Keys.ToArray(); var combinationGraph2 = new List <int>(); foreach (int graph2CountDicKey in graph2CountDicKeys) { combinationGraph2.AddRange(graph2IndexListPerCountDic[graph2CountDicKey]); } graph2MatrixTemp = MultiArrayHelper <int> .OrderArray(graph2MatrixTemp, combinationGraph2); //Permutate graphMatrix1 and check if it matches with graphMatrix2 var graph1IndexPermutations = new List <List <int[]> >(); for (int k = 0; k < graph1CountDicKeys.Length; k++) { var indexList = graph1IndexListPerCountDic[graph1CountDicKeys[k]]; var permutations = ListPermutations(indexList.ToArray()); graph1IndexPermutations.Add(permutations); } var combinations = GetCombinationsOfIntLists(graph1IndexPermutations); foreach (List <int> combination in combinations) { var graph1MatrixTempCombination = MultiArrayHelper <int> .OrderArray(graph1MatrixTemp, combination); bool isCombinationCorrect = true; for (int i = 0; i < combination.Count; i++) { var rowGraph1 = MultiArrayHelper <int> .GetRow(graph1MatrixTempCombination, i); var rowGraph2 = MultiArrayHelper <int> .GetRow(graph2MatrixTemp, i); if (!MultiArrayHelper <int> .CompareRows(rowGraph1, rowGraph2)) { isCombinationCorrect = false; break; } } if (isCombinationCorrect) { return(true); } } return(false); }