Reputation: 6035
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.
Upvotes: 1
Views: 289
Reputation: 5858
Here is the trace from a run:
[trace] 42 ?- start.
Call: (6) start ? creep
^ Call: (7) _G537 is random(9) ? creep
^ Exit: (7) 6 is random(9) ? creep
^ Call: (7) _G539 is random(9) ? creep
^ Exit: (7) 6 is random(9) ? creep
^ Call: (7) _G541 is random(9) ? creep
^ Exit: (7) 1 is random(9) ? creep
^ Call: (7) _G543 is random(9) ? creep
^ Exit: (7) 4 is random(9) ? creep
Call: (7) _G555=[6, 6, 1, 4] ? creep
Exit: (7) [6, 6, 1, 4]=[6, 6, 1, 4] ? creep
Call: (7) write('Generated number:') ? creep
Generated number:
Exit: (7) write('Generated number:') ? creep
Call: (7) writeln([6, 6, 1, 4]) ? creep
[6,6,1,4]
Exit: (7) writeln([6, 6, 1, 4]) ? creep
Call: (7) write('Make a guess:') ? creep
Make a guess:
Exit: (7) write('Make a guess:') ? creep
Call: (7) read(_G555) ? creep
| 6614.
Exit: (7) read(6614) ? creep
Call: (7) atom_chars(6614, _G556) ? creep
Exit: (7) atom_chars(6614, ['6', '6', '1', '4']) ? creep
Call: (7) nl ? creep
Exit: (7) nl ? creep
Call: (7) check_length(['6', '6', '1', '4'], [6, 6, 1, 4]) ? creep
Call: (8) length(['6', '6', '1', '4'], _G568) ? creep
Exit: (8) length(['6', '6', '1', '4'], 4) ? creep
Call: (8) length([6, 6, 1, 4], 4) ? creep
Exit: (8) length([6, 6, 1, 4], 4) ? creep
Call: (8) writeln('The Lists have the same length!') ? creep
The Lists have the same length!
Exit: (8) writeln('The Lists have the same length!') ? creep
Exit: (7) check_length(['6', '6', '1', '4'], [6, 6, 1, 4]) ? creep
Call: (7) check_equality(['6', '6', '1', '4'], [6, 6, 1, 4]) ? creep
Call: (8) equal(['6', '6', '1', '4'], [6, 6, 1, 4]) ? creep
Call: (9) '6'=6 ? creep
Fail: (9) '6'=6 ? creep
Fail: (8) equal(['6', '6', '1', '4'], [6, 6, 1, 4]) ? creep
Fail: (7) check_equality(['6', '6', '1', '4'], [6, 6, 1, 4]) ? creep
Fail: (6) start ? creep
false.
the important part is this:
Call: (9) '6'=6 ? creep
Fail: (9) '6'=6 ? creep
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
23 ?- atom_chars(0042,L).
L = ['4', '2'].
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
Upvotes: 2
Reputation: 2436
In your second code, List2
is a list of numbers. List1
however, as generated by atom_chars/2
, is a list of quoted atoms. Numbers and atoms are not the same thing. You will have to convert either one of the lists into the type of the other (e.g., using atom_number/2
) before your equality check will succeed.
Upvotes: 2