Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.07 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 WindowsFormsApplication1
  12. {
  13. public partial class Form1 : Form
  14. {
  15. bool Beurt = true;
  16. int Beurtenteller = 0;
  17. bool TegenComputer = false;
  18.  
  19. public Form1()
  20. {
  21. InitializeComponent();
  22. }
  23.  
  24. private void spelVerlatenToolStripMenuItem_Click(object sender, EventArgs e)
  25. {
  26. Application.Exit();
  27. }
  28.  
  29. private void Button_Click(object sender, EventArgs e)
  30. {
  31.  
  32. Button b = (Button)sender;
  33. if (Beurt)
  34. b.Text = "X";
  35. else
  36. b.Text = "O";
  37.  
  38. Beurt = !Beurt;
  39. b.Enabled = false;
  40. Beurtenteller = Beurtenteller + 1;
  41.  
  42. Kijkenvoorwinnaar();
  43.  
  44. //Kijken of je tegen de computer speelt
  45. if ((!Beurt) && (TegenComputer))
  46. {
  47. ComputerMaaktZet();
  48. }
  49.  
  50. }
  51.  
  52. private void Kijkenvoorwinnaar()
  53. {
  54. Boolean Eriseenwinnaar = false;
  55.  
  56. //Horizontaal
  57. if ((A1.Text == A2.Text) && (A2.Text == A3.Text) && (!A1.Enabled))
  58. Eriseenwinnaar = true;
  59. else if ((B1.Text == B2.Text) && (B2.Text == B3.Text) && (!B1.Enabled))
  60. Eriseenwinnaar = true;
  61. else if ((C1.Text == C2.Text) && (C2.Text == C3.Text) && (!C1.Enabled))
  62. Eriseenwinnaar = true;
  63.  
  64. //Verticaal
  65. if ((A1.Text == B1.Text) && (B1.Text == C1.Text) && (!A1.Enabled))
  66. Eriseenwinnaar = true;
  67. else if ((A2.Text == B2.Text) && (B2.Text == C2.Text) && (!A2.Enabled))
  68. Eriseenwinnaar = true;
  69. else if ((A3.Text == B3.Text) && (B3.Text == C3.Text) && (!A3.Enabled))
  70. Eriseenwinnaar = true;
  71.  
  72. //Diagonaal
  73. if ((A1.Text == B2.Text) && (B2.Text == C3.Text) && (!A1.Enabled))
  74. Eriseenwinnaar = true;
  75. else if ((A3.Text == B2.Text) && (B2.Text == C1.Text) && (!A3.Enabled))
  76. Eriseenwinnaar = true;
  77.  
  78.  
  79. if(Eriseenwinnaar)
  80. {
  81. ButtonsAfsluiten();
  82.  
  83. String Winnaar = "";
  84. if (Beurt)
  85. {
  86. Winnaar = Player2.Text;
  87. OGewonnen.Text = (Int32.Parse(OGewonnen.Text) + 1).ToString();
  88. }
  89.  
  90. else
  91. {
  92. Winnaar = Player1.Text;
  93. XGewonnen.Text = (Int32.Parse(XGewonnen.Text) + 1).ToString();
  94. }
  95.  
  96. MessageBox.Show(Winnaar + " Heeft gewonnen!", " Gefeliciteerd!");
  97. }// einde if
  98. else
  99. {
  100. if (Beurtenteller == 9)
  101. {
  102. Gelijk.Text = (Int32.Parse(Gelijk.Text) + 1).ToString();
  103. MessageBox.Show("Gelijk!", "Jammer!");
  104. }
  105.  
  106. }
  107.  
  108. }// Einde Kijkenvoorwinnaar
  109.  
  110. private void ButtonsAfsluiten()
  111. {
  112. try
  113. {
  114. foreach (Control c in Controls)
  115. {
  116. Button b = (Button)c;
  117. b.Enabled = false;
  118. }// Einde Foreach
  119. }// einde Try
  120. catch { }
  121. }// Einde ButtonsAfsluiten
  122.  
  123. private void nieuwSpelToolStripMenuItem_Click(object sender, EventArgs e)
  124. {
  125. Beurt = true;
  126. Beurtenteller = 0;
  127.  
  128.  
  129. foreach (Control c in Controls)
  130. {
  131. try
  132. {
  133. Button b = (Button)c;
  134. b.Enabled = true;
  135. b.Text = "";
  136. }// einde Try
  137. catch { }
  138. }// Einde Foreach
  139.  
  140. }
  141.  
  142. private void Button_Enter(object sender, EventArgs e)
  143. {
  144. Button b = (Button)sender;
  145. if (b.Enabled)
  146. {
  147. if (Beurt)
  148.  
  149. b.Text = "X";
  150.  
  151. else
  152. b.Text = "O";
  153. } //einde If
  154. }
  155.  
  156. private void Button_Leave(object sender, EventArgs e)
  157. {
  158. Button b = (Button)sender;
  159. if (b.Enabled)
  160. {
  161. b.Text = "";
  162. } // einde If
  163. }
  164.  
  165. private void label2_Click(object sender, EventArgs e)
  166. {
  167.  
  168. }
  169.  
  170. private void label3_Click(object sender, EventArgs e)
  171. {
  172.  
  173. }
  174.  
  175. private void label6_Click(object sender, EventArgs e)
  176. {
  177.  
  178. }
  179.  
  180. private void Form1_Load(object sender, EventArgs e)
  181. {
  182. MessageBox.Show("Hallo, Het spel staat automatisch opgesteld voor 2 personen, om tegen de computer te spelen typ computer in het vak van speler doet dit VOOR je de eerste zet maakt");
  183. }
  184.  
  185. private void resetScoresToolStripMenuItem_Click(object sender, EventArgs e)
  186. {
  187. XGewonnen.Text = "0";
  188. OGewonnen.Text = "0";
  189. Gelijk.Text = "0";
  190. }
  191.  
  192. private void Player2_TextChanged(object sender, EventArgs e)
  193. {
  194. if (Player2.Text.ToUpper() == "COMPUTER")
  195. TegenComputer = true;
  196. else
  197. TegenComputer = false;
  198. }
  199.  
  200. private void standaardSpeelinstellingToolStripMenuItem_Click(object sender, EventArgs e)
  201. {
  202. Player1.Text = "Rico";
  203. Player2.Text = "Computer";
  204. }
  205.  
  206. private void ComputerMaaktZet()
  207. {
  208.  
  209.  
  210. Button move = null;
  211.  
  212. //Kijken voor mogelijkheden
  213. move = WinOfBlokZoeken("O"); //Zoek een winkans
  214. if (move == null)
  215. {
  216. move = WinOfBlokZoeken("X"); //Zoek een blok
  217. if (move == null)
  218. {
  219. move = HoekZoeken();
  220. if (move == null)
  221. {
  222. move = OpenPlekZoeken();
  223. }//end if
  224. }//end if
  225. }//end if
  226.  
  227. move.PerformClick();
  228. }
  229.  
  230. private Button OpenPlekZoeken()
  231. {
  232. Console.WriteLine("Looking for open space");
  233. Button b = null;
  234. foreach (Control c in Controls)
  235. {
  236. b = c as Button;
  237. if (b != null)
  238. {
  239. if (b.Text == "")
  240. return b;
  241. }//end if
  242. }//end if
  243.  
  244. return null;
  245. }
  246.  
  247. private Button HoekZoeken()
  248. {
  249. Console.WriteLine("Looking for corner");
  250. if (A1.Text == "O")
  251. {
  252. if (A3.Text == "")
  253. return A3;
  254. if (C3.Text == "")
  255. return C3;
  256. if (C1.Text == "")
  257. return C1;
  258. }
  259.  
  260. if (A3.Text == "O")
  261. {
  262. if (A1.Text == "")
  263. return A1;
  264. if (C3.Text == "")
  265. return C3;
  266. if (C1.Text == "")
  267. return C1;
  268. }
  269.  
  270. if (C3.Text == "O")
  271. {
  272. if (A1.Text == "")
  273. return A3;
  274. if (A3.Text == "")
  275. return A3;
  276. if (C1.Text == "")
  277. return C1;
  278. }
  279.  
  280. if (C1.Text == "O")
  281. {
  282. if (A1.Text == "")
  283. return A3;
  284. if (A3.Text == "")
  285. return A3;
  286. if (C3.Text == "")
  287. return C3;
  288. }
  289.  
  290. if (A1.Text == "")
  291. return A1;
  292. if (A3.Text == "")
  293. return A3;
  294. if (C1.Text == "")
  295. return C1;
  296. if (C3.Text == "")
  297. return C3;
  298.  
  299. return null;
  300. }
  301.  
  302. private Button WinOfBlokZoeken(string mark)
  303. {
  304. Console.WriteLine("Kijk voor win of blok: " + mark);
  305. //Horizontaal
  306. if ((A1.Text == mark) && (A2.Text == mark) && (A3.Text == ""))
  307. return A3;
  308. if ((A2.Text == mark) && (A3.Text == mark) && (A1.Text == ""))
  309. return A1;
  310. if ((A1.Text == mark) && (A3.Text == mark) && (A2.Text == ""))
  311. return A2;
  312.  
  313. if ((B1.Text == mark) && (B2.Text == mark) && (B3.Text == ""))
  314. return B3;
  315. if ((B2.Text == mark) && (B3.Text == mark) && (B1.Text == ""))
  316. return B1;
  317. if ((B1.Text == mark) && (B3.Text == mark) && (B2.Text == ""))
  318. return B2;
  319.  
  320. if ((C1.Text == mark) && (C2.Text == mark) && (C3.Text == ""))
  321. return C3;
  322. if ((C2.Text == mark) && (C3.Text == mark) && (C1.Text == ""))
  323. return C1;
  324. if ((C1.Text == mark) && (C3.Text == mark) && (C2.Text == ""))
  325. return C2;
  326.  
  327. //Horizontaal
  328. if ((A1.Text == mark) && (B1.Text == mark) && (C1.Text == ""))
  329. return C1;
  330. if ((B1.Text == mark) && (C1.Text == mark) && (A1.Text == ""))
  331. return A1;
  332. if ((A1.Text == mark) && (C1.Text == mark) && (B1.Text == ""))
  333. return B1;
  334.  
  335. if ((A2.Text == mark) && (B2.Text == mark) && (C2.Text == ""))
  336. return C2;
  337. if ((B2.Text == mark) && (C2.Text == mark) && (A2.Text == ""))
  338. return A2;
  339. if ((A2.Text == mark) && (C2.Text == mark) && (B2.Text == ""))
  340. return B2;
  341.  
  342. if ((A3.Text == mark) && (B3.Text == mark) && (C3.Text == ""))
  343. return C3;
  344. if ((B3.Text == mark) && (C3.Text == mark) && (A3.Text == ""))
  345. return A3;
  346. if ((A3.Text == mark) && (C3.Text == mark) && (B3.Text == ""))
  347. return B3;
  348.  
  349. //Diagonaal
  350. if ((A1.Text == mark) && (B2.Text == mark) && (C3.Text == ""))
  351. return C3;
  352. if ((B2.Text == mark) && (C3.Text == mark) && (A1.Text == ""))
  353. return A1;
  354. if ((A1.Text == mark) && (C3.Text == mark) && (B2.Text == ""))
  355. return B2;
  356.  
  357. if ((A3.Text == mark) && (B2.Text == mark) && (C1.Text == ""))
  358. return C1;
  359. if ((B2.Text == mark) && (C1.Text == mark) && (A3.Text == ""))
  360. return A3;
  361. if ((A3.Text == mark) && (C1.Text == mark) && (B2.Text == ""))
  362. return B2;
  363.  
  364. return null;
  365. }
  366. }
  367. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement