private bool isDeadLock(int value, Posision pos) { List <Posision> array = listArrayOfValue[value - 1]; if (array.Count >= 2) { List <Posision> checkArray = getHorAndVerBaseOnPos(array, pos); List <Point> checkArrayNotInvite = getPosNotInviteBaseOnPos(checkArray, pos); int length; foreach (Point ptr in checkArrayNotInvite) { length = SIZE; if (ptr.X == pos.a) { for (int k = 0; k < SIZE; k++) { if (k != pos.c) { for (int m = 0; m < SIZE; m++) { if (!listBtn[ptr.X, ptr.Y, k, m].Text.Equals("")) { length++; } } } } if (length == 9) { return(false); } } else { for (int k = 0; k < SIZE; k++) { for (int m = 0; m < SIZE; m++) { if (m != pos.d) { if (!listBtn[ptr.X, ptr.Y, k, m].Text.Equals("")) { length++; } } } } if (length == 9) { return(false); } } } } return(true); }
public void setDefaultValue() { for (int i = 0; i < 42; i++) { Posision pos = getPosition(); int value = getInvalid(pos); listBtn[pos.a, pos.b, pos.c, pos.d].Text = value + ""; AutoSetValueMapGenerator(pos, false); } }
private List <Posision> getHorAndVerBaseOnPos(List <Posision> lst, Posision pos) { List <Posision> listPos = new List <Posision>(); foreach (Posision vt in lst) { if (vt.a == pos.a || vt.b == pos.b) { listPos.Add(vt); } } return(listPos); }
public int getInvalid(Posision pos) { Random r = new Random(); int value; do { value = r.Next(1, 10); }while (!isCheckVertical(value, pos) || !isCheckHorizontal(value, pos) || !isCheckSubMatrix(value, pos)); //|| !isDeadLock(value,pos) listArrayOfValue[value - 1].Add(pos); setInvalidForSPList(pos); return(value); }
private bool isCheckSubMatrix(int value, Posision pos) { for (int k = 0; k < SIZE; k++) { for (int m = 0; m < SIZE; m++) { if (listBtn[pos.a, pos.b, k, m].Text.Equals(value + "")) { return(false); } } } return(true); }
private bool isCheckHorizontal(int value, Posision pos) { //Kiểm tra hàng ngang for (int j = 0; j < SIZE; j++) { for (int m = 0; m < SIZE; m++) { if (listBtn[pos.a, j, pos.c, m].Text.Equals(value + "")) { return(false); } } } return(true); }
private bool isCheckVertical(int value, Posision pos) { //Kiểm tra hàng dọc for (int i = 0; i < SIZE; i++) { for (int k = 0; k < SIZE; k++) { if (listBtn[i, pos.b, k, pos.d].Text.Equals(value + "")) { return(false); } } } return(true); }
private void AutoSetValueMapGenerator(Posision pos, bool FillColor) { if (listCountHorizontal[(pos.a * SIZE) + pos.c] >= 7 && listCountHorizontal[(pos.a * SIZE) + pos.c] != 9) { fillInvalid(pos, 0, FillColor); } if (listCountVertical[(pos.b * SIZE) + pos.d] >= 7 && listCountVertical[(pos.b * SIZE) + pos.d] != 9) { fillInvalid(pos, 1, FillColor); } if (listCountSubMatrix[(pos.a * SIZE) + pos.b] >= 7 && listCountSubMatrix[(pos.a * SIZE) + pos.b] != 9) { fillInvalid(pos, 2, FillColor); } }
private List <Point> getPosNotInviteBaseOnPos(List <Posision> lstCheck, Posision pos) { List <Point> listPos = new List <Point>(); for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { if (i != pos.a && j != pos.b) { foreach (Posision vt in lstCheck) { if (vt.a != i && vt.b != j) { listPos.Add(new Point(i, j)); } } } } } return(listPos); }
private void fillInvalid(Posision pos, int caseValue, bool FillColor) { List <int> listGuestInvalid = new List <int>(); List <Posision> listGuestPos = new List <Posision>(); int[] arrayCheck = new int[9]; //Xử lý ngang => BUG if (caseValue == 0) { for (int j = 0; j < SIZE; j++) { for (int m = 0; m < SIZE; m++) { if (listBtn[pos.a, j, pos.c, m].Text.Equals("")) { listGuestPos.Add(new Posision(pos.a, j, pos.c, m)); } else { int number = Int16.Parse(listBtn[pos.a, j, pos.c, m].Text) - 1; arrayCheck[number] = 1; } } } } //Xử lý dọc => BUG else if (caseValue == 1) { for (int i = 0; i < SIZE; i++) { for (int k = 0; k < SIZE; k++) { if (listBtn[i, pos.b, k, pos.d].Text.Equals("")) { listGuestPos.Add(new Posision(i, pos.b, k, pos.d)); } else { int number = Int16.Parse(listBtn[i, pos.b, k, pos.d].Text) - 1; arrayCheck[number] = 1; } } } } //Xử lý ma trận con else { for (int k = 0; k < SIZE; k++) { for (int m = 0; m < SIZE; m++) { if (listBtn[pos.a, pos.b, k, m].Text.Equals("")) { listGuestPos.Add(new Posision(pos.a, pos.b, k, m)); } else { int number = Int16.Parse(listBtn[pos.a, pos.b, k, m].Text) - 1; arrayCheck[number] = 1; } } } } for (int stt = 0; stt < arrayCheck.Length; stt++) { if (arrayCheck[stt] == 0) { listGuestInvalid.Add(stt + 1); } } if (listGuestPos.Count == 2) { //||!isDeadLock(listGuestInvalid[0],listGuestPos[0]) if (!isCheckVertical(listGuestInvalid[0], listGuestPos[0]) || !isCheckHorizontal(listGuestInvalid[0], listGuestPos[0]) || !isCheckSubMatrix(listGuestInvalid[0], listGuestPos[0])) { listBtn[listGuestPos[1].a, listGuestPos[1].b, listGuestPos[1].c, listGuestPos[1].d].Text = listGuestInvalid[0] + ""; listBtn[listGuestPos[0].a, listGuestPos[0].b, listGuestPos[0].c, listGuestPos[0].d].Text = listGuestInvalid[1] + ""; } else { listBtn[listGuestPos[1].a, listGuestPos[1].b, listGuestPos[1].c, listGuestPos[1].d].Text = listGuestInvalid[1] + ""; listBtn[listGuestPos[0].a, listGuestPos[0].b, listGuestPos[0].c, listGuestPos[0].d].Text = listGuestInvalid[0] + ""; } } else { listBtn[listGuestPos[0].a, listGuestPos[0].b, listGuestPos[0].c, listGuestPos[0].d].Text = listGuestInvalid[0] + ""; } if (FillColor) { for (int i = 0; i < listGuestPos.Count; i++) { listBtn[listGuestPos[i].a, listGuestPos[i].b, listGuestPos[i].c, listGuestPos[i].d].ForeColor = Color.Red; } } //Check lại các value vừa điền for (int i = 0; i < listGuestPos.Count; i++) { setInvalidForSPList(new Posision(listGuestPos[i].a, listGuestPos[i].b, listGuestPos[i].c, listGuestPos[i].d)); } for (int i = 0; i < listGuestPos.Count; i++) { AutoSetValueMapGenerator(listGuestPos[i], FillColor); } }
private void setInvalidForSPList(Posision pos) { listCountHorizontal[(pos.a * 3) + pos.c] += 1; listCountVertical[(pos.b * 3) + pos.d] += 1; listCountSubMatrix[(pos.a * 3) + pos.b] += 1; }
private bool isValid(List <Posision> lst, Point posCompare, int value) { //Ma trận mô phỏng để check giá trị int[,] checkArray = new int[SIZE, SIZE]; for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { if (!listBtn[posCompare.X, posCompare.Y, i, j].Text.Equals("")) { checkArray[i, j] = 1; } } } foreach (Posision pos in lst) { if (pos.a == posCompare.X) { for (int j = 0; j < SIZE; j++) { if (checkArray[pos.c, j] == 0) { checkArray[pos.c, j] = 1; } } } else { for (int i = 0; i < SIZE; i++) { if (checkArray[i, pos.d] == 0) { checkArray[i, pos.d] = 1; } } } } int lenght = 0; for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { if (checkArray[i, j] == 1) { ++lenght; } } } if (lenght == 8) { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { if (checkArray[i, j] == 0) { Posision local = new Posision(posCompare.X, posCompare.Y, i, j); listBtn[local.a, local.b, local.c, local.d].Text = value + ""; listBtn[local.a, local.b, local.c, local.d].Font = new System.Drawing.Font("Microsoft Sans Serif", 14F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(163))); listBtn[local.a, local.b, local.c, local.d].ForeColor = Color.Red; lst.Add(local); setInvalidForSPList(local); AutoSetValueMapGenerator(local, true); } } } return(true); } return(false); }