Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.68 KB | None | 0 0
  1.  
  2.  
  3.  
  4.  
  5. using System.Windows.Forms;
  6. using System.Drawing;
  7. using System;
  8.  
  9. namespace Reversi
  10. {
  11. static class Program
  12. {
  13. static void Main()
  14. {
  15. Application.Run(new Scherm());
  16. }
  17.  
  18. class Scherm : Form
  19. {
  20. /* Declaraties */
  21. Button startknop;
  22. Button helpknop;
  23. Label roodlabel;
  24. Label blauwlabel;
  25. Label statuslabel;
  26.  
  27. /* Globale Variabelen */
  28. // Strings
  29. // Constants
  30. const int schermgrootte = 12, vakgrootte = 40;
  31. // Ints
  32. int aantal = 4, aantalrood = 0, aantalblauw = 0, kleur = 1 // 0 = leeg, 1 = blauw, 2 = rood
  33. , maxaantal = schermgrootte * schermgrootte;
  34. int mouseX, mouseY;
  35. // Arrays
  36. int[,] ganzenbord;
  37. // Bools
  38. bool rest = false, help = false;
  39.  
  40.  
  41.  
  42. public Scherm()
  43. {
  44. /* Objecten aanmaken */
  45. this.startknop = new Button();
  46. this.helpknop = new Button();
  47. this.roodlabel = new Label();
  48. this.blauwlabel = new Label();
  49. this.statuslabel = new Label();
  50.  
  51. /* Eigenschappen van objecten */
  52.  
  53. // startknop
  54. this.startknop.Location = new Point(12, 12);
  55. this.startknop.Name = "startknop";
  56. this.startknop.Size = new Size(78, 29);
  57. this.startknop.TabIndex = 0;
  58. this.startknop.Text = "Nieuw spel";
  59.  
  60. // helpknop
  61. this.helpknop.Location = new Point(96, 12);
  62. this.helpknop.Name = "helpknop";
  63. this.helpknop.Size = new Size(78, 29);
  64. this.helpknop.TabIndex = 1;
  65. this.helpknop.Text = "Halp!";
  66.  
  67. // roodlabel
  68. this.roodlabel.AutoSize = true;
  69. this.roodlabel.Location = new Point(392, 20);
  70. this.roodlabel.Name = "roodlabel";
  71. this.roodlabel.Size = new Size(40, 13);
  72. this.roodlabel.TabIndex = 2;
  73. this.roodlabel.Text = "2 Stenen";
  74.  
  75. // blauwlabel
  76. this.blauwlabel.AutoSize = true;
  77. this.blauwlabel.Location = new Point(266, 19);
  78. this.blauwlabel.Name = "blauwlabel";
  79. this.blauwlabel.Size = new Size(40, 13);
  80. this.blauwlabel.TabIndex = 3;
  81. this.blauwlabel.Text = "2 Stenen";
  82.  
  83. // statuslabel
  84. this.statuslabel.AutoSize = true;
  85. this.statuslabel.Location = new Point(13, 72);
  86. this.statuslabel.Name = "statuslabel";
  87. this.statuslabel.Size = new Size(35, 13);
  88. this.statuslabel.TabIndex = 4;
  89. this.statuslabel.Text = "Blauw is nu aan zet.";
  90.  
  91. // Form1
  92. this.Controls.Add(this.statuslabel);
  93. this.Controls.Add(this.blauwlabel);
  94. this.Controls.Add(this.roodlabel);
  95. this.Controls.Add(this.helpknop);
  96. this.Controls.Add(this.startknop);
  97. this.Text = "Reversi door Conrad en Inigo";
  98.  
  99. if (schermgrootte >= 8 && schermgrootte <= 10)
  100. {
  101. this.ClientSize = new Size(450, 450 + (schermgrootte - 8) * vakgrootte);
  102. }
  103. else if (schermgrootte >= 11)
  104. {
  105. this.ClientSize = new Size(370 + (schermgrootte - 8) * vakgrootte, 450 + (schermgrootte - 8) * vakgrootte);
  106. }
  107. else
  108. {
  109. this.ClientSize = new Size(450, 115 + (schermgrootte * vakgrootte));
  110. }
  111.  
  112.  
  113.  
  114. // Events
  115. this.Paint += this.tekenScherm;
  116. this.MouseClick += this.klik;
  117. this.startknop.Click += this.nieuwspel;
  118. this.helpknop.Click += this.helpklik;
  119.  
  120. // Arrays
  121. this.ganzenbord = new int[schermgrootte, schermgrootte];
  122.  
  123. int tellertje;
  124. int tellertje2;
  125. //initialiseren van het bord, alles op 0 zetten (geen stenen) behalve die eerste 4 stenen.
  126. for (tellertje = 0; tellertje < schermgrootte; tellertje++)
  127. {
  128. for (tellertje2 = 0; tellertje2 < schermgrootte; tellertje2++)
  129. {
  130. if (tellertje == (schermgrootte / 2) - 1 && tellertje2 == (schermgrootte / 2) - 1)
  131. ganzenbord[tellertje, tellertje2] = 1;
  132. else if (tellertje == (schermgrootte / 2) && tellertje2 == (schermgrootte / 2))
  133. ganzenbord[tellertje, tellertje2] = 1;
  134. else if (tellertje == (schermgrootte / 2) - 1 && tellertje2 == (schermgrootte / 2))
  135. ganzenbord[tellertje, tellertje2] = 2;
  136. else if (tellertje == (schermgrootte / 2) && tellertje2 == (schermgrootte / 2) - 1)
  137. ganzenbord[tellertje, tellertje2] = 2;
  138. else ganzenbord[tellertje, tellertje2] = 0;
  139.  
  140. }
  141.  
  142.  
  143. }
  144.  
  145.  
  146. }
  147.  
  148. public void helpklik(object o, EventArgs e)
  149. {
  150. if (help == true)
  151. {
  152. help = false;
  153. Invalidate();
  154. }
  155. else if (help == false)
  156. {
  157. help = true;
  158. Invalidate();
  159. }
  160. }
  161.  
  162. public void nieuwspel(object o, EventArgs e)
  163. {
  164. int t = 0; int t2 = 0;
  165. kleur = 1; this.statuslabel.Text = "Blauw is nu aan zet.";
  166. aantalrood = 0;
  167. aantalblauw = 0;
  168. help = false;
  169.  
  170. aantal = 4;
  171. for (t = 0; t < schermgrootte; t++)
  172. {
  173. for (t2 = 0; t2 < schermgrootte; t2++)
  174. {
  175. if (t == (schermgrootte / 2) - 1 && t2 == (schermgrootte / 2) - 1)
  176. ganzenbord[t, t2] = 1;
  177. else if (t == (schermgrootte / 2) && t2 == (schermgrootte / 2))
  178. ganzenbord[t, t2] = 1;
  179. else if (t == (schermgrootte / 2) - 1 && t2 == (schermgrootte / 2))
  180. ganzenbord[t, t2] = 2;
  181. else if (t == (schermgrootte / 2) && t2 == (schermgrootte / 2) - 1)
  182. ganzenbord[t, t2] = 2;
  183. else ganzenbord[t, t2] = 0;
  184.  
  185. }
  186.  
  187.  
  188. }
  189. Invalidate();
  190. }
  191.  
  192. public void tekenScherm(object o, PaintEventArgs e)
  193. {
  194.  
  195. Graphics gr = e.Graphics;
  196. int x = 15, y = 100, teller, teller2;
  197. tekenRondje(gr, 345, 5, 2);
  198. tekenRondje(gr, 220, 5, 1);
  199. //Bijhouden van hoeveel stenen van iedere kleur zich op het bord bevinden.
  200. stenenTellen();
  201.  
  202. for (teller = 0; teller < schermgrootte; teller++)
  203. {
  204. tekenvakje(gr, x, y);
  205. for (teller2 = 0; teller2 < schermgrootte; teller2++)
  206. {
  207. tekenvakje(gr, x, y);
  208. tekenRondje(gr, x, y, ganzenbord[teller, teller2]);
  209. y = y + vakgrootte;
  210.  
  211. }
  212. x = x + vakgrootte;
  213.  
  214.  
  215. y = 100;
  216. }
  217.  
  218. volgendeZet();
  219. if (help)
  220. tekenHelpvakjes(gr);
  221.  
  222.  
  223. }
  224.  
  225.  
  226. //Tekent in de vakken waar de huidige kleur kan zetten (alleen als help knop geklikt is)
  227. public void tekenHelpvakjes(Graphics gr)
  228. {
  229.  
  230. int x = 15;
  231. int y = 100;
  232. for (int t1 = 0; t1 < schermgrootte; t1++)
  233. {
  234. for (int t2 = 0; t2 < schermgrootte; t2++)
  235. {
  236. if (checkspot(t1, t2, kleur, false) == true && ganzenbord[t1, t2] == 0)
  237. {
  238. gr.DrawEllipse(Pens.Gray, x, y, vakgrootte, vakgrootte);
  239. }
  240. y = y + vakgrootte;
  241. }
  242. x = x + vakgrootte;
  243. y = 100;
  244. }
  245.  
  246. }
  247.  
  248.  
  249. //telt de stenen en zet de juiste getallen in de labels.
  250. public void stenenTellen()
  251. {
  252. aantalrood = 0;
  253. aantalblauw = 0;
  254. int teller, teller2;
  255.  
  256. for (teller = 0; teller < schermgrootte; teller++)
  257. {
  258. for (teller2 = 0; teller2 < schermgrootte; teller2++)
  259. {
  260. if (ganzenbord[teller, teller2] == 1) aantalblauw++;
  261. else if (ganzenbord[teller, teller2] == 2) aantalrood++;
  262. }
  263. }
  264. this.blauwlabel.Text = aantalblauw + " Stenen";
  265. this.roodlabel.Text = aantalrood + " Stenen";
  266. }
  267.  
  268.  
  269.  
  270. public void volgendeZet()
  271. {
  272.  
  273. checkzetten(); //checkt of de huidige kleur wel kan zetten
  274. if (aantal == maxaantal) // Het spel is afgelopen als het bord vol is.
  275. {
  276. eindigspel();
  277. }
  278. else if (rest == false)//is het bord niet vol, dan actie ondernemen als de huidige kleur niet kan zetten
  279. {
  280. switch (kleur)
  281. {
  282. case 1: kleur = 2;
  283. checkzetten();
  284. if (rest == true)
  285. this.statuslabel.Text = "Rood is nu aan zet";
  286. else
  287. eindigspel();
  288. break;
  289.  
  290. case 2: kleur = 1;
  291. checkzetten();
  292. if (rest == true)
  293. this.statuslabel.Text = "Blauw is nu aan zet";
  294. else
  295. eindigspel();
  296. break;
  297. }
  298.  
  299.  
  300.  
  301. }
  302. }
  303.  
  304.  
  305.  
  306. //Stenen van iedere kleur op het bord tellen.
  307. public void eindigspel()
  308. {
  309. if (aantalblauw == aantalrood)
  310. this.statuslabel.Text = "Gelijkspel!";
  311. if (aantalblauw > aantalrood)
  312. this.statuslabel.Text = "Blauw wint!";
  313. if (aantalblauw < aantalrood)
  314. this.statuslabel.Text = "Rood wint!";
  315. }
  316.  
  317.  
  318.  
  319. //Checkt of de huidige kleur kan zetten
  320. public void checkzetten()
  321. {
  322. rest = false;
  323. int teller = 0; int teller2 = 0;
  324. for (teller = 0; teller < schermgrootte; teller++)
  325. {
  326. for (teller2 = 0; teller2 < schermgrootte; teller2++)
  327. {
  328. if (ganzenbord[teller, teller2] == 0)
  329. checkspot(teller, teller2, kleur, false);
  330. }
  331. }
  332.  
  333. }
  334.  
  335.  
  336. //Tekent het spelvlak
  337. public void tekenvakje(Graphics gr, int x, int y)
  338. {
  339. gr.FillRectangle(Brushes.Khaki, x, y, vakgrootte, vakgrootte);
  340. gr.DrawRectangle(Pens.Black, x, y, vakgrootte, vakgrootte);
  341. }
  342.  
  343.  
  344. //Klikevent methode
  345. public void klik(object o, MouseEventArgs e)
  346. {
  347. rest = false;
  348.  
  349.  
  350. if (e.X >= 15 && e.Y >= 100 && e.X <= (15 + vakgrootte * schermgrootte - 1) && e.Y <= (100 + vakgrootte * schermgrootte - 1))
  351. {
  352. if (aantal < maxaantal)
  353. {
  354. mouseX = ((e.X - 15) / vakgrootte * vakgrootte);
  355. mouseY = ((e.Y - 100) / vakgrootte * vakgrootte);
  356. if (ganzenbord[(mouseX) / vakgrootte, (mouseY) / vakgrootte] == 0 && checkspot((mouseX / vakgrootte), (mouseY / vakgrootte), kleur, true) == true)
  357. {
  358. if (kleur == 1)
  359. {
  360. ganzenbord[mouseX / vakgrootte, mouseY / vakgrootte] = kleur;
  361. kleur = 2;
  362. this.statuslabel.Text = "Rood is nu aan zet";
  363. }
  364. else if (kleur == 2)
  365. {
  366. ganzenbord[mouseX / vakgrootte, mouseY / vakgrootte] = kleur;
  367. kleur = 1;
  368. this.statuslabel.Text = "Blauw is nu aan zet";
  369. }
  370. aantal++;
  371.  
  372. Invalidate();
  373. }
  374.  
  375. }
  376.  
  377.  
  378. }
  379. }
  380.  
  381.  
  382. //Bekijkt of op het geklikte vakje een steen gezet mag worden.
  383. public bool checkspot(int mousex, int mousey, int kleuraanzet, bool verander)
  384. {
  385. bool res = false;
  386.  
  387. // Check boven
  388. int t;
  389. for (t = 1; t < mousey && (ganzenbord[mousex, mousey - t] != 0 && ganzenbord[mousex, mousey - t] != kleuraanzet); t++)
  390. {
  391.  
  392. }
  393. if ((mousey - t) >= 0 && ganzenbord[mousex, mousey - t] == kleuraanzet && t > 1)
  394. {
  395. res = true;
  396. if (verander)
  397. {
  398. int t2;
  399. for (t2 = 1; t2 < t; t2++)
  400. {
  401. ganzenbord[mousex, mousey - t2] = kleuraanzet;
  402. }
  403. }
  404. }
  405.  
  406. if (res == true)
  407. rest = true;
  408. // Check rechts
  409.  
  410. for (t = 1; t < schermgrootte - mousex && (ganzenbord[mousex + t, mousey] != 0 && ganzenbord[mousex + t, mousey] != kleuraanzet); t++)
  411. {
  412.  
  413. }
  414. if ((mousex + t) <= (schermgrootte - 1) && ganzenbord[mousex + t, mousey] == kleuraanzet && t > 1)
  415. {
  416. res = true;
  417. if (verander)
  418. {
  419. int t2;
  420. for (t2 = 1; t2 < t; t2++)
  421. {
  422. ganzenbord[mousex + t2, mousey] = kleuraanzet;
  423. }
  424. }
  425. }
  426. if (res == true)
  427. rest = true;
  428. // Check links
  429.  
  430. for (t = 1; t < mousex && (ganzenbord[mousex - t, mousey] != 0 && ganzenbord[mousex - t, mousey] != kleuraanzet); t++)
  431. {
  432.  
  433. }
  434. if ((mousex - t) >= 0 && ganzenbord[mousex - t, mousey] == kleuraanzet && t > 1)
  435. {
  436. res = true;
  437. if (verander)
  438. {
  439. int t2;
  440. for (t2 = 1; t2 < t; t2++)
  441. {
  442. ganzenbord[mousex - t2, mousey] = kleuraanzet;
  443. }
  444. }
  445. }
  446. if (res == true)
  447. rest = true;
  448. // Check onder
  449.  
  450. for (t = 1; t < schermgrootte - mousey && (ganzenbord[mousex, mousey + t] != 0 && ganzenbord[mousex, mousey + t] != kleuraanzet); t++)
  451. {
  452.  
  453. }
  454. if ((mousey + t) <= (schermgrootte - 1) && ganzenbord[mousex, mousey + t] == kleuraanzet && t > 1)
  455. {
  456. res = true;
  457. if (verander)
  458. {
  459. int t2;
  460. for (t2 = 1; t2 < t; t2++)
  461. {
  462. ganzenbord[mousex, mousey + t2] = kleuraanzet;
  463. }
  464. }
  465. }
  466. if (res == true)
  467. rest = true;
  468. // Check rechtsboven
  469.  
  470. for (t = 1; t < mousey && t < schermgrootte - mousex && (ganzenbord[mousex + t, mousey - t] != 0 && ganzenbord[mousex + t, mousey - t] != kleuraanzet); t++)
  471. {
  472.  
  473. }
  474. if ((mousey - t) >= 0 && (mousex + t) <= (schermgrootte - 1) && ganzenbord[mousex + t, mousey - t] == kleuraanzet && t > 1)
  475. {
  476. res = true;
  477. if (verander)
  478. {
  479. int t2;
  480. for (t2 = 1; t2 < t; t2++)
  481. {
  482. ganzenbord[mousex + t2, mousey - t2] = kleuraanzet;
  483. }
  484. }
  485. }
  486. if (res == true)
  487. rest = true;
  488. // Check linksboven
  489.  
  490. for (t = 1; t < mousey && t < mousex && (ganzenbord[mousex - t, mousey - t] != 0 && ganzenbord[mousex - t, mousey - t] != kleuraanzet); t++)
  491. {
  492.  
  493. }
  494. if ((mousey - t) >= 0 && (mousex - t) >= 0 && ganzenbord[mousex - t, mousey - t] == kleuraanzet && t > 1)
  495. {
  496. res = true;
  497. if (verander)
  498. {
  499. int t2;
  500. for (t2 = 1; t2 < t; t2++)
  501. {
  502. ganzenbord[mousex - t2, mousey - t2] = kleuraanzet;
  503. }
  504. }
  505. }
  506. if (res == true)
  507. rest = true;
  508.  
  509. // Check linksonder
  510.  
  511. for (t = 1; t < schermgrootte - mousey && t < mousex && (ganzenbord[mousex - t, mousey + t] != 0 && ganzenbord[mousex - t, mousey + t] != kleuraanzet); t++)
  512. {
  513.  
  514. }
  515. if ((mousex - t) >= 0 && mousey + t <= (schermgrootte - 1) && ganzenbord[mousex - t, mousey + t] == kleuraanzet && t > 1)
  516. {
  517. res = true;
  518. if (verander)
  519. {
  520. int t2;
  521. for (t2 = 1; t2 < t; t2++)
  522. {
  523. ganzenbord[mousex - t2, mousey + t2] = kleuraanzet;
  524. }
  525. }
  526. }
  527. if (res == true)
  528. rest = true;
  529. // Check rechtsonder
  530.  
  531. for (t = 1; t < schermgrootte - mousey && t < schermgrootte - mousex && (ganzenbord[mousex + t, mousey + t] != 0 && ganzenbord[mousex + t, mousey + t] != kleuraanzet); t++)
  532. {
  533.  
  534. }
  535. if ((mousex + t) <= (schermgrootte - 1) && (mousey + t) <= (schermgrootte - 1) && ganzenbord[mousex + t, mousey + t] == kleuraanzet && t > 1)
  536. {
  537. res = true;
  538. if (verander)
  539. {
  540. int t2;
  541. for (t2 = 1; t2 < t; t2++)
  542. {
  543. ganzenbord[mousex + t2, mousey + t2] = kleuraanzet;
  544. }
  545. }
  546. }
  547.  
  548. if (res == true)
  549. rest = true;
  550. // Return aan het eind
  551. return res;
  552. }
  553.  
  554. //Tekent de steen
  555. public void tekenRondje(Graphics gr, int x, int y, int kleurwaarde)
  556. {
  557. if (kleurwaarde == 1)
  558. gr.FillEllipse(Brushes.Blue, x, y, vakgrootte, vakgrootte);
  559. else if (kleurwaarde == 2)
  560. gr.FillEllipse(Brushes.Red, x, y, vakgrootte, vakgrootte);
  561. }
  562.  
  563. }
  564. }
  565. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement