刚写的一个小游戏,我管它叫“最后的z”

来源:互联网 发布:淘宝怎么增长微淘粉丝 编辑:程序博客网 时间:2024/05/02 01:02

最近看《C语言程序设计现代方法》,看到了数组这一块了,写了个小程序练练手,呵呵。


这是个10*10的矩阵,开始的时候你要输入一个横坐标和一个纵坐标,然后程序会把这个点的值设为A,其后开始随机走向不同的方向,直到走出Z。

走出Z的话就赢了。

只会有一种情况走不出Z,那就是走到某一步的时候,周围的路线都是已经走过的,这种情况下,你的游戏就输了。


废话少说,上源码:

#include<stdio.h>#include<time.h>#include<stdlib.h>int main(void){int m[10][10] = {0};int x;int y;int dir;int counter = 0;int i = 0;srand(time(0));printf("LAST Z\n\n");for(x = 0;x < 10;x++){for(y = 0;y < 10;y++){if(m[x][y] != 0)printf("%c  ",m[x][y]);elseprintf("%d  ",m[x][y]);if(y == 9){putchar('\n');}}}printf("\nenter the x and y : ");scanf("%d%d",&x,&y);if((x >= 0 && x <= 9) && (y >= 0 && y <= 9)){m[x][y] = 'A';}while(counter < 25){dir = rand() % 4;skip://printf("randed %d\n",dir);if(i == 4){break;}switch(dir){case 0:if(x - 1 >= 0 && m[x - 1][y] == 0){m[x - 1][y] = 'A' + counter + 1;counter++;x--;i = 0;}else{i++;dir = 1;goto skip;}break;case 1:if(y + 1 <= 9 && m[x][y + 1] == 0){m[x][y + 1] = 'A' + counter + 1;counter++;y++;i = 0;}else{i++;dir = 2;goto skip;}break;case 2:if(x + 1 <= 9 && m[x + 1][y] == 0){m[x + 1][y] = 'A' + counter + 1;counter++;x++;i = 0;}else{i++;dir = 3;goto skip;}break;case 3:if(y - 1 >= 0 && m[x][y - 1] == 0){m[x][y - 1] = 'A' + counter + 1;counter++;y--;i = 0;}else{i++;dir = 0;goto skip;}break;}}if(counter == 25){printf("So you got the last z!\n\n");//printf("counter is %d and last char is %c\n",counter,'A' + counter);}else{printf("you failed to got the last z...\n");printf("The last char is %c.\n",'A' + counter);printf("And you need another %d chars to get z.\n\n",'Z' - 'A' - counter - 1);}for(x = 0;x < 10;x++){for(y = 0;y < 10;y++){if(m[x][y] != 0)printf("%c  ",m[x][y]);elseprintf("%d  ",m[x][y]);if(y == 9){putchar('\n');}}}putchar('\n');}