test script files
how do I write test scripts for the following functions?
------------------------------------------
%This function takes a board matrix input, a coords input, and an input for
%the coordinates of the shark. If the proposed move is legal, the shark
%moves one space closer to coords.
function [canMove newShark]=moveOneSquare(board,coords,shark)
%If the shark is more rows than columns away from coords, it will move one
%row closer to coords. Otherwise it will move one column closer.
if abs(coords(1)-shark(1))>= abs(coords(2)-shark(2))
rowDiff=coords(1)-shark(1);
if rowDiff<0
if isLegalMove(board,[shark(1)-1,shark(2)])==true
newShark=[shark(1)-1,shark(2)];
canMove=true;
else
canMove=false;
end
else
if isLegalMove(board,[shark(1)+1,shark(2)])==true
newShark=[shark(1)+1,shark(2)];
canMove=true;
else
canMove=false;
end
end
else
colDiff=coords(2)-shark(2);
if colDiff<0
if isLegalMove(board,[shark(1),(shark(2)-1)])==true
newShark=[shark(1),(shark(2)-1)];
canMove=true;
else
canMove=false;
end
else
if isLegalMove(board,[shark(1),(shark(2)+1)])==true
newShark=[shark(1),(shark(2)+1)];
canMove=true;
else
canMove=false;
end
end
end
--------------------------------------------
%This function moves the shark one square at a time toward a given goldfish
%and terminates when the shark is vertically or horizontally adjacent to
%the said goldfish.
function newShark=moveToGoldfish(board,goldfishCoords,shark)
while isAdjacent(goldfishCoords,shark)~=true
[canMove newShark]=moveOneSquare(board,goldfishCoords,shark);
if canMove==true
board(shark(1),shark(2))='-';
print(board,newShark);
shark=newShark;
else
disp('Cannot Move');
end
end
newShark=shark;
----------------------------------------
function []=play(board,shark,oldShark)
print(board,shark);
[isGoldfish coords]=getClosestGoldfish(board,shark);
if isGoldfish==true
oldShark=shark;
newShark=moveToGoldfish(board,coords,shark);
[newBoard newShark]=eatGoldfish(board,coords,newShark);
play(newBoard,newShark,oldShark);
else
disp('Game Ended');
end
--------------------------------------
I need help with these!!! 0
Comments
You're asking for help on such a general topic that there's no real way we can help.
The general way you should approach the problem is to:
1) Decide what features you want to test
2) Decide how you want to test them (test it with random values, values that you choose, values that are input)
3) Write a script .m file that calls your functions with the values.
-drasnor