我的还……支持撤销!用链表实现!

题目:推箱子小游戏(基于console)

功能要求:

  1. 将p09迷宫游戏改造为“推箱子”游戏;
  2. 在地图中增加箱子、箱子目标位置等图形;
  3. 当玩家将所有箱子归位,则显示玩家赢得了游戏;
  4. 按玩家走动步数记分;
  5. 设计多个关卡,每一关的地图从文件中读取,玩家每关的分数记录到文件中;
    1. #include<stdio.h>
    2. #include<conio.h>
    3. #include<windows.h>
    4. #define LEVEL 3
    5.  
    6. typedef struct Node{
    7. int x[3],y[3];
    8. char deled[3];
    9. struct Node *pre;
    10. }Node;
    11. Node *tail;
    12. void push_back(int xs[],int ys[],char dels[]);
    13.  
    14. char Map[110][110];
    15. int dx[110],dy[110],n,m;
    16. int had,all;
    17. int is_place[110][110];
    18.  
    19. void print_xy(const char* str,int len,int x,int y);
    20. int check_boxes(int x,int y,char dir);
    21. void calc_had();
    22.  
    23. int main(){
    24. char ch;
    25. //游戏说明
    26. printf("This is a Game of Pushing Boxes.\n");
    27. printf("You are at the Place of 'O'.\n");
    28. printf("Use the Direction Keys to Move.\n");
    29. printf("'#' is a box. You can Move Several Boxes at the Same Time.\n");
    30. printf("'*' is a Wall,You can not Move on it or Put the Boxes on it.\n");
    31. printf("'@' is a Place You Need to Put a Box on it.\n");
    32. printf("'Y' is a Box which is on the '@'.\n");
    33. printf("\n\t\tAuthor : Li Zitong, CCSE, UESTC, 2017/03\n");
    34. printf("\n\n请按任意键继续...\n");
    35. while(!kbhit());
    36. ch=getch();
    37. if(ch==-32){
    38. getch();
    39. }
    40.  
    41. system("cls");
    42. FILE *fp,*fp2=fopen("RESULTS.txt","w");
    43. char tmp_print[5]={0};
    44. char file_name[20];
    45. dx[72]=-1; dy[72]=0;
    46. dx[80]=1; dy[80]=0;
    47. dx[75]=0; dy[75]=-1;
    48. dx[77]=0; dy[77]=1;
    49.  
    50. int xs[3],ys[3];
    51. char dels[3];
    52.  
    53. for(int i=1;i<=LEVEL;++i){
    54. had=all=0;
    55. int steps=0,cancels=0;
    56. int x,y;
    57. sprintf(file_name,"LEVEL%d.txt",i);
    58. fp=fopen(file_name,"r");
    59. fscanf(fp,"%d%d%d%d",&n,&m,&x,&y);
    60. fgetc(fp);
    61. for(int j=0;j<n;++j){
    62. fgets(Map[j],m+2,fp);
    63. }
    64. fclose(fp);
    65. memset(is_place,0,sizeof(is_place));
    66. for(int j=0;j<n;++j){
    67. for(int k=0;k<m;++k){
    68. if(Map[j][k]=='@'){
    69. ++all;
    70. is_place[j][k]=1;
    71. }
    72. }
    73. }
    74. for(int j=0;j<n;++j){
    75. printf(Map[j]);
    76. }
    77. printf("LEVEL: %d\nSteps:0 Cancels:0 \n",i);
    78. while(1){
    79. //方向键控制,从键盘进行读入
    80. while(!kbhit());
    81. ch=getch();
    82. if(ch==8){
    83. if(tail==NULL){
    84. continue;
    85. }
    86. ++cancels;
    87. for(int j=0;j<=2 && (*tail).deled[j];++j){
    88. Map[(*tail).x[j]][(*tail).y[j]]=tmp_print[0]=(*tail).deled[j];
    89. print_xy(tmp_print,1,(*tail).x[j],(*tail).y[j]);
    90. }
    91. x=(*tail).x[1]; y=(*tail).y[1];
    92. Node* tmp=tail;
    93. tail=(*tail).pre;
    94. free(tmp);
    95. sprintf(tmp_print,"%d",cancels);
    96. print_xy(tmp_print,strlen(tmp_print),n+1,19);
    97. // calc_had();
    98. }
    99. else{
    100. if(ch==-32){
    101. ch=getch();
    102. }
    103. int tx,ty;
    104. if(ch==72 || ch==80 || ch==75 || ch==77){
    105. tx=x+dx[ch];
    106. ty=y+dy[ch];
    107. }
    108. else{
    109. continue;
    110. }
    111. if(Map[tx][ty]==' ' || Map[tx][ty]=='@'){
    112. ++steps;
    113. dels[0]=Map[tx][ty];
    114. xs[0]=tx; ys[0]=ty;
    115. Map[tx][ty]=tmp_print[0]='O';
    116. print_xy(tmp_print,1,tx,ty);
    117. xs[1]=x; ys[1]=y;
    118. dels[1]=Map[x][y];
    119. Map[x][y]=tmp_print[0]=(is_place[x][y] ? '@' : ' ');
    120. print_xy(tmp_print,1,x,y);
    121. dels[2]=0;
    122. x=tx; y=ty;
    123. push_back(xs,ys,dels);
    124. sprintf(tmp_print,"%d",steps);
    125. print_xy(tmp_print,strlen(tmp_print),n+1,6);
    126. }
    127. else{
    128. int sum_boxes=check_boxes(tx,ty,ch);
    129. if(sum_boxes){
    130. ++steps;
    131. int ttx=tx+dx[ch]*sum_boxes,tty=ty+dy[ch]*sum_boxes;
    132. dels[0]=Map[ttx][tty];
    133. xs[0]=ttx; ys[0]=tty;
    134. Map[ttx][tty]=tmp_print[0]=(is_place[ttx][tty] ? 'Y' : '#');
    135. print_xy(tmp_print,1,ttx,tty);
    136. dels[2]=Map[tx][ty];
    137. xs[2]=tx; ys[2]=ty;
    138. Map[tx][ty]=tmp_print[0]='O';
    139. print_xy(tmp_print,1,tx,ty);
    140. dels[1]=Map[x][y];
    141. xs[1]=x; ys[1]=y;
    142. Map[x][y]=tmp_print[0]=(is_place[x][y] ? '@' : ' ');
    143. print_xy(tmp_print,1,x,y);
    144. x=tx; y=ty;
    145. push_back(xs,ys,dels);
    146. sprintf(tmp_print,"%d",steps);
    147. print_xy(tmp_print,strlen(tmp_print),n+1,6);
    148. calc_had();
    149. if(had==all){
    150. printf("You Win!\n请按任意键继续...\n");
    151. break;
    152. }
    153. }
    154. }
    155. }
    156. }
    157. fprintf(fp2,"LEVEL%d : Steps:%d\tCancels:%d\n",i,steps,cancels);
    158. while(!kbhit());
    159. ch=getch();
    160. if(ch==-32){
    161. getch();
    162. }
    163. system("cls");
    164. while(tail!=NULL){
    165. Node* tmp=tail;
    166. tail=(*tail).pre;
    167. free(tmp);
    168. }
    169. }
    170. fclose(fp2);
    171. fp=fopen("RESULTS.txt","r");
    172. char tmp_print_2[110];
    173. printf("Game is Over.\nYou can Check Your Scores in the RESULTS.txt.\n\n");
    174. for(int i=1;i<=LEVEL;++i){
    175. fgets(tmp_print_2,100,fp);
    176. printf(tmp_print_2);
    177. }
    178. return 0;
    179. }
    180.  
    181. void print_xy(const char* str,int len,int x,int y){
    182. //向第x行y列输出len长度的字符串(位置从零开始)
    183. PDWORD NumberOfCharsWritten;
    184. HANDLE hnd=GetStdHandle(STD_OUTPUT_HANDLE);
    185. COORD coord={y,x};
    186. WriteConsoleOutputCharacter(hnd,str,len,coord,NumberOfCharsWritten);
    187. }
    188.  
    189. int check_boxes(int x,int y,char dir){
    190. if(Map[x][y]!='#' && Map[x][y]!='Y'){
    191. return 0;
    192. }
    193. int cnt=0;
    194. while(Map[x][y]=='#' || Map[x][y]=='Y'){
    195. ++cnt;
    196. x=x+dx[dir];
    197. y=y+dy[dir];
    198. }
    199. return Map[x][y]!='*' ? cnt : 0;
    200. }
    201.  
    202. void push_back(int xs[],int ys[],char dels[]){
    203. if(tail==NULL){
    204. tail=(Node*)malloc(sizeof(Node));
    205. (*tail).pre=NULL;
    206. }
    207. else{
    208. Node* tmp=(Node*)malloc(sizeof(Node));
    209. (*tmp).pre=tail;
    210. tail=tmp;
    211. }
    212. memcpy((*tail).x,xs,sizeof(int)*3);
    213. memcpy((*tail).y,ys,sizeof(int)*3);
    214. memcpy((*tail).deled,dels,sizeof(char)*3);
    215. }
    216.  
    217. void calc_had(){
    218. had=0;
    219. for(int i=0;i<n;++i){
    220. for(int j=0;j<m;++j){
    221. if(Map[i][j]=='Y'){
    222. ++had;
    223. }
    224. }
    225. }
    226. }

