本帖最後由 arthurliuliu 於 2015-1-13 08:22 PM 編輯
可以問一下c#的問題嗎?
就是以下的程式
可以幫我解說每一段程式是怎麼寫的嗎?
不是註解唷!
- using System.Text;
- using System.Windows.Forms;
- using System.Windows;
- namespace TicTacToe
- {
- public partial class Form1 : Form
- {
- Button[] btnTControls= new Button[9]; //放置井字遊戲按鍵
- Boolean isGameOver = false; //有連線成功則結束
- Boolean isO = true; //點選是 O 還是 X
- //連線成功的組合
- static private int[,] WinGroup = new int[8, 3]
- {
- {0,1,2},
- {3,4,5},
- {6,7,8},
- {0,3,6},
- {1,4,7},
- {2,5,8},
- {0,4,8},
- {2,4,6}
- };
- public Form1()
- {
- InitializeComponent();
- //井字遊戲的 9 個按鍵
- btnTControls = new Button[9] { button3, button4, button5, button6, button7, button8, button9, button10, button11 };
- initButtons();
- }
- //設定遊戲開始的按鍵文字、顏色、字型
- private void initButtons()
- {
- isGameOver = false;
- isO = true;
- for (int i = 0; i < btnTControls.Length; i++)
- {
- btnTControls[i].Text = "";
- [/i] btnTControls[i].Font = new System.Drawing.Font("Arial", 30, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel, ((System.Byte)1));
- }
- }
- //開始遊戲
- private void btnStart_Click(object sender, EventArgs e)
- {
- initButtons();
- }
- //關閉遊戲
- private void btnClose_Click(object sender, EventArgs e)
- {
- this.Close();
- }
- private void buttons_Click(object sender, EventArgs e)
- {
- Button tmpButton = (Button)sender;
- if (isGameOver)
- {
- MessageBox.Show("遊戲結束....請重新開始遊戲!", "遊戲結束", MessageBoxButtons.OK);
- return;
- }
- if (tmpButton.Text != "")
- {
- MessageBox.Show("這個按鍵已經選擇了,請點選其他位置!","提示",MessageBoxButtons.OK);
- return;
- }
- if (isO)
- {
- tmpButton.Text = "O";
- tmpButton.BackColor = Color.LightGreen;
- }
- else
- {
- tmpButton.Text = "X";
- tmpButton.BackColor = Color.LightYellow;
- }
- isO = !isO;
- bool[] GameStatus = CheckWinGroup(btnTControls);
- isGameOver = GameStatus[1];
- //有人獲勝
- if (GameStatus[0])
- {
- DialogResult dr = MessageBox.Show("遊戲結束....\r\n" + tmpButton.Text + " 獲勝\r\n是否重新開始遊戲", "遊戲結束", MessageBoxButtons.OKCancel);
- if (dr == DialogResult.OK)
- initButtons();
- return;
- }
- //和局
- if (GameStatus[1])
- {
- DialogResult dr = MessageBox.Show("遊戲結束....\r\n和局\r\n是否重新開始遊戲", "遊戲結束", MessageBoxButtons.OKCancel);
- if (dr == DialogResult.OK)
- initButtons();
- }
- }
- //檢查是否OX連線成功
- private bool[] CheckWinGroup(Button[] myControls)
- {
- //gameWinOver {是否有人獲勝, 是否遊戲結束(或是平局)}
- bool[] gameWinOver = new bool[2] {false,false};
- int btnIsUse = 1;
- for (int i = 0; i < 8; i++)
- {
- int a = WinGroup[i, 0];
- int b = WinGroup[i, 1];
- int c = WinGroup[i, 2];
- Button b1 = myControls[a];
- Button b2 = myControls[b];[/b]
- Button b3 = myControls[c];
- //沒有連線就繼續
- if (b1.Text == "" || b2.Text == "" || b3.Text == "")
- continue;
- //連線成功,遊戲結束
- if (b1.Text == b2.Text && b2.Text == b3.Text)
- {
- b1.BackColor = b2.BackColor = b3.BackColor = Color.LightCoral;
- b1.Font = b2.Font = b3.Font = new System.Drawing.Font("Times New Roman", 35, System.Drawing.FontStyle.Italic & System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel,((System.Byte)1));
- gameWinOver =new bool[2] {true , true};
- break;
- }
- //九格填完,顯示結束
- if (myControls.Text != "")
- {
- btnIsUse++;
- if (btnIsUse == 9)
- gameWinOver[1] = true;
- }
- }
- return gameWinOver;
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- private void label1_Click(object sender, EventArgs e)
- {
- }
- }
- }
複製代碼
... |