Перевод кода игры Крестики-нолики из Delphi в C#
Формулировка задачи:
Доброго времени суток. Можете перевести код из Delphi в C#
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
Label9: TLabel;
Button1: TButton;
RadioGroup1: TRadioGroup;
GroupBox1: TGroupBox;
Label10: TLabel;
Label11: TLabel;
Label12: TLabel;
procedure Button1Click(Sender: TObject);
procedure Label1Click(Sender: TObject);
procedure Label2Click(Sender: TObject);
procedure Label3Click(Sender: TObject);
procedure Label4Click(Sender: TObject);
procedure Label5Click(Sender: TObject);
procedure Label6Click(Sender: TObject);
procedure Label7Click(Sender: TObject);
procedure Label8Click(Sender: TObject);
procedure Label9Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
procedure VirtualMind;
procedure OutPut;
procedure Winer;
procedure TheEnd(q: integer);
var
Form1: TForm1;
M: array[1..9] of integer;
win_men, win_comp, win_nobody: integer;
implementation
{$R *.dfm}
procedure VirtualMind;
var
i, m_z: integer;
Z: array[1..9] of integer;
begin
if (M[1]=2) and (M[2]=2) and (M[3]=0) then begin M[3]:=2; exit; end;
if (M[1]=2) and (M[4]=2) and (M[7]=0) then begin M[7]:=2; exit; end;
if (M[1]=2) and (M[5]=2) and (M[9]=0) then begin M[9]:=2; exit; end;
if (M[1]=2) and (M[7]=2) and (M[4]=0) then begin M[4]:=2; exit; end;
if (M[1]=2) and (M[9]=2) and (M[5]=0) then begin M[5]:=2; exit; end;
if (M[1]=2) and (M[3]=2) and (M[2]=0) then begin M[2]:=2; exit; end;
if (M[2]=2) and (M[5]=2) and (M[8]=0) then begin M[8]:=2; exit; end;
if (M[2]=2) and (M[3]=2) and (M[1]=0) then begin M[1]:=2; exit; end;
if (M[2]=2) and (M[8]=2) and (M[5]=0) then begin M[5]:=2; exit; end;
if (M[3]=2) and (M[5]=2) and (M[7]=0) then begin M[7]:=2; exit; end;
if (M[3]=2) and (M[6]=2) and (M[9]=0) then begin M[9]:=2; exit; end;
if (M[3]=2) and (M[7]=2) and (M[5]=0) then begin M[5]:=2; exit; end;
if (M[3]=2) and (M[9]=2) and (M[6]=0) then begin M[6]:=2; exit; end;
if (M[4]=2) and (M[5]=2) and (M[6]=0) then begin M[6]:=2; exit; end;
if (M[4]=2) and (M[7]=2) and (M[1]=0) then begin M[1]:=2; exit; end;
if (M[4]=2) and (M[6]=2) and (M[5]=0) then begin M[5]:=2; exit; end;
if (M[5]=2) and (M[7]=2) and (M[3]=0) then begin M[3]:=2; exit; end;
if (M[5]=2) and (M[8]=2) and (M[2]=0) then begin M[2]:=2; exit; end;
if (M[5]=2) and (M[6]=2) and (M[4]=0) then begin M[4]:=2; exit; end;
if (M[6]=2) and (M[9]=2) and (M[3]=0) then begin M[3]:=2; exit; end;
if (M[7]=2) and (M[8]=2) and (M[9]=0) then begin M[9]:=2; exit; end;
if (M[7]=2) and (M[9]=2) and (M[8]=0) then begin M[8]:=2; exit; end;
if (M[8]=2) and (M[9]=2) and (M[7]=0) then begin M[7]:=2; exit; end;
if (M[1]=1) and (M[2]=1) and (M[3]=0) then begin M[3]:=2; exit; end;
if (M[1]=1) and (M[4]=1) and (M[7]=0) then begin M[7]:=2; exit; end;
if (M[1]=1) and (M[5]=1) and (M[9]=0) then begin M[9]:=2; exit; end;
if (M[1]=1) and (M[7]=1) and (M[4]=0) then begin M[4]:=2; exit; end;
if (M[1]=1) and (M[9]=1) and (M[5]=0) then begin M[5]:=2; exit; end;
if (M[1]=1) and (M[3]=1) and (M[2]=0) then begin M[2]:=2; exit; end;
if (M[2]=1) and (M[5]=1) and (M[8]=0) then begin M[8]:=2; exit; end;
if (M[2]=1) and (M[3]=1) and (M[1]=0) then begin M[1]:=2; exit; end;
if (M[2]=1) and (M[8]=1) and (M[5]=0) then begin M[5]:=2; exit; end;
if (M[3]=1) and (M[5]=1) and (M[7]=0) then begin M[7]:=2; exit; end;
if (M[3]=1) and (M[6]=1) and (M[9]=0) then begin M[9]:=2; exit; end;
if (M[3]=1) and (M[7]=1) and (M[5]=0) then begin M[5]:=2; exit; end;
if (M[3]=1) and (M[9]=1) and (M[6]=0) then begin M[6]:=2; exit; end;
if (M[4]=1) and (M[5]=1) and (M[6]=0) then begin M[6]:=2; exit; end;
if (M[4]=1) and (M[7]=1) and (M[1]=0) then begin M[1]:=2; exit; end;
if (M[4]=1) and (M[6]=1) and (M[5]=0) then begin M[5]:=2; exit; end;
if (M[5]=1) and (M[7]=1) and (M[3]=0) then begin M[3]:=2; exit; end;
if (M[5]=1) and (M[8]=1) and (M[2]=0) then begin M[2]:=2; exit; end;
if (M[5]=1) and (M[6]=1) and (M[4]=0) then begin M[4]:=2; exit; end;
if (M[6]=1) and (M[9]=1) and (M[3]=0) then begin M[3]:=2; exit; end;
if (M[7]=1) and (M[8]=1) and (M[9]=0) then begin M[9]:=2; exit; end;
if (M[7]=1) and (M[9]=1) and (M[8]=0) then begin M[8]:=2; exit; end;
if (M[8]=1) and (M[9]=1) and (M[7]=0) then begin M[7]:=2; exit; end;
if (M[5]=0) then begin M[5]:=2; exit; end;
for i:=1 to 9 do Z[i]:=0;
if (M[1]=0) and (M[2]<>1) and (M[3]<>1) and (M[4]<>1) and (M[7]<>1) then begin
if M[2]=1 then Z[1]:=Z[1]+1;
if M[3]=1 then Z[1]:=Z[1]+1;
if M[4]=1 then Z[1]:=Z[1]+1;
if M[7]=1 then Z[1]:=Z[1]+1;
end;
if (M[2]=0) and (M[1]<>1) and (M[3]<>1) and (M[5]<>1) and (M[8]<>1) then begin
if M[1]=1 then Z[2]:=Z[2]+1;
if M[3]=1 then Z[2]:=Z[2]+1;
if M[5]=1 then Z[2]:=Z[2]+1;
if M[8]=1 then Z[2]:=Z[2]+1;
end;
if (M[3]=0) and (M[1]<>1) and (M[2]<>1) and (M[6]<>1) and (M[9]<>1) then begin
if M[1]=1 then Z[3]:=Z[3]+1;
if M[2]=1 then Z[3]:=Z[3]+1;
if M[6]=1 then Z[3]:=Z[3]+1;
if M[9]=1 then Z[3]:=Z[3]+1;
end;
if (M[4]=0) and (M[1]<>1) and (M[7]<>1) and (M[5]<>1) and (M[6]<>1) then begin
if M[1]=1 then Z[4]:=Z[4]+1;
if M[7]=1 then Z[4]:=Z[4]+1;
if M[5]=1 then Z[4]:=Z[4]+1;
if M[6]=1 then Z[4]:=Z[4]+1;
end;
if (M[6]=0) and (M[3]<>1) and (M[9]<>1) and (M[4]<>1) and (M[5]<>1) then begin
if M[3]=1 then Z[6]:=Z[6]+1;
if M[9]=1 then Z[6]:=Z[6]+1;
if M[4]=1 then Z[6]:=Z[6]+1;
if M[5]=1 then Z[6]:=Z[6]+1;
end;
if (M[7]=0) and (M[1]<>1) and (M[4]<>1) and (M[8]<>1) and (M[9]<>1) then begin
if M[1]=1 then Z[7]:=Z[7]+1;
if M[4]=1 then Z[7]:=Z[7]+1;
if M[8]=1 then Z[7]:=Z[7]+1;
if M[9]=1 then Z[7]:=Z[7]+1;
end;
if (M[8]=0) and (M[7]<>1) and (M[9]<>1) and (M[2]<>1) and (M[5]<>1) then begin
if M[7]=1 then Z[8]:=Z[8]+1;
if M[9]=1 then Z[8]:=Z[8]+1;
if M[2]=1 then Z[8]:=Z[8]+1;
if M[5]=1 then Z[8]:=Z[8]+1;
end;
if (M[9]=0) and (M[3]<>1) and (M[6]<>1) and (M[7]<>1) and (M[8]<>1) then begin
if M[3]=1 then Z[9]:=Z[9]+1;
if M[6]=1 then Z[9]:=Z[9]+1;
if M[7]=1 then Z[9]:=Z[9]+1;
if M[8]=1 then Z[9]:=Z[9]+1;
end;
m_z:=1;
for i:=2 to 9 do if Z[i]>Z[m_z] then m_z:=i;
if Z[m_z]>1 then begin
M[m_z]:=2;
exit;
end;
if (M[1]=2) and (M[3]=0) and (M[2]=0) then begin M[2]:=2; exit; end;
if (M[1]=2) and (M[7]=0) and (M[4]=0) then begin M[4]:=2; exit; end;
if (M[1]=2) and (M[9]=0) and (M[5]=0) then begin M[5]:=2; exit; end;
if (M[2]=2) and (M[1]=0) and (M[3]=0) then begin M[1]:=2; exit; end;
if (M[2]=2) and (M[5]=0) and (M[8]=0) then begin M[5]:=2; exit; end;
if (M[3]=2) and (M[1]=0) and (M[2]=0) then begin M[2]:=2; exit; end;
if (M[3]=2) and (M[5]=0) and (M[7]=0) then begin M[5]:=2; exit; end;
if (M[3]=2) and (M[6]=0) and (M[9]=0) then begin M[6]:=2; exit; end;
if (M[4]=2) and (M[1]=0) and (M[7]=0) then begin M[1]:=2; exit; end;
if (M[4]=2) and (M[5]=0) and (M[6]=0) then begin M[5]:=2; exit; end;
if (M[6]=2) and (M[9]=0) and (M[3]=0) then begin M[9]:=2; exit; end;
if (M[7]=2) and (M[4]=0) and (M[1]=0) then begin M[1]:=2; exit; end;
if (M[7]=2) and (M[8]=0) and (M[9]=0) then begin M[8]:=2; exit; end;
if (M[8]=2) and (M[7]=0) and (M[9]=0) then begin M[7]:=2; exit; end;
if (M[9]=2) and (M[8]=0) and (M[7]=0) then begin M[8]:=2; exit; end;
for i:=1 to 9 do if M[i]=0 then begin
M[i]:=2;
exit;
end;
end;
procedure OutPut;
begin
if M[1]=2 then begin Form1.Label1.Caption:='0'; Form1.Label1.Enabled:=false; end;
if M[2]=2 then begin Form1.Label2.Caption:='0'; Form1.Label2.Enabled:=false; end;
if M[3]=2 then begin Form1.Label3.Caption:='0'; Form1.Label3.Enabled:=false; end;
if M[4]=2 then begin Form1.Label4.Caption:='0'; Form1.Label4.Enabled:=false; end;
if M[5]=2 then begin Form1.Label5.Caption:='0'; Form1.Label5.Enabled:=false; end;
if M[6]=2 then begin Form1.Label6.Caption:='0'; Form1.Label6.Enabled:=false; end;
if M[7]=2 then begin Form1.Label7.Caption:='0'; Form1.Label7.Enabled:=false; end;
if M[8]=2 then begin Form1.Label8.Caption:='0'; Form1.Label8.Enabled:=false; end;
if M[9]=2 then begin Form1.Label9.Caption:='0'; Form1.Label9.Enabled:=false; end;
end;
procedure Winer;
var
i: integer;
nobody: boolean;
begin
for i:=1 to 2 do begin
if (M[1]=i) and (M[4]=i) and (M[7]=i) then begin TheEnd(i); exit; end;
if (M[1]=i) and (M[5]=i) and (M[9]=i) then begin TheEnd(i); exit; end;
if (M[1]=i) and (M[2]=i) and (M[3]=i) then begin TheEnd(i); exit; end;
if (M[2]=i) and (M[5]=i) and (M[8]=i) then begin TheEnd(i); exit; end;
if (M[3]=i) and (M[5]=i) and (M[7]=i) then begin TheEnd(i); exit; end;
if (M[4]=i) and (M[5]=i) and (M[6]=i) then begin TheEnd(i); exit; end;
if (M[6]=i) and (M[3]=i) and (M[9]=i) then begin TheEnd(i); exit; end;
if (M[7]=i) and (M[8]=i) and (M[9]=i) then begin TheEnd(i); exit; end;
end;
nobody:=true;
for i:=1 to 9 do if M[i]=0 then nobody:=false;
if nobody then begin
ShowMessage('Ничья');
win_nobody:=win_nobody+1;
Form1.Label12.Caption:='Ничья: '+IntToStr(win_nobody);
end;
end;
procedure TheEnd(q: integer);
begin
if q=1 then begin
ShowMessage('Победа игрока!');
win_men:=win_men+1;
end
else begin
ShowMessage('Победа компьютера');
win_comp:=win_comp+1;
end;
Form1.Label1.Enabled:=false;
Form1.Label2.Enabled:=false;
Form1.Label3.Enabled:=false;
Form1.Label4.Enabled:=false;
Form1.Label5.Enabled:=false;
Form1.Label6.Enabled:=false;
Form1.Label7.Enabled:=false;
Form1.Label8.Enabled:=false;
Form1.Label9.Enabled:=false;
Form1.Label10.Caption:='Человек: '+IntToStr(win_men);
Form1.Label11.Caption:='Компьютер: '+IntToStr(win_comp);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
begin
for i:=1 to 9 do M[i]:=0;
Label1.Enabled:=true;
Label2.Enabled:=true;
Label3.Enabled:=true;
Label4.Enabled:=true;
Label5.Enabled:=true;
Label6.Enabled:=true;
Label7.Enabled:=true;
Label8.Enabled:=true;
Label9.Enabled:=true;
Label1.Caption:='';
Label2.Caption:='';
Label3.Caption:='';
Label4.Caption:='';
Label5.Caption:='';
Label6.Caption:='';
Label7.Caption:='';
Label8.Caption:='';
Label9.Caption:='';
if RadioGroup1.ItemIndex=1 then
begin
VirtualMind;
OutPut;
Winer;
end;
end;
procedure TForm1.Label1Click(Sender: TObject);
begin
M[1]:=1;
Label1.Caption:='X';
Label1.Enabled:=false;
VirtualMind;
OutPut;
Winer;
end;
procedure TForm1.Label2Click(Sender: TObject);
begin
M[2]:=1;
Label2.Caption:='X';
Label2.Enabled:=false;
VirtualMind;
OutPut;
Winer;
end;
procedure TForm1.Label3Click(Sender: TObject);
begin
M[3]:=1;
Label3.Caption:='X';
Label3.Enabled:=false;
VirtualMind;
OutPut;
Winer;
end;
procedure TForm1.Label4Click(Sender: TObject);
begin
M[4]:=1;
Label4.Caption:='X';
Label4.Enabled:=false;
VirtualMind;
OutPut;
Winer;
end;
procedure TForm1.Label5Click(Sender: TObject);
begin
M[5]:=1;
Label5.Caption:='X';
Label5.Enabled:=false;
VirtualMind;
OutPut;
Winer;
end;
procedure TForm1.Label6Click(Sender: TObject);
begin
M[6]:=1;
Label6.Caption:='X';
Label6.Enabled:=false;
VirtualMind;
OutPut;
Winer;
end;
procedure TForm1.Label7Click(Sender: TObject);
begin
M[7]:=1;
Label7.Caption:='X';
Label7.Enabled:=false;
VirtualMind;
OutPut;
Winer;
end;
procedure TForm1.Label8Click(Sender: TObject);
begin
M[8]:=1;
Label8.Caption:='X';
Label8.Enabled:=false;
VirtualMind;
OutPut;
Winer;
end;
procedure TForm1.Label9Click(Sender: TObject);
begin
M[9]:=1;
Label9.Caption:='X';
Label9.Enabled:=false;
VirtualMind;
OutPut;
Winer;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
win_men:=0;
win_comp:=0;
win_nobody:=0;
Label10.Caption:='Человек: '+IntToStr(win_men);
Label11.Caption:='Компьютер: '+IntToStr(win_comp);
Label12.Caption:='Ничья: '+IntToStr(win_nobody);
end;
end.Решение задачи: «Перевод кода игры Крестики-нолики из Delphi в C#»
textual
Листинг программы
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 XO
{
public partial class Form1 : Form
{
public int [] M = new int[9];
public int win_nobody;
public int win_men;
public int win_comp;
public Form1()
{
InitializeComponent();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
M[0] = 1;
pictureBox1.Enabled = false;
pictureBox1.Image = global::XO.Properties.Resources.X;
VirtualMind();
OutPut();
Winer();
}
// Тоже самое для всех pictureBox-ов.
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Enabled = false;
// -//- -//- //
pictureBox9.Enabled = false;
}
private void VirtualMind()
{
// Проверяем все варианты с М[0]
if ((M[0] == 2) && (M[1] == 2) && (M[2] == 0)) { M[2] = 2; return; } // 0 1 2
if ((M[0] == 2) && (M[3] == 2) && (M[6] == 0)) { M[6] = 2; return; } // 3 4 5
if ((M[0] == 2) && (M[6] == 2) && (M[3] == 0)) { M[3] = 2; return; } // 6 7 8
if ((M[0] == 2) && (M[2] == 2) && (M[1] == 0)) { M[1] = 2; return; } //
if ((M[0] == 2) && (M[4] == 2) && (M[8] == 0)) { M[8] = 2; return; }
if ((M[0] == 2) && (M[8] == 2) && (M[4] == 0)) { M[4] = 2; return; }
// Проверяем все варианты с М[1]
if ((M[1] == 2) && (M[2] == 2) && (M[0] == 0)) { M[0] = 2; return; }
if ((M[1] == 2) && (M[4] == 2) && (M[7] == 0)) { M[7] = 2; return; }
if ((M[1] == 2) && (M[7] == 2) && (M[4] == 0)) { M[4] = 2; return; }
// Проверяем все варианты с М[2]
if ((M[2] == 2) && (M[4] == 2) && (M[6] == 0)) { M[6] = 2; return; }
if ((M[2] == 2) && (M[6] == 2) && (M[4] == 0)) { M[4] = 2; return; }
if ((M[2] == 2) && (M[5] == 2) && (M[8] == 0)) { M[8] = 2; return; }
if ((M[2] == 2) && (M[8] == 2) && (M[4] == 0)) { M[4] = 2; return; }
// Проверяем все варианты с М[3]
if ((M[3] == 2) && (M[4] == 2) && (M[5] == 0)) { M[5] = 2; return; }
if ((M[3] == 2) && (M[5] == 2) && (M[4] == 0)) { M[4] = 2; return; }
if ((M[3] == 2) && (M[6] == 2) && (M[0] == 0)) { M[0] = 2; return; }
// Проверяем все варианты с М[4]
if ((M[4] == 2) && (M[5] == 2) && (M[3] == 0)) { M[3] = 2; return; }
if ((M[4] == 2) && (M[7] == 2) && (M[1] == 0)) { M[1] = 2; return; }
if ((M[4] == 2) && (M[8] == 2) && (M[0] == 0)) { M[0] = 2; return; }
if ((M[4] == 2) && (M[6] == 2) && (M[2] == 0)) { M[2] = 2; return; }
// Проверяем все варианты с М[5]
if ((M[5] == 2) && (M[8] == 2) && (M[2] == 0)) { M[2] = 2; return; }
// Проверяем все варианты с М[6]
if ((M[6] == 2) && (M[7] == 2) && (M[8] == 0)) { M[8] = 2; return; }
if ((M[6] == 2) && (M[8] == 2) && (M[7] == 0)) { M[7] = 2; return; }
// Проверяем все варианты с М[7]
if ((M[7] == 2) && (M[8] == 2) && (M[6] == 0)) { M[6] = 2; return; }
// Проверяем все варианты с М[0]
if ((M[0] == 1) && (M[1] == 1) && (M[2] == 0)) { M[2] = 2; return; } // 0 1 2
if ((M[0] == 1) && (M[3] == 1) && (M[6] == 0)) { M[6] = 2; return; } // 3 4 5
if ((M[0] == 1) && (M[6] == 1) && (M[3] == 0)) { M[3] = 2; return; } // 6 7 8
if ((M[0] == 1) && (M[2] == 1) && (M[1] == 0)) { M[1] = 2; return; } //
if ((M[0] == 1) && (M[4] == 1) && (M[8] == 0)) { M[8] = 2; return; }
if ((M[0] == 1) && (M[8] == 1) && (M[4] == 0)) { M[4] = 2; return; }
// Проверяем все варианты с М[1]
if ((M[1] == 1) && (M[2] == 1) && (M[0] == 0)) { M[0] = 2; return; }
if ((M[1] == 1) && (M[4] == 1) && (M[7] == 0)) { M[7] = 2; return; }
if ((M[1] == 1) && (M[7] == 1) && (M[4] == 0)) { M[4] = 2; return; }
// Проверяем все варианты с М[2]
if ((M[2] == 1) && (M[4] == 1) && (M[6] == 0)) { M[6] = 2; return; }
if ((M[2] == 1) && (M[6] == 1) && (M[4] == 0)) { M[4] = 2; return; }
if ((M[2] == 1) && (M[5] == 1) && (M[8] == 0)) { M[8] = 2; return; }
if ((M[2] == 1) && (M[8] == 1) && (M[5] == 0)) { M[5] = 2; return; }
// Проверяем все варианты с М[3]
if ((M[3] == 1) && (M[4] == 1) && (M[5] == 0)) { M[5] = 2; return; }
if ((M[3] == 1) && (M[5] == 1) && (M[4] == 0)) { M[4] = 2; return; }
if ((M[3] == 1) && (M[6] == 1) && (M[0] == 0)) { M[0] = 2; return; }
// Проверяем все варианты с М[4]
if ((M[4] == 1) && (M[5] == 1) && (M[3] == 0)) { M[3] = 2; return; }
if ((M[4] == 1) && (M[7] == 1) && (M[1] == 0)) { M[1] = 2; return; }
if ((M[4] == 1) && (M[8] == 1) && (M[0] == 0)) { M[0] = 2; return; }
if ((M[4] == 1) && (M[6] == 1) && (M[2] == 0)) { M[2] = 2; return; }
// Проверяем все варианты с М[5]
if ((M[5] == 1) && (M[8] == 1) && (M[2] == 0)) { M[2] = 2; return; }
// Проверяем все варианты с М[6]
if ((M[6] == 1) && (M[7] == 1) && (M[8] == 0)) { M[8] = 2; return; }
if ((M[6] == 1) && (M[8] == 1) && (M[7] == 0)) { M[7] = 2; return; }
// Проверяем все варианты с М[7]
if ((M[7] == 1) && (M[8] == 1) && (M[6] == 0)) { M[6] = 2; return; }
if (M[4] == 0) { M[4] = 2; return; }
int[] Z = new int[9];
for (int i = 0; i < 9; i++) Z[i] = 0;
if ((M[0] == 0) && (M[1] != 1) && (M[2] != 1) && (M[3]!=1) && (M[6] !=1 ))
{
if (M[1] == 2) Z[0]++;
if (M[2] == 2) Z[0]++;
if (M[3] == 2) Z[0]++;
if (M[6] == 2) Z[0]++;
}
if ((M[1] == 0) && (M[0] != 1) && (M[2] != 1) && (M[4] != 1) && (M[7] != 1))
{
if (M[0] == 2) Z[1]++;
if (M[2] == 2) Z[1]++;
if (M[4] == 2) Z[1]++;
if (M[7] == 2) Z[1]++;
}
if ((M[2] == 0) && (M[0] != 1) && (M[1] != 1) && (M[5] != 1) && (M[8] != 1))
{
if (M[0] == 2) Z[2]++;
if (M[1] == 2) Z[2]++;
if (M[5] == 2) Z[2]++;
if (M[8] == 2) Z[2]++;
}
if ((M[3] == 0) && (M[0] != 1) && (M[6] != 1) && (M[4] != 1) && (M[5] != 1))
{
if (M[0] == 2) Z[3]++;
if (M[6] == 2) Z[3]++;
if (M[4] == 2) Z[3]++;
if (M[5] == 2) Z[3]++;
}
if ((M[5] == 0) && (M[2] != 1) && (M[8] != 1) && (M[3] != 1) && (M[4] != 1))
{
if (M[2] == 2) Z[5]++;
if (M[8] == 2) Z[5]++;
if (M[3] == 2) Z[5]++;
if (M[4] == 2) Z[5]++;
}
if ((M[6] == 0) && (M[0] != 1) && (M[3] != 1) && (M[7] != 1) && (M[8] != 1))
{
if (M[0] == 2) Z[6]++;
if (M[3] == 2) Z[6]++;
if (M[7] == 2) Z[6]++;
if (M[8] == 2) Z[6]++;
}
if ((M[7] == 0) && (M[6] != 1) && (M[8] != 1) && (M[1] != 1) && (M[4] != 1))
{
if (M[6] == 2) Z[7]++;
if (M[8] == 2) Z[7]++;
if (M[1] == 2) Z[7]++;
if (M[4] == 2) Z[7]++;
}
if ((M[8] == 0) && (M[2] != 1) && (M[5] != 1) && (M[6] != 1) && (M[7] != 1))
{
if (M[6] == 2) Z[8]++;
if (M[8] == 2) Z[8]++;
if (M[1] == 2) Z[8]++;
if (M[4] == 2) Z[8]++;
}
int m_z = 0;
for (int i = 0; i < 9; i++) if (Z[i] > Z[m_z]) m_z = i;
if (Z[m_z] > 1)
{
M[m_z] = 2;
return;
}
if ((M[0] == 2) && (M[2] == 0) && (M[1] == 0)) { M[1] = 2; return; }
if ((M[0] == 2) && (M[6] == 0) && (M[3] == 0)) { M[3] = 2; return; }
if ((M[0] == 2) && (M[8] == 0) && (M[4] == 0)) { M[4] = 2; return; }
if ((M[1] == 2) && (M[0] == 0) && (M[2] == 0)) { M[0] = 2; return; }
if ((M[1] == 2) && (M[4] == 0) && (M[7] == 0)) { M[4] = 2; return; }
if ((M[2] == 2) && (M[0] == 0) && (M[1] == 0)) { M[1] = 2; return; }
if ((M[2] == 2) && (M[4] == 0) && (M[6] == 0)) { M[4] = 2; return; }
if ((M[2] == 2) && (M[5] == 0) && (M[8] == 0)) { M[5] = 2; return; }
if ((M[3] == 2) && (M[0] == 0) && (M[6] == 0)) { M[0] = 2; return; }
if ((M[3] == 2) && (M[4] == 0) && (M[5] == 0)) { M[4] = 2; return; }
if ((M[4] == 2) && (M[0] == 0) && (M[8] == 0)) { M[8] = 2; return; }
if ((M[5] == 2) && (M[8] == 0) && (M[2] == 0)) { M[8] = 2; return; }
if ((M[6] == 2) && (M[3] == 0) && (M[0] == 0)) { M[0] = 2; return; }
if ((M[6] == 2) && (M[7] == 0) && (M[8] == 0)) { M[7] = 2; return; }
if ((M[7] == 2) && (M[6] == 0) && (M[8] == 0)) { M[6] = 2; return; }
if ((M[8] == 2) && (M[6] == 0) && (M[7] == 0)) { M[7] = 2; return; }
for (int i = 0; i < 9; i++) if (M[i] == 0) { M[i] = 2; return; }
}
private void Winer()
{
for (int i = 1; i < 3; i++)
{
if ((M[0] == i) && (M[1] == i) && (M[2] == i)) { TheEnd(i); return; };
if ((M[3] == i) && (M[4] == i) && (M[5] == i)) { TheEnd(i); return; };
if ((M[6] == i) && (M[7] == i) && (M[8] == i)) { TheEnd(i); return; };
if ((M[0] == i) && (M[3] == i) && (M[6] == i)) { TheEnd(i); return; };
if ((M[1] == i) && (M[4] == i) && (M[7] == i)) { TheEnd(i); return; };
if ((M[2] == i) && (M[5] == i) && (M[8] == i)) { TheEnd(i); return; };
if ((M[0] == i) && (M[4] == i) && (M[8] == i)) { TheEnd(i); return; };
if ((M[2] == i) && (M[4] == i) && (M[6] == i)) { TheEnd(i); return; };
}
bool nobody = true;
for (int i = 0; i < 9; i++) if (M[i] == 0) nobody = false;
if (nobody)
{
win_nobody++;
MessageBox.Show("Ничья!", "Конец игры.", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
label1.Text = "Ничья: " + win_nobody;
TheEnd(0);
}
}
private void TheEnd(int q)
{
if (q == 1)
{
win_men++;
MessageBox.Show("Поздравляем, Вы победили!", "Конец игры.", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
if (q == 2)
{
win_comp++;
MessageBox.Show("Победил компьютер!", "Конец игры.", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
pictureBox1.Enabled = false;
// -//- -//- //
pictureBox9.Enabled = false;
label2.Text = "Компьютер: " + win_comp;
label3.Text = "Человек: " + win_men;
}
private void OutPut()
{
if (M[0] == 2) { pictureBox1.Image = global::XO.Properties.Resources.O; pictureBox1.Enabled = false; }
if (M[1] == 2) { pictureBox2.Image = global::XO.Properties.Resources.O; pictureBox2.Enabled = false; }
if (M[2] == 2) { pictureBox3.Image = global::XO.Properties.Resources.O; pictureBox3.Enabled = false; }
if (M[3] == 2) { pictureBox4.Image = global::XO.Properties.Resources.O; pictureBox4.Enabled = false; }
if (M[4] == 2) { pictureBox5.Image = global::XO.Properties.Resources.O; pictureBox5.Enabled = false; }
if (M[5] == 2) { pictureBox6.Image = global::XO.Properties.Resources.O; pictureBox6.Enabled = false; }
if (M[6] == 2) { pictureBox7.Image = global::XO.Properties.Resources.O; pictureBox7.Enabled = false; }
if (M[7] == 2) { pictureBox8.Image = global::XO.Properties.Resources.O; pictureBox8.Enabled = false; }
if (M[8] == 2) { pictureBox9.Image = global::XO.Properties.Resources.O; pictureBox9.Enabled = false; }
}
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < 9; i++) M[i] = 0;
pictureBox1.Image = null; pictureBox1.Enabled = true;
// -//- -//- //
pictureBox9.Image = null; pictureBox9.Enabled = true;
if (radioButton2.Checked)
{
VirtualMind();
OutPut();
Winer();
}
}
}
}