使用WPF寫一個井字小遊戲演算法
井字遊戲演算法(3*3)
如何判斷 某一條已經連成一直線
最簡單的方法
達成直線總共會有 8 種
橫線 3 條
直線 3 條
斜的 2 條
以此類推
5*5 跟 7*7 的斜線 也是兩條 !!!!
圈圈 跟 叉叉 有兩個 State 我們可以分別給 正跟負的數值作為Flag來定義它的State
其中一方達到直線就遊戲結束
我們可以使用陣列來存取這些 State
Sum[9] 存取 0~8 這些按鈕狀態結果
新增好一個WPF專案後
拉一個Button
去右側屬性區塊更改寬、高
版面設定(Layout) --> Width 跟 Height 皆設定為 100
一般(Common) --> Content 設為 1
Copy Paste 額外的 八個
完成後的最原始 程式碼
MainWindow.xaml.cs 版本1
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace KinectV2_funnyApp { ////// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } int[] Sum = new int[9];//0~8 //我們用Sum[1]~Sum[8]來對應連為一直線的累計數值狀態 int[] ButtonState = new int[10]; //ButtonState[1]~ButtonState[9] private void b1_Click(object sender, RoutedEventArgs e) { ButtonState[1] = 1; CheckSum(); } private void b2_Click(object sender, RoutedEventArgs e) { ButtonState[2] = 1; CheckSum(); } private void b3_Click(object sender, RoutedEventArgs e) { ButtonState[3] = 1; CheckSum(); } private void b4_Click(object sender, RoutedEventArgs e) { ButtonState[4] = 1; CheckSum(); } private void b5_Click(object sender, RoutedEventArgs e) { ButtonState[5] = 1; CheckSum(); } private void b6_Click(object sender, RoutedEventArgs e) { ButtonState[6] = 1; CheckSum(); } private void b7_Click(object sender, RoutedEventArgs e) { ButtonState[7] = 1; CheckSum(); } private void b8_Click(object sender, RoutedEventArgs e) { ButtonState[8] = 1; CheckSum(); } private void b9_Click(object sender, RoutedEventArgs e) { ButtonState[9] = 1; CheckSum(); } private void CheckSum() { Sum[1] = ButtonState[1] + ButtonState[2] + ButtonState[3]; Sum[2] = ButtonState[4] + ButtonState[5] + ButtonState[6]; Sum[3] = ButtonState[7] + ButtonState[8] + ButtonState[9]; Sum[4] = ButtonState[1] + ButtonState[4] + ButtonState[7]; Sum[5] = ButtonState[2] + ButtonState[5] + ButtonState[8]; Sum[6] = ButtonState[3] + ButtonState[6] + ButtonState[9]; Sum[7] = ButtonState[1] + ButtonState[5] + ButtonState[9]; Sum[8] = ButtonState[3] + ButtonState[5] + ButtonState[7]; if (Sum.Contains(3) == true) MessageBox.Show("OK"); } } }
不過你會發現重複好多程式碼
我們其實可以縮短程式碼
讓同一種功能的只用一段程式進行
所以我們把程式全砍只留下 Button1 的事件即可
MainWindow.xaml.cs 版本2
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace KinectV2_funnyApp { ////// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } int[] Sum = new int[9];//0~8 //我們用Sum[1]~Sum[8]來對應連為一直線的累計數值狀態 int[] ButtonState = new int[10]; //ButtonState[1]~ButtonState[9] private void b1_Click(object sender, RoutedEventArgs e) { ButtonState[1] = 1; CheckSum(); } private void CheckSum() { Sum[1] = ButtonState[1] + ButtonState[2] + ButtonState[3]; Sum[2] = ButtonState[4] + ButtonState[5] + ButtonState[6]; Sum[3] = ButtonState[7] + ButtonState[8] + ButtonState[9]; Sum[4] = ButtonState[1] + ButtonState[4] + ButtonState[7]; Sum[5] = ButtonState[2] + ButtonState[5] + ButtonState[8]; Sum[6] = ButtonState[3] + ButtonState[6] + ButtonState[9]; Sum[7] = ButtonState[1] + ButtonState[5] + ButtonState[9]; Sum[8] = ButtonState[3] + ButtonState[5] + ButtonState[7]; if (Sum.Contains(3) == true) MessageBox.Show("OK"); } } }
之後我們到
MainWindow.xaml
但是目前程式執行後不管你按下哪個都只會針對 編號為1的按鈕進行狀態切換
因此我們到右側屬性去搜尋 Uid 更改每一個Button的ID屬性內容
可以作為辨別按鈕的依據
改完後的
MainWindow.xaml
之後我們再回到
MainWindow.xaml.cs
寫 按下去之後取得對應的 Uid 及更改按鈕上面數值為 圈圈 符號的程式
效果呈現
接著就到了探討 是自己 還是 對手 的回合
判斷
我們可以使用 boolean 去做切換
最後程式碼的修改
增加 我方(Player1) 和 敵方(Player2) 的判斷
各自增加 一組 ButtonState 及 SumPlayer
紀錄 各自的 按鈕狀態 及 切換為正1狀態的統計陣列
最後程式碼
MainWindow.xaml.cs 版本3 (Final)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace KinectV2_funnyApp { ////// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } int[] SumPlayer1 = new int[9];//0~8 //我們用Sum[1]~Sum[8]來對應連為一直線的累計數值狀態 int[] SumPlayer2 = new int[9];//0~8 int[] ButtonState1 = new int[10]; //ButtonState[1]~ButtonState[9] int[] ButtonState2 = new int[10]; bool myTurn = true; private void b1_Click(object sender, RoutedEventArgs e) { Button tmpButton = (Button)sender;//tmpButton中會有Uid //ButtonState[1] = 1; if (myTurn ) { tmpButton.Content = "O"; ButtonState1[Convert.ToInt16(tmpButton.Uid)] = 1; CheckSum(); myTurn = false; } else { tmpButton.Content = "X"; ButtonState2[Convert.ToInt16(tmpButton.Uid)] = 1; CheckSum(); myTurn = true; } } private void CheckSum() { SumPlayer1[1] = ButtonState1[1] + ButtonState1[2] + ButtonState1[3]; SumPlayer1[2] = ButtonState1[4] + ButtonState1[5] + ButtonState1[6]; SumPlayer1[3] = ButtonState1[7] + ButtonState1[8] + ButtonState1[9]; SumPlayer1[4] = ButtonState1[1] + ButtonState1[4] + ButtonState1[7]; SumPlayer1[5] = ButtonState1[2] + ButtonState1[5] + ButtonState1[8]; SumPlayer1[6] = ButtonState1[3] + ButtonState1[6] + ButtonState1[9]; SumPlayer1[7] = ButtonState1[1] + ButtonState1[5] + ButtonState1[9]; SumPlayer1[8] = ButtonState1[3] + ButtonState1[5] + ButtonState1[7]; SumPlayer2[1] = ButtonState2[1] + ButtonState2[2] + ButtonState2[3]; SumPlayer2[2] = ButtonState2[4] + ButtonState2[5] + ButtonState2[6]; SumPlayer2[3] = ButtonState2[7] + ButtonState2[8] + ButtonState2[9]; SumPlayer2[4] = ButtonState2[1] + ButtonState2[4] + ButtonState2[7]; SumPlayer2[5] = ButtonState2[2] + ButtonState2[5] + ButtonState2[8]; SumPlayer2[6] = ButtonState2[3] + ButtonState2[6] + ButtonState2[9]; SumPlayer2[7] = ButtonState2[1] + ButtonState2[5] + ButtonState2[9]; SumPlayer2[8] = ButtonState2[3] + ButtonState2[5] + ButtonState2[7]; if (myTurn) if (SumPlayer1.Contains(3) == true) MessageBox.Show("你(玩家1)贏了"); else if (SumPlayer2.Contains(3) == true) MessageBox.Show("對方(玩家2)贏了"); } } }
最後可成功對於 玩家1 及 玩家2 做判斷
留言
張貼留言