如何用C语言编写一如图模型的推箱子游戏的程序!

如何用C语言编写一如图模型的推箱子游戏的程序!,第1张

/TC环境下编的/

#include <dosh>

#include <stdioh>

#include <ctypeh>

#include <conioh>

#include <biosh>

#include <alloch>

typedef struct winer

{

int x,y;

struct winer p;

}winer;

char status [20][20];

char far printScreen=(char far )0xB8000000;

void putoutChar(int y,int x,char ch,char fc,char bc);

void printWall(int x, int y);

void printBox(int x, int y);

void printBoxDes(int x, int y);

void printDestination(int x, int y);

void printDestination1(int x,int y,winer win,winer pw);

void printMan(int x, int y);

void init();

winer initStep1();

winer initStep2();

winer initStep3();

winer initStep4();

void moveBoxSpacetoSpace(int x ,int y, char a);

void moveBoxDestoSpace(int x ,int y, char a) ;

void moveBoxSpacetoDes(int x, int y, char a);

void moveBoxDestoDes(int x, int y, char a);

int judge(int x, int y);

void move(int x, int y, char a);

void reset(int i);

void putoutChar(int y,int x,char ch,char fc,char bc)

{

printScreen[(x160)+(y<<1)+0]=ch;

printScreen[(x160)+(y<<1)+1]=(bc16)+fc;

}

void printWall(int x,int y)

{

putoutChar(y-1,x-1,219,GREEN,BLACK);

status[x][y]='w';

}

void printBox(int x,int y)

{

putoutChar(y-1,x-1,10,WHITE,BLACK);

status[x][y]='b';

}

void printDestination1(int x,int y,winer win,winer pw)

{

winer qw;

putoutChar(y-1,x-1,003,YELLOW,BLACK);

status[x][y]='m';

if(win==NULL)

{

win=pw=qw=(winer )malloc(sizeof(winer));

(pw)->x=x;

(pw)->y=y;

(pw)->p=NULL;

}

else

{

qw=(winer )malloc(sizeof(winer));

qw->x=x;

qw->y=y;

(pw)->p=qw;

(pw)=qw;qw->p=NULL;

}

}

void printDestination(int x,int y)

{

putoutChar(y-1,x-1,003,YELLOW,BLACK);

status[x][y]='m';

}

void printMan(int x,int y)

{

gotoxy(y,x);

_AL=02;

_CX=01;

_AH=0xa;

geninterrupt(0x10);

}

void printBoxDes(int x,int y)

{

putoutChar(y-1,x-1,10,YELLOW,BLACK);

status[x][y]='i';

}

void init()

{

int i,j;

for(i=0;i<20;i++)

for(j=0;j<20;j++)

status[i][j]=0;

_AL=3;

_AH=0;

geninterrupt(0x10);

gotoxy(40,4);

printf("Welcome to the box world!");

gotoxy(40,6);

printf("You can use up, down, left,");

gotoxy(40,8);

printf("right key to control it, or");

gotoxy(40,10);

printf("you can press Esc to quit it");

gotoxy(40,12);

printf("Press space to reset the game");

gotoxy(40,14);

printf("Wish you have a good time !");

gotoxy(40,16);

printf("April , 2007");

}

winer initStep1()

{

int x;

int y;

winer win=NULL;

winer pw;

for(x=1,y=5;y<=9;y++)

printWall(x+4,y+10);

for(y=5,x=2;x<=5;x++)

printWall(x+4,y+10);

for(y=9,x=2;x<=5;x++)

printWall(x+4,y+10);

for(y=1,x=3;x<=8;x++)

printWall(x+4,y+10);

for(x=3,y=3;x<=5;x++)

printWall(x+4,y+10);

for(x=5,y=8;x<=9;x++)

printWall(x+4,y+10);

for(x=7,y=4;x<=9;x++)

printWall(x+4,y+10);

for(x=9,y=5;y<=7;y++)

printWall(x+4,y+10);

for(x=8,y=2;y<=3;y++)

printWall(x+4,y+10);

printWall(5+4,4+10);

printWall(5+4,7+10);

printWall(3+4,2+10);

printBox(3+4,6+10);

printBox(3+4,7+10);

printBox(4+4,7+10);

printDestination1(4+4,2+10,&win,&pw);

printDestination1(5+4,2+10,&win,&pw);

printDestination1(6+4,2+10,&win,&pw);

printMan(2+4,8+10);

return win;

}

winer initStep2()

{

int x;

int y;

winer win=NULL;

winer pw;

for(x=1,y=4;y<=7;y++)

printWall(x+4,y+10);

for(x=2,y=2;y<=4;y++)

printWall(x+4,y+10);

for(x=2,y=7;x<=4;x++)

printWall(x+4,y+10);

for(x=4,y=1;x<=8;x++)

printWall(x+4,y+10);

for(x=8,y=2;y<=8;y++)

printWall(x+4,y+10);

for(x=4,y=8;x<=8;x++)

printWall(x+4,y+10);

for(x=4,y=6;x<=5;x++)

printWall(x+4,y+10);

for(x=3,y=2;x<=4;x++)

printWall(x+4,y+10);

for(x=4,y=4;x<=5;x++)

printWall(x+4,y+10);

printWall(6+4,3+10);

printBox(3+4,5+10);

printBox(6+4,6+10);

printBox(7+4,3+10);

printDestination1(5+4,7+10,&win,&pw);

printDestination1(6+4,7+10,&win,&pw);

printDestination1(7+4,7+10,&win,&pw);

printMan(2+4,6+10);

return win;

}

winer initStep3()

{

int x;

int y;

winer win=NULL;

winer pw;

for(x=1,y=2;y<=8;y++)

printWall(x+4,y+10);

for(x=2,y=2;x<=4;x++)

printWall(x+4,y+10);

for(x=4,y=1;y<=3;y++)

printWall(x+4,y+10);

for(x=5,y=1;x<=8;x++)

printWall(x+4,y+10);

for(x=8,y=2;y<=5;y++)

printWall(x+4,y+10);

for(x=5,y=5;x<=7;x++)

printWall(x+4,y+10);

for(x=7,y=6;y<=9;y++)

printWall(x+4,y+10);

for(x=3,y=9;x<=6;x++)

printWall(x+4,y+10);

for(x=3,y=6;y<=8;y++)

printWall(x+4,y+10);

printWall(2+4,8+10);

printWall(5+4,7+10);

printBox(6+4,3+10);

printBox(4+4,4+10);

printBox(5+4,6+10);

printDestination1(2+4,5+10,&win,&pw);

printDestination1(2+4,6+10,&win,&pw);

printDestination1(2+4,7+10,&win,&pw);

printMan(2+4,4+10);

return win;

}

winer initStep4()

{

int x;

int y;

winer win=NULL;

winer pw;

for(x=1,y=1;y<=6;y++)

printWall(x+4,y+10);

for(x=2,y=7;y<=8;y++)

printWall(x+4,y+10);

for(x=2,y=1;x<=7;x++)

printWall(x+4,y+10);

for(x=7,y=2;y<=4;y++)

printWall(x+4,y+10);

for(x=6,y=4;y<=9;y++)

printWall(x+4,y+10);

for(x=3,y=9;x<=5;x++)

printWall(x+4,y+10);

for(x=3,y=3;y<=4;y++)

printWall(x+4,y+10);

printWall(3+4,8+10);

printBox(3+4,5+10);

printBox(4+4,4+10);

printBox(4+4,6+10);

printBox(5+4,5+10);

printBox(5+4,3+10);

printDestination1(3+4,7+10,&win,&pw);

printDestination1(4+4,7+10,&win,&pw);

printDestination1(5+4,7+10,&win,&pw);

printDestination1(4+4,8+10,&win,&pw);

printDestination1(5+4,8+10,&win,&pw);

printMan(2+4,2+10);

return win;

}

void moveBoxSpacetoSpace(int x,int y,char a)

{

switch(a)

{

case 'u':

status[x-1][y]=0;

printf(" ");

printBox(x-2,y);

printMan(x-1,y);

status[x-2][y]='b';

break;

case 'd':

status[x+1][y]=0;

printf(" ");

printBox(x+2,y);

printMan(x+1,y);

status[x+2][y]='b';

break;

case 'l':

status[x][y-1]=0;

printf(" ");

printBox(x,y-2);

printMan(x,y-1);

status[x][y-2]='b';

break;

case 'r':

status[x][y+1]=0;

printf(" ");

printBox(x,y+2);

printMan(x,y+1);

status[x][y+2]='b';

break;

default:

break;

}

}

void moveBoxDestoSpace(int x,int y,char a)

{

switch(a)

{

case 'u':

status[x-1][y]='m';

printf(" ");

printBox(x-2,y);

printMan(x-1,y);

status[x-2][y]='b';

break;

case 'd':

status[x+1][y]='m';

printf(" ");

printBox(x+2,y);

printMan(x+1,y);

status[x+2][y]='b';

break;

case 'l':

status[x][y-1]='m';

printf(" ");

printBox(x,y-2);

printMan(x,y-1);

status[x][y-2]='b';

break;

case 'r':

status[x][y+1]='m';

printf(" ");

printBox(x,y+2);

printMan(x,y+1);

status[x][y+2]='b';

break;

default:

break;

}

}

void moveBoxSpacetoDes(int x,int y,char a)

{

switch(a)

{

case 'u':

status[x-1][y]=0;

printf(" ");

printBoxDes(x-2,y);

printMan(x-1,y);

status[x-2][y]='i';

break;

case 'd':

status[x+1][y]=0;

printf(" ");

printBoxDes(x+2,y);

printMan(x+1,y);

status[x+2][y]='i';

break;

case 'l':

status[x][y-1]=0;

printf(" ");

printBoxDes(x,y-2);

printMan(x,y-1);

status[x][y-2]='i';

break;

case 'r':

status[x][y+1]=0;

printf(" ");

printBoxDes(x,y+2);

printMan(x,y+1);

status[x][y+2]='i';

break;

default:

break;

}

}

void moveBoxDestoDes(int x,int y,char a)

{

switch(a)

{

case 'u':

status[x-1][y]='m';

printf(" ");

printBoxDes(x-2,y);

printMan(x-1,y);

status[x-2][y]='i';

break;

case 'd':

status[x+1][y]='m';

printf(" ");

printBoxDes(x+2,y);

printMan(x+1,y);

status[x+2][y]='i';

break;

case 'l':

status[x][y-1]='m';

printf(" ");

printBoxDes(x,y-2);

printMan(x,y-1);

status[x][y-2]='i';

break;

case 'r':

status[x][y+1]='m';

printf(" ");

printBoxDes(x,y+2);

printMan(x,y+1);

status[x][y+2]='i';

break;

default:

break;

}

}

int judge(int x,int y)

{

int i;

switch(status[x][y])

{

case 0:

i=1;

break;

case 'w':

i=0;

break;

case 'b':

i=2;

break;

case 'i':

i=4;

break;

case 'm':

i=3;

break;

default:

break;

}

return i;

}

void move(int x,int y,char a)

{

switch(a)

{

case 'u':

if(!judge(x-1,y))

{

gotoxy(y,x);

break;

}

else if(judge(x-1,y)==1||judge(x-1,y)==3)

{

if(judge(x,y)==3)

{

printDestination(x,y);

printMan(x-1,y);

break;

}

else

{

printf(" ");

printMan(x-1,y);

break;

}

}

else if(judge(x-1,y)==2)

{

if(judge(x-2,y)==1)

{

moveBoxSpacetoSpace(x,y,'u');

if(judge(x,y)==3)

printDestination(x,y);

gotoxy(y,x-1);

}

else if(judge(x-2,y)==3)

{

moveBoxSpacetoDes(x,y,'u');

if(judge(x,y)==3)

printDestination(x,y);

gotoxy(y,x-1);

}

else

gotoxy(y,x);

break;

}

else if(judge(x-1,y)==4)

{

if(judge(x-2,y)==1)

{

moveBoxDestoSpace(x,y,'u');

if(judge(x,y)==3)

printDestination(x,y);

gotoxy(y,x-1);

}

else if(judge(x-2,y)==3)

{

moveBoxDestoDes(x,y,'u');

if(judge(x,y)==3)

printDestination(x,y);

gotoxy(y,x-1);

}

else

gotoxy(y,x);

break;

}

case 'd':

if(!judge(x+1,y))

{

gotoxy(y,x);

break;

}

else if(judge(x+1,y)==1||judge(x+1,y)==3)

{

if(judge(x,y)==3)

{

printDestination(x,y);

printMan(x+1,y);

break;

}

else

{

printf(" ");

printMan(x+1,y);

break;

}

}

else if(judge(x+1,y)==2)

{

if(judge(x+2,y)==1)

{

moveBoxSpacetoSpace(x,y,'d');

if(judge(x,y)==3)

printDestination(x,y);

gotoxy(y,x+1);

}

else if(judge(x+2,y)==3)

{

moveBoxSpacetoDes(x,y,'d');

if(judge(x,y)==3)

printDestination(x,y);

gotoxy(y,x+1);

}

else

gotoxy(y,x);

break;

}

else if(judge(x+1,y)==4)

{

if(judge(x+2,y)==1)

{

moveBoxDestoSpace(x,y,'d');

if(judge(x,y)==3)

printDestination(x,y);

gotoxy(y,x+1);

}

else if(judge(x+2,y)==3)

{

moveBoxDestoDes(x,y,'d');

if(judge(x,y)==3)

printDestination(x,y);

gotoxy(y,x+1);

}

else

gotoxy(y,x);

break;

}

case 'l':

if(!judge(x,y-1))

{

gotoxy(y,x);

break;

}

else if(judge(x,y-1)==1||judge(x,y-1)==3)

{

if(judge(x,y)==3)

{

printDestination(x,y);

printMan(x,y-1);

break;

}

else

{

printf(" ");

printMan(x,y-1);

break;

}

}

else if(judge(x,y-1)==2)

{

if(judge(x,y-2)==1)

{

moveBoxSpacetoSpace(x,y,'l');

if(judge(x,y)==3)

printDestination(x,y);

gotoxy(y-1,x);

}

else if(judge(x,y-2)==3)

{

moveBoxSpacetoDes(x,y,'l');

if(judge(x,y)==3)

printDestination(x,y);

gotoxy(y-1,x);

}

else

gotoxy(y,x);

break;

}

else if(judge(x,y-1)==4)

{

if(judge(x,y-2)==1)

{

moveBoxDestoSpace(x,y,'l');

if(judge(x,y)==3)

printDestination(x,y);

gotoxy(y-1,x);

}

else if(judge(x,y-2)==3)

{

moveBoxDestoDes(x,y,'l');

if(judge(x,y)==3)

printDestination(x,y);

gotoxy(y-1,x);

}

else

gotoxy(y,x);

break;

}

case 'r':

if(!judge(x,y+1))

{

gotoxy(y,x);

break;

}

else if(judge(x,y+1)==1||judge(x,y+1)==3)

{

if(judge(x,y)==3)

{

printDestination(x,y);

printMan(x,y+1);

break;

}

else

{

printf(" ");

printMan(x,y+1);

break;

}

}

else if(judge(x,y+1)==2)

{

if(judge(x,y+2)==1)

{

moveBoxSpacetoSpace(x,y,'r');

if(judge(x,y)==3)

printDestination(x,y);

gotoxy(y+1,x);

}

else if(judge(x,y+2)==3)

{

moveBoxSpacetoDes(x,y,'r');

if(judge(x,y)==3)

printDestination(x,y);

gotoxy(y+1,x);

}

else

gotoxy(y,x);

break;

}

else if(judge(x,y+1)==4)

{

if(judge(x,y+2)==1)

{

moveBoxDestoSpace(x,y,'r');

if(judge(x,y)==3)

printDestination(x,y);

gotoxy(y+1,x);

}

else if(judge(x,y+2)==3)

{

moveBoxDestoDes(x,y,'r');

if(judge(x,y)==3)

printDestination(x,y);

gotoxy(y+1,x);

}

else

gotoxy(y,x);

break;

}

default:

break;

}

}

void reset(int i)

{

switch(i)

{

case 0:

init();

initStep1();

break;

case 1:

init();

initStep2();

break;

case 2:

init();

initStep3();

break;

case 3:

init();

initStep4();

break;

default:

break;

}

}

void main()

{

int key;

int x;

int y;

int s;

int i=0;

winer win;

winer pw;

_AL=3;

_AH=0;

geninterrupt(0x10);

init();

win=initStep1();

do{

_AH=3;

geninterrupt(0x10);

x=_DH+1;

y=_DL+1;

while(bioskey(1)==0);

key=bioskey(0);

switch(key)

{

case 0x4800:

move(x,y,'u');

break;

case 0x5000:

move(x,y,'d');

break;

case 0x4b00:

move(x,y,'l');

break;

case 0x4d00:

move(x,y,'r');

break;

case 0x3920:

reset(i);

break;

default:

break;

}

s=0;

pw=win;

while(pw)

{

if(status[pw->x][pw->y]=='m')

s++;

pw=pw->p;

}

if(s==0)

{

free(win);

gotoxy(25,2);

printf("congratulate! You have done this step!");

getch();

i++;

switch(i)

{

case 1:

init();

win=initStep2();

break;

case 2:

init();

win=initStep3();

break;

case 3:

init();

win=initStep4();

break;

case 4:

gotoxy(15,21);

printf("Congratulation! \n");

gotoxy(15,23);

printf("You have done all the steps, Welcome to play again!");

key=0x011b;

getch();

break;

default:

break;

}

}

}while(key!=0x011b);

_AL=3;

_AH=0;

geninterrupt(0x10);

}

MOVE 指令就是数据传送,把一个数据创送给一个变量或者把一个变量的数值传送给另外一个变量,但是源 *** 作数和目的 *** 作数的数据类型要一致。否则可能会出现数据传送错误,比如把0传送给一个浮点类型变量,如果源 *** 作数是0是不会出现编译错误的,但是数据传送是错误。传送给浮点变量应该是00,若不是0

西门子有完善的的帮助信息,如果不知道某个指令的使用,可以在程序编辑页面插入这个指令,然后选中这个指令,接着按下F1按键就会出现帮助信息。西门子step7中的MOVE指令按照字节、字、双字的方式MOVE。MB0代表0这个字节,从m07排到m00,置位的是m00和m03,那么就是MB0=00001001,把这个数MOVE给MB0就可以了。

置位/复位指令根据RLO的值,来决定被寻址位的信号状态是否需要改变。被寻址位的信号状态被置1或清0;若RLO是0,则被寻址位的信号保持原状态不变。

对于置位 *** 作,一旦RLO为1,则被寻址信号(输出信号)状态置1,即使RLO又变为0,输出仍保持为1;对于复位 *** 作,一旦RLO为1,则被寻址信号(输出信号)状态置0,即使RLO又变为0,输出保持为0。

语句表STL表示的置位/复位指令

R Reset 复位指令S Set 置位指令

梯形图LAD表示的置位/复位指令( S ) Set Coil 线圈置位指令

( R ) Reset Coil 线圈复位指令

SR Set-Reset Flip Flop 复位优先型SR双稳态触发器指令

RS Reset-Set Flip Flop 置位优先型RS双稳态触发器指令

扩展资料:

SIMATIC S7-300 PLC S7-300是模块化小型PLC系统,能满足中等性能要求的应用。

各种单独西门子PLC之S7家族的模块之间可进行广泛组合构成不同要求的系统。

与S7-200 PLC比较,S7-300 PLC采用模块化结构,具备高速(06~01μs)的指令运算速度。

用浮点数运算比较有效地实现了更为复杂的算术运算。

一个带标准用户接口的软件工具方便用户给所有模块进行参数赋值。

方便的人机界面服务已经集成在S7-300 *** 作系统内,人机对话的编程要求大大减少。

SIMATIC人机界面(HMI)从S7-300中取得数据,S7-300按用户指定的刷新速度传送这些数据。

S7-300 *** 作系统自动地处理数据

def my_print(args):

print args

def move(n, a, b, c):

my_print ((a, '-->', c)) if n==1 else (move(n-1,a,c,b) or move(1,a,b,c) or move(n-1,b,a,c))

注释:汉诺塔模型输入move (n, 'a', 'b', 'c')

例如n=3

move(2,a,c,b)自循环

move(1,a,b,c)

move(2,b,a,c) 自循环

循环完毕,输出

你这段代码也是类似自循环

#include<fstream>

#include<iostream>

using namespace std;

ofstream fout("Honnoitxt");

int num=1;//记录步数

void Move(int n,char x,char y)

{

fout<<num<<":"<<"把"<<n<<" 号盘,从"<<x<<"柱移到"<<y<<"号柱"<<endl;

cout<<num<<":"<<"把"<<n<<" 号盘,从"<<x<<"柱移到"<<y<<"号柱"<<endl;

++num;

}

void Hannoi(int n,char A,char C,char B)

{

if(n==1)

Move(1,A,C);

else

{

Hannoi(n-1,A,B,C);

Move(n,A,C);

Hannoi(n-1,B,C,A);

}

}

int main()

{

Hannoi(5,'A','C','B');

cout<<"输出完毕!"<<endl;

cout<<"一共"<<num-1<<"步"<<endl;

return 0;

}

求理解程序move函数将字符串中的所有数字字符和小数点移到所有其他字符之后

move函数的功能是将字符串中的所有数字字符和小数点()移到所有其他字符之后。它首先创建一个新的可变StringBuilder对象来保存排序后的内容,然后遍历传递进来的字符串中的每一个字符:如果该位是数字或者小数点“”, 则将该位上的内容append到StringBuilder对象里面去; 否则直接insert在最前面即可。最后将StringBuilder对象toString()得到处理好了之后的文件内容并返回。

以上就是关于如何用C语言编写一如图模型的推箱子游戏的程序!全部的内容,包括:如何用C语言编写一如图模型的推箱子游戏的程序!、西门子300MOVE指令怎么用啊 求高手指点、求python大神帮忙解释一下 这个汉诺塔程序的步骤等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/10122474.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存