unit u_ttt;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, XPMan, ScktComp;
type
Tfm_ttt =
class(TForm)
p_11: TPanel;
p_12: TPanel;
p_13: TPanel;
p_21: TPanel;
p_22: TPanel;
p_23: TPanel;
p_31: TPanel;
p_32: TPanel;
p_33: TPanel;
rg_player: TRadioGroup;
gb_pt: TGroupBox;
lbl_x: TLabel;
lbl_dp: TLabel;
lbl_0: TLabel;
gb_control: TGroupBox;
btn_start: TButton;
XPManifest1: TXPManifest;
x: TLabel;
o: TLabel;
Image1: TImage;
btn_resetscore: TButton;
lbl_itsover: TLabel;
ClientSocket: TClientSocket;
edt_Adresse: TEdit;
edt_Port: TEdit;
btn_Verbinden: TButton;
btn_Trennen: TButton;
mmo_Memo: TMemo;
edt_Nachricht: TEdit;
edt_Nick: TEdit;
btn_nicklist: TButton;
lb_nicklist: TListBox;
btn_Senden: TButton;
lbl_partner: TLabel;
edt_partner: TEdit;
btn_invite: TButton;
procedure mdown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure mup(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure btn_startClick(Sender: TObject);
procedure chkwin(Sender: TObject);
procedure win(Sender: TObject);
procedure stop(Sender: TObject);
procedure reset(Sender: TObject);
procedure btn_resetscoreClick(Sender: TObject);
procedure ClientSocketConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure btn_VerbindenClick(Sender: TObject);
procedure ClientSocketRead(Sender: TObject; Socket: TCustomWinSocket);
procedure btn_nicklistClick(Sender: TObject);
procedure btn_SendenClick(Sender: TObject);
procedure btn_TrennenClick(Sender: TObject);
procedure edt_NachrichtKeyDown(Sender: TObject;
var Key: Word;
Shift: TShiftState);
procedure lb_nicklistClick(Sender: TObject);
procedure btn_inviteClick(Sender: TObject);
private
{ Private-Deklarationen }
procedure SetzeStatus;
procedure nicklist;
public
{ Public-Deklarationen }
end;
var
fm_ttt: Tfm_ttt;
Feld:
Array[1..3,1..3]
OF Byte;
zwsummewin:integer;
nicklisttemp:
string;
Server:
string;
player:boolean;
Feldname:
String;
//mdown
implementation
{$R *.dfm}
procedure Tfm_ttt.mdown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var Zeile,Spalte:Integer;
begin
TPanel(Sender).BevelInner:=bvLowered;
TPanel(Sender).BevelOuter:=bvNone;
if rg_player.ItemIndex=0
then TPanel(Sender).caption:='
0'
else TPanel(Sender).Caption:='
X';
Feldname:=TPanel(Sender).
Name;
Zeile:=StrToInt(Feldname[3]);
Spalte:=StrToInt(Feldname[4]);
if rg_player.ItemIndex=0
then Feld[Zeile,Spalte]:=1
else Feld[Zeile,Spalte]:=10;
end;
procedure Tfm_ttt.mup(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
TPanel(Sender).BevelInner:=bvNone;
//TPanel(Sender).BevelOuter:=bvRaised; //dann sieht man sofort das das feld nicht mehr zu haben ist
ClientSocket.Socket.SendText('
p:'+edt_partner.text+'
:'+Copy(TPanel(Sender).
name,3,2));
TPanel(Sender).Enabled:=false;
lbl_itsover.Caption:=IntToStr((StrToInt(lbl_itsover.Caption))+1);
chkwin(Sender);
if rg_player.itemindex=0
then rg_player.itemindex:=1
else rg_player.ItemIndex:=0;
end;
procedure Tfm_ttt.btn_startClick(Sender: TObject);
var z:integer;s:integer;feldtemp:
String;
begin
reset (Sender);
for z:=1
to 3
do
begin
for s:=1
to 3
do
begin
feldtemp:='
p_'+IntToStr(z)+IntToStr(s);
TPanel(FindComponent(feldtemp)).enabled:=true;
end;
end;
rg_player.Enabled:=false;
btn_resetscore.Enabled:=false;
btn_start.Enabled:=false;
//damit der eigensinnige spieler das spiel nicht extra oder ausversehen neustartet...
end;
procedure Tfm_ttt.reset(Sender: TObject);
var z,s:integer;feldtemp:
string;
begin
ZeroMemory(@Feld,SizeOf(Feld));
for z:=1
to 3
do
begin
for s:=1
to 3
do
begin
feldtemp:='
p_'+IntToStr(z)+IntToStr(s);
TPanel(FindComponent(feldtemp)).enabled:=false;
TPanel(FindComponent(feldtemp)).caption:='
-';
TPanel(FindComponent(feldtemp)).BevelOuter:=bvRaised;
//ich habs ja vorher auskommentiert
end;
end;
end;
procedure Tfm_ttt.chkwin(Sender: TObject);
var z,s:integer;
begin
zwsummewin:=0;
for z:=1
to 1
do //zu faul das zu löschen nachdem es nicht reingemusst hätte^^
begin
for s:=1
to 3
do
begin
zwsummewin:= zwsummewin + Feld[z,s];
end;
end;
win (Sender);
//ende zeile 1
zwsummewin:=0;
//Anfang Zeile2
for z:=2
to 2
do //zu faul das zu löschen nachdem es nicht reingemusst hätte^^
begin
for s:=1
to 3
do
begin
zwsummewin:= zwsummewin + Feld[z,s];
end;
end;
win (Sender);
//ende zeile 2
zwsummewin:=0;
//Anfang Zeile3
for z:=3
to 3
do //zu faul das zu löschen nachdem es nicht reingemusst hätte^^
begin
for s:=1
to 3
do
begin
zwsummewin:= zwsummewin + Feld[z,s];
end;
end;
win (Sender);
//ende zeile 3
zwsummewin:=0;
//Anfang Spalte1
for z:=1
to 3
do
begin
for s:=1
to 1
do //zu faul das zu löschen nachdem es nicht reingemusst hätte^^
begin
zwsummewin:= zwsummewin + Feld[z,s];
end;
end;
win (Sender);
//ende spalte 1
zwsummewin:=0;
//Anfang Spalte2
for z:=1
to 3
do
begin
for s:=2
to 2
do //zu faul das zu löschen nachdem es nicht reingemusst hätte^^
begin
zwsummewin:= zwsummewin + Feld[z,s];
end;
end;
win (Sender);
//ende spalte 2
zwsummewin:=0;
//Anfang Spalte3
for z:=1
to 3
do
begin
for s:=3
to 3
do //zu faul das zu löschen nachdem es nicht reingemusst hätte^^
begin
zwsummewin:= zwsummewin + Feld[z,s];
end;
end;
win (Sender);
//ende spalte 3
zwsummewin:=0;
//anfang diagonale1
for z:=1
to 3
do //nur eine vaialble nötig .....1,1 2,2 ,3,3
zwsummewin:=zwsummewin + Feld[z,z];
win(Sender);
//ende diagonale 1
zwsummewin:=0;
//Anfang diagonale2
for z:=1
to 3
do
begin
zwsummewin:= zwsummewin + Feld[z,4-z];
end;
win (Sender);
//ende diagonale 2
end;
procedure Tfm_ttt.win(Sender: TObject);
begin
if zwsummewin=3
then
begin
lbl_0.Caption:= IntToStr(StrToInt(lbl_0.Caption)+1);
stop (Sender);
ShowMessage('
Spieler 0 gewinnt!');
end
else
if zwsummewin=30
then
begin
lbl_x.Caption:= IntToStr(StrToInt(lbl_x.Caption)+1);
stop (Sender);
ShowMessage('
Spieler X gewinnt!');
end
else;
if lbl_itsover.caption='
9'
then
begin
stop (Sender);
end;
end;
procedure Tfm_ttt.stop(Sender: TObject);
var z:integer;s:integer;feld:
String;
begin
for z:=1
to 3
do
begin
for s:=1
to 3
do
begin
feld:='
p_'+IntToStr(z)+IntToStr(s);
TPanel(FindComponent(feld)).enabled:=false;
end;
end;
lbl_itsover.Caption:='
0';
rg_player.Enabled:=true;
btn_resetscore.Enabled:=true;
btn_start.Enabled:=true;
//Man will ja weiter spielen ;)
end;
procedure Tfm_ttt.btn_resetscoreClick(Sender: TObject);
begin
if MessageDlg('
Spielstand wirklich zurücksetzen?', mtConfirmation, mbOkCancel, 0) = mrOk
then
begin
lbl_0.Caption:='
0';
lbl_x.caption:='
0';
end;
end;
Procedure Tfm_ttt.SetzeStatus;
begin
btn_Verbinden.Enabled:=
not ClientSocket.Active;
btn_Trennen.Enabled := ClientSocket.Active;
btn_Senden.Enabled := ClientSocket.Active;
// btn_pn.Enabled := ClientSocket.Active;
btn_nicklist.Enabled := ClientSocket.Active;
edt_Nachricht.Enabled := ClientSocket.Active ;
edt_Nick.Enabled :=
not ClientSocket.Active;
end;
procedure Tfm_ttt.ClientSocketConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Server:= Socket.RemoteHost + '
' + Socket.RemoteAddress;
mmo_Memo.Lines.Add('
Verbunden mit Server ' + Server);
ClientSocket.Socket.SendText('
c:'+edt_Nick.text);
SetzeStatus;
end;
procedure Tfm_ttt.btn_VerbindenClick(Sender: TObject);
begin
With ClientSocket
do begin
Address:= edt_Adresse.Text;
Port:= StrToInt(edt_Port.Text);
Open;
mmo_Memo.Lines.Add('
Stelle Verbindung zum Server ' + Address + '
her...');
end;
end;
procedure Tfm_ttt.ClientSocketRead(Sender: TObject;
Socket: TCustomWinSocket);
var message,feldtemp,fullmessage,protokoll :
string;
begin
fullmessage := Socket.ReceiveText;
protokoll := Copy(fullmessage, 1, Pos('
:',fullmessage));
message:=Copy(fullmessage, Pos('
:',fullmessage)+1,length(fullmessage)-length(protokoll));
if Protokoll='
l:'
then
begin
nicklisttemp:=Copy(fullmessage, 3, length(fullmessage)-2);
nicklist;
end
else
if Protokoll='
a:'
then
begin
mmo_Memo.Lines.Add('
['+TimeToStr(Now)+'
] '+Copy(
message, 1, Pos('
:',
message))+'
'+Copy(
message, Pos('
:',
message)+1,length(
message)));
end
else
if protokoll='
s:'
then
mmo_Memo.lines.add('
Status: '+
message)
else
if protokoll='
c:'
then
mmo_Memo.Lines.Add('
Server: '+
message)
else
if protokoll='
p:'
then
if Copy(
message,1,Pos('
:',
message)-1)=edt_partner.Text
then
if Copy(
message,Pos('
:',
message)+1,2)='
s:'
then
begin
feldtemp:='
p_'+Copy(
message,Pos('
:',
message)+3,2);
player:=false;
//die Procedure starten
end
else
else
mmo_Memo.lines.Add('
['+TimeToStr(Now)+'
] >Private Nachricht< '+Copy(
message, 1, Pos('
:',
message))+'
'+Copy(
message, Pos('
:',
message)+1,length(
message)))
else
{
if Copy(message,Pos(':',message)+1,2)='b:'
then
if MessageDlg('Spielstand wirklich zurücksetzen?', mtConfirmation, mbOkCancel, 0) = mrOk
then
begin
end
else
}
mmo_Memo.Lines.add('
['+TimeToStr(Now)+'
] '+'
unbekanntes Protokoll '+protokoll+'
...:'+
message)
end;
procedure Tfm_ttt.nicklist;
var nicktemp:
string; listlange:integer;
begin
repeat
nicktemp:= copy(nicklisttemp,1,Pos('
~',nicklisttemp)-1) ;
lb_nicklist.items.Add(nicktemp);
nicklisttemp:=Copy(nicklisttemp,Pos('
~',nicklisttemp)+1,length(nicklisttemp));
listlange:= length(nicklisttemp)
until listlange=0;
end;
procedure Tfm_ttt.btn_nicklistClick(Sender: TObject);
begin
lb_nicklist.Clear;
ClientSocket.Socket.SendText('
l:');
end;
procedure Tfm_ttt.btn_SendenClick(Sender: TObject);
begin
btn_nicklistClick(Sender);
if edt_Nachricht.text='
'
then
else
if edt_Nachricht.text[2]='
:'
then
ClientSocket.Socket.SendText(edt_Nachricht.Text)
else
ClientSocket.Socket.SendText('
a:'+edt_Nachricht.Text);
edt_Nachricht.Text:='
';
end;
procedure Tfm_ttt.btn_TrennenClick(Sender: TObject);
begin
mmo_Memo.Lines.Add('
Trenne Verbindung zum Server ' + Server + '
...');
ClientSocket.Close;
SetzeStatus;
end;
procedure Tfm_ttt.edt_NachrichtKeyDown(Sender: TObject;
var Key: Word;
Shift: TShiftState);
begin
if Key = (VK_RETURN)
then btn_SendenClick(Sender);
end;
procedure Tfm_ttt.lb_nicklistClick(Sender: TObject);
begin
if lb_nicklist.ItemIndex> -1
then
begin
// ClientSocket.Socket.SendText('p:'+lb_nicklist.Items[lb_nicklist.itemindex]+':'+edt_Nachricht.Text);
edt_partner.Text:=lb_nicklist.Items[lb_nicklist.itemindex];
end
else
if lb_nicklist.itemindex = -1
then ShowMessage('
Bitte Nick auswählen!')
else;
end;
procedure Tfm_ttt.btn_inviteClick(Sender: TObject);
begin
ClientSocket.Socket.SendText('
p:'+edt_partner.Text+'
:b:');
end;
end.