2014-12-02 109 views
0

我給了填字遊戲填字遊戲[20] [20]其已充滿詞語,所以我不需要生成。我需要水平掃描單詞,並將它們放入新陣列中。每行一個字,所以新數組將會是數組[40] [20]。
注意:填字遊戲最多有40個水平詞語!所以我只需要40行數組。如果單詞< 40那麼數組中的其他單元格將保持'\ 0'。
黑盒子是'*'。
給定的填字是這樣的:C語言2d數組趣味遊戲填字遊戲

char crossword[20][20]={ 
       {'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'}, 
       {'*','T','Y','P','E','*','*','*','*','G','U','I','T','A','R','I','S','T','*','*'}, 
       {'*','E','*','*','V','A','I','N','*','A','*','*','*','V','*','*','T','*','I','*'}, 
       {'*','R','O','M','E','*','*','*','*','M','A','R','R','I','A','G','E','*','N','*'}, 
       {'*','R','*','*','N','O','O','D','L','E','*','O','*','A','*','*','A','N','T','*'}, 
       {'*','I','N','*','*','*','*','O','*','*','*','P','E','T','R','O','L','*','I','*'}, 
       {'*','F','*','C','*','C','U','T','*','L','I','E','*','I','*','A','T','O','M','*'}, 
       {'*','I','*','O','*','U','*','*','M','*','N','*','M','O','U','T','H','*','A','*'}, 
       {'*','E','N','V','E','L','O','P','E','*','F','*','I','N','*','H','*','A','T','*'}, 
       {'*','D','*','E','*','T','*','*','A','*','E','*','N','*','*','*','A','G','E','*'}, 
       {'*','*','A','R','T','*','I','N','T','E','R','I','O','R','*','A','*','O','*','*'}, 
       {'*','K','*','*','O','*','R','*','*','A','T','*','R','O','B','B','E','R','Y','*'}, 
       {'*','A','T','*','A','I','R','*','S','T','I','R','*','O','*','O','*','A','*','*'}, 
       {'*','N','O','*','S','*','I','T','*','*','L','*','S','M','I','L','E','*','S','*'}, 
       {'*','G','*','*','T','*','T','*','O','*','I','*','O','*','N','I','G','H','T','*'}, 
       {'*','A','C','E','*','M','A','N','D','A','T','O','R','Y','*','T','O','*','O','*'}, 
       {'*','R','*','N','Y','*','T','*','E','*','Y','*','T','*','*','I','*','*','P','*'}, 
       {'*','O','*','D','O','*','E','*','*','*','*','I','*','O','Z','O','N','E','*','*'}, 
       {'*','O','N','*','U','N','D','E','R','W','A','T','E','R','*','N','O','U','N','*'}, 
       {'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'} 
}; 

那麼,到目前爲止,我所做的就是這一點,但它是不正確的,因爲我有書呆子的方式做到這一點...:

#include<stdio.h> 
#include<stdlib.h> 

char cross[20][20]={ 
       {'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'}, 
       {'*','T','Y','P','E','*','*','*','*','G','U','I','T','A','R','I','S','T','*','*'}, 
       {'*','E','*','*','V','A','I','N','*','A','*','*','*','V','*','*','T','*','I','*'}, 
       {'*','R','O','M','E','*','*','*','*','M','A','R','R','I','A','G','E','*','N','*'}, 
       {'*','R','*','*','N','O','O','D','L','E','*','O','*','A','*','*','A','N','T','*'}, 
       {'*','I','N','*','*','*','*','O','*','*','*','P','E','T','R','O','L','*','I','*'}, 
       {'*','F','*','C','*','C','U','T','*','L','I','E','*','I','*','A','T','O','M','*'}, 
       {'*','I','*','O','*','U','*','*','M','*','N','*','M','O','U','T','H','*','A','*'}, 
       {'*','E','N','V','E','L','O','P','E','*','F','*','I','N','*','H','*','A','T','*'}, 
       {'*','D','*','E','*','T','*','*','A','*','E','*','N','*','*','*','A','G','E','*'}, 
       {'*','*','A','R','T','*','I','N','T','E','R','I','O','R','*','A','*','O','*','*'}, 
       {'*','K','*','*','O','*','R','*','*','A','T','*','R','O','B','B','E','R','Y','*'}, 
       {'*','A','T','*','A','I','R','*','S','T','I','R','*','O','*','O','*','A','*','*'}, 
       {'*','N','O','*','S','*','I','T','*','*','L','*','S','M','I','L','E','*','S','*'}, 
       {'*','G','*','*','T','*','T','*','O','*','I','*','O','*','N','I','G','H','T','*'}, 
       {'*','A','C','E','*','M','A','N','D','A','T','O','R','Y','*','T','O','*','O','*'}, 
       {'*','R','*','N','Y','*','T','*','E','*','Y','*','T','*','*','I','*','*','P','*'}, 
       {'*','O','*','D','O','*','E','*','*','*','*','I','*','O','Z','O','N','E','*','*'}, 
       {'*','O','N','*','U','N','D','E','R','W','A','T','E','R','*','N','O','U','N','*'}, 
       {'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'} 
}; 

