lundi 30 novembre 2015

Tictactoe game preventing first move from being used twice MySQL

I am making the basic tic-tac-toe game in MySQL and I can not figure out why my if statement is not stopping me from using the first move command twice. Here is what I have:

delimiter //

create procedure tictactoe(input varchar(4))
begin

declare i varchar(1);
declare j int;
declare computer_turn int;
declare user_turn int;

set computer_turn = 0;
set user_turn = 0;


if input = 'bm' then

if computer_turn = 0 then

case FLOOR(RAND()*(5-1)+1)
    when 1 then
    UPDATE grid
    SET B = 'M'
    WHERE ttt = '2';
when 2 then
    update grid
    set A = 'M'
    where ttt = '1';
when 3 then
    update grid
    set C = 'M'
    where ttt = '1';
when 4 then
    update grid
    set C = 'M'
    where ttt = '3';
when 5 then
    update grid
    set A = 'M'
    where ttt = '3';

end case;

set computer_turn = 1;

select * from grid;

else
  select 'First move has already been made. Use another command.';

  end if;

end if;

end //

Here is the table I am using:

CREATE TABLE `tictactoe_4213`.`grid` 
(`TTT` INT NOT NULL,
 `A` VARCHAR(45) NULL,
 `B` VARCHAR(45) NULL,
 `C` VARCHAR(45) NULL,
  PRIMARY KEY (`TTT`));

Insert into grid
values (1, null, null, null),
(2, null, null, null),
(3, null, null, null);

Aucun commentaire:

Enregistrer un commentaire