我正在创建一个游戏,其中光标'@'整个移动(二维数组)
所以我需要在我的大学指南中包含一个结构,但是我不知道如何从AZ(char)中分配一个字母数组,所以我做了手动操作,但是对于某些情况我打印出数组的原因是它显示了一些随机符号:'(
,还有什么方法可以将控件(WASD)放入函数中?我试图发挥作用,但由于某种原因,“ @”没有移动
我在跑步时在how it looks上贴了一张图片,以及suppose to look like的图片。(忘了点和等号,它们是用来制作木板的相同设计)
我的代码:
#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;
}
答案 0 :(得分:0)
您正在按值将N
传递给函数,这意味着它们在副本上运行。因此,struct game
中的“原始” struct game gamecp
不会被初始化。而是通过引用(作为main
指针参数)传递。