Check taken location on Tic Tac Toe board

2 views (last 30 days)
I'm trying to make a small function that checks whether a spot is taken on a Tic Tac Toe board or not. I have created an array of zeroes called tttArray where when each spot is filled, its location is changed to 1. So I first take the input from the player from the below function.
function [pXInputRow, pXInputCol] = pickXspot(playerInput)
%This function is to take inputs from Player X
pXInputRow = 0;
pXInputCol = 0;
%Set text for Row/Col Prompt
prompt = {'Row (1,2, or 3)', '(Col (1, 2, or 3)'};
name = 'Player X Turn';
%Show prompt to input values
playerInput = inputdlg(prompt, name);
pXInputRow = str2num(playerInput{2});
pXInputCol = str2num(playerInput{1});
tttArray(pXInputRow, pXInputCol) = 1;
end
And then use the below function to see if the spot is taken.
function [spotTaken] = checktaken(tttArray)
%Function used to check if spot is taken
%Setup Error Messages
errorMessage = 'This spot is taken, please choose another spot';
errorMessageTitle = 'Spot Taken';
if tttArray(pXInputRow, pXInputCol) || tttArray(pOInputRow, pOInputCol) == 1
msgbox(errorMessage, errorMessageTitle)
spotTaken = 1;
end
end
However, I keep getting the following error after I run and put a row/col in the prompt dialog box. Any Suggestions?
Not enough input arguments.
Error in checktaken (line 8)
if tttArray(pXInputRow, pXInputCol) || tttArray(pOInputRow, pOInputCol) == 1

Accepted Answer

Anas Abou Allaban
Anas Abou Allaban on 7 Oct 2015
So I solved the problem. First I did not pass the proper inputs to the function checktaken which obviously led to some errors. Then I rewrote my user input statements to use only 2 variables for rows/cols rather than 4, where there are 2 for each player. checktaken is rewritten as follows:
function [spotTaken] = checktaken(tttArray, x, y)
%This function is used to check if spot is taken
%Function takes users row/col input as indices for the taken locations
%array, 'tttArray'. It then returns whether the spot is taken or not.
%Setup Error Messages
errorMessage = 'This spot is taken, please choose another spot';
errorMessageTitle = 'Spot Taken';
spotTaken = 0; %Initialization
%If the location's value is 1(taken), show error message and return
%spotTaken as 1(true).
if tttArray(x,y) == 1
msgbox(errorMessage, errorMessageTitle)
pause(3)
spotTaken = 1;
end
end
And I take the input via
function [a,b] = pickunospot
%This nested function creates the prompt for the player and takes
%the inputs as indices to be used later on in our arrays
prompt = {'Row (1,2, or 3)', '(Col (1, 2, or 3)'};
name = 'Enter your choice of row or column';
pt=inputdlg(prompt, name);
a = str2num(pt{2});
b = str2num(pt{1});
end
and call it like this
[x,y] = pickunospot;
where x and y are the rows/cols and can be used as matrix indices in checktaken.

More Answers (1)

Walter Roberson
Walter Roberson on 4 Oct 2015
Edited: Walter Roberson on 4 Oct 2015
Your function pickXspot changes tttArray but does not return its value. The change is only going to affect the workspace of that one function, unless tttArray is defined in a function that picXspot is nested inside.
Your function checktaken uses pXInputRow and pXInputCol and pOInputRow and pOInputCol but they are not passed as parameters. This will be an error unless they happen to be defined as functions, or of they are defined as variables in a function that checktaken is nested inside.
The error you display would be consistent with the possibility that you invoked checktaken() without passing any input parameters.
Note: the syntax
if tttArray(pXInputRow, pXInputCol) || tttArray(pOInputRow, pOInputCol) == 1
is equivalent to
if tttArray(pXInputRow, pXInputCol) ~= 0 || tttArray(pOInputRow, pOInputCol) == 1
if you want to test that both values are 1 then you need to test them individually -- the "== 1" does not "distribute" to both locations.
Also note that if your "if" is not considered true then you do not assign a value to spotTaken which would cause a problem.
Might I also suggest that you should be testing whether the spot is taken before assigning the spot as taken in pickXspot, and if it is taken then force the user to pick a different spot before returning?
  1 Comment
Anas Abou Allaban
Anas Abou Allaban on 4 Oct 2015
So would you suggest I rewrite the function definition as follows?:
function [tttArray(pXInputRow, pXInputCol)] = pickXspot(playerInput)
I did call the checktaken function using
checktaken(tttArray)
And I get no error there. I do ask the user in another function that runs the game to re-input their spot if its taken and it checks before and after if the spot is taken. This is the code
while playerXTurn == 1
pickXspot %Prompt Player
checktaken(tttArray) %Check taken location
%If place is taken, prompt player to input again
if checktaken(tttArray) == 1
pickXspot
else
tttArray(pXInputRow, pXInputCol) = 1; %Set the position as taken
tttXArray(pXInputRow, pXInputCol) = 1; %Set the position for X(1)
plot(pXInputRow, pXInputCol, 'x')
hold on
playerXTurn = 0;
end
end
Also good call on the if statement syntax, thanks!

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!