04週作業 Tic-Tac-Toe遊戲-電腦玩家

1.程式碼

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{

    public partial class Form1 : Form
    {
        bool x = true;
        char record = ' ';
        char[] cells = { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
        int num = 0, x1 = 0, o1 = 0;

        Random Rm = new Random(Guid.NewGuid().GetHashCode());
        int O_rm = 4, O_num = 0;
        DialogResult Dr;
        public Form1()
        {
            InitializeComponent();
            Guid.NewGuid();

        }

        private void btn_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            if (!btn.Text.Equals("")) return;
            string s = btn.Name.ToString();
            if (x)
            {
                btn.Text = "X";
                cells[s[3] - '0' - 1] = 'x';
                num++;
                x = x ^ true;
            }
            if (!x && num < 8)
            {
                object[] Bt = { btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9 };
                while (!x)
                {
                    O_rm = Rm.Next(1, 9);
                    if (cells[O_rm] == ' ' && ((Button)Bt[O_rm]).Text.Equals(""))
                    {
                        ((Button)Bt[O_rm]).Text = "O";
                        cells[O_rm] = 'o';
                        num++;
                        x = x ^ true;
                    }
                    else x = false;
                }
            }
            record = testWin();
            if (record != ' ') //record有結果後判斷誰贏
            {
                if (record == 'X')
                {
                    x1++;
                    Dr = MessageBox.Show("X贏了","要再玩一場嗎?",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
                }
                if (record == 'O')
                {
                    o1++;
                    Dr = MessageBox.Show("O贏了", "要再玩一場嗎?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                }
                label2.Text = x1.ToString();
                label3.Text = o1.ToString();
                if(Dr==DialogResult.Yes)game_result();
                if (Dr == DialogResult.No) locking();
            }
            if (record == ' ' && num == 9) //如果按了9次,record還沒有結果為平手
            {
                label5.Text = "平手";
                game_result();
            }


            //MessageBox.Show(cells[0] + "," + cells[1] + "," + cells[2] + "," + cells[3] + "," + cells[4] + "," + cells[5] + "," + cells[6] + "," + cells[7] + "," + cells[8]);
        }

        private char testWin() //判斷O或X贏
        {
            char win = ' ';
            if ((cells[0] == 'x') && (cells[1] == 'x') && (cells[2] == 'x')) win = 'X';
            if ((cells[3] == 'x') && (cells[4] == 'x') && (cells[5] == 'x')) win = 'X';
            if ((cells[6] == 'x') && (cells[7] == 'x') && (cells[8] == 'x')) win = 'X';
            if ((cells[0] == 'x') && (cells[3] == 'x') && (cells[6] == 'x')) win = 'X';
            if ((cells[1] == 'x') && (cells[4] == 'x') && (cells[7] == 'x')) win = 'X';
            if ((cells[2] == 'x') && (cells[5] == 'x') && (cells[8] == 'x')) win = 'X';
            if ((cells[0] == 'x') && (cells[4] == 'x') && (cells[8] == 'x')) win = 'X';
            if ((cells[6] == 'x') && (cells[4] == 'x') && (cells[2] == 'x')) win = 'X';

            if ((cells[0] == 'o') && (cells[1] == 'o') && (cells[2] == 'o')) win = 'O';
            if ((cells[3] == 'o') && (cells[4] == 'o') && (cells[5] == 'o')) win = 'O';
            if ((cells[6] == 'o') && (cells[7] == 'o') && (cells[8] == 'o')) win = 'O';
            if ((cells[0] == 'o') && (cells[3] == 'o') && (cells[6] == 'o')) win = 'O';
            if ((cells[1] == 'o') && (cells[4] == 'o') && (cells[7] == 'o')) win = 'O';
            if ((cells[2] == 'o') && (cells[5] == 'o') && (cells[8] == 'o')) win = 'O';
            if ((cells[0] == 'o') && (cells[4] == 'o') && (cells[8] == 'o')) win = 'O';
            if ((cells[6] == 'o') && (cells[4] == 'o') && (cells[2] == 'o')) win = 'O';
            return win;
        }

        private void clear()  //清除圖片_副程式
        {
            for (int i = 0; i < 9; i++) cells[i] = ' ';
            object[] btn = { btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9 };
            for (int i = 0; i < btn.Length; i++) ((Button)btn[i]).Text = ""; 
            num = 0;
            label5.Text = "遊戲中";
            x = true;
            O_rm = 4;
            O_num = 0;
        }
        private void games_start()  //重新開始_副程式
        {
            label2.Text = "0";
            label3.Text = "0";
            x1 = 0;
            o1 = 0;
            clear();
            open_lock();
        }
        private void locking() //將圖片鎖住不能修改
        {
            object[] btn = { btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9 };
            for (int i = 0; i < btn.Length; i++) ((Button)btn[i]).Enabled = false;
        }
        private void open_lock() //圖片可修改
        {
            object[] btn = { btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9 };
            for (int i = 0; i < btn.Length; i++) ((Button)btn[i]).Enabled = true;
        }
        private void game_result() //判斷遊戲結果
        {
            clear(); locking(); open_lock();
        }
        private void X_Bt_Click(object sender, EventArgs e)  //X先_按鈕
        {
            if (num < 1) x = true;
        }
        private void O_Bt_Click(object sender, EventArgs e)  //O先_按鈕
        {
            if (num < 1) x = false;
        }
        private void NewGame_Bt(object sender, EventArgs e) //重新開始_按鈕
        {
            games_start();
        }
        private void ReGame_Bt(object sender, EventArgs e) //再來一局_按鈕
        {
            clear();
            open_lock();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

2.流程圖

3.程式結果

4.執行過程影片

results matching ""

    No results matching ""