Advertisement
Guest User

Tic Tac Toe

a guest
Jun 3rd, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.47 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace TicTacToe
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         bool turn = true;
  15.         int turn_count = 0;
  16.  
  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.         }
  21.  
  22.         private void beendenToolStripMenuItem_Click(object sender, EventArgs e)
  23.         {
  24.             Application.Exit();
  25.         }
  26.  
  27.         private void Form1_Load(object sender, EventArgs e)
  28.         {
  29.  
  30.         }
  31.  
  32.         private void button_Click(object sender, EventArgs e)
  33.         {
  34.             Button b = (Button)sender;
  35.             b.Enabled = false;
  36.             b.Text = (turn) ? "X" : "O";
  37.  
  38.             turn = !turn;
  39.             turn_count++;
  40.  
  41.             Check_for_winner();
  42.         }
  43.  
  44.         private void Check_for_winner()
  45.         {
  46.             bool win = false;
  47.  
  48.             if ((A1.Text == A2.Text) && (A1.Text == A3.Text) && !A1.Enabled)
  49.                 win = true;
  50.             else if ((B1.Text == B2.Text) && (B1.Text == B3.Text) && !B1.Enabled)
  51.                 win = true;
  52.             else if ((C1.Text == C2.Text) && (C1.Text == C3.Text) && !C1.Enabled)
  53.                 win = true;
  54.             else if ((A1.Text == B1.Text) && (A1.Text == C1.Text) && !A1.Enabled)
  55.                 win = true;
  56.             else if ((A2.Text == B2.Text) && (A2.Text == C2.Text) && !A2.Enabled)
  57.                 win = true;
  58.             else if ((A3.Text == B3.Text) && (A3.Text == C3.Text) && !A3.Enabled)
  59.                 win = true;
  60.             else if ((A1.Text == B2.Text) && (A1.Text == C3.Text) && !A1.Enabled)
  61.                 win = true;
  62.             else if ((A3.Text == B2.Text) && (A3.Text == C1.Text) && !C1.Enabled)
  63.                 win = true;
  64.  
  65.  
  66.             if (win)
  67.             {
  68.                 DisableButtons();
  69.                 string winner = (turn) ? "O" : "X";
  70.                 MessageBox.Show(winner + " Wins!","We have a Winner");
  71.             }
  72.             else
  73.             {
  74.                 if (turn_count == 9)
  75.                 {
  76.                     MessageBox.Show("Draw!");
  77.                 }
  78.             }
  79.         }
  80.  
  81.         private void neuesSpielToolStripMenuItem_Click(object sender, EventArgs e)
  82.         {
  83.             foreach (Control c in Controls)
  84.             {
  85.                 try
  86.                 {
  87.                     Button b = (Button)c;
  88.                     b.Text = "";
  89.                     b.Enabled = true;
  90.                 }
  91.                 catch { }
  92.             }
  93.             turn = true;
  94.             turn_count = 0;
  95.         }
  96.  
  97.         private void DisableButtons()
  98.         {
  99.             foreach (Control c in Controls)
  100.             {
  101.                 try
  102.                 {
  103.                     Button b = (Button)c;
  104.                     b.Enabled = false;
  105.                 }
  106.                 catch { }
  107.             }
  108.  
  109.         }
  110.  
  111.         private void button_MouseEnter(object sender, EventArgs e)
  112.         {
  113.             Button b = (Button)sender;
  114.             if (b.Enabled)
  115.                 b.Text = (turn) ? "X" : "O";
  116.         }
  117.  
  118.         private void button_MouseLeave(object sender, EventArgs e)
  119.         {
  120.             Button b = (Button)sender;
  121.             if (b.Enabled)
  122.                 b.Text = "";
  123.         }
  124.  
  125.         private void buttonComputer_Click(object sender, EventArgs e)
  126.         {
  127.  
  128.         }
  129.  
  130.  
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement