Search code examples
stringmatlabhelpertic-tac-toecell-array

Determining Moves and Outcomes for Tic-Tac-Toe in MATLAB W/ Helper Function


Note: I am up-dating this as I go along. I add in comments to let you know if I added in code or not. Since I have no comments, I doubt anyone will get confused. And to whomever down-voted me, I'm putting in as much information as I can. Calm yourself.

That's a pretty long title, but it sums up everything I need to do for this problem. I am given a tic-tac-toe board in the form of a 3x3 cell and a string that tells me whose move it is (Player X or Player O). I need to out a 2xN cell that lists what possible moves can be made, and what the outcome of that move would be. For example, the the player is Player X and my board looks like:

       { 'x' 'o' 'x'
         'o' 'x' 'o'
         'o' ' ' ' ' }

Then my options are

    { 'x' 'o' 'x'              {'x' 'o' 'x'
      'o' 'x' 'o'              'o' 'x' 'o'
      'o' 'x' ' '}             'o' ' ' 'x'}
 'Your move, player O.'       'Player X Wins!' }

Blanks spots are always indicated by a space. Now for this problem, I've been given a helper function called 'moveEvaluater' that looks like this:

 Helper Function Name: moveEvaluator
 Helper Function Inputs (2): - (char) A 3x3 character array representing
                                        a tic tac toe board, after a move
                                        has been made.
                               - (char) A single character of the the
                                        player that just moved.
   Helper Function Outputs (1): - (char) A string indicating the outcome
                                         of the turn.

   The helper function moveEvaluator takes in a 3x3 character array of a
   tic tac toe board after a move has been made, with spaces used to 
   represent the empty spaces on the board, and a single character of the 
   player who just went. This 2nd input will be a string of either 'x' or 
   'o'. This helper function will then output the outcome of that turn: if
   it was player X's turn, then the two possible outcomes are 'Player X
   Wins!' or 'Your turn, player O.' Similarly, if it was player O's turn,
   then the two possible outcomes are 'Player O Wins!' or 'Your turn,
   player X.'

So this basically does half my function for me. I seem to have this working thanks to Hoki :)

    function[Move_Choices] = ticTacToeTurn(Board, str)

 charBoard = reshape(char(Board) , 3 , 3 ) ;
 PlayerSymbol = str ;

%  Find the indices of possible move
Move_Choices = find(ismember(Board, ' '));

% // count them
nChoices = numel(Move_Choices) ;

% // pre-allocate output
outcome = cell(nChoices,1) ;
Options = '';
Strings = '';
% // Get outcome for each possible postition
for iSlot = 1:nChoices
   PossibleBoard = charBoard ; % // copy the initial board
   PossibleBoard(Move_Choices(iSlot) ) = PlayerSymbol;% // Add an 'x' at one of the empty position
   disp(PossibleBoard) ; % display the currently played board / optional, just for your information
 PossibleBoard = char(PossibleBoard);
  outcome = moveEvaluator(PossibleBoard, str);
    Strings = [Strings {outcome}];
end
for iSlot = 1:nChoices
   PossibleBoard = charBoard ; % // copy the initial board
   PossibleBoard(Move_Choices(iSlot) ) = PlayerSymbol;% // Add an 'x' at one of the empty position
   disp(PossibleBoard) ; % display the currently played board / optional, just for your information
 PossibleBoard = cellstr(PossibleBoard);
  Options = [Options, {PossibleBoard}];
end
Move_Choices = [Options;Strings]; %// Here's my issue. This outputs a cell, but the cellstr function separated the x's and o's into cells of three. I need each spot to be their own cell :/
end

I just need it to fill in the one x in either slot, not both. Furthermore, I need to do is figure out how to use the helper function from here to output what the possible string options there are.

Testcases:
 board1 = { 'x' 'o' 'x'
                'o' 'x' 'o'
                  'o' ' ' ' ' }
move1 = 'x'
possibleMoves1 = ticTacToeTurn(board1,move1)

This should give be a 2x2 cell that looks like:

Cell (1,1) This is a cell inside a cell. All of the 'x' and 'o's are a separate cell
{'x''o' 'x'
'o' 'x' 'o'
'o' 'x' ' '}
Cell (2,1) looks is the string that says "Your move, Player O"

Cell (1,2) Should be 
{ 'x'   'o' 'x'
'o' 'x' 'o'
'o'     ' ' 'x'}
 Cell (2, 2) looks is the string that says "Player X wins!'

I tried to look at the code for the evaluator function, but it's apparently a p file so I'm not too sure what to do. Any help would be appreciated :) Let me know if I missed anything. Should be all.


Solution

  • Ok, seeing you tried a bit by yourself I'll point you in the final direction. This is not a "function" and so it is not a direct answer to your assignment but it includes the complete resolution of the problem. You just have to repackage it into your function.

    %% // Initial conditions
    Board = {'x' 'o' 'x'
             'o' 'x' 'o'
             'o' ' ' ' ' };
    charBoard = reshape( char(Board) , 3 , 3 ) ;
    PlayerSymbol = 'x' ;
    
    %% // Resolution
    % // Find the indices of possible move
    Move_Choices = find(ismember(Board, ' '));
    
    % // count them
    nChoices = numel(Move_Choices) ;
    
    % // pre-allocate output
    outcome = cell(nChoices,1) ;
    
    % // Get outcome for each possible postition
    for iSlot = 1:nChoices
       PossibleBoard = charBoard ; % // copy the initial board
       PossibleBoard( Move_Choices(iSlot) ) = PlayerSymbol ; % // Add an 'x' at one of the empty position
       disp(PossibleBoard) ; % display the currently played board / optional, just for your information
       outcome(iSlot) = moveEvaluator( PossibleBoard , PlayerSymbol ) ;
    end
    

    A bit of advice: Matlab is an interpreted language. This type of programming language has its pro and cons, but it is great for one thing, you can execute lines of code and directly see the result without the need for a complex debugger.

    So you can try many different expressions and refine it until the output is what you are looking for. Seeing the way your initial code was organised, it looks like you could benefit a lot from that. Try to solve your problem bit by bit in the base workspace first until you get a good grasp on your problem and its solution. At this stage it will be a lot easier to put it into a function (just have to work out the input/output, but the core of the calculations is already solved).