C语言2d阵列趣味游戏填字游戏

时间:2014-12-02 15:26:33

标签: c arrays multidimensional-array codeblocks

我给了一个填字游戏填字游戏[20] [20]它已经充满了文字,所以我不需要生成。我需要扫描单词水平ONLY 并将它们放在一个新数组中。每行一个字,所以新数组将是数组[40] [20] 注意:填字游戏最多有40个水平字!所以我在阵列中只需要40行。如果单词< 40那么阵列中的其他单元将保持'\ 0' 黑匣子是'*' 给定的填字游戏是:

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中都像“MOUTH”一样),另一个问题是有些单词甚至没有印刷如:“LIE”
任何帮助表示感谢和奖励!

1 个答案:

答案 0 :(得分: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
}