Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.17 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using System.Windows.Forms;
  5.  
  6. namespace ReversiRaiz
  7. {
  8.     public partial class Form1 : Form
  9.     {
  10.         int zetten = 0;
  11.         int rood = 0;
  12.         int blauw = 0;
  13.  
  14.         int lastX = 0; int lastY = 0;
  15.         const int rijen = 6; //breedte
  16.         const int kolommen = 6; //hoogte
  17.         const int kleur = 1; //kleuren
  18.         const int legaal = 1;
  19.         int[,,] legaliteit = new int[kolommen, rijen, legaal];
  20.         int[,,] bordstand = new int[kolommen, rijen, kleur];
  21.  
  22.         Rectangle[,] posities = new Rectangle[kolommen, rijen];
  23.  
  24.         public Form1()
  25.         {
  26.             InitializeComponent();
  27.             panel1.Paint += TekenSteen;
  28.             this.DoubleBuffered = true;
  29.         }
  30.  
  31.         private void Form1_Load(object sender, EventArgs e)
  32.         {
  33.         }
  34.  
  35.         private void panel1_Paint(object sender, PaintEventArgs e)
  36.         {
  37.             // tekent de kolommen
  38.             for (int x = 0; x <= kolommen; x++)
  39.             {
  40.                 e.Graphics.DrawLine(Pens.Gray, x * ((panel1.Width) / kolommen), 0, x * ((panel1.Width) / kolommen), panel1.Height);
  41.             }
  42.  
  43.             // tekent de rijen
  44.             for (int y = 0; y <= rijen; y++)
  45.             {
  46.                 e.Graphics.DrawLine(Pens.Gray, 0, y * ((panel1.Height) / rijen), panel1.Width, y * ((panel1.Height) / rijen));
  47.             }
  48.  
  49.             // zorgt ervoor dat de juiste stenen op het bord staan
  50.             for (int x = 0; x < kolommen; x++)
  51.             {
  52.                 for (int y = 0; y < rijen; y++)
  53.                 {
  54.                     int lenhoog = panel1.Height / kolommen;
  55.                     int lenbreedt = panel1.Width / rijen;
  56.                     posities[x, y] = new Rectangle(x * lenhoog, y * lenbreedt, lenhoog, lenbreedt);
  57.                 }
  58.             }
  59.         }
  60.  
  61.         public void TekenSteen(object o, PaintEventArgs e)
  62.         {
  63.             CheckPlays();
  64.  
  65.             e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
  66.  
  67.  
  68.             try
  69.             {
  70.                 for (int x = 0; x < rijen; x++)
  71.                 {
  72.                     for (int y = 0; y < kolommen; y++)
  73.                     {
  74.                         if (!(bordstand[x, y, 0] == 0))
  75.                         {
  76.                             Brush kleur = GenereerKleur(bordstand[x, y, 0]);
  77.                             e.Graphics.FillEllipse(kleur, posities[x, y]);
  78.                         }
  79.                     }
  80.                 }
  81.  
  82.             }
  83.             catch (Exception)
  84.             {
  85.             }
  86.  
  87.  
  88.             try
  89.             {
  90.                 for (int x = 0; x < rijen; x++)
  91.                 {
  92.                     for (int y = 0; y < kolommen; y++)
  93.                     {
  94.                         if (legaliteit[x, y, 0] == 1)
  95.                         {
  96.                             e.Graphics.FillEllipse(Brushes.Yellow, posities[x, y]);
  97.                         }
  98.                     }
  99.                 }
  100.  
  101.             }
  102.             catch (Exception)
  103.             {
  104.             }
  105.  
  106.         }
  107.  
  108.         public void CheckLegality()
  109.         {
  110.             for (int x = 0; x < rijen; x++)
  111.             {
  112.                 for (int y = 0; y < kolommen; y++)
  113.                 {
  114.                     try
  115.                     {
  116.                         bool omringt = false;
  117.                         omringt = ReturnOmringd(x, y);
  118.  
  119.                         if (omringt)
  120.                         {
  121.                             legaliteit[x, y, 0] = 1;
  122.                         }
  123.  
  124.                         else
  125.                         {
  126.                             legaliteit[x, y, 0] = 0;
  127.                         }
  128.                     }
  129.                     catch { }
  130.                 }
  131.             }
  132.         }
  133.  
  134.         public bool ReturnOmringd(int kolom, int rij)
  135.         {
  136.             if (bordstand[kolom - 1, rij, 0] != 0)
  137.             {
  138.                 return true;
  139.             }
  140.  
  141.             if (bordstand[kolom + 1, rij, 0] != 0)
  142.             {
  143.                 return true;
  144.             }
  145.  
  146.             if (bordstand[kolom, rij + 1, 0] != 0)
  147.             {
  148.                 return true;
  149.             }
  150.  
  151.             if (bordstand[kolom, rij - 1, 0] != 0)
  152.             {
  153.                 return true;
  154.             }
  155.  
  156.             if (bordstand[kolom + 1, rij + 1, 0] != 0)
  157.             {
  158.                 return true;
  159.             }
  160.  
  161.             if (bordstand[kolom + 1, rij - 1, 0] != 0)
  162.             {
  163.                 return true;
  164.             }
  165.  
  166.             if (bordstand[kolom - 1, rij + 1, 0] != 0)
  167.             {
  168.                 return true;
  169.             }
  170.  
  171.             if (bordstand[kolom - 1, rij - 1, 0] != 0)
  172.             {
  173.                 return true;
  174.             }
  175.             else
  176.             {
  177.                 return false;
  178.             }
  179.         }
  180.  
  181.         public void CheckPlays()
  182.         {
  183.             try
  184.             {
  185.  
  186.                 if (bordstand[lastX, lastY, 0] == bordstand[lastX - 2, lastY, 0] && bordstand[lastX - 1, lastY, 0] != 0)
  187.                 {
  188.                     bordstand[lastX - 1, lastY, 0] = bordstand[lastX, lastY, 0];
  189.                 }
  190.  
  191.             }
  192.             catch
  193.             {
  194.             }
  195.  
  196.             try
  197.             {
  198.                 if (bordstand[lastX, lastY, 0] == bordstand[lastX + 2, lastY, 0] && bordstand[lastX + 1, lastY, 0] != 0)
  199.                 {
  200.                     bordstand[lastX + 1, lastY, 0] = bordstand[lastX, lastY, 0];
  201.                 }
  202.             }
  203.             catch
  204.             {
  205.             }
  206.             try
  207.             {
  208.  
  209.                 if (bordstand[lastX, lastY, 0] == bordstand[lastX, lastY - 2, 0] && bordstand[lastX, lastY - 1, 0] != 0)
  210.                 {
  211.                     bordstand[lastX, lastY - 1, 0] = bordstand[lastX, lastY, 0];
  212.                 }
  213.             }
  214.             catch
  215.             {
  216.             }
  217.             try
  218.             {
  219.  
  220.                 if (bordstand[lastX, lastY, 0] == bordstand[lastX, lastY + 2, 0] && bordstand[lastX, lastY + 1, 0] != 0)
  221.                 {
  222.                     bordstand[lastX, lastY + 1, 0] = bordstand[lastX, lastY, 0];
  223.                 }
  224.             }
  225.             catch
  226.             {
  227.             }
  228.  
  229.             //diagonaal
  230.             try
  231.             {
  232.                 if (bordstand[lastX, lastY, 0] == bordstand[lastX - 2, lastY - 2, 0] && bordstand[lastX - 1, lastY - 1, 0] != 0)
  233.                 {
  234.                     bordstand[lastX - 1, lastY - 1, 0] = bordstand[lastX, lastY, 0];
  235.                 }
  236.             }
  237.             catch
  238.             {
  239.             }
  240.             try
  241.             {
  242.                 if (bordstand[lastX, lastY, 0] == bordstand[lastX + 2, lastY + 2, 0] && bordstand[lastX + 1, lastY + 1, 0] != 0)
  243.                 {
  244.                     bordstand[lastX + 1, lastY + 1, 0] = bordstand[lastX, lastY, 0];
  245.                 }
  246.             }
  247.             catch
  248.             {
  249.             }
  250.             try
  251.             {
  252.                 if (bordstand[lastX, lastY, 0] == bordstand[lastX - 2, lastY + 2, 0] && bordstand[lastX - 1, lastY + 1, 0] != 0)
  253.                 {
  254.                     bordstand[lastX - 1, lastY + 1, 0] = bordstand[lastX, lastY, 0];
  255.                 }
  256.             }
  257.             catch
  258.             {
  259.             }
  260.             try
  261.             {
  262.                 if (bordstand[lastX, lastY, 0] == bordstand[lastX + 2, lastY - 2, 0] && bordstand[lastX + 1, lastY - 1, 0] != 0)
  263.                 {
  264.                     bordstand[lastX + 1, lastY - 1, 0] = bordstand[lastX, lastY, 0];
  265.                 }
  266.             }
  267.             catch
  268.             {
  269.             }
  270.         }
  271.         public void GetBoxClicked(int mouseX, int mouseY)
  272.         {
  273.  
  274.             int stapX = panel1.Width / rijen;
  275.             int stapY = panel1.Height / kolommen;
  276.             int countX = 0;
  277.             int countY = 0;
  278.  
  279.             for (int x = 0; x < mouseX; x += stapX)
  280.             {
  281.                 countX++;
  282.             }
  283.  
  284.             for (int y = 0; y < mouseY; y += stapY)
  285.             {
  286.                 countY++;
  287.             }
  288.             countX -= 1;
  289.             countY -= 1;
  290.  
  291.             if (bordstand[countX, countY, 0] == 0)
  292.             {
  293.                 lastX = countX;
  294.                 lastY = countY;
  295.                 bordstand[countX, countY, 0] = BepaalKleur();
  296.                 zetten++;
  297.             }
  298.  
  299.  
  300.  
  301.         }
  302.  
  303.  
  304.  
  305.         public Brush GenereerKleur(int nummer)
  306.         {
  307.             switch (nummer)
  308.             {
  309.                 default:
  310.                     return Brushes.Blue;
  311.                 case 1:
  312.                     return Brushes.Blue;
  313.                 case 2:
  314.                     return Brushes.Red;
  315.             }
  316.  
  317.         }
  318.  
  319.         public int BepaalKleur()
  320.         {
  321.  
  322.             if (zetten % 2 == 0)
  323.             {
  324.                 return 1;
  325.             }
  326.             else
  327.                 return 2;
  328.         }
  329.  
  330.         private void button1_Click(object sender, EventArgs e)
  331.         {
  332.             zetten = 0;
  333.             Array.Clear(bordstand, 0, bordstand.Length);
  334.             Array.Clear(posities, 0, posities.Length);
  335.             bordstand[2, 2, 0] = 1;
  336.             bordstand[3, 3, 0] = 1;
  337.             bordstand[2, 3, 0] = 2;
  338.             bordstand[3, 2, 0] = 2;
  339.  
  340.             panel1.Invalidate();
  341.         }
  342.  
  343.         private void panel1_MouseClick(object sender, MouseEventArgs e)
  344.         {
  345.             panel1.Invalidate();
  346.             GetBoxClicked(e.X, e.Y);
  347.             CountChips();
  348.             blauwlabel.Text = "Blauw: " + blauw.ToString();
  349.             roodlabel.Text = "Rood: " + rood.ToString();
  350.  
  351.             if (zetten % 2 == 0)
  352.             {
  353.                 status.Text = "Blauw is aan zet.";
  354.             }
  355.             else
  356.             {
  357.                 status.Text = "Rood is aan zet.";
  358.             }
  359.  
  360.             MessageBox.Show(legaliteit[lastX - 1, lastY, 0].ToString() + "," + legaliteit[lastX + 1, lastY, 0].ToString(), "," + legaliteit[lastX, lastY - 1, 0].ToString() + "," + legaliteit[lastX, lastY + 1, 0].ToString());
  361.  
  362.         }
  363.  
  364.         private void CountChips()
  365.         {
  366.             blauw = 0;
  367.             rood = 0;
  368.             for (int x = 0; x < rijen; x++)
  369.             {
  370.                 for (int y = 0; y < kolommen; y++)
  371.                 {
  372.                     if (bordstand[x, y, 0] != 0)
  373.                     {
  374.                         switch (bordstand[x, y, 0])
  375.                         {
  376.                             case 1:
  377.                                 blauw++;
  378.                                 break;
  379.                             case 2:
  380.                                 rood++;
  381.                                 break;
  382.                         }
  383.                     }
  384.                 }
  385.             }
  386.         }
  387.  
  388.         private void status_TextChanged(object sender, EventArgs e)
  389.         {
  390.             if (status.Text == "Blauw is aan zet.")
  391.             {
  392.                 status.ForeColor = Color.Blue;
  393.             }
  394.             else
  395.             {
  396.                 status.ForeColor = Color.Red;
  397.             }
  398.         }
  399.  
  400.         private void help_Click(object sender, EventArgs e)
  401.         {
  402.             CheckLegality();
  403.         }
  404.     }
  405. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement