swattw

Untitled

Dec 18th, 2016
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.29 KB | None | 0 0
  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 mutual_guess
  12. {
  13. public partial class Form1 : Form
  14. {
  15. char[] player1Value = new char[4];
  16. char[] player2Value = new char[4];
  17. int flow = 0, turns = 1;
  18. public Form1()
  19. {
  20. InitializeComponent();
  21. }
  22.  
  23. private void player1Button_Click(object sender, EventArgs e)
  24. {
  25. switch (flow)
  26. {
  27. case 0:
  28. if (player1Input.Text == "")
  29. {
  30. MessageBox.Show("請輸入名稱!");
  31. }
  32. else
  33. {
  34. player1Name.Text = player1Input.Text;
  35. player1Input.Text = "";
  36. player1Input.Enabled = false;
  37. player1Button.Enabled = false;
  38. player2Input.Enabled = true;
  39. player2Button.Enabled = true;
  40. post.Text = "請輸入玩家2名稱";
  41. player1Input.MaxLength = 4;
  42. player1Input.PasswordChar = '*';
  43. }
  44. break;
  45. case 1:
  46. if (player1Input.Text.ToCharArray().Length < 4)
  47. {
  48. MessageBox.Show("請輸入四位數!");
  49. }
  50. else
  51. {
  52. player1Value = player1Input.Text.ToCharArray();
  53. player1Output.Text = "****";
  54. player1Input.Text = "";
  55. player1Input.Enabled = false;
  56. player1Button.Enabled = false;
  57. player2Input.Enabled = true;
  58. player2Button.Enabled = true;
  59. player1Input.PasswordChar = '\0';
  60. post.Text = "請" + player2Name.Text + "輸入厲害的數字(不要給對手知道!)";
  61. }
  62. break;
  63. case 2:
  64. if (player1Input.Text.ToCharArray().Length < 4)
  65. {
  66. MessageBox.Show("請輸入四位數!");
  67. }
  68. else
  69. {
  70. var tmp = player1Input.Text.ToCharArray();
  71. int[] output = new int[tmp.Length];
  72. for (int i = 0; i < tmp.Length; i++)
  73. {
  74. output[i] = Math.Abs(tmp[i] - player2Value[i]);
  75. }
  76. player1Output2.Text = "你剛剛猜:" + player1Input.Text + " 差:" + string.Concat(output);
  77. if (string.Concat(output) == "0000")
  78. {
  79. player1Input.Enabled = false;
  80. player1Button.Enabled = false;
  81. player1Output.Text = string.Concat(player1Value);
  82. player2Output.Text = string.Concat(player2Value);
  83. MessageBox.Show(player1Name.Text + "獲勝!" + "\n點擊確認再來一局!");
  84. flow = 0;
  85. turns = 1;
  86. player1Input.MaxLength = 20;
  87. player2Input.MaxLength = 20;
  88. player1Input.Enabled = true;
  89. player1Button.Enabled = true;
  90. player1Name.Text = player1Output.Text = player1Output2.Text = player1Input.Text =
  91. player2Name.Text = player2Output.Text = player2Output2.Text = player2Input.Text = "";
  92. post.Text = "請輸入玩家1名稱";
  93. }
  94. else
  95. {
  96. player1Input.Enabled = false;
  97. player1Button.Enabled = false;
  98. player2Input.Enabled = true;
  99. player2Button.Enabled = true;
  100. player2Input.Text = "";
  101. turns++;
  102. post.Text = player2Name.Text + "的回合(第" + turns + "回合)";
  103. }
  104. }
  105. break;
  106. default:
  107. break;
  108. }
  109.  
  110. }
  111.  
  112. private void player2Button_Click(object sender, EventArgs e)
  113. {
  114. switch (flow)
  115. {
  116. case 0:
  117. if (player2Input.Text == "")
  118. {
  119. MessageBox.Show("請輸入名稱!");
  120. }
  121. else
  122. {
  123. player2Name.Text = player2Input.Text;
  124. player2Input.Text = "";
  125. player2Input.Enabled = false;
  126. player2Button.Enabled = false;
  127. player1Input.Enabled = true;
  128. player1Button.Enabled = true;
  129. player2Input.MaxLength = 4;
  130. flow++;
  131. player2Input.PasswordChar = '*';
  132. post.Text = "請" + player1Name.Text + "輸入厲害的數字(不要給對手知道!)";
  133. }
  134. break;
  135. case 1:
  136. if (player2Input.Text.ToCharArray().Length < 4)
  137. {
  138. MessageBox.Show("請輸入四位數!");
  139. }
  140. else
  141. {
  142. player2Value = player2Input.Text.ToCharArray();
  143. player2Output.Text = "****";
  144. player2Input.Text = "";
  145. player2Input.Enabled = false;
  146. player2Button.Enabled = false;
  147. player1Input.Enabled = true;
  148. player1Button.Enabled = true;
  149. player2Input.PasswordChar = '\0';
  150. post.Text = player1Name.Text + "的回合(第" + turns + "回合)";
  151. flow++;
  152. }
  153. break;
  154. case 2:
  155. if (player2Input.Text.ToCharArray().Length < 4)
  156. {
  157. MessageBox.Show("請輸入四位數!");
  158. }
  159. else
  160. {
  161. var tmp = player2Input.Text.ToCharArray();
  162. int[] output = new int[tmp.Length];
  163. for (int i = 0; i < tmp.Length; i++)
  164. {
  165. output[i] = Math.Abs(tmp[i] - player1Value[i]);
  166. }
  167. player2Output2.Text = "你剛剛猜:" + player2Input.Text + " 差:" + string.Concat(output);
  168. if (string.Concat(output) == "0000")
  169. {
  170. player2Input.Enabled = false;
  171. player2Button.Enabled = false;
  172. player1Output.Text = string.Concat(player1Value);
  173. player2Output.Text = string.Concat(player2Value);
  174. MessageBox.Show(player2Name.Text + "獲勝!" + "\n點擊確認再來一局!");
  175. flow = 0;
  176. turns = 1;
  177. player1Input.MaxLength = 20;
  178. player2Input.MaxLength = 20;
  179. player1Input.Enabled = true;
  180. player1Button.Enabled = true;
  181. player1Name.Text = player1Output.Text = player1Output2.Text = player1Input.Text =
  182. player2Name.Text = player2Output.Text = player2Output2.Text = player2Input.Text = "";
  183. post.Text = "請輸入玩家1名稱";
  184. }
  185. else
  186. {
  187. player2Input.Enabled = false;
  188. player2Button.Enabled = false;
  189. player1Input.Enabled = true;
  190. player1Button.Enabled = true;
  191. player1Input.Text = "";
  192. turns++;
  193. post.Text = player1Name.Text + "的回合(第" + turns + "回合)";
  194. }
  195. }
  196. break;
  197. default:
  198. break;
  199. }
  200. }
  201.  
  202. private void player1Input_KeyPress(object sender, KeyPressEventArgs e)
  203. {
  204. if (flow == 1 || flow == 2)
  205. {
  206. if ((int)e.KeyChar <= 31)
  207. {
  208. player1Ouput3.Visible = false;
  209. return;
  210. }
  211. if (!char.IsDigit(e.KeyChar))
  212. {
  213. e.Handled = true;
  214. System.Media.SystemSounds.Beep.Play();
  215. player1Ouput3.Visible = true;
  216. return;
  217. }
  218. else
  219. {
  220. player1Ouput3.Visible = false;
  221. }
  222. }
  223. }
  224.  
  225. private void player2Input_KeyPress(object sender, KeyPressEventArgs e)
  226. {
  227. if (flow == 1 || flow == 2)
  228. {
  229. if ((int)e.KeyChar <= 31)
  230. {
  231. player2Ouput3.Visible = false;
  232. return;
  233. }
  234. if (!char.IsDigit(e.KeyChar))
  235. {
  236. e.Handled = true;
  237. System.Media.SystemSounds.Beep.Play();
  238. player2Ouput3.Visible = true;
  239. return;
  240. }
  241. else
  242. {
  243. player2Ouput3.Visible = false;
  244. }
  245. }
  246. }
  247.  
  248.  
  249.  
  250.  
  251. }
  252. }
Advertisement
Add Comment
Please, Sign In to add comment