我正在Ada95中编写一个简单程序,以检查棋盘布局输入是否有效。我对Ada还是很陌生,我知道我有很多东西要学习。 我不断收到的错误消息是:
35:31:类型转换的参数必须为单个表达式
47:07:类型转换的参数必须为单个表达式
47:22:用于数组转换的非法操作数
53:10:类型转换的参数必须为单个表达式
58:10:类型转换的参数必须为单个表达式
61:13:类型转换的参数必须为单个表达式
64:13:类型转换的参数必须为单个表达式
66:13:类型转换的参数必须为单个表达式
68:13:类型转换的参数必须为单个表达式
76:15:在表达式或调用中无效使用子类型标记
我在下面提供了完整的代码,因此,如果声明中有任何可能导致问题的内容,可以参考。
-- RULES:
-- Only black squares may have pieces on them
-- Black squares start at the bottom left hand corner
-- Total checker count for a color cannot exceed 12
-- A color may not have more than 7 kings
with Ada.Text_IO, Ada.Integer_Text_IO;
use Ada.Text_IO, Ada.Integer_Text_IO;
procedure Checker_Checker is
type Checkers_Piece is ("b", "BK", "RK", "BC", "RC");
type Checkers_Board is array (1..8, 1..8) of Checkers_Piece;
Checkers_String: String(1..64);
invalid,
BK_Count, -- black king checker
RK_Count, -- red king checker
RC_Count, -- red checker
BC_Count, -- black checker
b_Count, -- blank space
Char_Count : Integer := 0;
procedure Print_Board (Object: in Checkers_Board) is
Print_Divider: String(1..17);
Print_Squares: String(1..17);
begin
Print_Divider := "-----------------";
Print_Squares := "| | | | | | | | |";
for X in 1..8 loop
Put_Line(Print_Divider);
for Y in 1..8 loop
Print_Squares(Y+1) := Checkers_Board(X, Y);
end loop;
Put_Line(Print_Squares);
end loop;
Put_Line(Print_Divider);
end Print_Board;
begin
Get(Checkers_String);
for I in 1..8 loop
for J in 1..8 loop
Char_Count := Char_Count + 1;
Checkers_Board(I, J) := Checkers_String(Char_Count);
end loop;
end loop;
for Y in 1..8 loop
for X in 1..8 loop
if Checkers_Board(Y, X) = 'b' then
Put_Line("There is a piece on a white space.");
invalid := invalid + 1;
end if;
if Checkers_Board(Y, X) = "BK" then
BK_Count := BK_Count + 1;
BC_Count := BC_Count + 1;
elsif Checkers_Board(Y, X) = "RK" then
RK_Count := RK_Count + 1;
RC_Count := RC_Count + 1;
elsif Checkers_Board(Y, X) = "RC" then
RC_Count := RC_Count + 1;
elsif Checkers_Board(Y, X) = "BC" then
BC_Count := BC_Count + 1;
elsif Checkers_Board(Y, X) = "b" then
b_Count := b_Count + 1;
else
Put_Line("There is an unidentified character on the board.");
end if;
end loop;
end loop;
Print_Board;
if RK_Count > 7 then
Put_Line("There are more than 7 Red Kings on the board.");
end if;
if BK_Count > 7 then
Put_Line("There are more than 7 Black Kings on the board.");
end if;
if RC_Count > 12 then
Put_Line("There are more than 12 Red Checkers on the board.");
end if;
if BC_Count > 12 then
Put_Line("There are more than 12 Black Checkers on the board.");
end if;
if b_Count < 32 then
Put_Line("There are too many checkers on the board.");
end if;
if invalid = 0 then
Put_Line("This is a valid checker board.");
else
Put_Line("This is an invalid checker board.");
end if;
end Checker_Checker;
我知道我的代码是一团糟..请不要对它过于批评。.我正在尽力学习Ada。 谢谢您的帮助。
答案 0 :(得分:5)
首先,
type Checkers_Piece is ("b", "BK", "RK", "BC", "RC");
试图声明一个枚举;但是枚举文字是标识符,而不是字符串。 所以应该是
type Checkers_Piece is (b, BK, RK, BC, RC);
然后,在
Print_Squares := "| | | | | | | | |";
for X in 1..8 loop
Put_Line(Print_Divider);
for Y in 1..8 loop
Print_Squares(Y+1) := Checkers_Board(X, Y);
end loop;
Put_Line(Print_Squares);
end loop;
您显然希望Checkers_Board(X, Y)
是一个单独的Character
,但是在声明中,您希望它是一种String
,最多2个Character
您必须确定所需的表示形式,以便您可以在单个字符中区分红色国王和黑色国王。或者,更好的是,接受您需要2个字符来代表作品。或者,您可以使用单个Character
并遵循以下约定:小写的值不加冠,大写的值加冠,空格表示不占用。
出现“类型转换参数”错误的原因是,像这样的行
Checkers_Board(I, J) := Checkers_String(Char_Count);
Checkers_Board
是类型;您在这里需要该类型的对象,而不是类型本身。您正在使用的语法用于类型转换(ARM 4.6)。