2014-03-30 68 views
0

對於我的uni項目,我試圖創建一個基本的坦克遊戲C,但我剛剛開始學習C並對C有一個非常基本的理解。所以我開始爲AI玩家編寫一些簡單的代碼,但是當我用GNU GCC編譯器進行編譯時,它遇到了這些錯誤,我不知道如何繼續。所以幫助會很好,請! :d實現AI指針錯誤

線41警告:傳遞的 'AIMove' 參數3時將整數指針,未作施放[默認啓用]

線19注:預期 '詮釋(*)()',但參數是類型 'INT'

int PosCheck(int T1Pos, int T2Pos) 
{ 
    int a; 

    a = T2Pos - T1Pos; 

    if(a == 0) // Stop the tanks trying to overlay 
    { 
     return 0; 
    } 

    if(a >= 1 || a < 0) // Allows the tanks to move foward 
    { 
     return 1; 
    } 
} 

int AIMove(int T1Pos, int T2Pos, int PosCheck()) // AI movement 
{ 
    int b, c; 

    if(PosCheck(T1Pos, T2Pos) == 0) // Choose retreat options or stands still 
    { 
     b = 3 + round(3*(int)rand()/(int)RAND_MAX); 
     return b; 
    } 
    if(PosCheck(T1Pos, T2Pos) == 1) // Chooses foward options 
    { 
     c = 1 + round(3*(int)rand()/(int)RAND_MAX);; 
     return c; 
    } 
} 

main() 
{ 
    int T1Pos; 
    int T2Pos; 
    int T2MC; 

    T2MC = AIMove(T1Pos, T2Pos, PosCheck(T1Pos, T2Pos)); 
} 

回答

1

這個函數有另一個功能,因爲這些括號的參數:

int AIMove(int T1Pos, int T2Pos, int PosCheck()) // AI movement 
              ^^ 

但是當你調用它,你傳遞一個同名函數的結果:

T2MC = AIMove(T1Pos, T2Pos, PosCheck(T1Pos, T2Pos)); 

什麼是PosCheck參數應該怎麼做?你在AIMove裏面叫它,但是不清楚你是想要全球的PosCheck函數還是論點。

順便說一句,聲明函數指針通常的方法是用星號:

int AIMove(int T1Pos, int T2Pos, int (*PosCheck)()) // Obviously a pointer. 

如果沒有什麼特別,你所要完成那裏,只是刪除了參數和參數。

T2MC = AIMove(T1Pos, T2Pos); 

int AIMove(int T1Pos, int T2Pos)