tic_tac_toe/Form1.cs

214 lines
5.5 KiB
C#
Raw Permalink Normal View History

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
bool turn = true;
int turn_count;
static string player1, player2;
bool pc = false;
public static void setName(String n1, String n2)
{
player1= n1;
player2= n2;
}
private void A1_MouseEnter(object sender, EventArgs e)
{
Button b = (Button)sender;
if (turn)
{
b.Text = "X";
}
else
{
b.Text = "O";
}
}
private void A1_MouseLeave(object sender, EventArgs e)
{
Button b = (Button)sender;
if (b.Enabled == true)
b.Text = "";
}
private void A1_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
turn_count++;
if (turn)
{
b.Text = "X";
b.Enabled = false;
turn = false;
Winner();
}
else
{
b.Text = "O";
b.Enabled = false;
turn = true;
Winner();
}
}
private void Enabled_b()
{
Button b = null;
foreach (Control c in Controls)
{
b = c as Button;
if (b != null)
{
b.Enabled = true;
b.Text = "";
}
}
}
private void Disable_b()
{
foreach (Control c in Controls)
{
if (c is Button b && b.Enabled)
{
b.Enabled = false;
}
}
}
private void Winner()
{
bool win = false;
if (A1.Text == A2.Text && A2.Text == A3.Text && A1.Enabled == false)
{
win= true;
}
if (B1.Text == B2.Text && B2.Text == B3.Text && B1.Enabled == false)
{
win = true;
}
if (C1.Text == C2.Text && C2.Text == C3.Text && C1.Enabled == false)
{
win = true;
}
if (A1.Text == B1.Text && B1.Text == C1.Text && A1.Enabled == false)
{
win = true;
}
if (A2.Text == B2.Text && B2.Text == C2.Text && A2.Enabled == false)
{
win = true;
}
if (A3.Text == B3.Text && B3.Text == C3.Text && A3.Enabled == false)
{
win = true;
}
if (A1.Text == B2.Text && B2.Text == C3.Text && A1.Enabled == false)
{
win = true;
}
if (A3.Text == B2.Text && B2.Text == C1.Text && A3.Enabled == false)
{
win = true;
}
if (win == true)
{
if (turn == false)
{
label4.Text = (int.Parse(label4.Text)+1).ToString();
MessageBox.Show("Победил Х");
Enabled_b();
turn_count = 0;
turn = true;
}
else
{
label6.Text = (int.Parse(label6.Text) + 1).ToString();
MessageBox.Show("Победил O");
Enabled_b();
turn_count = 0;
turn = true;
}
}
if (!win && turn_count == 9)
{
label5.Text = (int.Parse(label5.Text) + 1).ToString();
MessageBox.Show("Ничья");
Enabled_b();
turn_count = 0;
turn = true;
}
}
private void новаяИграToolStripMenuItem_Click(object sender, EventArgs e)
{
Enabled_b();
turn_count = 0;
turn = true;
label4.Text = "0";
label5.Text = "0";
label6.Text = "0";
}
private void Form1_Load(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog();
label1.Text = player1 + ":";
label2.Text = player2 + ":";
if (player2 == "PC")
{
pc = true;
}
}
private void изменитьИгроковToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog();
label1.Text = player1;
label2.Text = player2;
сброситьОчкиToolStripMenuItem.PerformClick();
новаяИграToolStripMenuItem.PerformClick();
this.Show();
}
private void сброситьОчкиToolStripMenuItem_Click(object sender, EventArgs e)
{
Enabled_b();
turn_count = 0;
turn = true;
label4.Text = "0";
label5.Text = "0";
label6.Text = "0";
}
}
}