Guest User

Untitled

a guest
Jan 19th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.97 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.Windows.Forms;
  9.  
  10. namespace Reversi3
  11. {
  12. public partial class Reversi : Form
  13. {
  14. int aantalx, aantaly, xpos, ypos, vierkant, beurt, ander, t, aantalrood, aantalblauw;
  15. int[,] bord;
  16. Pen p; Graphics gr;
  17.  
  18. public Reversi()
  19. {
  20. aantalx = 6; aantaly = 6; vierkant = 40;
  21. beurt = 1; ander = 2; aantalrood = 2; aantalblauw = 2;
  22.  
  23. InitializeComponent();
  24.  
  25. panel1.Paint += tekenBord;
  26. panel2.Paint += startRondjes;
  27.  
  28. mededelingBeurt(beurt);
  29. }
  30.  
  31. void tekenBord(Object o, PaintEventArgs pea)
  32. {
  33. p = new Pen(Brushes.Black);
  34. bord = new int[aantalx, aantaly];
  35.  
  36. panel1.Size = new Size(aantalx * vierkant + 1, aantaly * vierkant + 1);
  37.  
  38. for (int yArray = 0; yArray < aantaly; yArray++)
  39. {
  40. for (int xArray = 0; xArray < aantalx; xArray++)
  41. {
  42. pea.Graphics.DrawRectangle(p, xArray * vierkant, yArray * vierkant, vierkant, vierkant);
  43.  
  44. bord[xArray, yArray] = 0; //kleur is 'leeg'
  45. }
  46. }
  47.  
  48. // Startomgeving
  49. bord[aantalx / 2 - 1, aantaly / 2 - 1] = 2; tekenRondje(aantalx / 2 - 1, aantaly / 2 - 1);
  50. bord[aantalx / 2, aantaly / 2] = 2; tekenRondje(aantalx / 2, aantaly / 2);
  51. bord[aantalx / 2, aantaly / 2 - 1] = 1; tekenRondje(aantalx / 2, aantaly / 2 - 1);
  52. bord[aantalx / 2 - 1, aantaly / 2] = 1; tekenRondje(aantalx / 2 - 1, aantaly / 2);
  53.  
  54. label1.Text = aantalrood.ToString();
  55. label2.Text = aantalblauw.ToString();
  56.  
  57. // Invalidate();
  58. }
  59.  
  60. void startRondjes(Object o, PaintEventArgs pa)
  61. {
  62. panel2.Size = new Size(vierkant, 2 * vierkant + 2);
  63. pa.Graphics.FillEllipse(Brushes.Red, 0, 0, vierkant, vierkant);
  64. pa.Graphics.FillEllipse(Brushes.Blue, 0, vierkant + 2, vierkant, vierkant);
  65. }
  66.  
  67. void kleurRondje(int x, int y, int b)
  68. {
  69. if (bord[x, y] == 1 || bord[x,y] == 2)
  70. {
  71. MessageBox.Show("Dit veld is al bezet, probeer een andere.", "Bezet", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  72. }
  73. else
  74. {
  75. bord[x, y] = b;
  76.  
  77. if (beurt == 1)
  78. { beurt = 2; ander = 1; }
  79. else if (beurt == 2)
  80. { beurt = 1; ander = 2; }
  81.  
  82. mededelingBeurt(beurt);
  83. }
  84. }
  85.  
  86. void tekenRondje(int x, int y)
  87. {
  88. gr = panel1.CreateGraphics();
  89.  
  90. if (bord[x, y] == 1) //rood
  91. {
  92. gr.FillEllipse(Brushes.Red, x * vierkant, y * vierkant, vierkant, vierkant);
  93. }
  94. else if (bord[x, y] == 2) //blauw
  95. {
  96. gr.FillEllipse(Brushes.Blue, x * vierkant, y * vierkant, vierkant, vierkant);
  97. }
  98. }
  99.  
  100. void tekenHelp(int x, int y)
  101. {
  102. gr = panel1.CreateGraphics();
  103. gr.DrawEllipse(p, (x * vierkant) + 5, (y * vierkant) + 5, vierkant - 10, vierkant - 10);
  104. bord[x, y] = 3;
  105. }
  106.  
  107. void mededelingBeurt(int beurt)
  108. {
  109. if (beurt == 1)
  110. { label3.Text = "Rood is aan zet."; }
  111. else
  112. { label3.Text = "Blauw is aan zet."; }
  113. }
  114.  
  115. void checkHelp(bool sluit)
  116. {
  117. for (int y = 0; y < aantaly; y++)
  118. {
  119. for (int x = 0; x < aantalx; x++)
  120. {
  121. if (bord[x, y] == 0) //check alle lege velden in array
  122. {
  123. controleVelden(x, y, sluit);
  124. }
  125. }
  126. }
  127. }
  128.  
  129. int check(int x, int y, int teller)
  130. {
  131. if (x >= 0 && x < aantalx && y >= 0 && y < aantaly) // bord[x, y] kan niet buiten de array vallen
  132. {
  133. if (bord[x, y] == ander)
  134. {
  135. return 2; //er wordt een blauwe gevonden
  136. }
  137. else if (bord[x, y] == beurt && teller > 0)
  138. {
  139. return 1; //geldige zet
  140. }
  141. else
  142. {
  143. return 0; //ongeldige zet
  144. }
  145. }
  146. else
  147. {
  148. return 0; //valt buiten de array --> ongeldige zet
  149. }
  150. }
  151.  
  152. void controleVelden(int x, int y, bool sluit)
  153. {
  154. horizontaalrechts(x, y, sluit);
  155. Console.WriteLine("rechts");
  156. horizontaallinks(x, y, sluit);
  157. Console.WriteLine("links");
  158. verticaalomhoog(x, y, sluit);
  159. Console.WriteLine("omhoog");
  160. verticaalomlaag(x, y, sluit);
  161. Console.WriteLine("omlaag");
  162. diaglinksomhoog(x, y, sluit);
  163. Console.WriteLine("linksomhoog");
  164. diaglinksomlaag(x, y, sluit);
  165. Console.WriteLine("linksomlaag");
  166. diagrechtsomhoog(x, y, sluit);
  167. Console.WriteLine("rechtsomhoog");
  168. diagrechtsomlaag(x, y, sluit);
  169. Console.WriteLine("rechtsomlaag");
  170. }
  171.  
  172. #region Richtingen
  173.  
  174. void horizontaalrechts(int x, int y, bool sluit)
  175. {
  176. if (x < aantalx - 2 && bord[x + 1, y] != 0)
  177. {
  178. t = 0;
  179. for (int xcheck = x + 1; xcheck < (aantalx - 1) - x; xcheck++)
  180. {
  181. if (check(xcheck, y, t) == 2)
  182. {
  183. t++;
  184. }
  185. else if (check(xcheck, y, t) == 1 && sluit)
  186. {
  187. for (int n = 0; n < t; n++)
  188. {
  189. x++;
  190. bord[x, y] = beurt;
  191. tekenRondje(x, y);
  192. }
  193. return;
  194. }
  195. else if (check(xcheck, y, t) == 1)
  196. {
  197. tekenHelp(x, y);
  198. }
  199. else if (check(xcheck, y, t) == 0 && sluit)
  200. {
  201. //MessageBox.Show("Ongeldige zet.");
  202. }
  203. }
  204. }
  205. }
  206.  
  207. void horizontaallinks(int x, int y, bool sluit)
  208. {
  209. if (x - 2 >= 0 && bord[x - 1, y] != 0)
  210. {
  211. t = 0;
  212. for (int xcheck = x - 1; xcheck >= 0; xcheck--)
  213. {
  214. if (check(xcheck, y, t) == 2)
  215. {
  216. t++;
  217. }
  218. else if (check(xcheck, y, t) == 1 && sluit)
  219. {
  220. for (int n = 0; n < t; n++)
  221. {
  222. x--;
  223. bord[x, y] = beurt;
  224. tekenRondje(x, y);
  225. }
  226. return;
  227. }
  228. else if (check(xcheck, y, t) == 1)
  229. {
  230. tekenHelp(x, y);
  231. }
  232. else if (check(xcheck, y, t) == 0)
  233. {
  234. return;
  235. }
  236. }
  237. }
  238. }
  239.  
  240. void verticaalomhoog(int x, int y, bool sluit)
  241. {
  242. if (y - 1 >= 0 && bord[x, y - 1] != 0)
  243. {
  244. t = 0;
  245. for (int ycheck = y - 1; ycheck >= 0; ycheck--)
  246. {
  247. if (check(x, ycheck, t) == 2)
  248. {
  249. t++;
  250. }
  251. else if (check(x, ycheck, t) == 1 && sluit)
  252. {
  253. for (int n = 0; n < t; n++)
  254. {
  255. y--;
  256. bord[x, y] = beurt;
  257. tekenRondje(x, y);
  258. }
  259. return;
  260. }
  261. else if (check(x, ycheck, t) == 1)
  262. {
  263. tekenHelp(x, y);
  264. }
  265. else if (check(x, ycheck, t) == 0)
  266. {
  267. return;
  268. }
  269. }
  270. }
  271. }
  272.  
  273. void verticaalomlaag(int x, int y, bool sluit)
  274. {
  275. if (y < aantaly - 1 && bord[x, y + 1] != 0)
  276. {
  277. t = 0;
  278. for (int ycheck = y + 1; ycheck < (aantaly - 1) - y; ycheck++)
  279. {
  280. if (check(x, ycheck, t) == 2)
  281. {
  282. t++;
  283. }
  284. else if (check(x, ycheck, t) == 1 && sluit)
  285. {
  286. for (int n = 0; n < t; n++)
  287. {
  288. y++;
  289. bord[x, y] = beurt;
  290. tekenRondje(x, y);
  291. }
  292. return;
  293. }
  294. else if (check(x, ycheck, t) == 1)
  295. {
  296. tekenHelp(x, y);
  297. }
  298. else if (check(x, ycheck, t) == 0)
  299. {
  300. return;
  301. }
  302. }
  303. }
  304. }
  305.  
  306. void diaglinksomhoog(int x, int y, bool sluit)
  307. {
  308. if (x - 1 >= 0 && y - 1 >= 0 && bord[x - 1, y - 1] != 0)
  309. {
  310. int ycheck = y - 1;
  311. t = 0;
  312. for (int xcheck = x - 1; xcheck >= 0; xcheck--)
  313. {
  314. if (check(xcheck, ycheck, t) == 2)
  315. {
  316. t++;
  317. }
  318. else if (check(xcheck, ycheck, t) == 1 && sluit)
  319. {
  320. for (int n = 0; n < t; n++)
  321. {
  322. x--;
  323. y--;
  324. bord[x, y] = beurt;
  325. tekenRondje(x, y);
  326. }
  327. return;
  328. }
  329. else if (check(xcheck, ycheck, t) == 1)
  330. {
  331. tekenHelp(x, y);
  332. }
  333. else if (check(xcheck, ycheck, t) == 0)
  334. {
  335. return;
  336. }
  337. ycheck--;
  338. }
  339. }
  340. }
  341.  
  342. void diaglinksomlaag(int x, int y, bool sluit)
  343. {
  344. if (x - 1 >= 0 && y < aantaly - 1 && bord[x - 1, y + 1] != 0)
  345. {
  346. int ycheck = y + 1;
  347. t = 0;
  348. for (int xcheck = x - 1; xcheck >= 0; xcheck--)
  349. {
  350. if (check(xcheck, ycheck, t) == 2)
  351. {
  352. t++;
  353. }
  354. else if (check(xcheck, ycheck, t) == 1 && sluit)
  355. {
  356. for (int n = 0; n < t; n++)
  357. {
  358. x--;
  359. y++;
  360. bord[x, y] = beurt;
  361. tekenRondje(x, y);
  362. }
  363. return;
  364. }
  365. else if (check(xcheck, ycheck, t) == 1)
  366. {
  367. tekenHelp(x, y);
  368. }
  369. else if (check(xcheck, ycheck, t) == 0)
  370. {
  371. return;
  372. }
  373. ycheck++;
  374. }
  375. }
  376. }
  377.  
  378. void diagrechtsomhoog(int x, int y, bool sluit)
  379. {
  380. if (x < aantalx - 1 && y - 1 >= 0 && bord[x + 1, y - 1] != 0)
  381. {
  382. int ycheck = y - 1;
  383. t = 0;
  384. for (int xcheck = x + 1; xcheck < (aantalx - 1) - x; xcheck++)
  385. {
  386. if (check(xcheck, ycheck, t) == 2)
  387. {
  388. t++;
  389. }
  390. else if (check(xcheck, ycheck, t) == 1 && sluit)
  391. {
  392. for (int n = 0; n < t; n++)
  393. {
  394. x++;
  395. y--;
  396. bord[x, y] = beurt;
  397. tekenRondje(x, y);
  398. }
  399. return;
  400. }
  401. else if (check(xcheck, ycheck, t) == 1)
  402. {
  403. tekenHelp(x, y);
  404. }
  405. else if (check(xcheck, ycheck, t) == 0)
  406. {
  407. //return;
  408. }
  409. ycheck--;
  410. }
  411. }
  412. }
  413.  
  414. void diagrechtsomlaag(int x, int y, bool sluit)
  415. {
  416. if (x < aantalx - 1 && y < aantaly - 1 && bord[x + 1, y + 1] != 0)
  417. {
  418. int ycheck = y + 1;
  419. t = 0;
  420. for (int xcheck = x + 1; xcheck < (aantalx - 1) - x; xcheck++)
  421. {
  422. if (check(xcheck, ycheck, t) == 2)
  423. {
  424. t++;
  425. }
  426. else if (check(xcheck, ycheck, t) == 1 && sluit)
  427. {
  428. for (int n = 0; n < t; n++)
  429. {
  430. x++;
  431. y++;
  432. bord[x, y] = beurt;
  433. tekenRondje(x, y);
  434. }
  435. return;
  436. }
  437. else if (check(xcheck, ycheck, t) == 1)
  438. {
  439. tekenHelp(x, y);
  440. }
  441. else if (check(xcheck, ycheck, t) == 0)
  442. {
  443. return;
  444. }
  445. ycheck++;
  446. }
  447. }
  448. }
  449.  
  450. #endregion
  451.  
  452. bool winnaar()
  453. {
  454. for (int y = 0; y < aantaly; y++)
  455. {
  456. for (int x = 0; x < aantalx; x++)
  457. {
  458. if (bord[x, y] == 0)
  459. {
  460. return false;
  461. }
  462. }
  463. }
  464. return true;
  465. }
  466.  
  467. void eindespel()
  468. {
  469. if (winnaar() == true)
  470. {
  471. if (Convert.ToInt32(label1.Text) > Convert.ToInt32(label2.Text))
  472. {
  473. MessageBox.Show("Rood heeft gewonnen", "Gewonnen", MessageBoxButtons.OK, MessageBoxIcon.Information);
  474. return;
  475. }
  476.  
  477. else if (label1.Text == label2.Text)
  478. {
  479. MessageBox.Show("Het is een remise", "Remise", MessageBoxButtons.OK, MessageBoxIcon.Information);
  480. return;
  481. }
  482.  
  483. else
  484. {
  485. MessageBox.Show("Blauw heeft gewonnen", "Gewonnen", MessageBoxButtons.OK, MessageBoxIcon.Information);
  486. return;
  487. }
  488. }
  489. }
  490.  
  491. private void panel1_MouseClick(object sender, MouseEventArgs e)
  492. {
  493. Console.Clear();
  494. xpos = e.X / vierkant;
  495. ypos = e.Y / vierkant;
  496.  
  497. if (xpos > aantalx - 1)
  498. { xpos = aantalx - 1; }
  499. if (xpos < 0)
  500. { xpos = 0; }
  501. if (ypos > aantaly - 1)
  502. { ypos = aantaly - 1; }
  503. if (ypos < 0)
  504. { ypos = 0; }
  505.  
  506. controleVelden(xpos, ypos, true);
  507. kleurRondje(xpos, ypos, beurt);
  508. tekenRondje(xpos, ypos);
  509. wis();
  510. tel();
  511.  
  512. eindespel();
  513. }
  514.  
  515. void wis()
  516. {
  517. for (int x = 0; x < aantalx; x++)
  518. {
  519. for (int y = 0; y < aantaly; y++)
  520. {
  521. if (bord[x, y] == 3)
  522. {
  523. Graphics gr = panel1.CreateGraphics();
  524. gr.FillRectangle(SystemBrushes.Control, (x * vierkant) + 5, (y * vierkant) + 5, vierkant - 8, vierkant - 8);
  525. bord[x, y] = 0;
  526. }
  527. }
  528. }
  529. }
  530.  
  531. void tel()
  532. {
  533. aantalblauw = 0;
  534. aantalrood = 0;
  535.  
  536. for (int x = 0; x < aantalx; x++)
  537. {
  538. for (int y = 0; y < aantaly; y++)
  539. {
  540. if (bord[x, y] == 1)
  541. {
  542. aantalrood++;
  543. }
  544. else if (bord[x, y] == 2)
  545. {
  546. aantalblauw++;
  547. }
  548. }
  549. }
  550.  
  551. label1.Text = aantalrood.ToString();
  552. label2.Text = aantalblauw.ToString();
  553. }
  554.  
  555. private void button1_Click(object sender, EventArgs e)
  556. {
  557. beurt = 1; ander = 2;
  558. mededelingBeurt(beurt);
  559. aantalrood = 2;
  560. aantalblauw = 2;
  561. Refresh();
  562. }
  563.  
  564. private void button2_Click(object sender, EventArgs e)
  565. {
  566. checkHelp(false);
  567. }
  568. }
  569. }
Add Comment
Please, Sign In to add comment