【发布时间】:2015-04-01 22:18:34
【问题描述】:
-
这是外部函数:用于将棋盘的坐标更改为字母数字数据而不是纯数字。
function [] = convertIntegerToCoordinates (i) First=floor(i/10); if First ==1 fprintf('A') elseif First ==5 fprintf('E') elseif First ==6 fprintf('F') elseif First ==7 fprintf('G') elseif First ==8 fprintf('H') end Second=i-(10*First); if Second ==1 fprintf('1') elseif Second ==2 fprintf('2') elseif Second ==3 fprintf('3') elseif Second ==4 fprintf('4') elseif Second ==5 fprintf('5') elseif Second ==6 fprintf('6') elseif Second ==7 fprintf('7') elseif Second ==8 fprintf('8') end endfunction
2.这是主要功能:用于检测白国王是否受到Rook,Bishop,Queen和Knight四个中任何一个的威胁的功能。
function [] = ChessThreatCheck (n,b,c)
B = 'B';
R = 'R';
Q = 'Q';
K = 'K';
n= input("Please enter the coordinates of white king: ");
whiteFirst=floor(n/10);
whiteSecond=n-(10*whiteFirst);
if (whiteFirst>8 || whiteFirst<1) && (whiteSecond>8 || whiteSecond<1)
fprintf ('The x and y coordinates of the white king should be between 1 and 8\n');
return;
end
if whiteFirst>8 || whiteFirst<1
fprintf('The x coordinate of white king should be between 1 and 8\n');
elseif whiteSecond>8 || whiteSecond<1
fprintf('The y coordinate of white king should be between 1 and 8\n');
return;
end
b = input("Please enter the type of black chessman: ");
if b != B && b!= R && b!= Q && b!= K
fprintf('The type of the black chessman can be (B)ishop, (R)ook, (Q)ueen or (K)night\n');
return;
end
c = input("Please enter the coordinates of black chessman: ");
blackFirst = floor(c/10);
blackSecond = c-(10*blackFirst);
if (blackFirst<1 || blackFirst>8) && (blackSecond<1 || blackSecond>8)
fprintf('The x and y coordinates of the black chessman should be between 1 and 8\n')
return;
end
if blackFirst<1 || blackFirst>8
fprintf('The x coordinate of the black chessman should be between 1 and 8\n');
elseif blackSecond<1 || blackSecond>8
fprintf('The y coordinate of the black chessman should be between 1 and 8\n');
return;
end
if (b=='R')&&((blackFirst == whiteFirst) || (blackSecond == whiteSecond))
fprintf('The White King at %s is threatened by black rook at %s \n',convertIntegerToCoordinates(n) ,convertIntegerToCoordinates(c));
elseif fprintf('The White King at %s is not threatened by the black rook at %s \n', convertIntegerToCoordinates(n),convertIntegerToCoordinates(c));
end
return;
if (b=='B') && (abs(blackFirst-whiteFirst) == abs(blackSecond-whiteSecond))
fprintf('The white king at %s is threatened by the black bishop at %s \n', convertIntegerToCoordinates(n), convertIntegerToCoordinates(c));
elseif fprintf('The white king at %s is not threatened by the black bishop at %s \n', convertIntegerToCoordinates(n),convertIntegerToCoordinates(c));
end
if (b=='Q')&&((abs(blackFirst-whiteFirst) == abs(blackSecond-whiteSecond)) || ((blackFirst == whiteFirst) || (blackSecond == whiteSecond)))
fprintf ('The white king at %s is threatened by black queen at %s \n', convertIntegerToCoordinates(n),convertIntegerToCoordinates(c));
else if fprintf('The white king at %s is not threatened by black queen at %s\n', convertIntegerToCoordinates(n), convertIntegerToCoordinates(c));
end
if (b=='K')&& ((abs(blackFirst-whiteFirst == 2) && abs(blackSecond-whiteSecond==1)) || (abs(blackFirst-whiteFirst == 1) && abs(blackSecond-whiteSecond==2)))
fprintf('The white king at %s is threatened by black knight at %s \n', convertIntegerToCoordinates(n),convertIntegerToCoordinates(c));
elseif fprintf('The white king at %s is not threatened by black knight at %s \n', convertIntegerToCoordinates(n),convertIntegerToCoordinates(c));
end
end
-
输出以错误的方式打印。任何人都可以帮助我吗? 我得到的输出是
E5E6The White King at >>
但我希望
The White king at E5 is threatened by the black rook at E6 >>
【问题讨论】:
-
“输出打印方式错误”信息不足。请提供Minimal, Complete, and Verifiable example。这包括: 可运行 代码减少到有问题的部分。您期望的输出。以及您实际得到的输出。
-
您的代码中有几个不一致的地方。您指定
n、b和c作为输入,但稍后使用input请求它们。您使用了哪些值作为输入?打印结果有什么问题?我希望我能读懂你的想法,但不幸的是我不能;) -
@Ratbert 我使用 n 作为 55, b 作为 'R' 和 c 作为 56 。我还在那里添加了我的预期输出
标签: matlab function printf octave