Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Windows.Forms;
- using System.Drawing;
- using System;
- namespace Reversi
- {
- static class Program
- {
- static void Main()
- {
- Application.Run(new Scherm());
- }
- class Scherm : Form
- {
- /* Declaraties */
- Button startknop;
- Button helpknop;
- Button randomknop;
- Label roodlabel;
- Label blauwlabel;
- Label schermtext;
- Label statuslabel;
- TextBox schermbox;
- Button plusx;
- Button plusy;
- Button minx;
- Button miny;
- /* Globale Variabelen */
- // Statics
- static Random random = new Random();
- // Constants
- int schermgroottex = 6, schermgroottey = 6, vakgrootte = 40;
- // Ints
- int aantal = 4, aantalrood = 0, aantalblauw = 0, kleur = 1; // 0 = leeg, 1 = blauw, 2 = rood
- int mouseX, mouseY, schermgrootte2, maxaantal, geldigezetten = 0;
- // Arrays
- int[,] spelbord;
- int[] geldigx;
- int[] geldigy;
- // Bools
- bool rest = false, help = false;
- /* Einde Variabelen */
- public Scherm()
- {
- schermgrootte2 = schermgroottex;
- maxaantal = schermgroottex * schermgroottey;
- /* Objecten aanmaken */
- this.startknop = new Button();
- this.helpknop = new Button();
- this.randomknop = new Button();
- this.roodlabel = new Label();
- this.blauwlabel = new Label();
- this.statuslabel = new Label();
- this.schermbox = new TextBox();
- this.schermtext = new Label();
- this.plusx = new Button();
- this.minx = new Button();
- this.plusy = new Button();
- this.miny = new Button();
- /* Eigenschappen van objecten */
- // plusxknop
- this.plusx.Location = new Point(295, 68);
- this.plusx.Name = "plusx";
- this.plusx.Size = new Size(20, 20);
- this.plusx.Text = "+";
- // plusyknop
- this.plusy.Location = new Point(295, 90);
- this.plusy.Name = "plusy";
- this.plusy.Size = new Size(20, 20);
- this.plusy.Text = "+";
- // minxknop
- this.minx.Location = new Point(245, 68);
- this.minx.Name = "minx";
- this.minx.Size = new Size(20, 20);
- this.minx.Text = "-";
- // minxknop
- this.miny.Location = new Point(245, 90);
- this.miny.Name = "miny";
- this.miny.Size = new Size(20, 20);
- this.miny.Text = "-";
- // randomknop
- this.randomknop.Location = new Point(350, 65);
- this.randomknop.Name = "randomknop";
- this.randomknop.Size = new Size(80, 25);
- this.randomknop.Text = "Random zet";
- // schermbox
- this.schermbox.Location = new Point(270, 68);
- this.schermbox.Name = "schermgrootte2";
- this.schermbox.Size = new Size(20, 29);
- this.schermbox.TabIndex = 0;
- this.schermbox.Text = "6";
- // schermtext
- this.schermtext.AutoSize = true;
- this.schermtext.Location = new Point(200, 70);
- this.schermtext.Name = "roodlabel";
- this.schermtext.Size = new Size(40, 13);
- this.schermtext.TabIndex = 2;
- this.schermtext.Text = "Grootte";
- this.schermtext.BackColor = Color.Khaki;
- // startknop
- this.startknop.Location = new Point(12, 12);
- this.startknop.Name = "startknop";
- this.startknop.Size = new Size(78, 29);
- this.startknop.Text = "Nieuw spel";
- // helpknop
- this.helpknop.Location = new Point(96, 12);
- this.helpknop.Name = "helpknop";
- this.helpknop.Size = new Size(78, 29);
- this.helpknop.TabIndex = 1;
- this.helpknop.Text = "Halp!";
- // roodlabel
- this.roodlabel.AutoSize = true;
- this.roodlabel.Location = new Point(392, 20);
- this.roodlabel.Name = "roodlabel";
- this.roodlabel.Size = new Size(40, 13);
- this.roodlabel.TabIndex = 2;
- this.roodlabel.Text = "2 Stenen";
- this.roodlabel.BackColor = Color.Khaki;
- // blauwlabel
- this.blauwlabel.AutoSize = true;
- this.blauwlabel.Location = new Point(266, 19);
- this.blauwlabel.Name = "blauwlabel";
- this.blauwlabel.Size = new Size(40, 13);
- this.blauwlabel.TabIndex = 3;
- this.blauwlabel.Text = "2 Stenen";
- this.blauwlabel.BackColor = Color.Khaki;
- // statuslabel
- this.statuslabel.AutoSize = true;
- this.statuslabel.Location = new Point(13, 72);
- this.statuslabel.Name = "statuslabel";
- this.statuslabel.Size = new Size(35, 13);
- this.statuslabel.TabIndex = 4;
- this.statuslabel.Text = "Blauw is nu aan zet.";
- this.statuslabel.BackColor = Color.Khaki;
- // Het formulier
- this.Controls.Add(this.statuslabel);
- this.Controls.Add(this.blauwlabel);
- this.Controls.Add(this.roodlabel);
- this.Controls.Add(this.helpknop);
- this.Controls.Add(this.startknop);
- this.Controls.Add(this.schermbox);
- this.Controls.Add(this.schermtext);
- this.Controls.Add(this.randomknop);
- this.Controls.Add(this.plusx);
- this.Controls.Add(this.minx);
- this.Controls.Add(this.plusy);
- this.Controls.Add(this.miny);
- this.Text = "Reversi door Conrad en Inigo";
- // Als je op enter drukt, activeer dan de startknop.
- this.AcceptButton = startknop;
- // Fix voor de clientsize, als het bord groter is dan het scherm, pas dan het scherm aan.
- clientgrootte();
- if (int.Parse(this.schermbox.Text) > 4)
- schermgrootte2 = int.Parse(this.schermbox.Text);
- else
- {
- this.schermbox.Text = 4.ToString();
- schermgrootte2 = 4;
- }
- maxaantal = schermgroottex * schermgroottey;
- // Events
- this.Paint += this.tekenScherm;
- this.MouseClick += this.klik;
- this.startknop.Click += this.nieuwspel;
- this.helpknop.Click += this.helpklik;
- this.randomknop.Click += this.randomklik;
- this.plusx.Click += this.plusxklik;
- this.minx.Click += this.minxklik;
- this.plusy.Click += this.plusyklik;
- this.miny.Click += this.minyklik;
- // Arrays
- this.spelbord = new int[schermgrootte2, schermgrootte2];
- this.geldigx = new int[maxaantal];
- this.geldigy = new int[maxaantal];
- int tellertje;
- int tellertje2;
- //initialiseren van het bord, alles op 0 zetten (= geen stenen) behalve die eerste 4 stenen.
- for (tellertje = 0; tellertje < schermgrootte2; tellertje++)
- {
- for (tellertje2 = 0; tellertje2 < schermgrootte2; tellertje2++)
- {
- if (tellertje == (schermgrootte2 / 2) - 1 && tellertje2 == (schermgrootte2 / 2) - 1)
- spelbord[tellertje, tellertje2] = 1;
- else if (tellertje == (schermgrootte2 / 2) && tellertje2 == (schermgrootte2 / 2))
- spelbord[tellertje, tellertje2] = 1;
- else if (tellertje == (schermgrootte2 / 2) - 1 && tellertje2 == (schermgrootte2 / 2))
- spelbord[tellertje, tellertje2] = 2;
- else if (tellertje == (schermgrootte2 / 2) && tellertje2 == (schermgrootte2 / 2) - 1)
- spelbord[tellertje, tellertje2] = 2;
- else spelbord[tellertje, tellertje2] = 0;
- }
- }
- }
- public void plusxklik(object o, EventArgs e)
- {
- schermgroottex++;
- this.schermbox.Text = schermgroottex.ToString();
- nieuwspel(o, e);
- }
- public void minxklik(object o, EventArgs e)
- {
- schermgroottex--;
- this.schermbox.Text = schermgroottex.ToString();
- nieuwspel(o, e);
- }
- public void plusyklik(object o, EventArgs e)
- {
- schermgroottey++;
- this.schermbox.Text = schermgroottey.ToString();
- nieuwspel(o, e);
- }
- public void minyklik(object o, EventArgs e)
- {
- schermgroottey--;
- this.schermbox.Text = schermgroottey.ToString();
- nieuwspel(o, e);
- }
- public void clientgrootte()
- {
- if (int.Parse(this.schermbox.Text) > 4)
- schermgrootte2 = int.Parse(this.schermbox.Text);
- else
- {
- this.schermbox.Text = 4.ToString();
- schermgrootte2 = 4;
- }
- if (schermgrootte2 >= 8 && schermgrootte2 <= 10)
- {
- this.ClientSize = new Size(450, 450 + (schermgrootte2 - 8) * vakgrootte);
- }
- else if (schermgrootte2 >= 11)
- {
- this.ClientSize = new Size(370 + (schermgrootte2 - 8) * vakgrootte, 450 + (schermgrootte2 - 8) * vakgrootte);
- }
- else
- {
- this.ClientSize = new Size(450, 115 + (schermgrootte2 * vakgrootte));
- }
- }
- // Klik op de random knop voor een willekeurige (geldige) zet
- public void randomklik(object o, EventArgs e)
- {
- int willekeurig = random.Next(0, geldigezetten);
- doezet(geldigx[willekeurig], geldigy[willekeurig]);
- }
- // Gooit de bool "help" om als er op de hulpknop gedrukt wordt, en roept Invalidate aan.
- public void helpklik(object o, EventArgs e)
- {
- if (help == true)
- {
- help = false;
- Invalidate();
- }
- else if (help == false)
- {
- help = true;
- Invalidate();
- }
- }
- // Reset alle waarden zodat het weer een nieuw spel is, en zet de eerste vier stenen weer neer.
- public void nieuwspel(object o, EventArgs e)
- {
- if (int.Parse(this.schermbox.Text) > 4)
- schermgrootte2 = int.Parse(this.schermbox.Text);
- else
- {
- this.schermbox.Text = 4.ToString();
- schermgrootte2 = 4;
- }
- //maxaantal = schermgrootte2 * schermgrootte2;
- this.spelbord = new int[schermgrootte2, schermgrootte2];
- clientgrootte();
- int t = 0; int t2 = 0;
- kleur = 1; this.statuslabel.Text = "Blauw is nu aan zet.";
- aantalrood = 0;
- aantalblauw = 0;
- help = false;
- aantal = 4;
- for (t = 0; t < schermgrootte2; t++)
- {
- for (t2 = 0; t2 < schermgrootte2; t2++)
- {
- if (t == (schermgrootte2 / 2) - 1 && t2 == (schermgrootte2 / 2) - 1)
- spelbord[t, t2] = 1;
- else if (t == (schermgrootte2 / 2) && t2 == (schermgrootte2 / 2))
- spelbord[t, t2] = 1;
- else if (t == (schermgrootte2 / 2) - 1 && t2 == (schermgrootte2 / 2))
- spelbord[t, t2] = 2;
- else if (t == (schermgrootte2 / 2) && t2 == (schermgrootte2 / 2) - 1)
- spelbord[t, t2] = 2;
- else spelbord[t, t2] = 0;
- }
- }
- Invalidate();
- }
- // Bouwt het scherm op met de vakjes, de gekleurde rondjes, en (indien aangegeven) de hulpcirkeltjes.
- public void tekenScherm(object o, PaintEventArgs e)
- {
- volgendeZet();
- Graphics gr = e.Graphics;
- gr.FillRectangle(Brushes.Khaki, 0, 0, this.ClientSize.Width, this.ClientSize.Height);
- int x = 15, y = 100, teller, teller2;
- tekenRondje(gr, 345, 5, 2);
- tekenRondje(gr, 220, 5, 1);
- //Bijhouden van hoeveel stenen van iedere kleur zich op het bord bevinden.
- stenenTellen();
- geldigezetten = 0;
- for (teller = 0; teller < schermgrootte2; teller++)
- {
- tekenvakje(gr, x, y);
- for (teller2 = 0; teller2 < schermgrootte2; teller2++)
- {
- tekenvakje(gr, x, y);
- tekenRondje(gr, x, y, spelbord[teller, teller2]);
- if (help)
- {
- tekenHelpvakje(gr, x, y, teller, teller2, spelbord[teller, teller2]);
- }
- else if (!help)
- {
- doeRandom(gr, x, y, teller, teller2, spelbord[teller, teller2]);
- }
- y = y + vakgrootte;
- }
- x = x + vakgrootte;
- y = 100;
- }
- }
- // Voorkomt het flitsen bij klikken / nieuw game / help aan/uit
- override protected void OnPaintBackground(PaintEventArgs e)
- {
- }
- // Tekent in de vakken waar de huidige kleur kan zetten (alleen als help knop geklikt is, help == true)
- public void tekenHelpvakje(Graphics gr, int x, int y, int teller, int teller2, int kleuraanzet)
- {
- if (kleuraanzet == 0 && checkspot(teller, teller2, kleur, false) == true)
- {
- gr.DrawEllipse(Pens.Gray, x, y, vakgrootte, vakgrootte);
- geldigeZet(x, y);
- }
- }
- // Druk op deze knop voor een willekeurige (maar geldige!) zet.
- public void doeRandom(Graphics gr, int x, int y, int teller, int teller2, int kleuraanzet)
- {
- if (kleuraanzet == 0 && checkspot(teller, teller2, kleur, false) == true)
- {
- geldigeZet(x, y);
- }
- }
- // Sla de x en de y posities van alle geldige zetten op.
- public void geldigeZet(int x, int y)
- {
- geldigx[geldigezetten] = x;
- geldigy[geldigezetten] = y;
- geldigezetten++;
- }
- // Telt de stenen en zet de juiste getallen in de labels.
- public void stenenTellen()
- {
- aantalrood = 0;
- aantalblauw = 0;
- int teller, teller2;
- for (teller = 0; teller < schermgrootte2; teller++)
- {
- for (teller2 = 0; teller2 < schermgrootte2; teller2++)
- {
- if (spelbord[teller, teller2] == 1) aantalblauw++;
- else if (spelbord[teller, teller2] == 2) aantalrood++;
- }
- }
- this.blauwlabel.Text = aantalblauw + " Stenen";
- this.roodlabel.Text = aantalrood + " Stenen";
- }
- public void volgendeZet()
- {
- checkzetten(); // Checkt of de huidige kleur wel kan zetten.
- if (aantal == maxaantal) // Het spel is afgelopen als het bord vol is.
- {
- eindigspel();
- }
- else if (rest == false) // Is het bord niet vol, dan actie ondernemen als de huidige kleur niet kan zetten.
- {
- switch (kleur)
- {
- case 1: kleur = 2;
- checkzetten();
- if (rest == true)
- this.statuslabel.Text = "Rood is nu aan zet";
- else
- eindigspel();
- break;
- case 2: kleur = 1;
- checkzetten();
- if (rest == true)
- this.statuslabel.Text = "Blauw is nu aan zet";
- else
- eindigspel();
- break;
- }
- }
- }
- // Stenen van iedere kleur op het bord tellen.
- public void eindigspel()
- {
- if (aantalblauw == aantalrood)
- this.statuslabel.Text = "Gelijkspel!";
- if (aantalblauw > aantalrood)
- this.statuslabel.Text = "Blauw wint!";
- if (aantalblauw < aantalrood)
- this.statuslabel.Text = "Rood wint!";
- }
- // Checkt of de huidige kleur kan zetten.
- public void checkzetten()
- {
- rest = false;
- int teller = 0; int teller2 = 0;
- for (teller = 0; teller < schermgrootte2; teller++)
- {
- for (teller2 = 0; teller2 < schermgrootte2; teller2++)
- {
- if (spelbord[teller, teller2] == 0)
- checkspot(teller, teller2, kleur, false);
- }
- }
- }
- // Tekent een vakje van het speelbord.
- public void tekenvakje(Graphics gr, int x, int y)
- {
- gr.FillRectangle(Brushes.Khaki, x, y, vakgrootte, vakgrootte);
- gr.DrawRectangle(Pens.Black, x, y, vakgrootte, vakgrootte);
- }
- // Klikevent methode.
- public void klik(object o, MouseEventArgs e)
- {
- // Doe een zet als er in een vakje geklikt wordt.
- if (e.X >= 15 && e.Y >= 100 && e.X <= (15 + vakgrootte * schermgroottex - 1) && e.Y <= (100 + vakgrootte * schermgroottey - 1))
- {
- doezet(e.X, e.Y);
- }
- }
- // Alle acties die uitgevoerd moeten worden als ergens geklikt wordt.
- public void doezet(int x, int y)
- {
- if (aantal < maxaantal)
- {
- mouseX = ((x - 15) / vakgrootte * vakgrootte);
- mouseY = ((y - 100) / vakgrootte * vakgrootte);
- if (spelbord[(mouseX) / vakgrootte, (mouseY) / vakgrootte] == 0 && checkspot((mouseX / vakgrootte), (mouseY / vakgrootte), kleur, true) == true)
- {
- if (kleur == 1)
- {
- spelbord[mouseX / vakgrootte, mouseY / vakgrootte] = kleur;
- kleur = 2;
- this.statuslabel.Text = "Rood is nu aan zet";
- }
- else if (kleur == 2)
- {
- spelbord[mouseX / vakgrootte, mouseY / vakgrootte] = kleur;
- kleur = 1;
- this.statuslabel.Text = "Blauw is nu aan zet";
- }
- aantal++;
- Invalidate();
- }
- }
- }
- // Bekijkt of op het geklikte vakje een steen gezet mag worden, zo ja, verander alle stenen volgens de spelregels.
- public bool checkspot(int mousex, int mousey, int kleuraanzet, bool verander)
- {
- bool res = false;
- // Check boven
- int t;
- // Kijk eerst of er stenen van de andere kleur naast staan, zo ja, hoeveel = t.
- for (t = 1; t < mousey && (spelbord[mousex, mousey - t] != 0 && spelbord[mousex, mousey - t] != kleuraanzet); t++)
- {
- }
- // Staat er aan het eind van die rij een steen van eigen kleur? Zo ja, maak alle stenen ertussen eigen kleur.
- if ((mousey - t) >= 0 && spelbord[mousex, mousey - t] == kleuraanzet && t > 1)
- {
- res = true;
- if (verander)
- {
- int t2;
- for (t2 = 1; t2 < t; t2++)
- {
- // Verander de steen
- spelbord[mousex, mousey - t2] = kleuraanzet;
- }
- }
- }
- if (res == true)
- rest = true;
- // Check rechts
- for (t = 1; t < schermgrootte2 - mousex && (spelbord[mousex + t, mousey] != 0 && spelbord[mousex + t, mousey] != kleuraanzet); t++)
- {
- }
- if ((mousex + t) <= (schermgrootte2 - 1) && spelbord[mousex + t, mousey] == kleuraanzet && t > 1)
- {
- res = true;
- if (verander)
- {
- int t2;
- for (t2 = 1; t2 < t; t2++)
- {
- spelbord[mousex + t2, mousey] = kleuraanzet;
- }
- }
- }
- if (res == true)
- rest = true;
- // Check links
- for (t = 1; t < mousex && (spelbord[mousex - t, mousey] != 0 && spelbord[mousex - t, mousey] != kleuraanzet); t++)
- {
- }
- if ((mousex - t) >= 0 && spelbord[mousex - t, mousey] == kleuraanzet && t > 1)
- {
- res = true;
- if (verander)
- {
- int t2;
- for (t2 = 1; t2 < t; t2++)
- {
- spelbord[mousex - t2, mousey] = kleuraanzet;
- }
- }
- }
- if (res == true)
- rest = true;
- // Check onder
- for (t = 1; t < schermgrootte2 - mousey && (spelbord[mousex, mousey + t] != 0 && spelbord[mousex, mousey + t] != kleuraanzet); t++)
- {
- }
- if ((mousey + t) <= (schermgrootte2 - 1) && spelbord[mousex, mousey + t] == kleuraanzet && t > 1)
- {
- res = true;
- if (verander)
- {
- int t2;
- for (t2 = 1; t2 < t; t2++)
- {
- spelbord[mousex, mousey + t2] = kleuraanzet;
- }
- }
- }
- if (res == true)
- rest = true;
- // Check rechtsboven
- for (t = 1; t < mousey && t < schermgrootte2 - mousex && (spelbord[mousex + t, mousey - t] != 0 && spelbord[mousex + t, mousey - t] != kleuraanzet); t++)
- {
- }
- if ((mousey - t) >= 0 && (mousex + t) <= (schermgrootte2 - 1) && spelbord[mousex + t, mousey - t] == kleuraanzet && t > 1)
- {
- res = true;
- if (verander)
- {
- int t2;
- for (t2 = 1; t2 < t; t2++)
- {
- spelbord[mousex + t2, mousey - t2] = kleuraanzet;
- }
- }
- }
- if (res == true)
- rest = true;
- // Check linksboven
- for (t = 1; t < mousey && t < mousex && (spelbord[mousex - t, mousey - t] != 0 && spelbord[mousex - t, mousey - t] != kleuraanzet); t++)
- {
- }
- if ((mousey - t) >= 0 && (mousex - t) >= 0 && spelbord[mousex - t, mousey - t] == kleuraanzet && t > 1)
- {
- res = true;
- if (verander)
- {
- int t2;
- for (t2 = 1; t2 < t; t2++)
- {
- spelbord[mousex - t2, mousey - t2] = kleuraanzet;
- }
- }
- }
- if (res == true)
- rest = true;
- // Check linksonder
- for (t = 1; t < schermgrootte2 - mousey && t < mousex && (spelbord[mousex - t, mousey + t] != 0 && spelbord[mousex - t, mousey + t] != kleuraanzet); t++)
- {
- }
- if ((mousex - t) >= 0 && mousey + t <= (schermgrootte2 - 1) && spelbord[mousex - t, mousey + t] == kleuraanzet && t > 1)
- {
- res = true;
- if (verander)
- {
- int t2;
- for (t2 = 1; t2 < t; t2++)
- {
- spelbord[mousex - t2, mousey + t2] = kleuraanzet;
- }
- }
- }
- if (res == true)
- rest = true;
- // Check rechtsonder
- for (t = 1; t < schermgrootte2 - mousey && t < schermgrootte2 - mousex && (spelbord[mousex + t, mousey + t] != 0 && spelbord[mousex + t, mousey + t] != kleuraanzet); t++)
- {
- }
- if ((mousex + t) <= (schermgrootte2 - 1) && (mousey + t) <= (schermgrootte2 - 1) && spelbord[mousex + t, mousey + t] == kleuraanzet && t > 1)
- {
- res = true;
- if (verander)
- {
- int t2;
- for (t2 = 1; t2 < t; t2++)
- {
- spelbord[mousex + t2, mousey + t2] = kleuraanzet;
- }
- }
- }
- if (res == true)
- rest = true;
- // Return aan het eind
- return res;
- }
- // Tekent de steen
- public void tekenRondje(Graphics gr, int x, int y, int kleurwaarde)
- {
- if (kleurwaarde == 1)
- gr.FillEllipse(Brushes.Blue, x, y, vakgrootte, vakgrootte);
- else if (kleurwaarde == 2)
- gr.FillEllipse(Brushes.Red, x, y, vakgrootte, vakgrootte);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement