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;
- Label roodlabel;
- Label blauwlabel;
- Label statuslabel;
- /* Globale Variabelen */
- // Strings
- // Constants
- const int schermgrootte = 12, vakgrootte = 40;
- // Ints
- int aantal = 4, aantalrood = 0, aantalblauw = 0, kleur = 1 // 0 = leeg, 1 = blauw, 2 = rood
- , maxaantal = schermgrootte * schermgrootte;
- int mouseX, mouseY;
- // Arrays
- int[,] ganzenbord;
- // Bools
- bool rest = false, help = false;
- public Scherm()
- {
- /* Objecten aanmaken */
- this.startknop = new Button();
- this.helpknop = new Button();
- this.roodlabel = new Label();
- this.blauwlabel = new Label();
- this.statuslabel = new Label();
- /* Eigenschappen van objecten */
- // startknop
- this.startknop.Location = new Point(12, 12);
- this.startknop.Name = "startknop";
- this.startknop.Size = new Size(78, 29);
- this.startknop.TabIndex = 0;
- 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";
- // 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";
- // 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.";
- // Form1
- 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.Text = "Reversi door Conrad en Inigo";
- if (schermgrootte >= 8 && schermgrootte <= 10)
- {
- this.ClientSize = new Size(450, 450 + (schermgrootte - 8) * vakgrootte);
- }
- else if (schermgrootte >= 11)
- {
- this.ClientSize = new Size(370 + (schermgrootte - 8) * vakgrootte, 450 + (schermgrootte - 8) * vakgrootte);
- }
- else
- {
- this.ClientSize = new Size(450, 115 + (schermgrootte * vakgrootte));
- }
- // Events
- this.Paint += this.tekenScherm;
- this.MouseClick += this.klik;
- this.startknop.Click += this.nieuwspel;
- this.helpknop.Click += this.helpklik;
- // Arrays
- this.ganzenbord = new int[schermgrootte, schermgrootte];
- int tellertje;
- int tellertje2;
- //initialiseren van het bord, alles op 0 zetten (geen stenen) behalve die eerste 4 stenen.
- for (tellertje = 0; tellertje < schermgrootte; tellertje++)
- {
- for (tellertje2 = 0; tellertje2 < schermgrootte; tellertje2++)
- {
- if (tellertje == (schermgrootte / 2) - 1 && tellertje2 == (schermgrootte / 2) - 1)
- ganzenbord[tellertje, tellertje2] = 1;
- else if (tellertje == (schermgrootte / 2) && tellertje2 == (schermgrootte / 2))
- ganzenbord[tellertje, tellertje2] = 1;
- else if (tellertje == (schermgrootte / 2) - 1 && tellertje2 == (schermgrootte / 2))
- ganzenbord[tellertje, tellertje2] = 2;
- else if (tellertje == (schermgrootte / 2) && tellertje2 == (schermgrootte / 2) - 1)
- ganzenbord[tellertje, tellertje2] = 2;
- else ganzenbord[tellertje, tellertje2] = 0;
- }
- }
- }
- public void helpklik(object o, EventArgs e)
- {
- if (help == true)
- {
- help = false;
- Invalidate();
- }
- else if (help == false)
- {
- help = true;
- Invalidate();
- }
- }
- public void nieuwspel(object o, EventArgs e)
- {
- 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 < schermgrootte; t++)
- {
- for (t2 = 0; t2 < schermgrootte; t2++)
- {
- if (t == (schermgrootte / 2) - 1 && t2 == (schermgrootte / 2) - 1)
- ganzenbord[t, t2] = 1;
- else if (t == (schermgrootte / 2) && t2 == (schermgrootte / 2))
- ganzenbord[t, t2] = 1;
- else if (t == (schermgrootte / 2) - 1 && t2 == (schermgrootte / 2))
- ganzenbord[t, t2] = 2;
- else if (t == (schermgrootte / 2) && t2 == (schermgrootte / 2) - 1)
- ganzenbord[t, t2] = 2;
- else ganzenbord[t, t2] = 0;
- }
- }
- Invalidate();
- }
- public void tekenScherm(object o, PaintEventArgs e)
- {
- Graphics gr = e.Graphics;
- 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();
- for (teller = 0; teller < schermgrootte; teller++)
- {
- tekenvakje(gr, x, y);
- for (teller2 = 0; teller2 < schermgrootte; teller2++)
- {
- tekenvakje(gr, x, y);
- tekenRondje(gr, x, y, ganzenbord[teller, teller2]);
- y = y + vakgrootte;
- }
- x = x + vakgrootte;
- y = 100;
- }
- volgendeZet();
- if (help)
- tekenHelpvakjes(gr);
- }
- //Tekent in de vakken waar de huidige kleur kan zetten (alleen als help knop geklikt is)
- public void tekenHelpvakjes(Graphics gr)
- {
- int x = 15;
- int y = 100;
- for (int t1 = 0; t1 < schermgrootte; t1++)
- {
- for (int t2 = 0; t2 < schermgrootte; t2++)
- {
- if (checkspot(t1, t2, kleur, false) == true && ganzenbord[t1, t2] == 0)
- {
- gr.DrawEllipse(Pens.Gray, x, y, vakgrootte, vakgrootte);
- }
- y = y + vakgrootte;
- }
- x = x + vakgrootte;
- y = 100;
- }
- }
- //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 < schermgrootte; teller++)
- {
- for (teller2 = 0; teller2 < schermgrootte; teller2++)
- {
- if (ganzenbord[teller, teller2] == 1) aantalblauw++;
- else if (ganzenbord[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 < schermgrootte; teller++)
- {
- for (teller2 = 0; teller2 < schermgrootte; teller2++)
- {
- if (ganzenbord[teller, teller2] == 0)
- checkspot(teller, teller2, kleur, false);
- }
- }
- }
- //Tekent het spelvlak
- 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)
- {
- rest = false;
- if (e.X >= 15 && e.Y >= 100 && e.X <= (15 + vakgrootte * schermgrootte - 1) && e.Y <= (100 + vakgrootte * schermgrootte - 1))
- {
- if (aantal < maxaantal)
- {
- mouseX = ((e.X - 15) / vakgrootte * vakgrootte);
- mouseY = ((e.Y - 100) / vakgrootte * vakgrootte);
- if (ganzenbord[(mouseX) / vakgrootte, (mouseY) / vakgrootte] == 0 && checkspot((mouseX / vakgrootte), (mouseY / vakgrootte), kleur, true) == true)
- {
- if (kleur == 1)
- {
- ganzenbord[mouseX / vakgrootte, mouseY / vakgrootte] = kleur;
- kleur = 2;
- this.statuslabel.Text = "Rood is nu aan zet";
- }
- else if (kleur == 2)
- {
- ganzenbord[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.
- public bool checkspot(int mousex, int mousey, int kleuraanzet, bool verander)
- {
- bool res = false;
- // Check boven
- int t;
- for (t = 1; t < mousey && (ganzenbord[mousex, mousey - t] != 0 && ganzenbord[mousex, mousey - t] != kleuraanzet); t++)
- {
- }
- if ((mousey - t) >= 0 && ganzenbord[mousex, mousey - t] == kleuraanzet && t > 1)
- {
- res = true;
- if (verander)
- {
- int t2;
- for (t2 = 1; t2 < t; t2++)
- {
- ganzenbord[mousex, mousey - t2] = kleuraanzet;
- }
- }
- }
- if (res == true)
- rest = true;
- // Check rechts
- for (t = 1; t < schermgrootte - mousex && (ganzenbord[mousex + t, mousey] != 0 && ganzenbord[mousex + t, mousey] != kleuraanzet); t++)
- {
- }
- if ((mousex + t) <= (schermgrootte - 1) && ganzenbord[mousex + t, mousey] == kleuraanzet && t > 1)
- {
- res = true;
- if (verander)
- {
- int t2;
- for (t2 = 1; t2 < t; t2++)
- {
- ganzenbord[mousex + t2, mousey] = kleuraanzet;
- }
- }
- }
- if (res == true)
- rest = true;
- // Check links
- for (t = 1; t < mousex && (ganzenbord[mousex - t, mousey] != 0 && ganzenbord[mousex - t, mousey] != kleuraanzet); t++)
- {
- }
- if ((mousex - t) >= 0 && ganzenbord[mousex - t, mousey] == kleuraanzet && t > 1)
- {
- res = true;
- if (verander)
- {
- int t2;
- for (t2 = 1; t2 < t; t2++)
- {
- ganzenbord[mousex - t2, mousey] = kleuraanzet;
- }
- }
- }
- if (res == true)
- rest = true;
- // Check onder
- for (t = 1; t < schermgrootte - mousey && (ganzenbord[mousex, mousey + t] != 0 && ganzenbord[mousex, mousey + t] != kleuraanzet); t++)
- {
- }
- if ((mousey + t) <= (schermgrootte - 1) && ganzenbord[mousex, mousey + t] == kleuraanzet && t > 1)
- {
- res = true;
- if (verander)
- {
- int t2;
- for (t2 = 1; t2 < t; t2++)
- {
- ganzenbord[mousex, mousey + t2] = kleuraanzet;
- }
- }
- }
- if (res == true)
- rest = true;
- // Check rechtsboven
- for (t = 1; t < mousey && t < schermgrootte - mousex && (ganzenbord[mousex + t, mousey - t] != 0 && ganzenbord[mousex + t, mousey - t] != kleuraanzet); t++)
- {
- }
- if ((mousey - t) >= 0 && (mousex + t) <= (schermgrootte - 1) && ganzenbord[mousex + t, mousey - t] == kleuraanzet && t > 1)
- {
- res = true;
- if (verander)
- {
- int t2;
- for (t2 = 1; t2 < t; t2++)
- {
- ganzenbord[mousex + t2, mousey - t2] = kleuraanzet;
- }
- }
- }
- if (res == true)
- rest = true;
- // Check linksboven
- for (t = 1; t < mousey && t < mousex && (ganzenbord[mousex - t, mousey - t] != 0 && ganzenbord[mousex - t, mousey - t] != kleuraanzet); t++)
- {
- }
- if ((mousey - t) >= 0 && (mousex - t) >= 0 && ganzenbord[mousex - t, mousey - t] == kleuraanzet && t > 1)
- {
- res = true;
- if (verander)
- {
- int t2;
- for (t2 = 1; t2 < t; t2++)
- {
- ganzenbord[mousex - t2, mousey - t2] = kleuraanzet;
- }
- }
- }
- if (res == true)
- rest = true;
- // Check linksonder
- for (t = 1; t < schermgrootte - mousey && t < mousex && (ganzenbord[mousex - t, mousey + t] != 0 && ganzenbord[mousex - t, mousey + t] != kleuraanzet); t++)
- {
- }
- if ((mousex - t) >= 0 && mousey + t <= (schermgrootte - 1) && ganzenbord[mousex - t, mousey + t] == kleuraanzet && t > 1)
- {
- res = true;
- if (verander)
- {
- int t2;
- for (t2 = 1; t2 < t; t2++)
- {
- ganzenbord[mousex - t2, mousey + t2] = kleuraanzet;
- }
- }
- }
- if (res == true)
- rest = true;
- // Check rechtsonder
- for (t = 1; t < schermgrootte - mousey && t < schermgrootte - mousex && (ganzenbord[mousex + t, mousey + t] != 0 && ganzenbord[mousex + t, mousey + t] != kleuraanzet); t++)
- {
- }
- if ((mousex + t) <= (schermgrootte - 1) && (mousey + t) <= (schermgrootte - 1) && ganzenbord[mousex + t, mousey + t] == kleuraanzet && t > 1)
- {
- res = true;
- if (verander)
- {
- int t2;
- for (t2 = 1; t2 < t; t2++)
- {
- ganzenbord[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