function [current_state, game_over] = ttt(current_state, move)
% Input:
% current_state:  a 9 element vector where 1 = X, -1 = O and
%  0 = EMPTY.  The positions are as follows:
%   1 2 3
%   4 5 6
%   7 8 9
% move: a vector containing two elements.  The first is the location
%  of the move which is a number between 1 and 9 and the second
%  is a 1 for X and a -1 for O
%
% Output:
% next_state:  same representation as current_state.  The state
%  after doing a move
% game_over:  0 if the game isn't over, 1 if it is
%
% Note:  the function will report an error if:
%   - the move specified already contains a mark
%   - the current_state vector doesn't contain 9 elements between 1 and -1

% check to make sure the current state is ok
if( length(current_state) ~= 9 | sum(current_state < -1) > 0 | sum(current_state > 1) > 0 )
    error('Bad current state');
end
   
% check to make sure the move is legal
if( length(move) ~= 2 | move(1) > 9 | move(1) < 1 | current_state(move(1)) ~= 0 | ~(move(2) == 1 | move(2) == -1))
    error('Bad move specified');
end

% otherwise, update the current state
current_state(move(1)) = move(2);

game_over = 0;

% check to see if the game is over
% probably a better way to do this
if( abs(sum(current_state(1:3))) == 3 | ...
   abs(sum(current_state(4:6))) == 3 | ...
   abs(sum(current_state(7:9))) == 3 | ...
   abs(sum(current_state([1,4,7]))) == 3 | ...
   abs(sum(current_state([2,5,8]))) == 3 | ...
   abs(sum(current_state([3,6,9]))) == 3 | ...
   abs(sum(current_state([1,5,9]))) == 3 | ...
   abs(sum(current_state([3,5,7]))) == 3 | ...
   sum(current_state == 0) == 0)
   game_over = 1;
end
