public void AnswerBoxCheck(ref Numpre[,] numpre, ref int checknum, ref int timeover) { int countblank, i_out, j_out; bool IsSameNum = false; countblank = 0; i_out = 0; j_out = 0; execbutton[0].PerformClick(); // それぞれのグループの左上だけを指定する for (int i = 0; i <= 6; i += 3) { for (int j = 0; j <= 6; j += 3) { // 指定した位置から順に1行目、2行目、3行目と数値判定を行う for (int k = i; k <= i + 2; k++) { for (int l = j; l <= j + 2; l++) { // checknumの効いていない空白があったら if (numpre[k, l].IsError == true && numpre[k, l].IsValid == false) { // とりあえず覚えておく countblank += 1; i_out = k; j_out = l; } // そのグループに既にchecknumがあったら if (numpre[k, l].Number == checknum) { // そのグループはもう終了 IsSameNum = true; continue; } } } // そのグループにchecknumがまだなく // 空白が一つ、かつその空白のIsValidがfalseであった場合 if (countblank == 1 && IsSameNum == false) { // そのマスにchecknumを入れる numpre[i_out, j_out].Number = checknum; int txtbxi = FindTextBoxIndexFromNumpreIndex(i_out, j_out); if (txtbxi == -1) { ConsoleList.Items.Add("エラー: Numpre[,]配列のインデックスが不正です"); ConsoleList.Update(); return; } else { txtbox[txtbxi].Text = checknum.ToString(); txtbox[txtbxi].Update(); ConsoleList.Items.Add("自動解答: ボックスの消去法により (" + (i_out + 1) + ", " + (j_out + 1) + ") は " + checknum); ConsoleList.Update(); timeover = 0; // で、もう一度最初から試行する AnswerHrizonCheck(ref numpre, ref checknum, ref timeover); } } IsSameNum = false; countblank = 0; } } // 何もせずにループを抜けたら // タイムアウトカウンタを増やす timeover += 1; }
// 0以外の数値(答え)が入っているマスの数を数える public int AnswerBreakCheck(Numpre[,] numpre) { int compnum = 0; for (int i = 0; i <= 8; i++) { for (int j = 0; j <= 8; j++) { if (numpre[i, j].Number != 0) { compnum += 1; } } } return compnum; }
public void OverlapChangeColor(ref Numpre[,] numpre) { int x, y; x = 0; y = 0; for (int i = 0; i <= 80; i++) { if (numpre[x, y].IsOverlap == true) { txtbox[i].BackColor = Color.LightCoral; } if (y == 8) { y = 0; x += 1; } else { y += 1; } } }
public void AnswerVerticalCheck(ref Numpre[,] numpre, ref int checknum, ref int timeover) { int countblank, i_out, j_out; bool IsSameNum = false; countblank = 0; i_out = 0; j_out = 0; execbutton[0].PerformClick(); for (int i = 0; i <= 8; i++) { for (int j = 0; j <= 8; j++) { // checknumの効いていない空白があったら if (numpre[j, i].IsError == true && numpre[j, i].IsValid == false) { // とりあえず覚えておく countblank += 1; i_out = i; j_out = j; } // その列に既にchecknumがあったら if (numpre[j, i].Number == checknum) { // その列はもう終了 IsSameNum = true; continue; } } // その行で該当する空白がぴったり1個だった場合のみ if (countblank == 1 && IsSameNum == false) { // そのマスにchecknumを入れる numpre[j_out, i_out].Number = checknum; int txtbxi = FindTextBoxIndexFromNumpreIndex(j_out, i_out); if (txtbxi == -1) { ConsoleList.Items.Add("エラー: Numpre[,]配列のインデックスが不正です"); ConsoleList.Update(); return; } else { txtbox[txtbxi].Text = checknum.ToString(); txtbox[txtbxi].Update(); ConsoleList.Items.Add("自動解答: タテの消去法により (" + (j_out + 1) + ", " + (i_out + 1) + ") は " + checknum); ConsoleList.Update(); timeover = 0; // で、もう一度最初から試行する AnswerHrizonCheck(ref numpre, ref checknum, ref timeover); } } IsSameNum = false; countblank = 0; } // 何もせずにループを抜けたら // タイムアウトカウンタを増やす timeover += 1; }
// CheckOverlapで使用します public void CheckOverlapOfVerticalAndHorizon(ref Numpre[,] numpre, ref int ovl) { for (int i = 0; i <= 8; i++) { for (int j = 0; j <= 7; j++) { for (int k = j + 1; k <= 8; k++) { if (numpre[i, j].IsError == false) { if (numpre[i, j].Number == numpre[i, k].Number) { numpre[i, j].IsOverlap = true; numpre[i, k].IsOverlap = true; ovl += 1; } } if (numpre[j, i].IsError == false) { if (numpre[j, i].Number == numpre[k, i].Number) { numpre[j, i].IsOverlap = true; numpre[k, i].IsOverlap = true; ovl += 1; } } } } } }
// CheckOverlapで使用します public void CheckOverlapOfBox(ref Numpre[,] numpre, int x, int y, int a, int b, int c, int d, int e, int f, int g, int h, ref int ovl) { for (int i = 0; i <= 8; i++) { for (int j = 0; j <= 8; j++) { if (numpre[i, j].IsError == false) { if (((i == x + a && j == y + b) && numpre[x, y].Number == numpre[i, j].Number) || ((i == x + c && j == y + d) && numpre[x, y].Number == numpre[i, j].Number) || ((i == x + e && j == y + f) && numpre[x, y].Number == numpre[i, j].Number) || ((i == x + g && j == y + h) && numpre[x, y].Number == numpre[i, j].Number)) { numpre[i, j].IsOverlap = true; ovl += 1; } } } } }
public void CountExist(ref Numpre[,] numpre, int[] exist, string[] ins) { for (int i = 0; i <= 8; i++) { for (int j = 0; j <= 8; j++) { for (int num = 1; num <= 9; num++) { if (numpre[i, j].Number == num) { exist[num - 1] += 1; } } } } for (int i = 0; i <= 8; i++) { for (int j = 0; j <= 8 - exist[i]; j++) { ins[i] = ins[i] + "-"; } chkbox[i].Text = (i + 1).ToString() + " " + ins[i]; } }
public void CheckOverlap(ref Numpre[,] numpre, ref int ovl) { int x, y; CheckOverlapOfVerticalAndHorizon(ref numpre, ref ovl); for (int i = 0; i <= 8; i++) { for (int j = 0; j <= 8; j++) { x = i; y = j; if (x % 3 == 0 && y % 3 == 0) { CheckOverlapOfBox(ref numpre, x, y, 1, 1, 2, 1, 1, 2, 2, 2, ref ovl); } else if (x % 3 == 1 && y % 3 == 0) { CheckOverlapOfBox(ref numpre, x, y, -1, 1, -1, 2, 1, 1, 1, 2, ref ovl); } else if (x % 3 == 2 && y % 3 == 0) { CheckOverlapOfBox(ref numpre, x, y, -1, 1, -1, 2, -2, 1, -2, 2, ref ovl); } else if (x % 3 == 0 && y % 3 == 1) { CheckOverlapOfBox(ref numpre, x, y, 1, -1, 2, -1, 1, 1, 2, 1, ref ovl); } else if (x % 3 == 1 && y % 3 == 1) { CheckOverlapOfBox(ref numpre, x, y, -1, -1, -1, 1, 1, 1, 1, -1, ref ovl); } else if (x % 3 == 2 && y % 3 == 1) { CheckOverlapOfBox(ref numpre, x, y, -1, 1, -1, -1, -2, 1, -2, -1, ref ovl); } else if (x % 3 == 0 && y % 3 == 2) { CheckOverlapOfBox(ref numpre, x, y, 1, -1, +2, -1, 1, -2, +2, -2, ref ovl); } else if (x % 3 == 1 && y % 3 == 2) { CheckOverlapOfBox(ref numpre, x, y, 1, -1, 1, -2, -1, -1, -1, -2, ref ovl); } else if (x % 3 == 2 && y % 3 == 2) { CheckOverlapOfBox(ref numpre, x, y, -1, -1, -1, -2, -2, -1, -2, -2, ref ovl); } } } }
public void ChangeColor(ref Numpre[,] numpre) { int x, y; x = 0; y = 0; for (int i = 0; i <= 80; i++) { if (numpre[x, y].IsValid == true) { txtbox[i].BackColor = Color.Yellow; } if (y == 8) { y = 0; x += 1; } else { y += 1; } } }
public void CountNull(ref Numpre[,] numpre, int[] VErr, int[] HErr, ref int ErrNum) { for (int j = 0; j <= 8; j++) { for (int i = 0; i <= 8; i++) { if (numpre[i, j].IsError == true) { VErr[j] += 1; } if (numpre[j, i].IsError == true) { HErr[j] += 1; } } label[j].Text = VErr[j].ToString(); label[j + 9].Text = HErr[j].ToString(); ErrNum += VErr[j]; } }
// CheckValid()で使用します public void CheckValidOfBox(ref Numpre[,] numpre, int x, int y, int a, int b, int c, int d, int e, int f, int g, int h) { for (int i = 0; i <= 8; i++) { for (int j = 0; j <= 8; j++) { if ((i == x + a && j == y + b) || (i == x + c && j == y + d) || (i == x + e && j == y + f) || (i == x + g && j == y + h)) { numpre[i, j].IsValid = true; } } } }
// CheckValid()で使用します public void CheckValidOfVerticalAndHorizon(ref Numpre[,] numpre, int x, int y) { for (int i = 0; i <= 8; i++) { for (int j = 0; j <= 8; j++) { if (i == x || j == y) { numpre[i, j].IsValid = true; } } } }
public void CheckValid(ref Numpre[,] numpre) { int x, y; for (int k = 0; k <= 8; k++) { if (chkbox[k].Checked == true) { for (int i = 0; i <= 8; i++) { for (int j = 0; j <= 8; j++) { if (numpre[i, j].Number == k + 1) { x = i; y = j; numpre[i, j].IsValid = true; CheckValidOfVerticalAndHorizon(ref numpre, x, y); if (x % 3 == 0 && y % 3 == 0) { CheckValidOfBox(ref numpre, x, y, 1, 1, 2, 1, 1, 2, 2, 2); } else if (x % 3 == 1 && y % 3 == 0) { CheckValidOfBox(ref numpre, x, y, -1, 1, -1, 2, 1, 1, 1, 2); } else if (x % 3 == 2 && y % 3 == 0) { CheckValidOfBox(ref numpre, x, y, -1, 1, -1, 2, -2, 1, -2, 2); } else if (x % 3 == 0 && y % 3 == 1) { CheckValidOfBox(ref numpre, x, y, 1, -1, 2, -1, 1, 1, 2, 1); } else if (x % 3 == 1 && y % 3 == 1) { CheckValidOfBox(ref numpre, x, y, -1, -1, -1, 1, 1, 1, 1, -1); } else if (x % 3 == 2 && y % 3 == 1) { CheckValidOfBox(ref numpre, x, y, -1, 1, -1, -1, -2, 1, -2, -1); } else if (x % 3 == 0 && y % 3 == 2) { CheckValidOfBox(ref numpre, x, y, 1, -1, +2, -1, 1, -2, +2, -2); } else if (x % 3 == 1 && y % 3 == 2) { CheckValidOfBox(ref numpre, x, y, 1, -1, 1, -2, -1, -1, -1, -2); } else if (x % 3 == 2 && y % 3 == 2) { CheckValidOfBox(ref numpre, x, y, -1, -1, -1, -2, -2, -1, -2, -2); } } } } } } }
public void CheckNull(ref Numpre[,] numpre) { int i, j, k; i = 0; j = 0; for (k = 0; k <= 80; k++) { try { numpre[i, j].Number = int.Parse(txtbox[k].Text); } catch (Exception ex) { numpre[i, j].IsError = true; } if (j == 8) { j = 0; i += 1; } else { j += 1; } } }
// 構造体の初期化 public void NumpreInitialization(ref Numpre[,] numpre, ref int[] VErr, ref int[] HErr, ref int[] exist, ref string[] ins) { for (int i = 0; i <= 8; i++) { for (int j = 0; j <= 8; j++) { Num[i, j].IsError = false; Num[i, j].IsValid = false; } VErr[i] = 0; HErr[i] = 0; exist[i] = 0; ins[i] = ""; } }