Kodlar:
Kod: Tümünü seç
unit Unit2;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.StdCtrls;
type
TForm2 = class(TForm)
StringGrid1: TStringGrid;
Button1: TButton;
Button2: TButton;
Label1: TLabel;
Label2: TLabel;
procedure FormCreate(Sender: TObject);
procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
CurrentPlayer: Char;
procedure InitializeBoard();
function CheckWin(): Boolean;
procedure EndGame(Winner: Char);
function IsBoardFull(): Boolean;
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TForm2.FormCreate(Sender: TObject);
var
begin
InitializeBoard();
CurrentPlayer := 'X';
StringGrid1.Options := StringGrid1.Options + [goEditing, goRangeSelect, goDrawFocusSelected, goTabs];
begin
// Tablonun sütun ve satır sayısını belirleyin
StringGrid1.RowCount := 3; // Satır sayısı
StringGrid1.ColCount := 3; // Sütun sayısı
// Sütun genişliği ayarları
StringGrid1.DefaultColWidth := 90; // Varsayılan sütun genişliği
StringGrid1.ColWidths[0] := 90; // İlk sütun genişliği
StringGrid1.ColWidths[1] := 90; // İkinci sütun genişliği
StringGrid1.ColWidths[2] := 90; // Üçüncü sütun genişliği
// Satır yüksekliği ayarları
StringGrid1.DefaultRowHeight := 90; // Varsayılan satır yüksekliği
StringGrid1.RowHeights[0] := 90; // İlk satır yüksekliği
StringGrid1.RowHeights[1] := 90; // İkinci satır yüksekliği
StringGrid1.RowHeights[2] := 90; // Üçüncü satır yüksekliği
// Diğer özellikler
StringGrid1.FixedCols := 0; // Sabit sütun sayısı
StringGrid1.FixedRows := 0; // Sabit satır sayısı
// Sütun ve satırların başlıklarını gizlemek isterseniz:
StringGrid1.FixedCols := 0;
StringGrid1.FixedRows := 0;
end;
end;
procedure TForm2.InitializeBoard();
var
i, j: Integer;
begin
for i := 0 to StringGrid1.RowCount - 1 do
for j := 0 to StringGrid1.ColCount - 1 do
StringGrid1.Cells[j, i] := '';
end;
procedure TForm2.Button1Click(Sender: TObject);
begin
InitializeBoard();
CurrentPlayer := 'X';
end;
procedure TForm2.Button2Click(Sender: TObject);
begin
if MessageDlg('Çıkmak istediğinize emin misiniz?', mtConfirmation, [mbYes, mbNo], 0) = mrYes then
Application.Terminate;
end;
function TForm2.CheckWin(): Boolean;
var
i, j: Integer;
Winner: Char;
begin
Result := False;
Winner := #0;
// Satırları ve sütunları kontrol et
for i := 0 to 2 do
begin
// Satırları kontrol et
if (StringGrid1.Cells[i, 0] <> '') and
(StringGrid1.Cells[i, 0] = StringGrid1.Cells[i, 1]) and
(StringGrid1.Cells[i, 1] = StringGrid1.Cells[i, 2]) then
begin
Winner := StringGrid1.Cells[i, 0][1];
Break;
end;
// Sütunları kontrol et
if (StringGrid1.Cells[0, i] <> '') and
(StringGrid1.Cells[0, i] = StringGrid1.Cells[1, i]) and
(StringGrid1.Cells[1, i] = StringGrid1.Cells[2, i]) then
begin
Winner := StringGrid1.Cells[0, i][1];
Break;
end;
end;
// Çapraz kontrol
if (StringGrid1.Cells[0, 0] <> '') and
(StringGrid1.Cells[0, 0] = StringGrid1.Cells[1, 1]) and
(StringGrid1.Cells[1, 1] = StringGrid1.Cells[2, 2]) then
Winner := StringGrid1.Cells[0, 0][1];
if (StringGrid1.Cells[0, 2] <> '') and
(StringGrid1.Cells[0, 2] = StringGrid1.Cells[1, 1]) and
(StringGrid1.Cells[1, 1] = StringGrid1.Cells[2, 0]) then
Winner := StringGrid1.Cells[0, 2][1];
// Kazanan var mı kontrol et
if Winner <> #0 then
begin
EndGame(Winner);
Result := True;
end
end;
function TForm2.IsBoardFull(): Boolean;
var
i, j: Integer;
begin
Result := True;
for i := 0 to 2 do
begin
for j := 0 to 2 do
begin
if StringGrid1.Cells[i, j] = '' then
begin
Result := False;
Exit;
end;
end;
end;
end;
procedure TForm2.EndGame(Winner: Char);
begin
ShowMessage('Tebrikler! ' + Winner + ' oyuncusu kazandı.');
InitializeBoard();
CurrentPlayer := 'X';
end;
procedure TForm2.StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
begin
// Seçilen hücre boş mu diye kontrol et
if StringGrid1.Cells[ACol, ARow] = '' then
begin
// Seçilen hücreye oyuncu değerini ata (örneğin: 'X' veya 'O')
StringGrid1.Cells[ACol, ARow] := CurrentPlayer;
// Oyunun kazananını kontrol et
if not CheckWin() then
begin
// Oyun devam ederse sırayı diğer oyuncuya ver
if CurrentPlayer = 'X' then
CurrentPlayer := 'O'
else
CurrentPlayer := 'X';
end;
end
else
ShowMessage('Bu hücre zaten dolu. Lütfen başka bir hücre seçin.');
CanSelect := True;
end;
end.
https://hizliresim.com/ifkphzc
https://hizliresim.com/65iy3dl