if(p1 == 'n')
if(p2 == 'n')
if(p3 == 'n')
...
Te wszystkie sprawdzenia w poleXClick są niepotrzebne ponieważ poleX->Enabled=false wyłącza też obsługę zdarzeń dla tej kontrolki.
char p1,p2,p3,p4,p5,p6,p7,p8,p9;
To powinno być tablicą.
Co prawda wykracza to poza zakres kursu ale skróciłem na szybko kod.
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
char p[9];
//p[0]..p[8] pola w grze (ich zawartosc: p[0]='n'; nic lub 'x' lub 'o' )
char kto;
void sprawdz()
{
if((p[0] == p[1] && p[1] == p[2] && p[0] != 'n') ||
(p[3] == p[4] && p[4] == p[5] && p[3] != 'n') ||
(p[6] == p[7] && p[7] == p[8] && p[6] != 'n') ||
(p[0] == p[3] && p[3] == p[6] && p[6] != 'n') ||
(p[1] == p[4] && p[4] == p[7] && p[1] != 'n') ||
(p[2] == p[5] && p[5] == p[8] && p[2] != 'n') ||
(p[0] == p[4] && p[4] == p[8] && p[0] != 'n') ||
(p[2] == p[4] && p[4] == p[6] && p[2] != 'n'))
{
char *w;
if (kto == 'x') w = "Wygrywa kółko!";
else w = "Wygrywa krzyżyk!";
Application->MessageBox(w, "Koniec gry", MB_OK);
}
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent *Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
TImage *images[] = {pole1, pole2, pole3, pole4, pole5, pole6, pole7, pole8, pole9};
for(int i = 0; i < ARRAYSIZE(images); ++i)
{
images[i]->Picture->LoadFromFile("img/nic.bmp");
images[i]->Enabled = true;
images[i]->Tag = i;
}
tura->Picture->LoadFromFile("img/osmall.bmp");
for(int i = 0; i < 9; ++i)
p[i] = 'n';
kto = 'o';
}
//---------------------------------------------------------------------------
void __fastcall TForm1::poleClick(TObject *Sender)
{
TImage *pole = (TImage *)Sender;
if(kto == 'o')
{
pole->Picture->LoadFromFile("img/o.bmp");
p[pole->Tag] = 'o';
kto = 'x';
tura->Picture->LoadFromFile("img/xsmall.bmp");
}
else
{
pole->Picture->LoadFromFile("img/x.bmp");
p[pole->Tag] = 'x';
kto = 'o';
tura->Picture->LoadFromFile("img/osmall.bmp");
}
pole->Enabled = false;
sprawdz();
}
//---------------------------------------------------------------------------