【CCpp程序设计2017】推箱子游戏
我的还……支持撤销!用链表实现!
题目:推箱子小游戏(基于console)
功能要求:
- 将p09迷宫游戏改造为“推箱子”游戏;
- 在地图中增加箱子、箱子目标位置等图形;
- 当玩家将所有箱子归位,则显示玩家赢得了游戏;
- 按玩家走动步数记分;
- 设计多个关卡,每一关的地图从文件中读取,玩家每关的分数记录到文件中;
- #include<stdio.h>
- #include<conio.h>
- #include<windows.h>
- #define LEVEL 3
- typedef struct Node{
- int x[3],y[3];
- char deled[3];
- struct Node *pre;
- }Node;
- Node *tail;
- void push_back(int xs[],int ys[],char dels[]);
- char Map[110][110];
- int dx[110],dy[110],n,m;
- int had,all;
- int is_place[110][110];
- void print_xy(const char* str,int len,int x,int y);
- int check_boxes(int x,int y,char dir);
- void calc_had();
- int main(){
- char ch;
- //游戏说明
- printf("This is a Game of Pushing Boxes.\n");
- printf("You are at the Place of 'O'.\n");
- printf("Use the Direction Keys to Move.\n");
- printf("'#' is a box. You can Move Several Boxes at the Same Time.\n");
- printf("'*' is a Wall,You can not Move on it or Put the Boxes on it.\n");
- printf("'@' is a Place You Need to Put a Box on it.\n");
- printf("'Y' is a Box which is on the '@'.\n");
- printf("\n\t\tAuthor : Li Zitong, CCSE, UESTC, 2017/03\n");
- printf("\n\n请按任意键继续...\n");
- while(!kbhit());
- ch=getch();
- if(ch==-32){
- getch();
- }
- system("cls");
- FILE *fp,*fp2=fopen("RESULTS.txt","w");
- char tmp_print[5]={0};
- char file_name[20];
- dx[72]=-1; dy[72]=0;
- dx[80]=1; dy[80]=0;
- dx[75]=0; dy[75]=-1;
- dx[77]=0; dy[77]=1;
- int xs[3],ys[3];
- char dels[3];
- for(int i=1;i<=LEVEL;++i){
- had=all=0;
- int steps=0,cancels=0;
- int x,y;
- sprintf(file_name,"LEVEL%d.txt",i);
- fp=fopen(file_name,"r");
- fscanf(fp,"%d%d%d%d",&n,&m,&x,&y);
- fgetc(fp);
- for(int j=0;j<n;++j){
- fgets(Map[j],m+2,fp);
- }
- fclose(fp);
- memset(is_place,0,sizeof(is_place));
- for(int j=0;j<n;++j){
- for(int k=0;k<m;++k){
- if(Map[j][k]=='@'){
- ++all;
- is_place[j][k]=1;
- }
- }
- }
- for(int j=0;j<n;++j){
- printf(Map[j]);
- }
- printf("LEVEL: %d\nSteps:0 Cancels:0 \n",i);
- while(1){
- //方向键控制,从键盘进行读入
- while(!kbhit());
- ch=getch();
- if(ch==8){
- if(tail==NULL){
- continue;
- }
- ++cancels;
- for(int j=0;j<=2 && (*tail).deled[j];++j){
- Map[(*tail).x[j]][(*tail).y[j]]=tmp_print[0]=(*tail).deled[j];
- print_xy(tmp_print,1,(*tail).x[j],(*tail).y[j]);
- }
- x=(*tail).x[1]; y=(*tail).y[1];
- Node* tmp=tail;
- tail=(*tail).pre;
- free(tmp);
- sprintf(tmp_print,"%d",cancels);
- print_xy(tmp_print,strlen(tmp_print),n+1,19);
- // calc_had();
- }
- else{
- if(ch==-32){
- ch=getch();
- }
- int tx,ty;
- if(ch==72 || ch==80 || ch==75 || ch==77){
- tx=x+dx[ch];
- ty=y+dy[ch];
- }
- else{
- continue;
- }
- if(Map[tx][ty]==' ' || Map[tx][ty]=='@'){
- ++steps;
- dels[0]=Map[tx][ty];
- xs[0]=tx; ys[0]=ty;
- Map[tx][ty]=tmp_print[0]='O';
- print_xy(tmp_print,1,tx,ty);
- xs[1]=x; ys[1]=y;
- dels[1]=Map[x][y];
- Map[x][y]=tmp_print[0]=(is_place[x][y] ? '@' : ' ');
- print_xy(tmp_print,1,x,y);
- dels[2]=0;
- x=tx; y=ty;
- push_back(xs,ys,dels);
- sprintf(tmp_print,"%d",steps);
- print_xy(tmp_print,strlen(tmp_print),n+1,6);
- }
- else{
- int sum_boxes=check_boxes(tx,ty,ch);
- if(sum_boxes){
- ++steps;
- int ttx=tx+dx[ch]*sum_boxes,tty=ty+dy[ch]*sum_boxes;
- dels[0]=Map[ttx][tty];
- xs[0]=ttx; ys[0]=tty;
- Map[ttx][tty]=tmp_print[0]=(is_place[ttx][tty] ? 'Y' : '#');
- print_xy(tmp_print,1,ttx,tty);
- dels[2]=Map[tx][ty];
- xs[2]=tx; ys[2]=ty;
- Map[tx][ty]=tmp_print[0]='O';
- print_xy(tmp_print,1,tx,ty);
- dels[1]=Map[x][y];
- xs[1]=x; ys[1]=y;
- Map[x][y]=tmp_print[0]=(is_place[x][y] ? '@' : ' ');
- print_xy(tmp_print,1,x,y);
- x=tx; y=ty;
- push_back(xs,ys,dels);
- sprintf(tmp_print,"%d",steps);
- print_xy(tmp_print,strlen(tmp_print),n+1,6);
- calc_had();
- if(had==all){
- printf("You Win!\n请按任意键继续...\n");
- break;
- }
- }
- }
- }
- }
- fprintf(fp2,"LEVEL%d : Steps:%d\tCancels:%d\n",i,steps,cancels);
- while(!kbhit());
- ch=getch();
- if(ch==-32){
- getch();
- }
- system("cls");
- while(tail!=NULL){
- Node* tmp=tail;
- tail=(*tail).pre;
- free(tmp);
- }
- }
- fclose(fp2);
- fp=fopen("RESULTS.txt","r");
- char tmp_print_2[110];
- printf("Game is Over.\nYou can Check Your Scores in the RESULTS.txt.\n\n");
- for(int i=1;i<=LEVEL;++i){
- fgets(tmp_print_2,100,fp);
- printf(tmp_print_2);
- }
- return 0;
- }
- void print_xy(const char* str,int len,int x,int y){
- //向第x行y列输出len长度的字符串(位置从零开始)
- PDWORD NumberOfCharsWritten;
- HANDLE hnd=GetStdHandle(STD_OUTPUT_HANDLE);
- COORD coord={y,x};
- WriteConsoleOutputCharacter(hnd,str,len,coord,NumberOfCharsWritten);
- }
- int check_boxes(int x,int y,char dir){
- if(Map[x][y]!='#' && Map[x][y]!='Y'){
- return 0;
- }
- int cnt=0;
- while(Map[x][y]=='#' || Map[x][y]=='Y'){
- ++cnt;
- x=x+dx[dir];
- y=y+dy[dir];
- }
- return Map[x][y]!='*' ? cnt : 0;
- }
- void push_back(int xs[],int ys[],char dels[]){
- if(tail==NULL){
- tail=(Node*)malloc(sizeof(Node));
- (*tail).pre=NULL;
- }
- else{
- Node* tmp=(Node*)malloc(sizeof(Node));
- (*tmp).pre=tail;
- tail=tmp;
- }
- memcpy((*tail).x,xs,sizeof(int)*3);
- memcpy((*tail).y,ys,sizeof(int)*3);
- memcpy((*tail).deled,dels,sizeof(char)*3);
- }
- void calc_had(){
- had=0;
- for(int i=0;i<n;++i){
- for(int j=0;j<m;++j){
- if(Map[i][j]=='Y'){
- ++had;
- }
- }
- }
- }
- #include<stdio.h>
【CCpp程序设计2017】推箱子游戏的更多相关文章
- 【CCpp程序设计2017】迷宫游戏
大一寒假作业!写了第一个小游戏! //maze_test By lizitong #include<stdio.h> #include<time.h> #include< ...
- JavaScript写一个小乌龟推箱子游戏
推箱子游戏是老游戏了, 网上有各种各样的版本, 说下推箱子游戏的简单实现,以及我找到的一些参考视频和实例: 推箱子游戏的在线DEMO : 打开 如下是效果图: 这个拖箱子游戏做了移动端的适配, 我使用 ...
- 用HTML5+原生js实现的推箱子游戏
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- C# 推箱子游戏&对战游戏
推箱子游戏提纲,只有向右向上的操作,向左向下同理,后期需完善. namespace 推箱子 { class Program { static void Main(string[] args) { // ...
- JavaScript 推箱子游戏
推箱子游戏的 逻辑非常简单,但是如果不动手的话,还是不太清楚.我在这里讲一下自己的思路. 制作推箱子,首先要有自己的设计素材.如下我也是网上找的素材 第二步,理清游戏的规则. 游戏规则: 1.小人将箱 ...
- three.js 制作一个三维的推箱子游戏
今天郭先生发现大家更喜欢看我发的three.js小作品,今天我就发一个3d版本推箱子的游戏,其实webGL有很多框架,three.js并不合适做游戏引擎,但是可以尝试一些小游戏.在线案例请点击博客原文 ...
- 用C写一个简单的推箱子游戏(二)
下面接着上一篇随笔<用C写一个简单的推箱子游戏(一)>来写 tuidong()函数是用来判断游戏人物前方情况的函数,是推箱子游戏中非常重要的一个函数,下面从它开始继续介绍推箱子的小程序怎么 ...
- 用C写一个简单的推箱子游戏(一)
我现在在读大二,我们有一门课程叫<操作系统>,课程考查要求我们可以写一段程序或者写Windows.iOS.Mac的发展历程.后面我结合网上的资料参考,就想用自己之前简单学过的C写一关的推箱 ...
- C语言实现推箱子游戏完整代码
C语言实现推箱子游戏完整代码 前言 自己做的,可能有些代码不够工整,或者有些小问题,但游戏的基本操作是可以实现的 代码效果 代码一共分为8个部分,4个控制上下左右移动,2个判断输赢,1个统计归为的个数 ...
随机推荐
- F题 hdu 1431 素数回文
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1431 素数回文 Time Limit: 2000/1000 MS (Java/Others) M ...
- 线程,JSP,Servlet面试题
线程编程方面 60.java中有几种方法可以实现一个线程?用什么关键字修饰同步方法? stop()和suspend()方法为何不推荐使用? 答:有两种实现方法,分别是继承Thread类与实现Runna ...
- linux系统下git使用
转载:http://www.cnblogs.com/bear2flymoon/p/4335364.html?ADUIN=563508762&ADSESSION=1430887070&A ...
- linux pthread【转】
转自:http://www.cnblogs.com/alanhu/articles/4748943.html Posix线程编程指南(1) 内容: 一. 线程创建 二.线程取消 关于作者 线程创 ...
- 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 ...
- Android IPC
1. 什么是Android IPC IPC:inter-process Commnication跨进程的通信,多进程之间的通信,不同的操作系统有不同的通信方式,Android继承自Linux,但其IP ...
- jQuery 选中tr下面的第某个td
1.问题描述 点击 table 中的某行 tr,获取该 tr 下的第一个 td 标签下的< input type="hidden" value="92"/ ...
- java的集合类面试题
转自:https://yq.aliyun.com/articles/78788?spm=5176.8252056.759076.3.uFYrmt java.util包中包含了一系列重要的集合类,而对于 ...
- SRM 638 Div.2
250 给一个字符串 要求从一种形式换成另一形式 class NamingConvention{ private: int a, b, c; public: int d; string toCamel ...
- 不同版本的jquery的复选框checkbox的相关问题
在尝试写复选框时候遇到一个问题,调试了很久都没调试出来,极其郁闷: IE10,Chrome,FF中,对于选中状态,第一次$('#checkbox').attr('checked',true)可以实现 ...