C# 井字遊戲

開始畫面。
遊戲畫面。




  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace CSharp_井字遊戲
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. Symbol1_tb.TextChanged += SymbolTextChenge;
  19. Symbol2_tb.TextChanged += SymbolTextChenge;
  20. Symbol1_tb.MouseClick += Symbol_tb_MouseClick;
  21. Symbol2_tb.MouseClick += Symbol_tb_MouseClick;
  22. }
  23.  
  24. private void Symbol_tb_MouseClick(object sender, MouseEventArgs e)
  25. {
  26. TextBox tb = sender as TextBox;
  27. tb.SelectAll();
  28. }
  29.  
  30. private void SymbolTextChenge(object sender, EventArgs e)
  31. {
  32. TextBox tb = sender as TextBox;
  33. if (tb.TextLength >= 2)
  34. {
  35. tb.SelectAll();
  36. MessageBox.Show("只能輸入一個字!", "錯誤!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  37. }
  38. }
  39.  
  40. Button[,] btn = new Button[3, 3];
  41. Label label;
  42. Font font = new Font("微軟正黑體", 12);
  43. bool nowIndex = false;
  44. string symbol1, symbol2;
  45. int count;
  46. DialogResult result;
  47. Panel panel;
  48. private void Start_btn_Click(object sender, EventArgs e)
  49. {
  50. StartGame();
  51. }
  52.  
  53. private void StartGame()
  54. {
  55. symbol1 = Symbol1_tb.Text;
  56. symbol2 = Symbol2_tb.Text;
  57. panel1.Visible = false;
  58. if (panel != null)
  59. {
  60. panel.Visible = true;
  61. Start_btn.Enabled = false;
  62. }
  63. nowIndex = false;
  64. this.Size = new Size(420, 440);
  65. if (panel == null)
  66. {
  67. panel = new Panel();
  68. panel.Size = new Size(this.DisplayRectangle.Width, this.DisplayRectangle.Height);
  69. }
  70. this.Controls.Add(panel);
  71. Creat_btn_label();
  72. }
  73.  
  74. private void Creat_btn_label()
  75. {
  76. for (int x = 0; x < btn.GetLength(0); x++)
  77. for (int y = 0; y < btn.GetLength(0); y++)
  78. {
  79. panel.Controls.Remove(btn[x, y]);
  80. if (btn[x, y] != null)
  81. btn[x, y].Dispose();
  82. }
  83. btn = new Button[3, 3];
  84. for (int x = 0; x < btn.GetLength(0); x++)
  85. for (int y = 0; y < btn.GetLength(1); y++)
  86. {
  87. btn[x, y] = new Button();
  88. btn[x, y].Size = new Size(100, 100);
  89. btn[x, y].Text = "";
  90. btn[x, y].Location = new Point(50 + x * 100, 50 + y * 100);
  91. btn[x, y].Font = font;
  92. btn[x, y].Click += ButtonsClick;
  93. panel.Controls.Add(btn[x, y]);
  94. }
  95. if (label == null)
  96. label = new Label();
  97. label.Font = font;
  98. label.Size = new Size(200, 20);
  99. label.Text = "玩家一,請選擇。";
  100. label.Location = new Point(50, 20);
  101. panel.Controls.Add(label);
  102. }
  103.  
  104. private void ButtonsClick(object sender, EventArgs e)
  105. {
  106. Button btn = sender as Button;
  107. if (btn.Text == "" && count < 9)
  108. {
  109. btn.Text = nowIndex == true ? symbol2 : symbol1;
  110. nowIndex = !nowIndex;
  111. label.Text = nowIndex == true ? "玩家二,請選擇。" : "玩家一,請選擇。";
  112. Check();
  113. count++;
  114. if (count == 9)
  115. {
  116. label.Text = "GameOver!";
  117. GameOver("GameOver");
  118. }
  119. Console.WriteLine(count);
  120. }
  121. }
  122.  
  123. private void Check()
  124. {
  125. for (int x = 0; x < btn.GetLength(0); x++)
  126. if (btn[x, 0].Text == btn[x, 1].Text && btn[x, 1].Text == btn[x, 2].Text && btn[x, 0].Text != "" && btn[x, 1].Text != "" && btn[x, 2].Text != "")
  127. GameOver(btn[x, 0].Text);
  128. for (int y = 0; y < btn.GetLength(1); y++)
  129. if (btn[0, y].Text == btn[1, y].Text && btn[1, y].Text == btn[2, y].Text && btn[0, y].Text != "" && btn[1, y].Text != "" && btn[2, y].Text != "")
  130. GameOver(btn[0, y].Text);
  131. if (btn[0, 0].Text == btn[1, 1].Text && btn[1, 1].Text == btn[2, 2].Text && btn[0, 0].Text != "" && btn[1, 1].Text != "" && btn[2, 2].Text != "")
  132. GameOver(btn[0, 0].Text);
  133. if (btn[2, 0].Text == btn[1, 1].Text && btn[1, 1].Text == btn[0, 2].Text && btn[2, 0].Text != "" && btn[1, 1].Text != "" && btn[0, 2].Text != "")
  134. GameOver(btn[2, 0].Text);
  135. }
  136.  
  137. private void GameOver(string str)
  138. {
  139. count = 10;
  140. string msg = "";
  141. if (str == symbol1)
  142. {
  143. msg = "玩家一獲勝!";
  144. count = -1;
  145. }
  146. else if (str == symbol2)
  147. {
  148. msg = "玩家二獲勝!";
  149. count = -1;
  150. }
  151. else if (str == "GameOver")
  152. {
  153. msg = "平局";
  154. count = 0;
  155. }
  156. label.Text = "GameOver!";
  157. result = MessageBox.Show(msg, "", MessageBoxButtons.OKCancel, MessageBoxIcon.None);
  158. if (result == DialogResult.OK)
  159. StartGame();
  160. else
  161. {
  162. panel1.Visible = true;
  163. Start_btn.Enabled = true;
  164. panel.Visible = false;
  165. this.Size = new Size(307, 254);
  166. }
  167. }
  168. }
  169. }
  170.  

專案檔下載。

留言

這個網誌中的熱門文章