- 最後登錄
- 2024-12-22
- 在線時間
- 34 小時
- 註冊時間
- 2009-3-12
- 閱讀權限
- 20
- 精華
- 0
- UID
- 6005428
- 帖子
- 64
- 積分
- 84 點
- 潛水值
- 8409 米
| 若新密碼無法使用,可能是數據未更新。請使用舊密碼看看。 本帖最後由 sango99001 於 2020-12-27 12:37 PM 編輯
這是我這陣子自己寫出來的井字遊戲,無ai,只有雙方玩家輪流下子,請高手多多指教
以下為我的程式碼- //宣告物件陣列及變數
- Label[,] lbl; int[] player = new int[2];
- const int ranks = 3; //行列個數(依照物件數量更改)
- const int line = 3; //連線子數(依需求更改)
- // 最大需求子數 中間數 落子數
- int need = line * 2 - 1, median, counter;
- private void frmOOXX_Load(object sender, EventArgs e)
- {
- //中間數,Label物件陣列設定
- median = need - line;
- lbl = new Label[ranks, ranks]; player[0] = -1; counter = 1;
- for (int x = 0; x < ranks; x++)
- {
- for (int y = 0; y < ranks; y++)
- {
- lbl[x, y] = (Label)this.Controls.Find("label" + (x * ranks + y + 1).ToString(), true).FirstOrDefault();
- }
- } button1_Click(sender, e);
- }
- private void label1_Click(object sender, EventArgs e)
- {
- //按下物件後的動作int row = -1, col = -1;
- int row = -1, col = -1;
- for (int x = 0; x < ranks; x++)
- {
- for (int y = 0; y < ranks; y++)
- {
- if (lbl[x, y] == sender)
- {
- row = x; col = y;
- if (lbl[x, y].Text == "" && lblText.Text == "")
- {
- if (player[1] == 1) lbl[x, y].Text = "○";
- else lbl[x, y].Text = "●";
- counter = counter + 1; player[1] = player[1] * -1;
- c(row, col);
- }
- }
- if (row > -1 && col > -1) break;
- }
- if (row > -1 && col > -1) break;
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- //遊戲重置
- if (counter > 0)
- {
- player[0] = player[0] * -1; player[1] = player[0] * -1; counter = 0; lblText.Text = "";
- for (int x = 0; x < ranks; x++)
- {
- for (int y = 0; y < ranks; y++)
- {
- lbl[x, y].Text = "";
- }
- }
- }
- }
- private void c(int row, int col)
- {
- //宣告變數
- string[,] str = new string[4, need];
- int x1 = line - 2, x2 = ranks - 1, x3 = x2 - x1;
- bool[] t = new bool[4] { true, true, true, true };
-
- //進入判斷4角不足連子數的函式
- for (int cols = 0; cols < x1; cols++)
- for (int rows = 0; rows == cols; rows++)
- for (int i = 0; i < 4; i++)
- b(row, col, rows, cols, i, x1, x2, x3, ref t[i]);
- //依按下物件的行列編號,將對應的線段物件標記寫入陣列(判斷是否連線使用)
- for (int c = 0; c < 4; c++)
- {
- switch (c)
- {
- case 0: //橫線
- for (int i = 0; i < line; i++)
- {
- bool b1 = false, b2 = false;
- b1 = rc1(col, i);
- if (b1)
- str[c, median - i] = lbl[row, col - i].Text;
- b2 = rc2(col, i);
- if (b2)
- str[c, median + i] = lbl[row, col + i].Text;
- }
- break;
- case 1: //直線
- for (int i = 0; i < line; i++)
- {
- bool a1 = false, a2 = false;
- a1 = rc1(row, i);
- if (a1)
- str[c, median - i] = lbl[row - i, col].Text;
- a2 = rc2(row, i);
- if (a2)
- str[c, median + i] = lbl[row + i, col].Text;
- }
- break;
- case 2: //反斜線
- for (int i = 0; i < line; i++)
- {
- bool a1 = false, b1 = false, a2 = false, b2 = false;
- a1 = rc1(row, i); b1 = rc1(col, i);
- if (a1 && b1 && t[0])
- str[c, median - i] = lbl[row - i, col - i].Text;
- a2 = rc2(row, i); b2 = rc2(col, i);
- if (a2 && b2 && t[1])
- str[c, median + i] = lbl[row + i, col + i].Text;
- }
- break;
- case 3: //斜線
- for (int i = 0; i < line; i++)
- {
- bool a1 = false, b2 = false, a2 = false, b1 = false;
- a1 = rc1(row, i); b2 = rc2(col, i);
- if (a1 && b2 && t[2])
- str[c, median - i] = lbl[row - i, col + i].Text;
- a2 = rc2(row, i); b1 = rc1(col, i);
- if (a2 && b1 && t[3])
- str[c, median + i] = lbl[row + i, col - i].Text;
- }
- break;
- }
- result(ref str, c, str[c, median]); if (lblText.Text != "") break;
- }
- }
- private void result(ref string[,] str, int s, string str1)
- {
- ////判斷是否連線或平手
- for (int x = 0; x <= need - line; x++)
- {
- int y = 0;
- for (int i = 0; i < line; i++) {
- if (str[s, i + x] != str1) continue;
- y++;
- if (y == line) lblText.Text = str1 + "勝";
- }
- }
- if (counter == Math.Pow(ranks, 2) && lblText.Text == "")
- lblText.Text = "平手";
- if (lblText.Text == "")
- s++;
- }
- bool rc1(int c, int x)
- {//遞減找所需的子
- bool a = false;
- if (c - x > -1) a = true;
- return a;
- }
- bool rc2(int c, int x)
- { //遞增找所需的子
- bool a = false;
- if (c + x < ranks) a = true;
- return a;
- }
- void b(int row, int col, int rows, int cols, int i, int x1, int x2, int x3, ref bool t)
- { //排除連子數不足的線段
- bool[] a = new bool[4] { false, false, false, false };
- switch (i)
- {
- case 0:
- a = new bool[4] { rl(row, x1, rows), rl(col, x2, cols), rl(row, x2, rows), rl(col, x1, cols) };
- break;
- case 1:
- a = new bool[4] { lr(row, 0, rows), lr(col, x3, cols), lr(row, x3, rows), lr(col, 0, cols) };
- break;
- case 2:
- a = new bool[4] { rl(row, x1, rows), lr(col, 0, cols), rl(row, x2, rows), lr(col, x3, cols) };
- break;
- case 3:
- a = new bool[4] { lr(row, 0, rows), rl(col, x1, cols), lr(row, x3, rows), rl(col, x2, cols) };
- break;
- }
- if ((a[0] && a[1]) | (a[2] && a[3])) t = false;
- }
-
- bool rl (int c, int x, int d)
- { //遞減判斷左右上邊界
- bool a = false;
- if (c == x - d) a = true;
- return a;
- }
- bool lr(int c, int x, int d)
- { //遞增判斷右或下邊界
- bool a = false;
- if (c == x + d) a = true;
- return a;
- }
複製代碼
... |
附件: 你需要登錄才可以下載或查看附件。沒有帳號?註冊 |