首页 > 解决方案 > 数组中没有出现的字母(出现奇怪的字符)-C编程

问题描述

我正在创建一个游戏,光标'@'在棋盘上移动(二维数组)

所以我需要包含一个结构作为我的大学指南的一部分,但我不知道如何在结构中从 AZ (char) 分配一个字母数组,所以我做了手动方式,但由于某种原因,当我打印出数组,它显示了一些随机符号:'(

还有什么方法可以让我将控件(WASD)放入一个函数中?我试图发挥作用,但由于某种原因,“@”没有移动

我附上了一张关于我跑步时的样子的图片,以及它的样子。(忘记点和等号,它们与制作板的设计相同)

我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <conio.h>

struct game{

char name[25], input, arr[10][10];
int x, y, c,d, m , n, count, countalpha;

};




/*This function is to display the title of TSP and display instruction for user to enter name and start game*/
void displayRules(){

printf("~~~~~~~~~~~~~~~~~~How The Game Works~~~~~~~~~~~~~~~~~~~~~~\n");
printf("--> Press 'A' to go left.\n");
printf("--> Press 'W' to go up.\n");
printf("--> Press 'S' to go down.\n");
printf("--> Press 'D' to go right.\n");
printf("--> And press Q to quit.\n");
printf("\n\n");
printf("--->You are this guy '@'.\n");
printf("--->The alphabets are known as cities.\n");
printf("--->To win, you need to visit the maximum number of cities in the 
given 40 steps!\n");
printf("--->You only have a maximum of 40 steps!\n");
printf("--->Beat the player with the higest score to win!\n\n\n");
printf("Press any key to start game.");


} 


/*This function is to initialize the array*/
void init(struct game gameinit){
for (gameinit.x=0;gameinit.x<10;gameinit.x++){
    for(gameinit.y=0;gameinit.y<10;gameinit.y++){

        gameinit.arr[gameinit.x][gameinit.y]= '=';
    }
}


}

/*This function is to assign the random alphabets, randomly in array*/
void randomAlpha(struct game gamealpha,char abjad[26]){
for (gamealpha.c=1;gamealpha.c<6;gamealpha.c++){
    for (gamealpha.d=1;gamealpha.d<6;gamealpha.d++){

            gamealpha.arr[rand()%10][rand()%10]=abjad[rand()%10];

    }
}



}

/*This function is to print the array with the randomized alphabets inside*/
void disArray(struct game disparr){

for (disparr.x=0;disparr.x<10;disparr.x++){
    for(disparr.y=0;disparr.y<10;disparr.y++){
        disparr.arr[disparr.m][disparr.n]='@';
        printf(" %c \t", disparr.arr[disparr.x][disparr.y]);
    }


    printf("\n\n");

}



}

/*Function for the controls display and the number of steps left and cities visited */
void controlDis(struct game contdis){

printf(
"**********************************\n"
"-------------CONTROLS-------------\n"
"|\tPress 'A' to go left.\t|\n"
"|\t Press 'W' to go up. \t|\n"
"|\tPress 'S' to go down.\t|\n"
"|\tPress 'D' to go right\t|\n"
"|\t   Press Q to quit   \t|\n"
"**********************************\n"
);
printf("Number of steps left: %i \t Number of cities: %i", contdis.count, contdis.countalpha);
}



/*The main function*/
int main()
{

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

struct game gamecp;


FILE *fp;
FILE *rp;
gamecp.m=0;
gamecp.n=0;
gamecp.count=40;
gamecp.countalpha=0;


fp=fopen("Score.txt","a");
if (fp== NULL){
    exit(1);

}
rp=fopen("Score.txt","r");
if (rp== NULL){
    exit(1);

}

printf("*********WELCOME TO THE TRAVELLING SALESMAN PROBLEM*******\n\n");
printf("~~~~~~~~~~~~~~~Previous Player's Record~~~~~~~~~~~~~~~~~~\n\n");
printf("Name\t\t Number of Cities Visited\n\n");
/*Read the file with name and score, until the last line*/
while(!feof(rp)){
    fgets(gamecp.name, 25, rp);
    puts(gamecp.name);
    }

printf("-----------------------New Player------------------------\n\n");
printf("Please key in your name: ");
scanf("%s", &gamecp.name);
printf("\nHello, let's start the game. Good luck!\n\n");
fflush(stdin);
printf("Press any key to go to Rules Page.");
getch();
srand(time(NULL));
system("cls");

/*Adding some color*/

system("color 74");

/*Rules Page*/
displayRules();
getch();
srand(time(NULL));

system("cls");

/*Calling the array functions*/
init(gamecp);

randomAlpha(gamecp, alpha);

disArray(gamecp);

/*While loops set to inifinity unless pressed q*/
while (1){

printf("\n");

disArray(gamecp);

controlDis(gamecp);


if(gamecp.count==0){

printf("\n\nYou have reached the maximum number of steps. Good bye.");
printf("\nPress enter to exit.");
getche();
break;

}
printf("\nWhat would you like to do?");

gamecp.input=getche();

if (gamecp.input=='d' || gamecp.input=='D'){
    gamecp.arr[gamecp.m][gamecp.n++]='@';
    gamecp.count--;
    if(gamecp.arr[gamecp.m][gamecp.n]!= '=' && gamecp.arr[gamecp.m][gamecp.n] != '@'){
        gamecp.countalpha++;
    }

    system("cls");

}

if(gamecp.input=='w' || gamecp.input=='W'){

    gamecp.arr[gamecp.m--][gamecp.n]='@';
    gamecp.count--;
    if(gamecp.arr[gamecp.m][gamecp.n]!= '=' && gamecp.arr[gamecp.m][gamecp.n]!= '@'){
        gamecp.countalpha++;
    }
    system("cls");


}

if(gamecp.input=='a' || gamecp.input=='A'){

    gamecp.arr[gamecp.m][gamecp.n--]='@';
    gamecp.count--;
    if(gamecp.arr[gamecp.m][gamecp.n]!= '=' && gamecp.arr[gamecp.m][gamecp.n]!= '@'){
        gamecp.countalpha++;
    }
    system("cls");


}

if(gamecp.input=='s' || gamecp.input=='S'){

    gamecp.arr[gamecp.m++][gamecp.n]='@';
    gamecp.count--;
    if(gamecp.arr[gamecp.m][gamecp.n]!= '=' && gamecp.arr[gamecp.m][gamecp.n]!= '@'){
        gamecp.countalpha++;
    }
    system("cls");


}

if(gamecp.input=='q' || gamecp.input=='Q'){
    printf("\nThank you for playing.");
    printf("\nPress any key to exit.");
    getche();
    exit(1);

}



}




fprintf(fp, "%s \t\t %i\n", gamecp.name,gamecp.countalpha);
fclose(fp);
fclose(rp);    

return 0;
}

标签: c

解决方案


struct game按值将 传递给函数,这意味着它们对副本进行操作。struct game gamecp因此,main永远不会初始化“原始” 。而是通过引用传递(作为struct game *指针参数)。


推荐阅读