function Lab28()
%---------------------------------------------------------------
%       USAGE: Lab28
%
%      AUTHOR: ?????????       DATE: ?????????     
%
% DESCRIPTION: Play Tick-Tack-Toe
%
%      INPUTS: None
%
%     OUTPUTS: None
%
%  REFERENCES: None
%---------------------------------------------------------------

clc();

% All calculations and data structures in this program are based on the
% size of the Tick-tack-toe board. You can change the board size to
% create harder games.
Board_size = 3;

Display_tick_tack_toe_board(Board_size);

Board = Create_tick_tack_toe_board(Board_size);

Player         = 'O';
Key            = ' ';
Winning_player = ' ';
while Winning_player == ' ' && Key ~= 'q'
  Button = waitforbuttonpress();
  if Button == 0
    
    % A mouse button was pressed -- get its location
    Location = get(gca(), 'CurrentPoint');
    
    % Calculate the row and column position of the mouse cursor
    Row = floor(Location(1,2));
    Col = floor(Location(1,1));
    
    % If the selected square is blank, this is a legal play
    if Board(Row, Col) == ' '
      Board(Row,Col) = Player;
      Draw_mark(Row, Col, Player);
      Winning_player = Check_for_winner(Board, Board_size, Player);
      
      % Switch Players
      if Player == 'O'
        Player = 'X';
      else
        Player = 'O';
      end
    end
  else % Button == 1; A keyboard key was pressed
    Key = get( gcf(), 'CurrentCharacter');
  end
end

Display_winner(Board_size, Winning_player);

waitforbuttonpress();
close();

end % Lab28_solution

%-------------------------------------------------------------------------------
function Display_tick_tack_toe_board(Board_size)

% Create the tick-tack-toe board
Axes_handle = axes();
set(Axes_handle, 'XLim', [1 Board_size+1]);  % Set the x-axis to be Board_size units long
set(Axes_handle, 'YLim', [1 Board_size+1]);  % Set the y-axis to be Board_size units long

set(Axes_handle, 'XTick', []);      % remove the tick marks on the x-axis
set(Axes_handle, 'XTickLabel', []); % remove the tick mark labels on the x-axis
set(Axes_handle, 'YTick', []);      % remove the tick marks on the y-axis
set(Axes_handle, 'YTickLabel', []); % remove the tick mark labels on the y-axis
hold('on');

for x = 2:Board_size
  plot([x x], [0 Board_size+1]); % draw vertical lines
end

for y = 2:Board_size
  plot([0 Board_size+1], [y y]); % draw horizontal lines
end

end % Display_Tick_Tack_Toe_Board()

%-------------------------------------------------------------------------------
function Board = Create_tick_tack_toe_board(Board_size)

for row = 1:Board_size
  Board(row, 1:Board_size) = blanks(Board_size);
end

end % Create_Tick_Tack_Toe_Board()

%-------------------------------------------------------------------------------
function Draw_mark(Row, Col, Player)

if Player == 'O' % If the player is an 'O', draw a circle
  % Create the points that define an upper half circle around (0,0)
  Circle_radius = 0.4;
  x = [-Circle_radius : 0.025 : Circle_radius];
  y = sqrt(Circle_radius^2 - x.^2);
  
  % Offset the circle to get it in the middle of a unit square [0 1]
  xCircle = x + 0.5;
  yCircle = y + 0.5;
  
  plot(xCircle+Col,yCircle+Row, 'LineWidth', 3); % Plot upper half circle in (row,col) square
  yCircle = -y + 0.5;
  plot(xCircle+Col,yCircle+Row, 'LineWidth', 3); % Plot lower half circle in (row,col) square
  
else % The player is an 'X', draw an X
  
  plot([0.2 0.8]+Col, [0.2 0.8]+Row, 'LineWidth', 3);
  plot([0.8 0.2]+Col, [0.2 0.8]+Row, 'LineWidth', 3);
  
end

end % Display_Tick_Tack_Toe_Board()

%-------------------------------------------------------------------------------
function Winning_player = Check_for_winner(Board, Board_size, Player)

  Winning_player = ' ';  
  
  % Check to see if this created a sequence -- i.e., is there a winner?
  if sum( sum( Board == Player ) == Board_size) > 0 % a column is full of the same symbols
    Winning_player = Player;
  elseif sum( sum( Board' == Player ) == Board_size) > 0 % a row is full of the same symbols 
    Winning_player = Player;
  elseif sum( diag(Board,0) == Player ) == Board_size % the diagonal has all the same symbols
    Winning_player = Player;
  else % check the upper-right to lower left diagonal for all of the same symbols
    Total = 0;
    for row = 1:Board_size
      if Board(row,(Board_size-row+1) ) == Player
        Total = Total + 1;
      end
    end
    if Total == Board_size
      Winning_player = Player;
    end
  end
  
  % Check to see if all the blanks are gone and the game is over
  if sum( sum( Board == ' ') ) == 0
    Winning_player = 'C'; % "Cat" got the game
  end

end

%-------------------------------------------------------------------------------
function Display_winner(Board_size, Winning_player)


if Winning_player == 'O'
  O_won();
elseif Winning_player == 'X';
  X_won()
else % 'Cat' got the game
  Cat_won();
end

end % Display_winner

%-------------------------------------------------------------------------------
function O_won()
end % O_won

%-------------------------------------------------------------------------------
function X_won()
end % X_won

%-------------------------------------------------------------------------------
function Cat_won()
end % Cat_won
