Tic Tac Toe Win Checking Function
7 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Anas Abou Allaban
il 2 Ott 2015
Commentato: Anas Abou Allaban
il 3 Ott 2015
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?
0 Commenti
Risposta accettata
John D'Errico
il 2 Ott 2015
Your function has no arguments. How do you expect it to know what those variables are?
6 Commenti
Thorsten
il 2 Ott 2015
No. You should define
function won = checkwin(tttXArray)
and call using
checkwin(tttXArray)
Più risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!