Click here to Skip to main content
Click here to Skip to main content

Triki Game Using SQL Triggers

, 25 Feb 2013
Rate this:
Please Sign up or sign in to vote.
Triki game created in SQL Server with triggers.

Triki

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
--Creación de la Tabla
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]

--Insertar Valores de la tabla
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

--Creación del trigger
  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 
    --Fin del juego Ganando
    --Fila1
    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
    --fila 2
    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
    --fila 3
    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
    --Columna 1
    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
    --Columna 2
    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
    --Columna 3
    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
--Primera Movida
    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        
--Ataque-------------------------------------------------------
--Filas
--Primera Fila
    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
--Segunda Fila
    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
--Tercera Fila
    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
--Columnas
--Primera Columna
    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
--Segunda Columna
    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
--Tercera Columna
    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

--Defensa
--Filas
--Primera Fila
    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
--Segunda Fila
    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
--Tercera Fila
    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
--Columnas
--Primera Columna
    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
--Segunda Columna
    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
--Tercera Columna
    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

--Fin del juego sin ganar
 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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

salchichurria

United States United States
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 25 Feb 2013
Article Copyright 2013 by salchichurria
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid