2012-02-08 69 views
1

所以我有一個任務,我必須做一個猜謎遊戲(bulls and cows)。 首先,我讓我輸入兩個數字的小程序,如果他們有相同的長度,然後他們是否相等,它會檢查 。 *數字有列表中,爲了能夠在每個猜測中公牛和奶牛的數量(見遊戲規則)。序言 - 奇怪的問題

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). 

到目前爲止好。它工作正常。然後我繼續下一步並對其進行更改,因此它會生成一個包含4個隨機整數的列表,然後等待用戶進行猜測並像以前一樣比較兩個列表。 *顯然我將生成的數字打印到屏幕上,以便知道程序是否正常工作。

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). 

的問題是,即使您鍵入正確的數字這段時間,程序沒有弄清楚,如果列表(數字)具有相同的長度,但在平等檢查失敗。 我在做什麼錯?

在此先感謝。

回答

2

下面是從運行軌跡:

[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. 

的重要組成部分,是這樣的:

Call: (9) '6'=6 ? creep 
    Fail: (9) '6'=6 ? creep 

所以問題是,焦炭「6」是不是與6號 等於另一個問題是,在零開始將被刪除

23 ?- atom_chars(0042,L). 
L = ['4', '2']. 

所以我會建議讓ri d atom_chars/2並做其他事情;逐個讀取四個數字或手動分割數字

2

在您的第二個代碼中,List2是一個數字列表。然而,如atom_chars/2生成的,是引用原子的列表。數字和原子不是一回事。在平等檢查成功之前,您必須將其中一個列表轉換爲另一個列表的類型(例如,使用atom_number/2)。