Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace Reversi3
- {
- public partial class Reversi : Form
- {
- int aantalx, aantaly, xpos, ypos, vierkant, beurt, ander, t, aantalrood, aantalblauw;
- int[,] bord;
- Pen p; Graphics gr;
- public Reversi()
- {
- aantalx = 6; aantaly = 6; vierkant = 40;
- beurt = 1; ander = 2; aantalrood = 2; aantalblauw = 2;
- InitializeComponent();
- panel1.Paint += tekenBord;
- panel2.Paint += startRondjes;
- mededelingBeurt(beurt);
- }
- void tekenBord(Object o, PaintEventArgs pea)
- {
- p = new Pen(Brushes.Black);
- bord = new int[aantalx, aantaly];
- panel1.Size = new Size(aantalx * vierkant + 1, aantaly * vierkant + 1);
- for (int yArray = 0; yArray < aantaly; yArray++)
- {
- for (int xArray = 0; xArray < aantalx; xArray++)
- {
- pea.Graphics.DrawRectangle(p, xArray * vierkant, yArray * vierkant, vierkant, vierkant);
- bord[xArray, yArray] = 0; //kleur is 'leeg'
- }
- }
- // Startomgeving
- bord[aantalx / 2 - 1, aantaly / 2 - 1] = 2; tekenRondje(aantalx / 2 - 1, aantaly / 2 - 1);
- bord[aantalx / 2, aantaly / 2] = 2; tekenRondje(aantalx / 2, aantaly / 2);
- bord[aantalx / 2, aantaly / 2 - 1] = 1; tekenRondje(aantalx / 2, aantaly / 2 - 1);
- bord[aantalx / 2 - 1, aantaly / 2] = 1; tekenRondje(aantalx / 2 - 1, aantaly / 2);
- label1.Text = aantalrood.ToString();
- label2.Text = aantalblauw.ToString();
- // Invalidate();
- }
- void startRondjes(Object o, PaintEventArgs pa)
- {
- panel2.Size = new Size(vierkant, 2 * vierkant + 2);
- pa.Graphics.FillEllipse(Brushes.Red, 0, 0, vierkant, vierkant);
- pa.Graphics.FillEllipse(Brushes.Blue, 0, vierkant + 2, vierkant, vierkant);
- }
- void kleurRondje(int x, int y, int b)
- {
- if (bord[x, y] == 1 || bord[x,y] == 2)
- {
- MessageBox.Show("Dit veld is al bezet, probeer een andere.", "Bezet", MessageBoxButtons.OK, MessageBoxIcon.Stop);
- }
- else
- {
- bord[x, y] = b;
- if (beurt == 1)
- { beurt = 2; ander = 1; }
- else if (beurt == 2)
- { beurt = 1; ander = 2; }
- mededelingBeurt(beurt);
- }
- }
- void tekenRondje(int x, int y)
- {
- gr = panel1.CreateGraphics();
- if (bord[x, y] == 1) //rood
- {
- gr.FillEllipse(Brushes.Red, x * vierkant, y * vierkant, vierkant, vierkant);
- }
- else if (bord[x, y] == 2) //blauw
- {
- gr.FillEllipse(Brushes.Blue, x * vierkant, y * vierkant, vierkant, vierkant);
- }
- }
- void tekenHelp(int x, int y)
- {
- gr = panel1.CreateGraphics();
- gr.DrawEllipse(p, (x * vierkant) + 5, (y * vierkant) + 5, vierkant - 10, vierkant - 10);
- bord[x, y] = 3;
- }
- void mededelingBeurt(int beurt)
- {
- if (beurt == 1)
- { label3.Text = "Rood is aan zet."; }
- else
- { label3.Text = "Blauw is aan zet."; }
- }
- void checkHelp(bool sluit)
- {
- for (int y = 0; y < aantaly; y++)
- {
- for (int x = 0; x < aantalx; x++)
- {
- if (bord[x, y] == 0) //check alle lege velden in array
- {
- controleVelden(x, y, sluit);
- }
- }
- }
- }
- int check(int x, int y, int teller)
- {
- if (x >= 0 && x < aantalx && y >= 0 && y < aantaly) // bord[x, y] kan niet buiten de array vallen
- {
- if (bord[x, y] == ander)
- {
- return 2; //er wordt een blauwe gevonden
- }
- else if (bord[x, y] == beurt && teller > 0)
- {
- return 1; //geldige zet
- }
- else
- {
- return 0; //ongeldige zet
- }
- }
- else
- {
- return 0; //valt buiten de array --> ongeldige zet
- }
- }
- void controleVelden(int x, int y, bool sluit)
- {
- horizontaalrechts(x, y, sluit);
- Console.WriteLine("rechts");
- horizontaallinks(x, y, sluit);
- Console.WriteLine("links");
- verticaalomhoog(x, y, sluit);
- Console.WriteLine("omhoog");
- verticaalomlaag(x, y, sluit);
- Console.WriteLine("omlaag");
- diaglinksomhoog(x, y, sluit);
- Console.WriteLine("linksomhoog");
- diaglinksomlaag(x, y, sluit);
- Console.WriteLine("linksomlaag");
- diagrechtsomhoog(x, y, sluit);
- Console.WriteLine("rechtsomhoog");
- diagrechtsomlaag(x, y, sluit);
- Console.WriteLine("rechtsomlaag");
- }
- #region Richtingen
- void horizontaalrechts(int x, int y, bool sluit)
- {
- if (x < aantalx - 2 && bord[x + 1, y] != 0)
- {
- t = 0;
- for (int xcheck = x + 1; xcheck < (aantalx - 1) - x; xcheck++)
- {
- if (check(xcheck, y, t) == 2)
- {
- t++;
- }
- else if (check(xcheck, y, t) == 1 && sluit)
- {
- for (int n = 0; n < t; n++)
- {
- x++;
- bord[x, y] = beurt;
- tekenRondje(x, y);
- }
- return;
- }
- else if (check(xcheck, y, t) == 1)
- {
- tekenHelp(x, y);
- }
- else if (check(xcheck, y, t) == 0 && sluit)
- {
- //MessageBox.Show("Ongeldige zet.");
- }
- }
- }
- }
- void horizontaallinks(int x, int y, bool sluit)
- {
- if (x - 2 >= 0 && bord[x - 1, y] != 0)
- {
- t = 0;
- for (int xcheck = x - 1; xcheck >= 0; xcheck--)
- {
- if (check(xcheck, y, t) == 2)
- {
- t++;
- }
- else if (check(xcheck, y, t) == 1 && sluit)
- {
- for (int n = 0; n < t; n++)
- {
- x--;
- bord[x, y] = beurt;
- tekenRondje(x, y);
- }
- return;
- }
- else if (check(xcheck, y, t) == 1)
- {
- tekenHelp(x, y);
- }
- else if (check(xcheck, y, t) == 0)
- {
- return;
- }
- }
- }
- }
- void verticaalomhoog(int x, int y, bool sluit)
- {
- if (y - 1 >= 0 && bord[x, y - 1] != 0)
- {
- t = 0;
- for (int ycheck = y - 1; ycheck >= 0; ycheck--)
- {
- if (check(x, ycheck, t) == 2)
- {
- t++;
- }
- else if (check(x, ycheck, t) == 1 && sluit)
- {
- for (int n = 0; n < t; n++)
- {
- y--;
- bord[x, y] = beurt;
- tekenRondje(x, y);
- }
- return;
- }
- else if (check(x, ycheck, t) == 1)
- {
- tekenHelp(x, y);
- }
- else if (check(x, ycheck, t) == 0)
- {
- return;
- }
- }
- }
- }
- void verticaalomlaag(int x, int y, bool sluit)
- {
- if (y < aantaly - 1 && bord[x, y + 1] != 0)
- {
- t = 0;
- for (int ycheck = y + 1; ycheck < (aantaly - 1) - y; ycheck++)
- {
- if (check(x, ycheck, t) == 2)
- {
- t++;
- }
- else if (check(x, ycheck, t) == 1 && sluit)
- {
- for (int n = 0; n < t; n++)
- {
- y++;
- bord[x, y] = beurt;
- tekenRondje(x, y);
- }
- return;
- }
- else if (check(x, ycheck, t) == 1)
- {
- tekenHelp(x, y);
- }
- else if (check(x, ycheck, t) == 0)
- {
- return;
- }
- }
- }
- }
- void diaglinksomhoog(int x, int y, bool sluit)
- {
- if (x - 1 >= 0 && y - 1 >= 0 && bord[x - 1, y - 1] != 0)
- {
- int ycheck = y - 1;
- t = 0;
- for (int xcheck = x - 1; xcheck >= 0; xcheck--)
- {
- if (check(xcheck, ycheck, t) == 2)
- {
- t++;
- }
- else if (check(xcheck, ycheck, t) == 1 && sluit)
- {
- for (int n = 0; n < t; n++)
- {
- x--;
- y--;
- bord[x, y] = beurt;
- tekenRondje(x, y);
- }
- return;
- }
- else if (check(xcheck, ycheck, t) == 1)
- {
- tekenHelp(x, y);
- }
- else if (check(xcheck, ycheck, t) == 0)
- {
- return;
- }
- ycheck--;
- }
- }
- }
- void diaglinksomlaag(int x, int y, bool sluit)
- {
- if (x - 1 >= 0 && y < aantaly - 1 && bord[x - 1, y + 1] != 0)
- {
- int ycheck = y + 1;
- t = 0;
- for (int xcheck = x - 1; xcheck >= 0; xcheck--)
- {
- if (check(xcheck, ycheck, t) == 2)
- {
- t++;
- }
- else if (check(xcheck, ycheck, t) == 1 && sluit)
- {
- for (int n = 0; n < t; n++)
- {
- x--;
- y++;
- bord[x, y] = beurt;
- tekenRondje(x, y);
- }
- return;
- }
- else if (check(xcheck, ycheck, t) == 1)
- {
- tekenHelp(x, y);
- }
- else if (check(xcheck, ycheck, t) == 0)
- {
- return;
- }
- ycheck++;
- }
- }
- }
- void diagrechtsomhoog(int x, int y, bool sluit)
- {
- if (x < aantalx - 1 && y - 1 >= 0 && bord[x + 1, y - 1] != 0)
- {
- int ycheck = y - 1;
- t = 0;
- for (int xcheck = x + 1; xcheck < (aantalx - 1) - x; xcheck++)
- {
- if (check(xcheck, ycheck, t) == 2)
- {
- t++;
- }
- else if (check(xcheck, ycheck, t) == 1 && sluit)
- {
- for (int n = 0; n < t; n++)
- {
- x++;
- y--;
- bord[x, y] = beurt;
- tekenRondje(x, y);
- }
- return;
- }
- else if (check(xcheck, ycheck, t) == 1)
- {
- tekenHelp(x, y);
- }
- else if (check(xcheck, ycheck, t) == 0)
- {
- //return;
- }
- ycheck--;
- }
- }
- }
- void diagrechtsomlaag(int x, int y, bool sluit)
- {
- if (x < aantalx - 1 && y < aantaly - 1 && bord[x + 1, y + 1] != 0)
- {
- int ycheck = y + 1;
- t = 0;
- for (int xcheck = x + 1; xcheck < (aantalx - 1) - x; xcheck++)
- {
- if (check(xcheck, ycheck, t) == 2)
- {
- t++;
- }
- else if (check(xcheck, ycheck, t) == 1 && sluit)
- {
- for (int n = 0; n < t; n++)
- {
- x++;
- y++;
- bord[x, y] = beurt;
- tekenRondje(x, y);
- }
- return;
- }
- else if (check(xcheck, ycheck, t) == 1)
- {
- tekenHelp(x, y);
- }
- else if (check(xcheck, ycheck, t) == 0)
- {
- return;
- }
- ycheck++;
- }
- }
- }
- #endregion
- bool winnaar()
- {
- for (int y = 0; y < aantaly; y++)
- {
- for (int x = 0; x < aantalx; x++)
- {
- if (bord[x, y] == 0)
- {
- return false;
- }
- }
- }
- return true;
- }
- void eindespel()
- {
- if (winnaar() == true)
- {
- if (Convert.ToInt32(label1.Text) > Convert.ToInt32(label2.Text))
- {
- MessageBox.Show("Rood heeft gewonnen", "Gewonnen", MessageBoxButtons.OK, MessageBoxIcon.Information);
- return;
- }
- else if (label1.Text == label2.Text)
- {
- MessageBox.Show("Het is een remise", "Remise", MessageBoxButtons.OK, MessageBoxIcon.Information);
- return;
- }
- else
- {
- MessageBox.Show("Blauw heeft gewonnen", "Gewonnen", MessageBoxButtons.OK, MessageBoxIcon.Information);
- return;
- }
- }
- }
- private void panel1_MouseClick(object sender, MouseEventArgs e)
- {
- Console.Clear();
- xpos = e.X / vierkant;
- ypos = e.Y / vierkant;
- if (xpos > aantalx - 1)
- { xpos = aantalx - 1; }
- if (xpos < 0)
- { xpos = 0; }
- if (ypos > aantaly - 1)
- { ypos = aantaly - 1; }
- if (ypos < 0)
- { ypos = 0; }
- controleVelden(xpos, ypos, true);
- kleurRondje(xpos, ypos, beurt);
- tekenRondje(xpos, ypos);
- wis();
- tel();
- eindespel();
- }
- void wis()
- {
- for (int x = 0; x < aantalx; x++)
- {
- for (int y = 0; y < aantaly; y++)
- {
- if (bord[x, y] == 3)
- {
- Graphics gr = panel1.CreateGraphics();
- gr.FillRectangle(SystemBrushes.Control, (x * vierkant) + 5, (y * vierkant) + 5, vierkant - 8, vierkant - 8);
- bord[x, y] = 0;
- }
- }
- }
- }
- void tel()
- {
- aantalblauw = 0;
- aantalrood = 0;
- for (int x = 0; x < aantalx; x++)
- {
- for (int y = 0; y < aantaly; y++)
- {
- if (bord[x, y] == 1)
- {
- aantalrood++;
- }
- else if (bord[x, y] == 2)
- {
- aantalblauw++;
- }
- }
- }
- label1.Text = aantalrood.ToString();
- label2.Text = aantalblauw.ToString();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- beurt = 1; ander = 2;
- mededelingBeurt(beurt);
- aantalrood = 2;
- aantalblauw = 2;
- Refresh();
- }
- private void button2_Click(object sender, EventArgs e)
- {
- checkHelp(false);
- }
- }
- }
Add Comment
Please, Sign In to add comment