function ticnotgui()

% Tic Tac Toe without GUI
% Developed by Charles Riedesel, 4/28/10

% its           1 or 2, determines whose turn it is
% count         if reaches 9, game is over
% mark          'XO' or 'OX' - marks for players 1 and 2
% board         3 by 3 array of char representing the board
% names         cell array of the two players' names
% wins          number of wins for each player
% anotherGame   true until player says done
% notDone       no winner yet and places to play
% pos           chosen square 1-9

global board its mark names wins count 
its = 1;
count = 0;
mark = 'XO';
board = ['   ';'   ';'   '];
wins = [0,0];

% vvvvvvvvvvv FOLLOWING IS HANDLED BY CALL BACK IN GUI vvvvvvvvvvvvv
names{1} = input('Enter first player''s name: ','s');
names{2} = input('Enter second player''s name: ','s');

% vvvvvvvvvvv FOLLOWING DOES NOT EXIST IN GUI VERSION vvvvvvvvvvvvvv
anotherGame = true;
while anotherGame

   notDone = true;
   displayBoard();
   while notDone 
      fprintf('%s (playing %c) your play (1-9): ', names{its}, mark(its));
      pos = input('');
      notDone = process(pos);
   end
   
   anotherGame = playAgain();
end
end
% ^^^^^^^^^^^^^ ABOVE DOES NOT EXIST IN GUI VERSION ^^^^^^^^^^^^^^^^^

% ------------ FOLLOWING HAPPENS UPON ENTERING A PLAY ---------------
function notDone = process(pos)
global board its mark names wins count 
r = ceil(pos/3);
c = mod(pos-1,3)+1;            % determine row and column given 1-9

% Continue processing only if open position and game not done
if board(r, c) == ' ' && count < 9 
    count = count + 1;         % cat game if reaches 9 plays
    board(r, c) = mark(its);   % place the X or O on the board
    displayBoard()             % display updated board
    if winner()
        count = 9;
        fprintf('%s (playing %c) wins!\n', names{its}, mark(its));
        wins(its) = wins(its) + 1;
    elseif count == 9
        fprintf('Cat game!\n')
    else
        its = 3 - its;
    end
    notDone = ~winner() && count < 9;
else
    fprintf('Already picked!  ');
    notDone = true;
end
end

% --------------------------------------------------------------------
function win = winner()
global board its mark
win = (board(1,1) == mark(its) && board(1,1) == board(1,2) && board(1,2) == board(1,3)) || ...
      (board(2,1) == mark(its) && board(2,1) == board(2,2) && board(2,2) == board(2,3)) || ...
      (board(3,1) == mark(its) && board(3,1) == board(3,2) && board(3,2) == board(3,3)) || ...
      (board(1,1) == mark(its) && board(1,1) == board(2,1) && board(2,1) == board(3,1)) || ...
      (board(1,2) == mark(its) && board(1,2) == board(2,2) && board(2,2) == board(3,2)) || ...
      (board(1,3) == mark(its) && board(1,3) == board(2,3) && board(2,3) == board(3,3)) || ...
      (board(1,1) == mark(its) && board(1,1) == board(2,2) && board(2,2) == board(3,3)) || ...
      (board(1,3) == mark(its) && board(1,3) == board(2,2) && board(2,2) == board(3,1));
end

% -------- FOLLOWING HAPPENS WHEN AGREEING TO ANOTHER GAME ------------
function anotherGame = playAgain()
   global board its count mark
   count = 0;                      % reset everything for possible next game
   board = ['   ';'   ';'   '];
   if strcmp(mark, 'XO')           % switch which player starts (with X)
      mark = 'OX';
      its = 2;
   else
      mark = 'XO';
      its = 1;
   end
   anotherGame = input('Play again?  1 - yes, 0 - no: ');
end

% ---------- FOLLOWING DOES NOT EXIST IN GUI VERSION ------------------
function displayBoard()
   global board its mark names wins
   fprintf('-------------------------------------------\n');
   fprintf('\n%s has %d wins\n', names{1}, wins(1));
   fprintf('%s has %d wins\n\n', names{2}, wins(2));
   fprintf('      %c|%c|%c\n', board(1,1), board(1,2), board(1,3));
   fprintf('      -+-+-\n');
   fprintf('      %c|%c|%c\n', board(2,1), board(2,2), board(2,3));
   fprintf('      -+-+-\n');
   fprintf('      %c|%c|%c\n\n', board(3,1), board(3,2), board(3,3));
end

