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?