【CCpp程序设计2017】推箱子游戏的更多相关文章

  1. 【CCpp程序设计2017】迷宫游戏

    大一寒假作业!写了第一个小游戏! //maze_test By lizitong #include<stdio.h> #include<time.h> #include< ...

  2. JavaScript写一个小乌龟推箱子游戏

    推箱子游戏是老游戏了, 网上有各种各样的版本, 说下推箱子游戏的简单实现,以及我找到的一些参考视频和实例: 推箱子游戏的在线DEMO : 打开 如下是效果图: 这个拖箱子游戏做了移动端的适配, 我使用 ...

  3. 用HTML5+原生js实现的推箱子游戏

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. C# 推箱子游戏&对战游戏

    推箱子游戏提纲,只有向右向上的操作,向左向下同理,后期需完善. namespace 推箱子 { class Program { static void Main(string[] args) { // ...

  5. JavaScript 推箱子游戏

    推箱子游戏的 逻辑非常简单,但是如果不动手的话,还是不太清楚.我在这里讲一下自己的思路. 制作推箱子,首先要有自己的设计素材.如下我也是网上找的素材 第二步,理清游戏的规则. 游戏规则: 1.小人将箱 ...

  6. three.js 制作一个三维的推箱子游戏

    今天郭先生发现大家更喜欢看我发的three.js小作品,今天我就发一个3d版本推箱子的游戏,其实webGL有很多框架,three.js并不合适做游戏引擎,但是可以尝试一些小游戏.在线案例请点击博客原文 ...

  7. 用C写一个简单的推箱子游戏(二)

    下面接着上一篇随笔<用C写一个简单的推箱子游戏(一)>来写 tuidong()函数是用来判断游戏人物前方情况的函数,是推箱子游戏中非常重要的一个函数,下面从它开始继续介绍推箱子的小程序怎么 ...

  8. 用C写一个简单的推箱子游戏(一)

    我现在在读大二,我们有一门课程叫<操作系统>,课程考查要求我们可以写一段程序或者写Windows.iOS.Mac的发展历程.后面我结合网上的资料参考,就想用自己之前简单学过的C写一关的推箱 ...

  9. C语言实现推箱子游戏完整代码

    C语言实现推箱子游戏完整代码 前言 自己做的,可能有些代码不够工整,或者有些小问题,但游戏的基本操作是可以实现的 代码效果 代码一共分为8个部分,4个控制上下左右移动,2个判断输赢,1个统计归为的个数 ...

随机推荐

  1. F题 hdu 1431 素数回文

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1431 素数回文 Time Limit: 2000/1000 MS (Java/Others)    M ...

  2. 线程,JSP,Servlet面试题

    线程编程方面 60.java中有几种方法可以实现一个线程?用什么关键字修饰同步方法? stop()和suspend()方法为何不推荐使用? 答:有两种实现方法,分别是继承Thread类与实现Runna ...

  3. linux系统下git使用

    转载:http://www.cnblogs.com/bear2flymoon/p/4335364.html?ADUIN=563508762&ADSESSION=1430887070&A ...

  4. linux pthread【转】

    转自:http://www.cnblogs.com/alanhu/articles/4748943.html Posix线程编程指南(1) 内容:  一. 线程创建  二.线程取消 关于作者  线程创 ...

  5. 64_a1

    AGReader-1.2-16.fc26.x86_64.rpm 13-Feb-2017 23:31 50654 ATpy-0.9.7-11.fc26.noarch.rpm 13-Feb-2017 22 ...

  6. Android IPC

    1. 什么是Android IPC IPC:inter-process Commnication跨进程的通信,多进程之间的通信,不同的操作系统有不同的通信方式,Android继承自Linux,但其IP ...

  7. jQuery 选中tr下面的第某个td

    1.问题描述 点击 table 中的某行 tr,获取该 tr 下的第一个 td 标签下的< input type="hidden" value="92"/ ...

  8. java的集合类面试题

    转自:https://yq.aliyun.com/articles/78788?spm=5176.8252056.759076.3.uFYrmt java.util包中包含了一系列重要的集合类,而对于 ...

  9. SRM 638 Div.2

    250 给一个字符串 要求从一种形式换成另一形式 class NamingConvention{ private: int a, b, c; public: int d; string toCamel ...

  10. 不同版本的jquery的复选框checkbox的相关问题

    在尝试写复选框时候遇到一个问题,调试了很久都没调试出来,极其郁闷: IE10,Chrome,FF中,对于选中状态,第一次$('#checkbox').attr('checked',true)可以实现 ...