2015-12-24 31 views
0

這是連接四個遊戲原型的驗證,但似乎我做錯了什麼。 我希望每次玩家進行移動時,函數都會通過垂直,水平和最終確認對角線來驗證他是否贏得勝利。 但它似乎沒有正確驗證,因爲在某些情況下,即使只有2個做運動時,函數返回1C - 驗證Connect Four遊戲中的勝利條件

int verifyGame(int gamePosition, int gameVariable, char gameArray[HEIGTH][WIDTH]) 
{ 

    if(gameArray[gamePosition][gameVariable] == gameArray[gamePosition + 1][gameVariable] == gameArray[gamePosition + 2][gameVariable] == gameArray[gamePosition + 3][gameVariable]) //verify vertically 
     return 1; 
     else 
     if(gameArray[gamePosition][gameVariable] == gameArray[gamePosition][gameVariable - 3] == gameArray[gamePosition][gameVariable - 2] == gameArray[gamePosition][gameVariable - 1]) //verify horizontally 
      return 1; 
     else 
     if(gameArray[gamePosition][gameVariable] == gameArray[gamePosition][gameVariable - 2] == gameArray[gamePosition][gameVariable - 1] == gameArray[gamePosition][gameVariable + 1]) 
      return 1; 
     else 
     if(gameArray[gamePosition][gameVariable] == gameArray[gamePosition][gameVariable - 1] == gameArray[gamePosition][gameVariable + 1] == gameArray[gamePosition][gameVariable + 2]) 
      return 1; 
     else 
     if(gameArray[gamePosition][gameVariable] == gameArray[gamePosition][gameVariable + 1] == gameArray[gamePosition][gameVariable+ 2] == gameArray[gamePosition][gameVariable + 3]) 
      return 1; 
     //verify diagonally 


     else return 0; 
}; 

這就是函數被調用。開關驗證用戶輸入,然後將其放置在矩陣中的值,然後將驗證贏得

printf("playerPick is : %d\n", playerPick); 
    fflush(stdout); 
    switch(playerPick) 
    { 
    case 1: 
    if(gameVariables[0] >0 && gameVariables[0] < 7) 
     { 
     --gameVariables[0]; 
     gameArray[gameVariables[0]][0] = (char) 82; 
     ifWon = verifyGame(gameVariables[0], 0, gameArray); 
     } 
     printArray(gameArray); 
     break; 
    case 2: 

     if(gameVariables[1] >0 && gameVariables[1] < 7) 
     { 
     --gameVariables[1]; 
     gameArray[gameVariables[1]][1] = (char) 82; 
     ifWon = verifyGame(gameVariables[1], 1, gameArray); 
     } 
     printArray(gameArray); 
     break; 
    case 3: 
     if(gameVariables[2] >0 && gameVariables[2] < 7) 
     { 
      --gameVariables[2]; 
      gameArray[gameVariables[2]][2] = (char) 82; 
      ifWon = verifyGame(gameVariables[2], 2, gameArray); 
     } 
     printArray(gameArray); 
     break; 
    case 4: 
     if(gameVariables[3] >0 && gameVariables[3] < 7) 
     { 
      --gameVariables[3]; 
      gameArray[gameVariables[3]][3] = (char) 82; 
      ifWon = verifyGame(gameVariables[3], 3, gameArray); 
     } 
     printArray(gameArray); 
     break; 
    case 5: 
     if(gameVariables[4] >0 && gameVariables[4] < 7) 
     { 
     --gameVariables[4]; 
     gameArray[gameVariables[4]][4] = (char) 82; 
     ifWon = verifyGame(gameVariables[4], 4, gameArray); 
     } 
     printArray(gameArray); 
     break; 
    case 6: 
     if(gameVariables[5] >0 && gameVariables[5] < 7) 
     { 
     --gameVariables[5]; 
     gameArray[gameVariables[5]][5] = (char) 82; 
     ifWon = verifyGame(gameVariables[5], 5, gameArray); 
     } 
     printArray(gameArray); 
     break; 
    case 7: 
     if(gameVariables[6] >0 && gameVariables[6] < 7) 
     { 
     --gameVariables[6]; 
     gameArray[gameVariables[6]][6] = (char) 82; 
     ifWon = verifyGame(gameVariables[6], 6, gameArray); 
     } 
     printArray(gameArray); 
     break; 

    } 
    printf("%d %d %d %d %d %d %d\n", gameVariables[0], gameVariables[1], gameVariables[2], gameVariables[3], gameVariables[4], gameVariables[5], gameVariables[6]); 
    printf("ifwon : %d\n", ifWon); 
+2

行:'gameArray [gameVariables [0]] [0] =(char)82;'也可以寫成gameArray [gameVariables [0]] [0] = \'R \'; – ryyker

+1

作爲一種風格,代碼將更清潔,沒有所有這些「其他」,因爲每個「if」代碼塊都以「return」結尾。 –

+0

好的。不過,驗證是否有用?這是該計劃中最糟糕的部分,如果能夠真正實現這一目標,那會很好。 – Justplayit94

回答

1

不能平等鏈測試,您嘗試。代碼將執行,但不是你想象的那樣。您的代碼

if(gameArray[gamePosition][gameVariable] == 
    gameArray[gamePosition + 1][gameVariable] == 
    gameArray[gamePosition + 2][gameVariable] == 
    gameArray[gamePosition + 3][gameVariable]) 

必須拆分成單獨的測試,如:

if(gameArray[gamePosition][gameVariable] == gameArray[gamePosition + 1][gameVariable] && 
    gameArray[gamePosition][gameVariable] == gameArray[gamePosition + 2][gameVariable] && 
    gameArray[gamePosition][gameVariable] == gameArray[gamePosition + 3][gameVariable]) 

,並在其他行了。

+0

這就是我所懷疑的。謝謝。 – Justplayit94

2

@Weather Vane的回答是正確的。原始帖子中使用的邏輯對於驗證不正確。

原因之一您可能沒有抓住它自己可能是它寫的複雜的方式。儘量簡化用戶輸入驗證碼:(範圍檢查用戶輸入的值是一切必要)

//User input range checking: 
if((gamePosition >= x)&& //where `x` is minimum for gamePosition 
    (gamePosition <= y)&& //where `y` is maximum for gamePosition 
    (gameVariable >= z)&& //where `z` is minimum for gameVariable 
    (gameVariable <= w))  //where `w` is maximum for gameVariable 
{//continue } 
else 
{ 
    printf("Invalid value. Please re-enter"); 
    return -1; 
} 

另一個機會的簡化是要注意,每個case語句包含相同的代碼,除了案件的價值。正因爲如此,整個switch(...){...}可以替換爲一個單一的如果聲明:

//assuming playerPick >= 1 
    if(gameVariables[playerPick-1] >0 && gameVariables[playerPick-1] < 7) 
    { 
    --gameVariables[playerPick-1]; 
    gameArray[gameVariables[playerPick-1]][playerPick-1] = (char) 82; 
    ifWon = verifyGame(gameVariables[playerPick-1], playerPick-1, gameArray); 
    } 
    printArray(gameArray); 

還要注意,雖然聲明:

gameArray[gameVariables[0][0] = (char) 82; //what is 82? 

是完全合法的,變量gameArray [0] [0]只是一個字符,因此不需要強制轉換值82。此外,C語法提供了一種通過與墳墓符號包圍它,允許下述的形式,這是更易讀拉出字符的ASCII十進制值:

gameArray[gameVariables[0]][0] = `R`;  //intuitive 
+0

謝謝,我已經做了所有的修改,現在我正在討論'if'語句,希望它能更好地工作。 – Justplayit94

+1

@ Justplayit94 - 注意: 該開關可以替換爲_single if_語句:) – ryyker

+1

它確實有效,我現在要做的就是進行對角線驗證,它會很棒 – Justplayit94