搜索
查看: 737|回复: 3
打印 上一主题 下一主题

关于推箱子的问题

[复制链接]
跳转到指定楼层
楼主
发表于 2018-2-21 21:25:14 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
5啊哈币
[mw_shl_code=c,true]#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main()
{
        char a[50][50]={"   ###     ",
                                "   #*#     ",
                    "   # #     ",
                    "####O######",
                    "#*  OS O *#",
                    "#####O#####",
                    "    # #    ",
                    "    #*#    ",
                    "    ###    "};
        int i,start_x,start_y;
        char move;
   
    for(i=0;i<=8;i++)
                puts(a);
        
     start_x=4; start_y=5;
    while(a[4][1]!='O' && a[1][4]!='O' && a[4][9]!='O' && a[9][5]!='O')
    {
                move=getch();
        switch(move)
                {
                        case'w':
                        if(a[start_x-1][start_y]!='#')
                        {
                               
                                a[start_x][start_y]=' ';
                if(a[start_x-1][start_y]=='O')
                {
                                        a[start_x-1][start_y]=' ';
                                        a[start_x-2][start_y]='O';
                }
                                start_x--;
                                a[start_x][start_y]='S';
                        }
            break;
            
                        case'a':
            if(a[start_x][start_y-1]!='#')
                        {
                                a[start_x][start_y]=' ';
                 if(a[start_x][start_y-1]=='O')
                {
                                        a[start_x][start_y-1]=' ';
                                        a[start_x][start_y-2]='O';
                }
                                start_y--;
                                a[start_x][start_y]='S';
                        }
            break;       
            
            case's':
            if(a[start_x+1][start_y]!='#')
                        {
                                a[start_x][start_y]=' ';
                 if(a[start_x+1][start_y]=='O')
                {
                                        a[start_x+1][start_y]=' ';
                                        a[start_x+2][start_y]='O';
                }
                                start_x++;
                                a[start_x][start_y]='S';
                        }
            break;
            
            case'd':
            if(a[start_x][start_y+1]!='#')
                        {
                                if(a[start_x][start_y+1]=='O')
                {
                                        a[start_x][start_y+1]=' ';
                                        a[start_x][start_y+2]='O';
                }
                                a[start_x][start_y]=' ';
                                start_y++;
                                a[start_x][start_y]='S';
                        }
            break;
            
        }
        system("cls");
        for(i=0;i<=8;i++)
                        puts(a);
            
    }
    if(a[4][1]=='O' && a[1][4]=='O' && a[4][9]=='O' && a[9][5]=='O')
    {
                system("cls");
        Sleep(1000);
        printf("你赢了");
        
    }
        
        system("pause");
        return 0;
}
[/mw_shl_code]
运行时会在一个或兩個箱子到時就结束了,能如何改善

最佳答案

查看完整内容

将第 22 行[mw_shl_code=c,false]while(a[4][1]!='O' && a[1][4]!='O' && a[4][9]!='O' && a[9][5]!='O')[/mw_shl_code]改为[mw_shl_code=c,false]while(a[4][1]!='O' || a[1][4]!='O' || a[4][9]!='O' || a[9][5]!='O')[/mw_shl_code]
沙发
发表于 2018-2-21 21:25:15 | 只看该作者
将第 22 行[mw_shl_code=c,false]while(a[4][1]!='O' && a[1][4]!='O' && a[4][9]!='O' && a[9][5]!='O')[/mw_shl_code]改为[mw_shl_code=c,false]while(a[4][1]!='O' || a[1][4]!='O' || a[4][9]!='O' || a[9][5]!='O')[/mw_shl_code]

点评

……  发表于 2018-2-22 13:31
老是让你回答,每次想回答一个都抢不着( ̄ー ̄)  发表于 2018-2-22 13:05
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

广播台
特别关注
快速回复 返回顶部 返回列表