03週作業-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 circle = false;
        Control[] s = new Control[9];
        int draw=9;
        public Form1()
        {
            int i = 8;
            InitializeComponent();
            foreach (Control c in panel1.Controls)
            {
                s[i] = c;
                i--;
            }
        }

        private void Buttton_Click(object sender, EventArgs e)
        {            
            Button b = (Button)sender;
            if (b.Text == "")
            {
                if (circle)
                {
                    b.Text = "Ο";
                    circle = false;
                    winers("Ο");
                }
                else
                {
                    b.Text = "×";
                    circle = true;
                    winers("×");
                }
            }
        }

        public void winers(string symbol)
        {           
            draw--;
            if (((s[0].Text == symbol) && (s[1].Text == symbol) && (s[2].Text == symbol)) ||
                ((s[3].Text == symbol) && (s[4].Text == symbol) && (s[5].Text == symbol)) ||
                ((s[6].Text == symbol) && (s[7].Text == symbol) && (s[8].Text == symbol)) ||                
                ((s[0].Text == symbol) && (s[3].Text == symbol) && (s[6].Text == symbol)) ||
                ((s[1].Text == symbol) && (s[4].Text == symbol) && (s[7].Text == symbol)) ||
                ((s[2].Text == symbol) && (s[5].Text == symbol) && (s[8].Text == symbol)) ||                
                ((s[0].Text == symbol) && (s[4].Text == symbol) && (s[8].Text == symbol)) ||
                ((s[2].Text == symbol) && (s[4].Text == symbol) && (s[6].Text == symbol)))
            {
                MessageBox.Show("恭喜" + symbol + "獲勝!", "結果");
                foreach (Control c in panel1.Controls)
                {
                    c.Text = "";
                    circle = false;
                }
                draw = 9;
            }
            else 
            {
                if (draw == 0)
                {
                    MessageBox.Show("平手", "結果");
                    foreach (Control c in panel1.Controls)
                    {
                        c.Text = "";
                        circle = false;
                    }
                    draw = 9;
                }                
            }
         }     
      }
  }

2.流程圖

3.程式結果

4.執行過程影片

results matching ""

    No results matching ""