%   TTTCompMove.m
%   Description:
%       Generates a computer move based on difficulty setting and state 
%   of the game, and refreshes the 'TicTacToe.fig' figure with selected 
%   move.
%       After computer has made it's move, TTTWhoWon is called to check 
%   for a winner.
%
%   Variables:
%       DifficultyLevel, integer, keeps track of level of difficulty
%       CompMoveValid, integer, confirms if computer's move is a valid move
%       CompMoveSquare, integer, the position of the square the computer
%          tries to take until the move is valid
%       CurrentPlayer, string, keeps track of who's turn it is
%       MainGame, 3x3 matrix, board with values for X's and O's in
%             appropriate positions
%       CurrentTurn, char, keeps track of which mark is current (X or O)
%
%   Output:
%       Updates figure with appropriate computer move (X or O)
%
%   Team DATT "All DATT and dim sum."
%   12-07-05

if DifficultyLevel == 1 %Easy, generates entirely random moves
    CompMoveValid = 0;
    while CompMoveValid == 0
        CompMoveSquare = 10; %CompMoveSquare has not been generated.
        while (CompMoveSquare == 10)|(CompMoveSquare == 0)
            CompMoveSquare = floor(9*rand(1,1)+1); %Random move 1-9
        end
        
        %To generate a random integer in version MatLab 7.0.1 use:
        %CompMoveSquare = randint(1,1,[1,9])
        if get(findobj('tag',strcat('Pos',num2str(CompMoveSquare))),'string') == ' '
            if CurrentTurn == 'X'
                set(findobj('tag',strcat('Pos',num2str(CompMoveSquare))),'string','X')
                MainGame(CompMoveSquare) = -1;
                CurrentTurn = 'O';
            else
                set(findobj('tag',strcat('Pos',num2str(CompMoveSquare))),'string','O')
                MainGame(CompMoveSquare) = 1;
                CurrentTurn = 'X';
            end
            CurrentPlayer = 'Human';
            CompMoveValid = 1; %Computer's move was a valid move
        end
    end

