So i have an assignment in which i have to make a guessing game (bulls and cows).
Firstly i made i little program where you enter two numbers and it checks
if they have the same length and then if they are equal.
*The numbers have have to be in lists, in order to be able to give in every guess the number of bulls and cows (see the rules of the game).
equal([],[]).
equal([Ha|Ta],[Hb|Tb]) :-
Ha = Hb, equal(Ta,Tb).
check_length(List1,List2):-
same_length(List1,List2),writeln('The Lists have the same length!').
check_equality(List1,List2):-
equal(List1,List2),writeln('The Lists are equal!').
start:-
write('give 1st list:'), read(X),atom_chars(X, List1),
write('give 2nd list:'), read(Y),atom_chars(Y, List2),
check_length(List1,List2),
check_equality(List1,List2).
So far so good. It works fine. Then i went on to the next step and altered it, so it generates a list with 4 random integers and then it waits for the user to make a guess and compares the two lists like before. *Obviously i print the generated number to the screen in order to know if the program works ok.
start:-
A is random(9),
B is random(9),
C is random(9),
D is random(9),
List2=[A,B,C,D],
write('Generated number:'),writeln(List2),
write('Make a guess:'), read(Guess),atom_chars(Guess, List1),
nl,
check_length(List1,List2),
check_equality(List1,List2).
The problem is that this time even if you type the right number, the program does figure out if the lists (numbers) have the same length but fails in equality check.
What am i doing wrong?
Thanks in advance.
Here is the trace from a run:
the important part is this:
so the problem is that the char ‘6’ is not equal with the number 6
another issue is that zeroes in the beginning will be deleted
so I would suggest to get rid of atom_chars/2 and do something else; read the four numbers one by one or split the number manually