
Introduction
This is a Triki game created in SQL Server with triggers.
Using the code
This is the code for use:
update Tbl_Triki set Col1 = '-', Col2 = '-', Col3 = '-'
UPDATE Tbl_Triki set Col1 = 'o' where id = 2
select * from Tbl_Triki
UPDATE Tbl_Triki set Col2 = 'o' where id = 2
select * from Tbl_Triki
UPDATE Tbl_Triki set Col1 = 'o' where id = 0
select * from Tbl_Triki
UPDATE Tbl_Triki set Col3 = 'o' where id = 0
select * from Tbl_Triki
CREATE TABLE [dbo].[Tbl_Triki](
[Id] [numeric](18, 0) ,
[Col1] [char](1) NULL,
[Col2] [char](1) NULL,
[Col3] [char](1) NULL,
[Usuario] [numeric](5, 0) NULL,
[Computado] [numeric](5, 0) NULL
) ON [PRIMARY]
go
insert into Tbl_Triki values (0,'-','-','-',-1,-1)
insert into Tbl_Triki values (1,'-','-','-',0,0)
insert into Tbl_Triki values (2,'-','-','-',-1,-1)
go
Create TRIGGER [dbo].[Tr_Triki]
ON [dbo].[Tbl_Triki] for UPDATE
AS
declare @X11 varchar(1)
declare @X12 varchar(1)
declare @X13 varchar(1)
declare @X21 varchar(1)
declare @X22 varchar(1)
declare @X23 varchar(1)
declare @X31 varchar(1)
declare @X32 varchar(1)
declare @X33 varchar(1)
set @X11 = (select Col1 from Tbl_Triki where id = 0)
set @X12 = (select Col2 from Tbl_Triki where id = 0)
set @X13 = (select Col3 from Tbl_Triki where id = 0)
set @X21 = (select Col1 from Tbl_Triki where id = 1)
set @X22 = (select Col2 from Tbl_Triki where id = 1)
set @X23 = (select Col3 from Tbl_Triki where id = 1)
set @X31 = (select Col1 from Tbl_Triki where id = 2)
set @X32 = (select Col2 from Tbl_Triki where id = 2)
set @X33 = (select Col3 from Tbl_Triki where id = 2)
IF UPDATE ([Col1]) OR UPDATE ([Col2]) OR UPDATE ([Col3])
begin
IF @X11 = 'o' and @X12 = 'o' and @x13 = 'o'
begin
UPDATE Tbl_Triki set Usuario = Usuario + 1
update Tbl_Triki set Col1 = '-', Col2 = '-', Col3 = '-'
print 'Juego Terminado'
end
else IF @X21 = 'o' and @X22 = 'o' and @x23 = 'o'
begin
UPDATE Tbl_Triki set Usuario = Usuario + 1
update Tbl_Triki set Col1 = '-', Col2 = '-', Col3 = '-'
print 'Juego Terminado'
end
else IF @X31 = 'o' and @X32 = 'o' and @x33 = 'o'
begin
UPDATE Tbl_Triki set Usuario = Usuario + 1
update Tbl_Triki set Col1 = '-', Col2 = '-', Col3 = '-'
print 'Juego Terminado'
end
else IF @X11 = 'o' and @X21 = 'o' and @x31 = 'o'
begin
UPDATE Tbl_Triki set Usuario = Usuario + 1
update Tbl_Triki set Col1 = '-', Col2 = '-', Col3 = '-'
print 'Juego Terminado'
end
else IF @X21 = 'o' and @X22 = 'o' and @x32 = 'o'
begin
UPDATE Tbl_Triki set Usuario = Usuario + 1
update Tbl_Triki set Col1 = '-', Col2 = '-', Col3 = '-'
print 'Juego Terminado'
end
else IF @X31 = 'o' and @X32 = 'o' and @x33 = 'o'
begin
UPDATE Tbl_Triki set Usuario = Usuario + 1
update Tbl_Triki set Col1 = '-', Col2 = '-', Col3 = '-'
print 'Juego Terminado'
end
else
IF @X11 = '-' and @X12 = '-' and @X13 = '-' and @X21 = '-' and @X22 = 'o' and @X23 = '-' and @X31 = '-' and @X32 = '-' and @X33 = '-'
begin
UPDATE Tbl_Triki set Col1 = 'x' where id = 0
end
ELSE IF @X11 = 'o' and @X12 = '-' and @X13 = '-' and @X21 = '-' and @X22 = '-' and @X23 = '-' and @X31 = '-' and @X32 = '-' and @X33 = '-'
begin
UPDATE Tbl_Triki set Col2 = 'x' where id = 1
end
ELSE IF @X11 = '-' and @X12 = 'o' and @X13 = '-' and @X21 = '-' and @X22 = '-' and @X23 = '-' and @X31 = '-' and @X32 = '-' and @X33 = '-'
begin
UPDATE Tbl_Triki set Col2 = 'x' where id = 1
end
ELSE IF @X11 = '-' and @X12 = '-' and @X13 = 'o' and @X21 = '-' and @X22 = '-' and @X23 = '-' and @X31 = '-' and @X32 = '-' and @X33 = '-'
begin
UPDATE Tbl_Triki set Col2 = 'x' where id = 1
end
ELSE IF @X11 = '-' and @X12 = '-' and @X13 = '-' and @X21 = 'o' and @X22 = '-' and @X23 = '-' and @X31 = '-' and @X32 = '-' and @X33 = '-'
begin
UPDATE Tbl_Triki set Col2 = 'x' where id = 1
end
ELSE IF @X11 = '-' and @X12 = '-' and @X13 = '-' and @X21 = '-' and @X22 = '-' and @X23 = 'o' and @X31 = '-' and @X32 = '-' and @X33 = '-'
begin
UPDATE Tbl_Triki set Col2 = 'x' where id = 1
end
ELSE IF @X11 = '-' and @X12 = '-' and @X13 = '-' and @X21 = '-' and @X22 = '-' and @X23 = '-' and @X31 = 'o' and @X32 = '-' and @X33 = '-'
begin
UPDATE Tbl_Triki set Col2 = 'x' where id = 1
end
ELSE IF @X11 = '-' and @X12 = '-' and @X13 = '-' and @X21 = '-' and @X22 = '-' and @X23 = '-' and @X31 = '-' and @X32 = 'o' and @X33 = '-'
begin
UPDATE Tbl_Triki set Col2 = 'x' where id = 1
end
ELSE IF @X11 = '-' and @X12 = '-' and @X13 = '-' and @X21 = '-' and @X22 = '-' and @X23 = '-' and @X31 = '-' and @X32 = '-' and @X33 = 'o'
begin
UPDATE Tbl_Triki set Col2 = 'x' where id = 1
end
ELSE IF @X11 = 'x' and @X12 = 'x' and @x13 = '-'
begin
UPDATE Tbl_Triki set Col3 = 'x' where id = 0
update Tbl_Triki set Computado =Computado + 1
update Tbl_Triki set Col1 = '-', Col2 = '-', Col3 = '-'
end
ELSE IF @X11 = '-' and @X12 = 'x' and @x13 = 'x'
begin
UPDATE Tbl_Triki set Col1 = 'x' where id = 0
update Tbl_Triki set Computado =Computado + 1
update Tbl_Triki set Col1 = '-', Col2 = '-', Col3 = '-'
end
ELSE IF @X11 = 'x' and @X12 = '-' and @x13 = 'x'
begin
UPDATE Tbl_Triki set Col2 = 'x' where id = 0
update Tbl_Triki set Computado =Computado + 1
update Tbl_Triki set Col1 = '-', Col2 = '-', Col3 = '-'
end
ELSE IF @X21 = 'x' and @X22 = 'x' and @x23 = '-'
begin
UPDATE Tbl_Triki set Col3 = 'x' where id = 1
update Tbl_Triki set Computado =Computado + 1
update Tbl_Triki set Col1 = '-', Col2 = '-', Col3 = '-'
end
ELSE IF @X21 = '-' and @X22 = 'x' and @x23 = 'x'
begin
UPDATE Tbl_Triki set Col1 = 'x' where id = 1
update Tbl_Triki set Computado =Computado + 1
update Tbl_Triki set Col1 = '-', Col2 = '-', Col3 = '-'
end
ELSE IF @X21 = 'x' and @X22 = '-' and @x23 = 'x'
begin
UPDATE Tbl_Triki set Col2 = 'x' where id = 1
update Tbl_Triki set Computado =Computado + 1
update Tbl_Triki set Col1 = '-', Col2 = '-', Col3 = '-'
end
ELSE IF @X31 = 'x' and @X32 = 'x' and @x33 = '-'
begin
UPDATE Tbl_Triki set Col3 = 'x' where id = 2
update Tbl_Triki set Computado =Computado + 1
update Tbl_Triki set Col1 = '-', Col2 = '-', Col3 = '-'
end
ELSE IF @X31 = '-' and @X32 = 'x' and @x33 = 'x'
begin
UPDATE Tbl_Triki set Col1 = 'x' where id = 2
update Tbl_Triki set Computado =Computado + 1
update Tbl_Triki set Col1 = '-', Col2 = '-', Col3 = '-'
end
ELSE IF @X31 = 'x' and @X32 = '-' and @x33 = 'x'
begin
UPDATE Tbl_Triki set Col2 = 'x' where id = 2
update Tbl_Triki set Computado =Computado + 1
update Tbl_Triki set Col1 = '-', Col2 = '-', Col3 = '-'
end
ELSE IF @X11 = 'x' and @X21 = 'x' and @x31 = '-'
begin
UPDATE Tbl_Triki set Col1 = 'x' where id = 2
update Tbl_Triki set Computado =Computado + 1
update Tbl_Triki set Col1 = '-', Col2 = '-', Col3 = '-'
end
ELSE IF @X11 = '-' and @X21 = 'x' and @x31 = 'x'
begin
UPDATE Tbl_Triki set Col1 = 'x' where id = 0
update Tbl_Triki set Computado =Computado + 1
update Tbl_Triki set Col1 = '-', Col2 = '-', Col3 = '-'
end
ELSE IF @X11 = 'x' and @X21 = '-' and @x31 = 'x'
begin
UPDATE Tbl_Triki set Col1 = 'x' where id = 1
update Tbl_Triki set Computado =Computado + 1
update Tbl_Triki set Col1 = '-', Col2 = '-', Col3 = '-'
end
ELSE IF @X12 = 'x' and @X22 = 'x' and @x32 = '-'
begin
UPDATE Tbl_Triki set Col2 = 'x' where id = 2
update Tbl_Triki set Computado =Computado + 1
update Tbl_Triki set Col1 = '-', Col2 = '-', Col3 = '-'
end
ELSE IF @X12 = '-' and @X22 = 'x' and @x32 = 'x'
begin
UPDATE Tbl_Triki set Col2 = 'x' where id = 0
update Tbl_Triki set Computado =Computado + 1
update Tbl_Triki set Col1 = '-', Col2 = '-', Col3 = '-'
end
ELSE IF @X12 = 'x' and @X22 = '-' and @x32 = 'x'
begin
UPDATE Tbl_Triki set Col2 = 'x' where id = 1
update Tbl_Triki set Computado =Computado + 1
update Tbl_Triki set Col1 = '-', Col2 = '-', Col3 = '-'
end
ELSE IF @X13 = 'x' and @X23 = 'x' and @x33 = '-'
begin
UPDATE Tbl_Triki set Col3 = 'x' where id = 2
update Tbl_Triki set Computado =Computado + 1
update Tbl_Triki set Col1 = '-', Col2 = '-', Col3 = '-'
end
ELSE IF @X13 = '-' and @X23 = 'x' and @x33 = 'x'
begin
UPDATE Tbl_Triki set Col3 = 'x' where id = 0
update Tbl_Triki set Computado =Computado + 1
update Tbl_Triki set Col1 = '-', Col2 = '-', Col3 = '-'
end
ELSE IF @X13 = 'x' and @X23 = '-' and @x33 = 'x'
begin
UPDATE Tbl_Triki set Col3 = 'x' where id = 1
update Tbl_Triki set Computado =Computado + 1
update Tbl_Triki set Col1 = '-', Col2 = '-', Col3 = '-'
end
ELSE IF @X11 = 'o' and @X12 = 'o' and @x13 = '-'
begin
UPDATE Tbl_Triki set Col3 = 'x' where id = 0
end
ELSE IF @X11 = '-' and @X12 = 'o' and @x13 = 'o'
begin
UPDATE Tbl_Triki set Col1 = 'x' where id = 0
end
ELSE IF @X11 = 'o' and @X12 = '-' and @x13 = 'o'
begin
UPDATE Tbl_Triki set Col2 = 'x' where id = 0
end
ELSE IF @X21 = 'o' and @X22 = 'o' and @x23 = '-'
begin
UPDATE Tbl_Triki set Col3 = 'x' where id = 1
end
ELSE IF @X21 = '-' and @X22 = 'o' and @x23 = 'o'
begin
UPDATE Tbl_Triki set Col1 = 'x' where id = 1
end
ELSE IF @X21 = 'o' and @X22 = '-' and @x23 = 'o'
begin
UPDATE Tbl_Triki set Col2 = 'x' where id = 1
end
ELSE IF @X31 = 'o' and @X32 = 'o' and @x33 = '-'
begin
UPDATE Tbl_Triki set Col3 = 'x' where id = 2
end
ELSE IF @X31 = '-' and @X32 = 'o' and @x33 = 'o'
begin
UPDATE Tbl_Triki set Col1 = 'x' where id = 2
end
ELSE IF @X31 = 'o' and @X32 = '-' and @x33 = 'o'
begin
UPDATE Tbl_Triki set Col2 = 'x' where id = 2
end
ELSE IF @X11 = 'o' and @X21 = 'o' and @x31 = '-'
begin
UPDATE Tbl_Triki set Col1 = 'x' where id = 2
end
ELSE IF @X11 = '-' and @X21 = 'o' and @x31 = 'o'
begin
UPDATE Tbl_Triki set Col1 = 'x' where id = 0
end
ELSE IF @X11 = 'o' and @X21 = '-' and @x31 = 'o'
begin
UPDATE Tbl_Triki set Col1 = 'x' where id = 1
end
ELSE IF @X12 = 'o' and @X22 = 'o' and @x32 = '-'
begin
UPDATE Tbl_Triki set Col2 = 'x' where id = 2
end
ELSE IF @X12 = '-' and @X22 = 'o' and @x32 = 'o'
begin
UPDATE Tbl_Triki set Col2 = 'x' where id = 0
end
ELSE IF @X12 = 'o' and @X22 = '-' and @x32 = 'o'
begin
UPDATE Tbl_Triki set Col2 = 'x' where id = 1
end
ELSE IF @X13 = 'o' and @X23 = 'o' and @x33 = '-'
begin
UPDATE Tbl_Triki set Col3 = 'x' where id = 2
end
ELSE IF @X13 = '-' and @X23 = 'o' and @x33 = 'o'
begin
UPDATE Tbl_Triki set Col3 = 'x' where id = 0
end
ELSE IF @X13 = 'o' and @X23 = '-' and @x33 = 'o'
begin
UPDATE Tbl_Triki set Col3 = 'x' where id = 1
end
IF @X11 <> '-' and @X12 <> '-' and @X13 <> '-' and @X21 <> '-' and @X22 <> '-' and
@X23 <> '-' and @X31 <> '-' and @X32 <> '-' and @X33 <> '-'
begin
update Tbl_Triki set Col1 = '-', Col2 = '-', Col3 = '-'
print 'Juego Terminado'
end
end