elseif DifficultyLevel == 2 %Hard, generates random moves but if 
                            %it has a chance to block, it will take it
    CompMoveValid = 0;
    
    %Check for computer block first
    if CurrentTurn == 'X'
        switch 2 %If two O's are in line
            case sum(MainGame(:,1))
                for x = 1:3
                    if MainGame(x) == 0
                        set(findobj('tag',strcat('Pos',num2str(x))),'string','X')
                        MainGame(x) = -1;
                        CompMoveValid = 1;
                    end
                end 
            case sum(MainGame(:,2))
                for x = 4:6
                    if MainGame(x) == 0
                        set(findobj('tag',strcat('Pos',num2str(x))),'string','X')
                        MainGame(x) = -1;
                        CompMoveValid = 1;
                    end
                end
            case sum(MainGame(:,3))
                for x = 7:9
                    if MainGame(x) == 0
                        set(findobj('tag',strcat('Pos',num2str(x))),'string','X')
                        MainGame(x) = -1;
                        CompMoveValid = 1;
                    end
                end
            case sum(MainGame(1,:))
                for x = [1,4,7]
                    if MainGame(x) == 0
                        set(findobj('tag',strcat('Pos',num2str(x))),'string','X')
                        MainGame(x) = -1;
                        CompMoveValid = 1;
                    end
                end 
            case sum(MainGame(2,:))
                for x = [2,5,8]
                    if MainGame(x) == 0
                        set(findobj('tag',strcat('Pos',num2str(x))),'string','X')
                        MainGame(x) = -1;
                        CompMoveValid = 1;
                    end
                end 
            case sum(MainGame(3,:))
                for x = [3,6,9]
                    if MainGame(x) == 0
                        set(findobj('tag',strcat('Pos',num2str(x))),'string','X')
                        MainGame(x) = -1;
                        CompMoveValid = 1;
                    end
                end 
            case (MainGame(1)+MainGame(5)+MainGame(9)) %Diagonal
                for x = [1,5,9]
                    if MainGame(x) == 0
                        set(findobj('tag',strcat('Pos',num2str(x))),'string','X')
                        MainGame(x) = -1;
                        CompMoveValid = 1;
                    end
                end 
            case (MainGame(7)+MainGame(5)+MainGame(3)) %Diagonal
                for x = [7,5,3]
                    if MainGame(x) == 0
                        set(findobj('tag',strcat('Pos',num2str(x))),'string','X')
                        MainGame(x) = -1;
                        CompMoveValid = 1;
                    end
                end 
        end
    else %CurrentTurn == 'O'
        switch -2 %If two X's are in line
            case sum(MainGame(:,1))
                for x = 1:3
                    if MainGame(x) == 0
                        set(findobj('tag',strcat('Pos',num2str(x))),'string','O')
                        MainGame(x) = 1;
                        CompMoveValid = 1;
                    end
                end 
            case sum(MainGame(:,2))
                for x = 4:6
                    if MainGame(x) == 0
                        set(findobj('tag',strcat('Pos',num2str(x))),'string','O')
                        MainGame(x) = 1;
                        CompMoveValid = 1;
                    end
                end
            case sum(MainGame(:,3))
                for x = 7:9
                    if MainGame(x) == 0
                        set(findobj('tag',strcat('Pos',num2str(x))),'string','O')
                        MainGame(x) = 1;
                        CompMoveValid = 1;
                    end
                end
            case sum(MainGame(1,:))
                for x = [1,4,7]
                    if MainGame(x) == 0
                        set(findobj('tag',strcat('Pos',num2str(x))),'string','O')
                        MainGame(x) = 1;
                        CompMoveValid = 1;
                    end
                end 
            case sum(MainGame(2,:))
                for x = [2,5,8]
                    if MainGame(x) == 0
                        set(findobj('tag',strcat('Pos',num2str(x))),'string','O')
                        MainGame(x) = 1;
                        CompMoveValid = 1;
                    end
                end 
            case sum(MainGame(3,:))
                for x = [3,6,9]
                    if MainGame(x) == 0
                        set(findobj('tag',strcat('Pos',num2str(x))),'string','O')
                        MainGame(x) = 1;
                        CompMoveValid = 1;
                    end
                end 
            case (MainGame(1)+MainGame(5)+MainGame(9)) %Diagonal
                for x = [1,5,9]
                    if MainGame(x) == 0
                        set(findobj('tag',strcat('Pos',num2str(x))),'string','O')
                        MainGame(x) = 1;
                        CompMoveValid = 1;
                    end
                end 
            case (MainGame(7)+MainGame(5)+MainGame(3)) %Diagonal
                for x = [7,5,3]
                    if MainGame(x) == 0
                        set(findobj('tag',strcat('Pos',num2str(x))),'string','O')
                        MainGame(x) = 1;
                        CompMoveValid = 1;
                    end
                end 
        end
    end
    
    if CompMoveValid == 1
        if CurrentTurn =='X'
            CurrentTurn='O';
        else
            CurrentTurn='X';
        end
    end

    %Generates a random move if there was no block (CompMoveValid still
    %   equals 0)
    while CompMoveValid == 0
        CompMoveSquare = 10; %CompMoveSquare has not been generated.
        while (CompMoveSquare == 10)|(CompMoveSquare == 0)
            CompMoveSquare = floor(9*rand(1,1)+1); %Random move for 1-9
        end
        
        %To generate a random integer in MatLab Version 7.0.1 use:
        %CompMoveSquare = randint(1,1,[1,9])
        
        if get(findobj('tag',strcat('Pos',num2str(CompMoveSquare))),...
                'string') == ' '
            if CurrentTurn == 'X'
                set(findobj('tag',strcat('Pos',num2str(CompMoveSquare))),...
                    'string','X')
                MainGame(CompMoveSquare) = -1;
                CurrentTurn = 'O';
            else
                set(findobj('tag',strcat('Pos',num2str(CompMoveSquare))),...
                    'string','O')
                MainGame(CompMoveSquare) = 1;
                CurrentTurn = 'X';
            end
            CompMoveValid = 1; %Computer's move was a valid move
        end
    end

    CurrentPlayer = 'Human';
end

TTTWhoWon