function latrunculi()

% most basic idea
% set up the game
% play the game 
% get the play
% valid play?
% finish valid play - open gap for move?

global board score it marker player
disp('Welcome to Latrunculi, a game from classic Rome!');
player = getPlayerNames();
another = true;
while another
  setUpGame();
  playGame();
  another = anotherGame();
end
return

function player = getPlayerNames()
player{1} = input('Enter first player''s name: ', 's');
player{2} = input('Enter second player''s name: ', 's');
return

function setUpGame()
global board score it marker
disp('Set up game');
marker = 'BWbw';
board(1:8, 1:12) = ' ';
board(1,:) = marker(4);
board(2,6) = marker(2);
board(7,7) = marker(1);
board(8,:) = marker(3);
board = char(board);
score(1:2) = 0;
it = 1
return

function playGame()
global board score it marker player
disp('Play game');
alive = true;
while alive
  displayBoard()
  play = getPlay()
  switch play
    case 'Q'
      alive = false
    case 'H'
      makeHint()
    otherwise
      makePlay(play)
      checkCapture(play)
      alive = checkBlock()
      it = 3 - it;
  end
end
declareWin()
return

function displayBoard()
disp('Display Board')
return

function play = getPlay()
global player it marker
disp([player{it} ', your turn (q - quit, h - hint, cr-cr - from-to)'])
fprintf('(You play %c) ', marker(it));
play = upper(input(': ','s'))
while ~validPlay(play)
  displayBoard()
  disp('Whoops, not valid! Choose ''q'', ''h'', or col row - col row')
  disp('as in C4-C6')
  play = upper(input('Play? ','s'))
end
return

function valid = validPlay(play)
disp('Valid Play?')
valid = strcmp(play, 'Q') || strcmp(play, 'H') || ...
        length(play) == 5 && validMove(play)
return

function valid = validMove(play)
validFormat = play(1) >= 'A' && play(1) <= 'L' && play(2) >= '1' && play(2) <= '8' &&  ...
              play(4) >= 'A' && play(4) <= 'L' && play(5) >= '1' && play(5) <= '8'
valid = validFormat && ((play(1) == play(4) && play(2) ~= play(5) && openGap(play)) || ...
                        (play(2) == play(5) && play(1) ~= play(4) && openGap(play)))
return

function valid = openGap(play)
global board marker it
disp('Open gap for a move?')
[startCol, startRow] = parse(play(1), play(2))
[destCol, destRow]  = parse(play(4), play(5))
validPiece = upper(board(startRow, startCol)) == marker(it)
startRow = startRow - 1 + 2 * (startRow < destRow) + (startRow == destRow)
startCol = startCol - 1 + 2 * (startCol < destCol) + (startCol == destCol)
validGap = board(min([startRow destRow]):max([startRow destRow]), ...
                 min([startCol destCol]):max([startCol destCol])) == ' '
valid = validPiece && all(validGap)
return

function [c, r] = parse(ca, ra)
disp('Parsing')
c = ca - '@'
r = ra - '0'
return

function makeHint()
disp('Make a Hint')
return

function makePlay(play)
disp('Make a Play')
return

function checkCapture(play)
disp('Check for a Capture')
return

function alive = checkBlock()
disp('Check for Blocking')
alive = true;
return

function declareWin()
disp('Declare Winner')
return


function another = anotherGame()
another = strcmpi(input('Another game (y or n)? ','s'), 'Y');
return
 
