博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
推箱子游戏
阅读量:6151 次
发布时间:2019-06-21

本文共 21763 字,大约阅读时间需要 72 分钟。

恩..可能在回退机制上会出现一些问题

//推箱子小游戏C版#include
#include
#include
#include
typedef int ElementType;const int roomsize=10; //设计房间内部为正方形,边长为9int map[roomsize+2][roomsize+2]; //推箱子房间布局的数据结构:二维数组int data; //记录最短步骤数目 int times=0; char ch;int array[2]={
100,100}; //记录最好成绩char string[30]="正在装入..................";//以下为前几轮游戏房间中细节布局的数据结构:二维数组的实际内容int map1[roomsize+2][roomsize+2]={ //0,1,2,3,4,5,6,7,8,9,10 {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, //0 {-1,0,0,0,0,1,1,1,1,1,-1}, //1 {-1,0,0,0,0,1,0,0,0,1,-1}, //2 {-1,1,1,1,0,1,0,0,0,1,-1}, //3 {-1,1,2,1,0,1,0,0,0,1,-1}, //4 {-1,1,2,1,0,1,0,3,0,1,-1}, //5 {-1,1,2,1,1,1,0,3,0,1,-1}, //6 {-1,1,0,0,0,0,3,4,0,1,-1}, //7 {-1,1,0,0,1,0,0,0,0,1,-1}, //8 {-1,1,1,1,1,1,1,1,1,1,-1}, //9 {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1} //10};int map2[roomsize+2][roomsize+2]={ //0,1,2,3,4,5,6,7,8,9,10 {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},//0 {-1,0,1,1,1,1,0,0,0,-1,-1}, //1 {-1,0,1,4,0,1,1,1,0,-1,-1}, //2 {-1,0,1,0,3,0,0,1,0,-1,-1}, //3 {-1,1,1,1,0,1,0,1,1,-1,-1}, //4 {-1,1,2,1,0,1,0,0,1,-1,-1}, //5 {-1,1,2,3,0,0,1,0,1,-1,-1}, //6 {-1,1,2,0,0,0,3,0,1,-1,-1}, //7 {-1,1,1,1,1,1,1,1,1,-1,-1}, //8 {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},//9 {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1} //10};int map3[roomsize+2][roomsize+2]= { //0,1,2,3,4,5,6,7,8,9,10 {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},//0 {-1,1,1,1,1,1,1,1,1,-1,-1}, //1 {-1,1,4,0,0,0,1,1,1,-1,-1}, //2 {-1,1,0,3,3,0,0,0,1,-1,-1}, //36 {-1,1,0,2,1,2,0,0,1,-1,-1}, //4 {-1,1,0,0,1,1,3,0,1,-1,-1}, //5 {-1,1,0,0,1,1,2,1,1,-1,-1}, //6 {-1,1,0,0,0,0,0,1,1,-1,-1}, //7 {-1,1,1,1,1,1,1,1,1,-1,-1}, //8 {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},//9 {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1} //10};//其他关的房间布局模板int map4[roomsize+2][roomsize+2]={ //0,1,2,3,4,5,6,7,8,9,10 {-1,-1,-1,-1,-1,-1,-1,-1,-1},//0 {-1,1,1,1,1,1,1,1,1,-1,-1}, //1 {-1,1,1,1,1,1,1,1,1,-1,-1}, //2 {-1,1,1,0,0,0,0,0,1,1,1,}, //3 {-1,1,1,3,1,1,1,0,0,0,1}, //4 {-1,1,0,4,0,3,0,0,3,0,1}, //5 {-1,1,0,2,2,1,0,3,0,1,1}, //6 {-1,1,1,2,2,1,0,0,0,1,-1}, //7 {-1,1,1,1,1,1,1,1,1,1,-1}, //8 {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},//9 {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1} //10};typedef struct SNode *PtrToSNode;struct SNode { ElementType peopleh; ElementType peoplel; /*ElementType position1h; ElementType position1l; ElementType position2h; ElementType position2l; */ int downdata1; int downdata2; int updata1; int updata2; int rightdata1; int rightdata2; int leftdata1; int leftdata2; char ch; PtrToSNode Next;};typedef PtrToSNode Stack;int positionh;//人的位置纵坐标int positionl;//人的位置横坐标int flag;//标志位,记录人在目标位置上int gate;//记录关数int step;//记录步数bool returned();//后退 void initbox();//初始化函数void begin();//开始界面void choose_gate();//选关提示void choose();//游戏时c选项的提示void replay();//重玩void playing();//玩游戏时界面void display();//显示地图void moveleft();//移向左方向void moveright();//移向右方向void movedown();//移向下方向void moveup();//移向上方向void test_flag();//过关提示void record();//成绩排行榜Stack CreateStack( );bool IsEmpty ( Stack S );bool Push( Stack S);bool Pop( Stack S );//=====Stack CreateStack( ) { /* 构建一个堆栈的头结点,返回该结点指针 */ Stack S; S = (Stack)malloc(sizeof(struct SNode)); S->Next = NULL; return S;}bool IsEmpty ( Stack S ){ /* 判断堆栈S是否为空,若是返回true;否则返回false */ return ( S->Next == NULL );}bool Push( Stack S){ /* 将元素X压入堆栈S */ PtrToSNode TmpCell; TmpCell = (PtrToSNode)malloc(sizeof(struct SNode)); TmpCell->Next = S; S = TmpCell; return true;}bool Pop( Stack S ) { /* 删除并返回堆栈S的栈顶元素 */ PtrToSNode FirstCell; //ElementType TopElem; if( IsEmpty(S) ) { printf("堆栈空"); return ERROR; } else { FirstCell = S->Next; //TopElem = FirstCell->Data; S->Next = FirstCell->Next; free(FirstCell); //return TopElem; return true; }}Stack S =CreateStack( ); void playing()//Ascii码键盘键位:左为75 右为77 上为72 下为80{ int choice; step=0; printf("开始游戏!"); while(1) { display(); ch = getch(); switch(ch) { case 72: moveup(); step++; break; case 80: movedown(); step++; break; case 75: moveleft(); step++; break; case 77: moveright(); step++; break; case 't': case 'T': returned(); break; case 'c': case 'C': choose(); break; case 'q': case 'Q': printf(" ╭─────────────╮\n"); printf(" │是退出游戏还是返回到主界面? │\n"); printf(" │ 1. 返回主界面 │\n"); printf(" │ 2. 退出游戏 │\n"); printf(" ╰──────────────╯\n"); scanf("%d",&choice); switch(choice) { case 1: step=0; Sleep(500); system("cls"); begin(); break; case 2: exit(0); } default: break; } system("cls"); }}void display(){ printf("\n\n\n\n\n"); for(int i=1;i<=roomsize;i++) { printf(" "); for(int j=1;j<=roomsize;j++) { if(map[i][j]==0) printf(" "); if(map[i][j]==1) printf("■");//墙 if(map[i][j]==2) printf("○");//目标位置 if(map[i][j]==3) printf("");//箱子 if(map[i][j]==4) printf("");//人 if(map[i][j]==5) printf("㊣");//箱子在目标位置上 } printf("\n"); } printf("\n\n"); printf("选项(c) 步数:%d\n",step);}bool returned(){ if(S->ch == 't') { return false; } if(S->ch == 80) { if(map[positionh+1][positionl]==0) { flag = 0; } positionl = S->peoplel; positionh = S->peopleh; map[positionh][positionl] = 4; map[positionh+1][positionl] = S->downdata1; if(S->updata1 == 2) { flag = 1; } if(S->updata1 == 0) { flag = 0; } if(S->downdata1 == 3&&S->downdata2 == 0) { map[positionh+2][positionl] = 0; } if(S->downdata2 == 0) { map[positionh+2][positionl] = 2; } if(S->downdata1 == 3&&S->downdata2 == 2) { map[positionh+2][positionl] = 2; } if(S->downdata1 == 5&&S->downdata2 == 0) { map[positionh+2][positionl] = 0; } step--; } if(S->ch == 72) { if(map[positionh-1][positionl]==0) { flag = 0; } positionl = S->peoplel; positionh = S->peopleh; map[positionh][positionl] = 4; map[positionh-1][positionl] = S->updata1; if(S->downdata1 == 2) { flag = 1; } if(S->downdata1 == 0) { flag = 0; } if(S->updata1 == 3) { map[positionh-2][positionl] = 0; } if(S->updata2 == 2) { map[positionh-2][positionl] = 2; } if(S->updata1 == 3&&S->updata2 == 2) { map[positionh-2][positionl] = 2; } step--; } if(S->ch == 77) { if(map[positionh][positionl+1]==0) { flag = 0; } positionl = S->peoplel; positionh = S->peopleh; map[positionh][positionl] = 4; map[positionh][positionl+1] = S->rightdata1; if(S->leftdata1 == 2) { flag = 1; } if(S->leftdata1 == 0) { flag = 0; } if(S->rightdata1 == 3) { map[positionh][positionl+2] = 0; } if(S->rightdata2 == 2) { map[positionh][positionl+2] = 2; } step--; } if(S->ch == 75) { if(map[positionh][positionl-1]==0) { flag = 0; } positionl = S->peoplel; positionh = S->peopleh; map[positionh][positionl] = 4; map[positionh][positionl-1] = S->leftdata1; if(S->rightdata1 == 2) { flag = 1; } if(S->rightdata1 == 0) { flag = 0; } if(S->leftdata1 == 3) { map[positionh][positionl-2] = 0; } if(S->leftdata2 == 2) { map[positionh][positionl-2] = 2; } step--; } Push(S); S->ch = ch; return true;}void movedown() //向下移动{ Push(S); S->ch = ch; S->peopleh = positionh; //定好人所在的位置 S->peoplel = positionl; if(map[positionh+1][positionl]==0)//向空白位置移动 { S->downdata1 = 0; map[positionh+1][positionl]=4; if(flag==1) { map[positionh][positionl]=2;flag=0; } else map[positionh][positionl]=0; positionh++; } else if(map[positionh+1][positionl]==2)//人要到目标位置上 { S->downdata1 = 2; map[positionh+1][positionl]=4; if(flag==1)//人在目标位置上 map[positionh][positionl]=2;//恢复目标位置 else { map[positionh][positionl]=0;//恢复原来的状态 flag=1;//标志位,记录人在目标位置上 } positionh++; } else if(map[positionh+1][positionl]==3&&map[positionh+2][positionl]==0)//将箱子推到空白位置上 { S->downdata1 = 3; S->downdata2 = 0; map[positionh+2][positionl]=3; map[positionh+1][positionl]=4; if(flag==1) { map[positionh][positionl]=2; flag=0; } else map[positionh][positionl]=0; positionh++; } else if(map[positionh+1][positionl]==5&&map[positionh+2][positionl]!=1)//要将箱子从目标位置上推出 { S->downdata1 = 5; if(map[positionh+2][positionl]==2)//下一个位置还是目标位置 { S->downdata2 = 2; map[positionh+2][positionl]=5; map[positionh+1][positionl]=4; if(flag==1) map[positionh][positionl]=2; else { map[positionh][positionl]=0; flag=1; } } else if(map[positionh+2][positionl]==0)//下一个位置是空白 { S->downdata2 = 0; map[positionh+2][positionl]=3; map[positionh+1][positionl]=4; if(flag==1) map[positionh][positionl]=2; else { map[positionh][positionl]=0; flag=1; } } positionh++; } else if(map[positionh+1][positionl]==3&&map[positionh+2][positionl]==2)//要将箱子推到目标位置上 { S->downdata1 = 3; S->downdata2 = 2; map[positionh+2][positionl]=5;//箱子在目标位置上 map[positionh+1][positionl]=4; if(flag==1)//人在目标位置上 { map[positionh][positionl]=2; flag=0; } else //人不在目标位置上 map[positionh][positionl]=0; positionh++; } else step--;//抵消人不动的情况 S->updata1 = map[positionh-1][positionl]; test_flag();}void moveright() //右边移动 { Push(S); S->ch = ch; S->peopleh = positionh; //定好人所在的位置 S->peoplel = positionl; if(map[positionh][positionl+1]==0)//向空白位置移动 { S->rightdata1 = 0; map[positionh][positionl+1]=4; if(flag==1) { map[positionh][positionl]=2;flag=0; } else map[positionh][positionl]=0; positionl++; } else if(map[positionh][positionl+1]==2)//人要到目标位置上 { S->rightdata1 = 2; map[positionh][positionl+1]=4; if(flag==1)//人在目标位置上 map[positionh][positionl]=2;//恢复目标位置 else { map[positionh][positionl]=0;//恢复原来的状态 flag=1;//标志位,记录人在目标位置上 } positionl++; } else if(map[positionh][positionl+1]==3&&map[positionh][positionl+2]==0)//将箱子推到空白位置上 { S->rightdata1 = 3; S->rightdata2 = 0; map[positionh][positionl+2]=3; map[positionh][positionl+1]=4; if(flag==1) { map[positionh][positionl]=2; flag=0; } else map[positionh][positionl]=0; positionl++; } else if(map[positionh][positionl+1]==5&&map[positionh][positionl+2]!=1)//要将箱子从目标位置上推出 { S->rightdata1 = 5; if(map[positionh][positionl+2]==2)//下一个位置还是目标位置 { S->rightdata2 = 2; map[positionh][positionl+2]=5; map[positionh][positionl+1]=4; if(flag==1) map[positionh][positionl]=2; else { map[positionh][positionl]=0; flag=1; } } else if(map[positionh][positionl+2]==0)//下一个位置是空白 { S->rightdata2 = 0; map[positionh][positionl+2]=3; map[positionh][positionl+1]=4; if(flag==1) map[positionh][positionl]=2; else { map[positionh][positionl]=0; flag=1; } } positionl++; } else if(map[positionh][positionl+1]==3&&map[positionh][positionl+2]==2)//要将箱子推到目标位置上 { S->rightdata1 = 3; S->rightdata2 = 2; map[positionh][positionl+2]=5;//箱子在目标位置上 map[positionh][positionl+1]=4; if(flag==1)//人在目标位置上 { map[positionh][positionl]=2; flag=0; } else //人不在目标位置上 map[positionh][positionl]=0; positionl++; } else step--;//抵消人不动的情况 S->leftdata1 = map[positionh][positionl-1]; test_flag();}void moveleft() //左边移动 { Push(S); S->ch = ch; S->peopleh = positionh; //定好人所在的位置 S->peoplel = positionl; if(map[positionh][positionl-1]==0)//向空白位置移动 { S->leftdata1 = 0; map[positionh][positionl-1]=4; if(flag==1) { map[positionh][positionl]=2; flag=0; } else map[positionh][positionl]=0; positionl--; } else if(map[positionh][positionl-1]==2)//人要到目标位置上 { S->leftdata1 = 2; map[positionh][positionl-1]=4; if(flag==1)//人在目标位置上 map[positionh][positionl]=2;//恢复目标位置 else { map[positionh][positionl]=0;//恢复原来的状态 flag=1;//标志位,记录人在目标位置上 } positionl--; } else if(map[positionh][positionl-1]==3&&map[positionh][positionl-2]==0)//将箱子推到空白位置上 { S->leftdata1 = 3; S->leftdata2 = 0; map[positionh][positionl-2]=3; map[positionh][positionl-1]=4; if(flag==1) { map[positionh][positionl]=2; flag=0; } else map[positionh][positionl]=0; positionl--; } else if(map[positionh][positionl-1]==5&&map[positionh][positionl-2]!=1)//要将箱子从目标位置上推出 { S->leftdata1 = 5; if(map[positionh][positionl-2]==2)//下一个位置还是目标位置 { S->leftdata2 = 2; map[positionh][positionl-2]=5; map[positionh][positionl-1]=4; if(flag==1) map[positionh][positionl]=2; else { map[positionh][positionl]=0; flag=1; } } else if(map[positionh][positionl-2]==0)//下一个位置是空白 { S->leftdata2 = 0; map[positionh][positionl-2]=3; map[positionh][positionl-1]=4; if(flag==1) map[positionh][positionl]=2; else { map[positionh][positionl]=0; flag=1; } } positionl--; } else if(map[positionh][positionl-1]==3&&map[positionh][positionl-2]==2)//要将箱子推到目标位置上 { S->leftdata1 = 3; S->leftdata2 = 2; map[positionh][positionl-2]=5;//箱子在目标位置上 map[positionh][positionl-1]=4; if(flag==1)//人在目标位置上 { map[positionh][positionl]=2; flag=0; } else //人不在目标位置上 map[positionh][positionl]=0; positionl--; } else step--;//抵消人不动的情况 S->rightdata1 = map[positionh][positionl+1]; test_flag();}void moveup() //向上移动{ Push(S); S->ch = ch; S->peopleh = positionh; //定好人所在的位置 S->peoplel = positionl; if(map[positionh-1][positionl]==0)//向空白位置移动 { S->updata1 = 0; map[positionh-1][positionl]=4; if(flag==1) { map[positionh][positionl]=2;flag=0; } else map[positionh][positionl]=0; positionh--; } else if(map[positionh-1][positionl]==2)//人要到目标位置上 { S->updata1 = 2; map[positionh-1][positionl]=4; if(flag==1)//人在目标位置上 map[positionh][positionl]=2;//恢复目标位置 else { map[positionh][positionl]=0;//恢复原来的状态 flag=1;//标志位,记录人在目标位置上 } positionh--; } else if(map[positionh-1][positionl]==3&&map[positionh-2][positionl]==0)//将箱子推到空白位置上 { S->updata1 = 3; S->updata2 = 0; map[positionh-2][positionl]=3; map[positionh-1][positionl]=4; if(flag==1) { map[positionh][positionl]=2; flag=0; } else map[positionh][positionl]=0; positionh--; } else if(map[positionh-1][positionl]==5&&map[positionh-2][positionl]!=1)//要将箱子从目标位置上推出 { S->updata1 = 5; if(map[positionh-2][positionl]==2)//下一个位置还是目标位置 { S->updata2 = 2; map[positionh-2][positionl]=5; map[positionh-1][positionl]=4; if(flag==1) map[positionh][positionl]=2; else { map[positionh][positionl]=0; flag=1; } } else if(map[positionh-2][positionl]==0)//下一个位置是空白 { S->updata2 = 0; map[positionh-2][positionl]=3; map[positionh-1][positionl]=4; if(flag==1) map[positionh][positionl]=2; else { map[positionh][positionl]=0; flag=1; } } positionh--; } else if(map[positionh-1][positionl]==3&&map[positionh-2][positionl]==2)//要将箱子推到目标位置上 { S->updata1 = 3; S->updata2 = 2; map[positionh-2][positionl]=5;//箱子在目标位置上 map[positionh-1][positionl]=4; if(flag==1)//人在目标位置上 { map[positionh][positionl]=2; flag=0; } else //人不在目标位置上 map[positionh][positionl]=0; positionh--; } else step--;//抵消人不动的情况 S->downdata1 = map[positionh+1][positionl]; test_flag();}void initbox()//初始化函数{ positionh=0; positionl=0; flag=0; step=0; gate=0;}void begin(){ printf(" ╭────────────────────────────────╮\n"); printf(" │ │\n"); printf(" │ 推箱子 小组名称 │\n"); printf(" │ ╭─────╮ │\n"); printf(" │ │ 游戏简介 │ │\n"); printf(" │ ╰──────╯ │\n"); printf(" │推箱子游戏:最终目的是操作一个人()将箱子()推到目标位置(○)上。 │\n"); printf(" │当箱子在目标位置上时显示㊣. │\n"); printf(" │ ╭─────╮ │\n"); printf(" │ │ 操作说明 │ │\n"); printf(" │ ╰──────╯ │\n"); printf(" │按方向键移动,'c'选项,'q'退出,'t'回退 │\n"); printf(" │(建议使用最大化窗口,这样效果较好。) │\n"); printf(" │ 注:回退只能回退一步 │\n"); printf(" ╰─────────────────────────────────╯\n"); choose_gate();//选择关数 printf("%s\n",string); Sleep(500); system("cls"); playing();}void choose_gate(){ int j,k; printf(" ╭──────╮\n"); printf("│ 1.第一关 │\n"); printf("│ 2.第二关 │\n"); printf("│ 3.第三关 │\n"); printf("│ 4.第四关 │\n"); printf("╰───────╯\n"); printf("请选择:"); scanf("%d",&gate); do { switch(gate) { case 1: for(j=0;j
5); }void choose()//选项{ int choice; printf(" ╭──────--╮\n"); printf("│ 1. 重玩 │\n"); printf("│ 2. 返回主界面 │\n"); printf("│ 3. 最佳记录 │\n"); printf("│ 4. 复盘 │\n"); printf("│ 5. 退出 │\n"); printf("╰────────╯\n"); scanf("%d",&choice); switch(choice) { case 1: system("cls"); replay(); break; case 2: system("cls"); begin(); break; case 3: record(); system("cls"); playing(); break; // case 4: case 5: exit(0); }}void replay(){ int j,k; step=0; flag=0; do { switch(gate) { case 1: for(j=0;j
3); playing();}void test_flag(){ int choice; for(int i=1;i<=roomsize;i++) for(int j=1;j<=roomsize;j++) { if(map[i][j]==3) return; } system("cls"); step++; data=step; times++; display(); printf(" ╭─────────────╮\n"); printf("│ 恭喜你!你已经通过了这关。 │\n"); printf("│是不是很有成就感?是否继续?│\n"); printf("│1. 继续 │\n"); printf("│2. 最佳记录 │\n"); printf("│3. 退出 │\n"); printf("╰──────────────╯\n"); scanf("%d",&choice); switch(choice) { case 1: step=0; Sleep(500); system("cls"); begin(); break; case 2: record(); system("cls"); printf("按任意键返回主界面...\n"); begin(); break; case 3: printf("欢迎再来玩!\n"); printf("按任意键退出...\n"); getchar(); exit(0); } }void record()//最佳记录{ int rhigh; if(times%2) array[0]=data; else array[1]=data; if(array[0]>array[1]) rhigh=array[1]; else rhigh=array[0]; if(times%2) array[0]=rhigh; else array[1]=rhigh; printf("最佳记录:%d\n",rhigh); system("pause");}//主函数int main(void){ system("color f0");//设置颜色 SetConsoleTitle("推箱子小游戏(C版)"); //设置标题 initbox();//初始化 begin();//开始游戏 return 0;}

转载地址:http://wuzfa.baihongyu.com/

你可能感兴趣的文章
数据加密插件
查看>>
linux后台运行程序
查看>>
win7 vs2012/2013 编译boost 1.55
查看>>
Tar打包、压缩与解压缩到指定目录的方法
查看>>
配置spring上下文
查看>>
Python异步IO --- 轻松管理10k+并发连接
查看>>
Oracle中drop user和drop user cascade的区别
查看>>
登记申请汇总
查看>>
Android Jni调用浅述
查看>>
CodeCombat森林关卡Python代码
查看>>
第一个应用程序HelloWorld
查看>>
(二)Spring Boot 起步入门(翻译自Spring Boot官方教程文档)1.5.9.RELEASE
查看>>
Java并发编程73道面试题及答案
查看>>
企业级负载平衡简介(转)
查看>>
ICCV2017 论文浏览记录
查看>>
科技巨头的交通争夺战
查看>>
Shell基础之-正则表达式
查看>>
JavaScript异步之Generator、async、await
查看>>
讲讲吸顶效果与react-sticky
查看>>
c++面向对象的一些问题1 0
查看>>