Advertisement
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.Threading.Tasks;
- using System.Windows.Forms;
- namespace WindowsFormsApplication1
- {
- public partial class Form1 : Form
- {
- bool Beurt = true;
- int Beurtenteller = 0;
- bool TegenComputer = false;
- public Form1()
- {
- InitializeComponent();
- }
- private void spelVerlatenToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Application.Exit();
- }
- private void Button_Click(object sender, EventArgs e)
- {
- Button b = (Button)sender;
- if (Beurt)
- b.Text = "X";
- else
- b.Text = "O";
- Beurt = !Beurt;
- b.Enabled = false;
- Beurtenteller = Beurtenteller + 1;
- Kijkenvoorwinnaar();
- //Kijken of je tegen de computer speelt
- if ((!Beurt) && (TegenComputer))
- {
- ComputerMaaktZet();
- }
- }
- private void Kijkenvoorwinnaar()
- {
- Boolean Eriseenwinnaar = false;
- //Horizontaal
- if ((A1.Text == A2.Text) && (A2.Text == A3.Text) && (!A1.Enabled))
- Eriseenwinnaar = true;
- else if ((B1.Text == B2.Text) && (B2.Text == B3.Text) && (!B1.Enabled))
- Eriseenwinnaar = true;
- else if ((C1.Text == C2.Text) && (C2.Text == C3.Text) && (!C1.Enabled))
- Eriseenwinnaar = true;
- //Verticaal
- if ((A1.Text == B1.Text) && (B1.Text == C1.Text) && (!A1.Enabled))
- Eriseenwinnaar = true;
- else if ((A2.Text == B2.Text) && (B2.Text == C2.Text) && (!A2.Enabled))
- Eriseenwinnaar = true;
- else if ((A3.Text == B3.Text) && (B3.Text == C3.Text) && (!A3.Enabled))
- Eriseenwinnaar = true;
- //Diagonaal
- if ((A1.Text == B2.Text) && (B2.Text == C3.Text) && (!A1.Enabled))
- Eriseenwinnaar = true;
- else if ((A3.Text == B2.Text) && (B2.Text == C1.Text) && (!A3.Enabled))
- Eriseenwinnaar = true;
- if(Eriseenwinnaar)
- {
- ButtonsAfsluiten();
- String Winnaar = "";
- if (Beurt)
- {
- Winnaar = Player2.Text;
- OGewonnen.Text = (Int32.Parse(OGewonnen.Text) + 1).ToString();
- }
- else
- {
- Winnaar = Player1.Text;
- XGewonnen.Text = (Int32.Parse(XGewonnen.Text) + 1).ToString();
- }
- MessageBox.Show(Winnaar + " Heeft gewonnen!", " Gefeliciteerd!");
- }// einde if
- else
- {
- if (Beurtenteller == 9)
- {
- Gelijk.Text = (Int32.Parse(Gelijk.Text) + 1).ToString();
- MessageBox.Show("Gelijk!", "Jammer!");
- }
- }
- }// Einde Kijkenvoorwinnaar
- private void ButtonsAfsluiten()
- {
- try
- {
- foreach (Control c in Controls)
- {
- Button b = (Button)c;
- b.Enabled = false;
- }// Einde Foreach
- }// einde Try
- catch { }
- }// Einde ButtonsAfsluiten
- private void nieuwSpelToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Beurt = true;
- Beurtenteller = 0;
- foreach (Control c in Controls)
- {
- try
- {
- Button b = (Button)c;
- b.Enabled = true;
- b.Text = "";
- }// einde Try
- catch { }
- }// Einde Foreach
- }
- private void Button_Enter(object sender, EventArgs e)
- {
- Button b = (Button)sender;
- if (b.Enabled)
- {
- if (Beurt)
- b.Text = "X";
- else
- b.Text = "O";
- } //einde If
- }
- private void Button_Leave(object sender, EventArgs e)
- {
- Button b = (Button)sender;
- if (b.Enabled)
- {
- b.Text = "";
- } // einde If
- }
- private void label2_Click(object sender, EventArgs e)
- {
- }
- private void label3_Click(object sender, EventArgs e)
- {
- }
- private void label6_Click(object sender, EventArgs e)
- {
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- MessageBox.Show("Hallo, Het spel staat automatisch opgesteld voor 2 personen, om tegen de computer te spelen typ computer in het vak van speler doet dit VOOR je de eerste zet maakt");
- }
- private void resetScoresToolStripMenuItem_Click(object sender, EventArgs e)
- {
- XGewonnen.Text = "0";
- OGewonnen.Text = "0";
- Gelijk.Text = "0";
- }
- private void Player2_TextChanged(object sender, EventArgs e)
- {
- if (Player2.Text.ToUpper() == "COMPUTER")
- TegenComputer = true;
- else
- TegenComputer = false;
- }
- private void standaardSpeelinstellingToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Player1.Text = "Rico";
- Player2.Text = "Computer";
- }
- private void ComputerMaaktZet()
- {
- Button move = null;
- //Kijken voor mogelijkheden
- move = WinOfBlokZoeken("O"); //Zoek een winkans
- if (move == null)
- {
- move = WinOfBlokZoeken("X"); //Zoek een blok
- if (move == null)
- {
- move = HoekZoeken();
- if (move == null)
- {
- move = OpenPlekZoeken();
- }//end if
- }//end if
- }//end if
- move.PerformClick();
- }
- private Button OpenPlekZoeken()
- {
- Console.WriteLine("Looking for open space");
- Button b = null;
- foreach (Control c in Controls)
- {
- b = c as Button;
- if (b != null)
- {
- if (b.Text == "")
- return b;
- }//end if
- }//end if
- return null;
- }
- private Button HoekZoeken()
- {
- Console.WriteLine("Looking for corner");
- if (A1.Text == "O")
- {
- if (A3.Text == "")
- return A3;
- if (C3.Text == "")
- return C3;
- if (C1.Text == "")
- return C1;
- }
- if (A3.Text == "O")
- {
- if (A1.Text == "")
- return A1;
- if (C3.Text == "")
- return C3;
- if (C1.Text == "")
- return C1;
- }
- if (C3.Text == "O")
- {
- if (A1.Text == "")
- return A3;
- if (A3.Text == "")
- return A3;
- if (C1.Text == "")
- return C1;
- }
- if (C1.Text == "O")
- {
- if (A1.Text == "")
- return A3;
- if (A3.Text == "")
- return A3;
- if (C3.Text == "")
- return C3;
- }
- if (A1.Text == "")
- return A1;
- if (A3.Text == "")
- return A3;
- if (C1.Text == "")
- return C1;
- if (C3.Text == "")
- return C3;
- return null;
- }
- private Button WinOfBlokZoeken(string mark)
- {
- Console.WriteLine("Kijk voor win of blok: " + mark);
- //Horizontaal
- if ((A1.Text == mark) && (A2.Text == mark) && (A3.Text == ""))
- return A3;
- if ((A2.Text == mark) && (A3.Text == mark) && (A1.Text == ""))
- return A1;
- if ((A1.Text == mark) && (A3.Text == mark) && (A2.Text == ""))
- return A2;
- if ((B1.Text == mark) && (B2.Text == mark) && (B3.Text == ""))
- return B3;
- if ((B2.Text == mark) && (B3.Text == mark) && (B1.Text == ""))
- return B1;
- if ((B1.Text == mark) && (B3.Text == mark) && (B2.Text == ""))
- return B2;
- if ((C1.Text == mark) && (C2.Text == mark) && (C3.Text == ""))
- return C3;
- if ((C2.Text == mark) && (C3.Text == mark) && (C1.Text == ""))
- return C1;
- if ((C1.Text == mark) && (C3.Text == mark) && (C2.Text == ""))
- return C2;
- //Horizontaal
- if ((A1.Text == mark) && (B1.Text == mark) && (C1.Text == ""))
- return C1;
- if ((B1.Text == mark) && (C1.Text == mark) && (A1.Text == ""))
- return A1;
- if ((A1.Text == mark) && (C1.Text == mark) && (B1.Text == ""))
- return B1;
- if ((A2.Text == mark) && (B2.Text == mark) && (C2.Text == ""))
- return C2;
- if ((B2.Text == mark) && (C2.Text == mark) && (A2.Text == ""))
- return A2;
- if ((A2.Text == mark) && (C2.Text == mark) && (B2.Text == ""))
- return B2;
- if ((A3.Text == mark) && (B3.Text == mark) && (C3.Text == ""))
- return C3;
- if ((B3.Text == mark) && (C3.Text == mark) && (A3.Text == ""))
- return A3;
- if ((A3.Text == mark) && (C3.Text == mark) && (B3.Text == ""))
- return B3;
- //Diagonaal
- if ((A1.Text == mark) && (B2.Text == mark) && (C3.Text == ""))
- return C3;
- if ((B2.Text == mark) && (C3.Text == mark) && (A1.Text == ""))
- return A1;
- if ((A1.Text == mark) && (C3.Text == mark) && (B2.Text == ""))
- return B2;
- if ((A3.Text == mark) && (B2.Text == mark) && (C1.Text == ""))
- return C1;
- if ((B2.Text == mark) && (C1.Text == mark) && (A3.Text == ""))
- return A3;
- if ((A3.Text == mark) && (C1.Text == mark) && (B2.Text == ""))
- return B2;
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement