kn/Form1.cs
2025-01-29 14:27:59 +07:00

368 lines
10 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.Web;
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();
}
if (!turn && pc)
{
comp_move();
}
}
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";
}
private Button win_or_block(string mark)
{
// Горизонтальные линии
if (A1.Text == mark && A2.Text == mark && A3.Text == "") return A3;
if (A1.Text == mark && A3.Text == mark && A2.Text == "") return A2;
if (A2.Text == mark && A3.Text == mark && A1.Text == "") return A1;
if (B1.Text == mark && B2.Text == mark && B3.Text == "") return B3;
if (B1.Text == mark && B3.Text == mark && B2.Text == "") return B2;
if (B2.Text == mark && B3.Text == mark && B1.Text == "") return B1;
if (C1.Text == mark && C2.Text == mark && C3.Text == "") return C3;
if (C1.Text == mark && C3.Text == mark && C2.Text == "") return C2;
if (C2.Text == mark && C3.Text == mark && C1.Text == "") return C1;
// Вертикальные линии
if (A1.Text == mark && B1.Text == mark && C1.Text == "") return C1;
if (A1.Text == mark && C1.Text == mark && B1.Text == "") return B1;
if (B1.Text == mark && C1.Text == mark && A1.Text == "") return A1;
if (A2.Text == mark && B2.Text == mark && C2.Text == "") return C2;
if (A2.Text == mark && C2.Text == mark && B2.Text == "") return B2;
if (B2.Text == mark && C2.Text == mark && A2.Text == "") return A2;
if (A3.Text == mark && B3.Text == mark && C3.Text == "") return C3;
if (A3.Text == mark && C3.Text == mark && B3.Text == "") return B3;
if (B3.Text == mark && C3.Text == mark && A3.Text == "") return A3;
// Диагонали
if (A1.Text == mark && B2.Text == mark && C3.Text == "") return C3;
if (A1.Text == mark && C3.Text == mark && B2.Text == "") return B2;
if (B2.Text == mark && C3.Text == mark && A1.Text == "") return A1;
if (A3.Text == mark && B2.Text == mark && C1.Text == "") return C1;
if (A3.Text == mark && C1.Text == mark && B2.Text == "") return B2;
if (B2.Text == mark && C1.Text == mark && A3.Text == "") return A3;
return null;
}
private Button corner()
{
if (A1.Text == "O")
{
if (A3.Text == "")
{
return A3;
}
if (C3.Text == "")
{
return C3;
}
if (C1.Text == "")
{
return C1;
}
}
if (A3.Text == "")
{
if (A1.Text == "")
{
return A1;
}
if (C3.Text == "")
{
return C3;
}
if (C1.Text == "")
{
return C1;
}
}
if (C1.Text == "")
{
if (A1.Text == "")
{
return A1;
}
if (C3.Text == "")
{
return C3;
}
if (A3.Text == "")
{
return A3;
}
}
if (C3.Text == "")
{
if (A1.Text == "")
{
return A1;
}
if (C1.Text == "")
{
return A3;
}
if (A3.Text == "")
{
return A3;
}
}
return null;
}
//private Button corner()
//{
// if (A1.Text == "") return A1;
// if (A3.Text == "") return A3;
// if (C1.Text == "") return C1;
// if (C3.Text == "") return C3;
// return null;
//}
private Button open_space()
{
Button b = null;
foreach (Control c in Controls)
{
b = c as Button;
if (b != null)
{
if (b.Text == "") return b;
}
}
return null;
}
private void comp_move()
{
Button move = null;
move = win_or_block("O");
if (move == null)
{
move = win_or_block("X");
if (move == null)
{
move = corner();
if (move == null)
{
move = open_space();
}
}
}
move.PerformClick();
}
}
}