[C#][WinForm] 井字遊戲

  • 14934
  • 0
  • 2014-02-25

[C#][WinForm] 井字遊戲

就是弄個井字遊戲,首先弄個介面,如下圖

image

顏色、框線等請自行設定,另外我是使用 button 來做井字遊戲 然後,程式碼部分都有註解好了,直接看應該就知道了。


        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 = "";
                btnTControls[i].BackColor = Color.LightBlue;
                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];
                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[i].Text != "")
                {
                    btnIsUse++;
                    if (btnIsUse == 9)
                        gameWinOver[1] = true;
                }
            }
            return gameWinOver;
        }

 

對於 GraphicsUnit 和 GdiCharSet 比較不熟悉的人,可以參考 MSDN 上的資訊,如下:

GraphicsUnit:連結

GdiCharSet:連結

執行畫面:

image

範例檔案 下載