I am writing a simple program in Ada95 to check to see if a chess board layout input is valid or not. I am quite new to Ada and I understand I have a lot to learn. The error messages that I keep getting are:
35:31: argument of type conversion must be single expression
47:07: argument of type conversion must be single expression
47:22: illegal operand for array conversion
53:10: argument of type conversion must be single expression
58:10: argument of type conversion must be single expression
61:13: argument of type conversion must be single expression
64:13: argument of type conversion must be single expression
66:13: argument of type conversion must be single expression
68:13: argument of type conversion must be single expression
76:15: invalid use of subtype mark in expression or call
I have included my full code below so that if there is anything that could be causing an issue in a declaration, it is available for reference.
-- 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;
I know my code is a mess.. please don't be too critical of it.. I am trying my best at learning Ada. Thank you for your help.
For a start,
type Checkers_Piece is ("b", "BK", "RK", "BC", "RC");
is trying to declare an enumeration; but enumeration literals are identifiers, not strings. So that should be
type Checkers_Piece is (b, BK, RK, BC, RC);
Then, in
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;
you were clearly expecting Checkers_Board(X, Y) to be a single Character, but in your declaration you intended it to be a sort of String, with up to 2 Characters.
You have to decide what representation you want that lets you distinguish a red king from a black king in a single character. Or, better, accept that you need 2 characters to represent your pieces. Or perhaps you could use single Characters with the convention that lower-case values are uncrowned, upper-case are crowned, space means unoccupied.
The reason for your 'argument of type conversion' errors is that, in lines like
Checkers_Board(I, J) := Checkers_String(Char_Count);
Checkers_Board is a type; you need an object of that type here, not the type itself. The syntax you’re using is for a type conversion (ARM 4.6).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With