Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Windows.Forms;
- namespace ReversiRaiz
- {
- public partial class Form1 : Form
- {
- int zetten = 0;
- int rood = 0;
- int blauw = 0;
- int lastX = 0; int lastY = 0;
- const int rijen = 6; //breedte
- const int kolommen = 6; //hoogte
- const int kleur = 1; //kleuren
- const int legaal = 1;
- int[,,] legaliteit = new int[kolommen, rijen, legaal];
- int[,,] bordstand = new int[kolommen, rijen, kleur];
- Rectangle[,] posities = new Rectangle[kolommen, rijen];
- public Form1()
- {
- InitializeComponent();
- panel1.Paint += TekenSteen;
- this.DoubleBuffered = true;
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- private void panel1_Paint(object sender, PaintEventArgs e)
- {
- // tekent de kolommen
- for (int x = 0; x <= kolommen; x++)
- {
- e.Graphics.DrawLine(Pens.Gray, x * ((panel1.Width) / kolommen), 0, x * ((panel1.Width) / kolommen), panel1.Height);
- }
- // tekent de rijen
- for (int y = 0; y <= rijen; y++)
- {
- e.Graphics.DrawLine(Pens.Gray, 0, y * ((panel1.Height) / rijen), panel1.Width, y * ((panel1.Height) / rijen));
- }
- // zorgt ervoor dat de juiste stenen op het bord staan
- for (int x = 0; x < kolommen; x++)
- {
- for (int y = 0; y < rijen; y++)
- {
- int lenhoog = panel1.Height / kolommen;
- int lenbreedt = panel1.Width / rijen;
- posities[x, y] = new Rectangle(x * lenhoog, y * lenbreedt, lenhoog, lenbreedt);
- }
- }
- }
- public void TekenSteen(object o, PaintEventArgs e)
- {
- CheckPlays();
- e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
- try
- {
- for (int x = 0; x < rijen; x++)
- {
- for (int y = 0; y < kolommen; y++)
- {
- if (!(bordstand[x, y, 0] == 0))
- {
- Brush kleur = GenereerKleur(bordstand[x, y, 0]);
- e.Graphics.FillEllipse(kleur, posities[x, y]);
- }
- }
- }
- }
- catch (Exception)
- {
- }
- try
- {
- for (int x = 0; x < rijen; x++)
- {
- for (int y = 0; y < kolommen; y++)
- {
- if (legaliteit[x, y, 0] == 1)
- {
- e.Graphics.FillEllipse(Brushes.Yellow, posities[x, y]);
- }
- }
- }
- }
- catch (Exception)
- {
- }
- }
- public void CheckLegality()
- {
- for (int x = 0; x < rijen; x++)
- {
- for (int y = 0; y < kolommen; y++)
- {
- try
- {
- bool omringt = false;
- omringt = ReturnOmringd(x, y);
- if (omringt)
- {
- legaliteit[x, y, 0] = 1;
- }
- else
- {
- legaliteit[x, y, 0] = 0;
- }
- }
- catch { }
- }
- }
- }
- public bool ReturnOmringd(int kolom, int rij)
- {
- if (bordstand[kolom - 1, rij, 0] != 0)
- {
- return true;
- }
- if (bordstand[kolom + 1, rij, 0] != 0)
- {
- return true;
- }
- if (bordstand[kolom, rij + 1, 0] != 0)
- {
- return true;
- }
- if (bordstand[kolom, rij - 1, 0] != 0)
- {
- return true;
- }
- if (bordstand[kolom + 1, rij + 1, 0] != 0)
- {
- return true;
- }
- if (bordstand[kolom + 1, rij - 1, 0] != 0)
- {
- return true;
- }
- if (bordstand[kolom - 1, rij + 1, 0] != 0)
- {
- return true;
- }
- if (bordstand[kolom - 1, rij - 1, 0] != 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public void CheckPlays()
- {
- try
- {
- if (bordstand[lastX, lastY, 0] == bordstand[lastX - 2, lastY, 0] && bordstand[lastX - 1, lastY, 0] != 0)
- {
- bordstand[lastX - 1, lastY, 0] = bordstand[lastX, lastY, 0];
- }
- }
- catch
- {
- }
- try
- {
- if (bordstand[lastX, lastY, 0] == bordstand[lastX + 2, lastY, 0] && bordstand[lastX + 1, lastY, 0] != 0)
- {
- bordstand[lastX + 1, lastY, 0] = bordstand[lastX, lastY, 0];
- }
- }
- catch
- {
- }
- try
- {
- if (bordstand[lastX, lastY, 0] == bordstand[lastX, lastY - 2, 0] && bordstand[lastX, lastY - 1, 0] != 0)
- {
- bordstand[lastX, lastY - 1, 0] = bordstand[lastX, lastY, 0];
- }
- }
- catch
- {
- }
- try
- {
- if (bordstand[lastX, lastY, 0] == bordstand[lastX, lastY + 2, 0] && bordstand[lastX, lastY + 1, 0] != 0)
- {
- bordstand[lastX, lastY + 1, 0] = bordstand[lastX, lastY, 0];
- }
- }
- catch
- {
- }
- //diagonaal
- try
- {
- if (bordstand[lastX, lastY, 0] == bordstand[lastX - 2, lastY - 2, 0] && bordstand[lastX - 1, lastY - 1, 0] != 0)
- {
- bordstand[lastX - 1, lastY - 1, 0] = bordstand[lastX, lastY, 0];
- }
- }
- catch
- {
- }
- try
- {
- if (bordstand[lastX, lastY, 0] == bordstand[lastX + 2, lastY + 2, 0] && bordstand[lastX + 1, lastY + 1, 0] != 0)
- {
- bordstand[lastX + 1, lastY + 1, 0] = bordstand[lastX, lastY, 0];
- }
- }
- catch
- {
- }
- try
- {
- if (bordstand[lastX, lastY, 0] == bordstand[lastX - 2, lastY + 2, 0] && bordstand[lastX - 1, lastY + 1, 0] != 0)
- {
- bordstand[lastX - 1, lastY + 1, 0] = bordstand[lastX, lastY, 0];
- }
- }
- catch
- {
- }
- try
- {
- if (bordstand[lastX, lastY, 0] == bordstand[lastX + 2, lastY - 2, 0] && bordstand[lastX + 1, lastY - 1, 0] != 0)
- {
- bordstand[lastX + 1, lastY - 1, 0] = bordstand[lastX, lastY, 0];
- }
- }
- catch
- {
- }
- }
- public void GetBoxClicked(int mouseX, int mouseY)
- {
- int stapX = panel1.Width / rijen;
- int stapY = panel1.Height / kolommen;
- int countX = 0;
- int countY = 0;
- for (int x = 0; x < mouseX; x += stapX)
- {
- countX++;
- }
- for (int y = 0; y < mouseY; y += stapY)
- {
- countY++;
- }
- countX -= 1;
- countY -= 1;
- if (bordstand[countX, countY, 0] == 0)
- {
- lastX = countX;
- lastY = countY;
- bordstand[countX, countY, 0] = BepaalKleur();
- zetten++;
- }
- }
- public Brush GenereerKleur(int nummer)
- {
- switch (nummer)
- {
- default:
- return Brushes.Blue;
- case 1:
- return Brushes.Blue;
- case 2:
- return Brushes.Red;
- }
- }
- public int BepaalKleur()
- {
- if (zetten % 2 == 0)
- {
- return 1;
- }
- else
- return 2;
- }
- private void button1_Click(object sender, EventArgs e)
- {
- zetten = 0;
- Array.Clear(bordstand, 0, bordstand.Length);
- Array.Clear(posities, 0, posities.Length);
- bordstand[2, 2, 0] = 1;
- bordstand[3, 3, 0] = 1;
- bordstand[2, 3, 0] = 2;
- bordstand[3, 2, 0] = 2;
- panel1.Invalidate();
- }
- private void panel1_MouseClick(object sender, MouseEventArgs e)
- {
- panel1.Invalidate();
- GetBoxClicked(e.X, e.Y);
- CountChips();
- blauwlabel.Text = "Blauw: " + blauw.ToString();
- roodlabel.Text = "Rood: " + rood.ToString();
- if (zetten % 2 == 0)
- {
- status.Text = "Blauw is aan zet.";
- }
- else
- {
- status.Text = "Rood is aan zet.";
- }
- 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());
- }
- private void CountChips()
- {
- blauw = 0;
- rood = 0;
- for (int x = 0; x < rijen; x++)
- {
- for (int y = 0; y < kolommen; y++)
- {
- if (bordstand[x, y, 0] != 0)
- {
- switch (bordstand[x, y, 0])
- {
- case 1:
- blauw++;
- break;
- case 2:
- rood++;
- break;
- }
- }
- }
- }
- }
- private void status_TextChanged(object sender, EventArgs e)
- {
- if (status.Text == "Blauw is aan zet.")
- {
- status.ForeColor = Color.Blue;
- }
- else
- {
- status.ForeColor = Color.Red;
- }
- }
- private void help_Click(object sender, EventArgs e)
- {
- CheckLegality();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement