Check taken location on Tic Tac Toe board
古いコメントを表示
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
採用された回答
その他の回答 (1 件)
Walter Roberson
2015 年 10 月 4 日
編集済み: Walter Roberson
2015 年 10 月 4 日
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 件のコメント
Anas Abou Allaban
2015 年 10 月 4 日
カテゴリ
ヘルプ センター および File Exchange で Entering Commands についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!