int main(){ 
    int i, j, start_index, end_index, a, b, start_index1, end_index1; 
    char cross1[40][20], cross11[40][20]; 
    for(i=0;i<40;i++){ 
     for(j=0;j<20;j++){ 
      cross1[i][j]='\0'; 
     } 
    } 
    for(i=0;i<40;i++){ 
     for(j=0;j<20;j++){ 
      cross11[i][j]='\0'; 
     } 
    } 
    for(i=0;i<19;i++){ 
     start_index = -1; 
     end_index=-1; 
     start_index1 = -1; 
     end_index1=-1; 
     for(j=0;j<20;j++){ 
      if(isalpha(cross[i][j]) && (!isalpha(cross[i][j-1])) && isalpha(cross[i][j+1]) && start_index==-1){ 
       start_index = j; 
      } 
      if(isalpha(cross[i][j]) && (!isalpha(cross[i][j+1])) && isalpha(cross[i][j-1]) && end_index==-1){ 
       end_index = j; 
      } 
      if(isalpha(cross[i][j]) && (!isalpha(cross[i][j-1])) && isalpha(cross[i][j+1])){ 
       start_index1 = j; 
      } 
      if(isalpha(cross[i][j]) && (!isalpha(cross[i][j+1])) && isalpha(cross[i][j-1])){ 
       end_index1 = j; 
      } 
     } 
     for(a=start_index,b=0;a<=end_index;a++,b++){ 
      cross1[i][b]=cross[i][a]; 
     } 
     for(a=start_index1,b=0;a<=end_index1;a++,b++){ 
      cross11[i][b]=cross[i][a]; 
     } 
    } 
    printf("CROSS1\n"); 
    for(i=0;i<20;i++){ 
     for(j=0;j<20;j++){ 
      printf("%c ", cross1[i][j]); 
     } 
     printf("\n"); 
    } 
    printf("CROSS11\n"); 
    for(i=0;i<20;i++){ 
     for(j=0;j<20;j++){ 
      printf("%c ", cross11[i][j]); 
     } 
     printf("\n"); 
    } 
    return 0; 
} 

而輸出是這樣的:

CROSS1 

TYPE 
VAIN 
ROME 
NOODLE 
IN 
CUT 
MOUTH 
ENVELOPE 
AGE 
ART 
AT 
AT 
NO 
NIGHT 
ACE 
NY 
DO 
ON 

CROSS11 

GUITARIST 
VAIN 
MARRIAGE 
ANT 
PETROL 
ATOM 
MOUTH 
AT 
AGE 
INTERIOR 
ROBBERY 
STIR 
SMILE 
NIGHT 
TO 
NY 
OZONE 
NOUN 

的問題是,我希望它在一個未arrray在兩(cross1,cross11),也有一些字打印兩次(均在cross1和cross11像「MO UTH「),另一個問題是有些詞甚至不打印如:」LIE「
任何幫助表示讚賞和獎勵!

回答

0

你的問題是,如果你每行最多有兩個單詞,你的代碼纔有效。看看你的原始數組:在cross[6]你有CUT,LIE和ATOM。您將CUT放入cross1,然後將LIE放入cross11,您遇到ATOM,並且您在cross11中覆蓋LIE,因此LIE永遠不會被存儲。

一個很好的解決辦法是擺脫start_indexend_indexcross11,因爲start_index1end_index1做他們應該是什麼。您還需要有一個變量row,其類型爲int以跟蹤您在cross1之內的位置,以確保您不會覆蓋任何東西。並且只要你對end_index1設置不同的值,把它粘在cross1,是這樣的:

if(isalpha(cross[i][j]) && (!isalpha(cross[i][j+1])) && isalpha(cross[i][j-1])){ 
    end_index1 = j; 
    //now do this right here, not outside the j-loop 
    for(a=start_index1,b=0;a<=end_index1;a++,b++){ 
     cross1[row][b]=cross[i][a]; 
    } 
    row++; //variable which will keep track of the row you're in for cross1 
} 
+0

非常感謝您!現在我明白我該如何工作了!我很親密:P我的壞!反正DONE再次感謝! – Konstantinos 2014-12-02 15:53:48

+0

很高興你知道了。 – RockOnRockOut 2014-12-02 15:56:16