1

I'm writing a tic tac toe game and part of the 'coding rules' is that there should be a 'checkwin' function to see whether a player has won or not. I have defined two variables called 'tttXArray' and 'tttOArray' to see whether one player has gotten three in a row for horizontal, vertical, or diagonal inputs. This is the function with the tttXArray placed as an example:

function [won] = checkwin
%Check to see whether the game has been won or not
% Horizontal
    if (tttXArray(1,1) == tttXArray(1,2) && tttXArray(1,1) == tttXArray(1,3))
        won = 1;
    elseif (tttXArray(2,1) == tttXArray(2,2) && tttXArray(2,1) == tttXArray(2,3))
        won = 1;
    elseif (tttXArray(3,1) == tttXArray(3,2) && tttXArray(3,1) == tttXArray(3,3))
        won = 1;
    % Vertical
    elseif (tttXArray(1,1) == tttXArray(2,1) && tttXArray(1,1) == tttXArray(3,1)) 
        won = 1;
    elseif (tttXArray(1,2) == tttXArray(2,2) && tttXArray(1,2) == tttXArray(3,2)) 
        won = 1;
    elseif (tttXArray(1,3) == tttXArray(2,3) && tttXArray(1,3) == tttXArray(3,3)) 
        won = 1;
    % Diagonal
    elseif (tttXArray(1,1) == tttXArray(2,2) && tttXArray(1,1) == tttXArray(3,3))
        won = 1;
    elseif (tttXArray(1,3) == tttXArray(2,2) && tttXArray(1,3) == tttXArray(3,1))
        won = 1;
    end
end

Checkwin is part of a while loop:

while ~checkwin

    playerXTurn = 1;
    playerOTurn = 1;
    %Let Player X go first
    while playerXTurn
        pickXSpot %Prompt  Player
        disp('Test1')
        checktaken %Check taken location
        %If place is taken, prompt player to input again
        if checktaken == 1
            pickXspot
        else
            tttArray(pXInputRow, pXInputCol) = 1; %Set the position as taken
            tttXOArray(pXInputRow, pXInputCol) = 1; %Set the position for X(1)
            plot(pXInputRow, pXInputCol, 'x')
            hold on
            playerXTurn = 0;
        end
    end

    %Check if theres a win
    checkwin

    %Otherwise continue to Player O's turn
    while playerOTurn == 1
        pickOSpot %Prompt Player
        checktaken
        %If place is taken, prompt player to input again
        if checktaken == 1
            pickOspot
        else
            tttArray(pOInputRow, pOInputCol) = 1;%Set the position as taken
            tttXOArray(pOInputRow, pOInputCol) = 0;%Set the position for O(0)
            plot(pOInputRow, pOInputCol,'o')
            hold on
        end
    end  

    %Check win again
    checkwin
end

The error I get is:

Undefined function 'tttXArray' for input arguments of type 'double'.

What seems to be the problem?

Anas Yousef
  • 93
  • 1
  • 7
  • 4
    You call tttXArray lots of times inside your function, but where do you DEFINE the function? The error says that you pass a double (2 arguments) to the function, but that the function definition (if it exists) does not allow the double as an argument. – wintvelt Oct 02 '15 at 13:47
  • So much `if`statements. Just loop over the condition instead of explicitly writing out all possibilities, since that will take you a lot of time for larger systems – Adriaan Oct 02 '15 at 13:48
  • 1
    `checkwin` needs a parameter passed to it... i.e. `function [won] = checkwin(tttXArray)` – Dan Oct 02 '15 at 14:08
  • why does it smell like the code isnt yours ? the arrays `tttArray` and `tttXOArray` must be defined and __filled__, and the function `pickOspot` is unreferenced. – Abr001am Oct 02 '15 at 15:03
  • I defined all these arrays as global arrays in a another function called boardplot and set them all to an array of 3x3 zeroes. They were defined like this: `global tttArray; tttArray = zeros(3);` – Anas Yousef Oct 03 '15 at 11:46

1 Answers1

0

so I realized I was not calling the function properly nor was I giving it any arguments. Here is what I'm doing now

function [won] = checkwin(tttXArray)

I also simplified all the if/else statements as follows:

won = any(all(tttXArray) | all(tttXArray, 2)' | all(diag(tttXArray)) | all(fliplr(diag(tttXArray))));

Thanks for the tips!

Anas Yousef
  • 93
  • 1
  • 7