function mancala()

% 1: most basic idea
% 2: Mimic playing the game
% 3: Display the board
% 4: Move pebbles and update score
% 5: Detect end of game
% 6: Declare winner
% 7: Clean up

% --------- Main Loop to Play the Game -------------
% pn:      Player Names (two string cell array)
% again:   Play Again? (logical)
% b:       Board (twelve integer array of pockets)
% ps:      Player Scores (two integer array)

   pn = getPlayerNames();
   again = true;
   while again
     [b ps it] = setUpGame();
     [ps] = playGame(b, ps, pn, it);
     declareWinner(ps, pn)
     again = anotherGame();
   end

% --------- Get Player Names from Input ------------
function player = getPlayerNames()
   player{1} = input('Enter first player''s name: ', 's');
   player{2} = input('Enter second player''s name: ', 's');
return

% --------- Set up the Initial Board etc. ----------
function [board playerScores it] = setUpGame()
   board(1:12) = 4;        % four stones in each hole to start
   playerScores(1:2) = 0;  % zero out the opening scores
   it = 1;                 % start with player one
return

% --------- Play the Game to its Conclusion --------
function [playerScores] = playGame(board, playerScores, playerNames, it)

% -- anonymous function fin:  check for game completed, given array a
% --    1. all holes have no more than 1 pebble each
% --    2. not any instance of adjacent 1's
% --    3. any (at least one) instance of adjacent 0's
fin =@ (a) all(a <= 1) & ~any(a & circshift(a,1)) & any(~a & ~circshift(a,1));

   while ~fin(board) 
     displayBoard(board, playerScores, playerNames);
     pickedHole = pickHole(board, playerNames{it});
     [board finalHole] = distributePebbles(board, pickedHole);
     [board playerScores] = updateScore(board, finalHole, playerScores, it);
     it = 3 - it;
   end
   displayBoard(board, playerScores, playerNames);
return

% ------- Display the Board ----------------------
function displayBoard(board, playerScores, playerNames)
   fprintf('\n\n');
   fprintf('  1   2   3   4   5   6  \n');
   fprintf('+---+---+---+---+---+---+\n');
   for ii = 1:6
     fprintf('|%3d', board(ii));
   end
   fprintf('|\n');
   fprintf('+---+---+---+---+---+---+\n');
   for ii = 12:-1:7
     fprintf('|%3d', board(ii));
   end
   fprintf('|\n');
   fprintf('+---+---+---+---+---+---+\n');
   fprintf(' 12  11  10   9   8   7  \n');
   fprintf('\n');
   fprintf('%s: %d   %s: %d \n', playerNames{1}, playerScores(1), ...
                                 playerNames{2}, playerScores(2));
   fprintf('\n\n');
   return

% ------- Player Picks a Hole --------------------
function pick = pickHole(board, player)
   fprintf('%s, Enter hole 1-12: ', player);
   pick = input(': ');
   while pick < 1 || pick > 12 || board(pick) == 0
      pick = input('Invalid!  Enter hole 1-12: ');
   end
return

% -------- Distribute the Pebbles (stub) ----------
% -- and return the hole position of final pebble -

function [board, finalHole] = distributePebbles(board, pick)
   hand = board(pick);
   board(pick) = 0;
   for ii = 1:hand
      hole = mod(pick + ii -1, 12) + 1;
      board(hole) = board(hole) + 1;
   end
   finalHole = hole;
return

% -------- Update Score ----------------------------
function [board playerScores] = updateScore(board, hole, playerScores, it)
   if board(hole) == 2 || board(hole) == 3
     playerScores(it) = playerScores(it) + board(hole);
     board(hole) = 0;
   end
return

% --------- Declare the Winner (stub) ---------------
function declareWinner(scores, players)
   if scores(1) > scores(2)
     fprintf('Congrats, %s!  You win!\n', players{1});
   elseif scores(1) < scores(2)
     fprintf('Congrats, %s!  You win!\n', players{2});
   else
     fprintf('Tie game!\n');
   end
return

% --------- Another Game request --------------------
function another = anotherGame()
   another = input('Another game (1 if yes, 0 if no)? ');
return
 
