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
The error I get is:
Undefined function 'tttXArray' for input arguments of type 'double'.
What seems to be the problem?

 채택된 답변

John D'Errico
John D'Errico 2015년 10월 2일

1 개 추천

Your function has no arguments. How do you expect it to know what those variables are?

댓글 수: 6

Joseph Cheng
Joseph Cheng 2015년 10월 2일
what are the values of tttXArray for each marker? instead of a a long list of if-else statements for the horizontal/vertical wouldn't it be easier to and cleaner to check for the sum of a row and sum of columns? that would leave just 2 if statements for the diagonals which can be simplified as well.
Thorsten
Thorsten 2015년 10월 2일
편집: Thorsten 2015년 10월 2일
Joseph is right. You can use something like
function won = checkwin(A)
won = any(all(A) | all(A, 2)' | all(diag(A)) | all(fliplr(diag(A))));
So the values of tttXArray come from another function where the player chooses their spot.
function [pXInputRow, pXInputCol] = pickXspot(playerInput)
%This function is to take inputs from Player X
global pXInputRow;
global pXInputCol;
%Set text for Roq/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});
end
So I should take tttXArray as an input argument. However, I get
Undefined function or variable 'pXInputRow'.
Thanks for simplified if/else statement suggestion, definitely much cleaner.
Anas Abou Allaban
Anas Abou Allaban 2015년 10월 2일
편집: Anas Abou Allaban 2015년 10월 2일
Forgot last line:
tttArray(pXInputRow, pXInputCol) = 1;
No. You should define
function won = checkwin(tttXArray)
and call using
checkwin(tttXArray)
Anas Abou Allaban
Anas Abou Allaban 2015년 10월 3일
So I realized my rather beginner mistake, I was not calling the function properly nor was I giving it any proper arguments. The checkwin function works good now, time to debug the rest of the code. Thanks Thorsten for the simplified checking algorithm!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Strategy & Logic에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by