| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 338 人关注过本帖
标题:编程新手求解,请大神指点
取消只看楼主 加入收藏
AKSTTY
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2014-11-17
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
编程新手求解,请大神指点
#include<stdio.h>
main()
{
    int a[4][4],b[4],i,j,k,l;
    char c[4];
       for (i=1;i<=4;i++)
       {
           for (j=1;j<=4;j++)
           {
               scanf ("%d",&a[i][j]);
               }
           }
     scanf("\n");      
    for (i=1;i<=3;i++)
    scanf ("%c ",&c[i]);
    scanf("%c",&c[4]);
    scanf("\n");
    for (j=1;j<=4;j++)
    scanf ("%d",&b[i]);
    for (l=1;l<=4;l++)
    {
        if (c[l]=='a')
        {
            for (i=1;i<=4;i++)
            {
                    for (j=1;j<=4;j++)
                    {
                        if (a[i][j]==0)
                        {
                            for (k=j;k<=4;k++)
                            {
                                if (a[i][k]!=0)
                                {
                                    a[i][j]=a[i][k];
                                    a[i][k]=0;
                                }
                                break;
                            }
                        }
                    }        
                    for (j=1;j<=3;j++)
                    {
                        if (a[i][j]==0)
                        break;
                        else if (a[i][j]==a[i][j+1])
                        {
                            a[i][j]=a[i][j]+a[i][j+1];
                            a[i][j+1]=0;
                            for (k=j+1;k<=3;k++)
                            {
                            a[i][k]=a[i][k+1];
                            a[i][4]=0;
                            }                           
                        }
                    }
           }
            
                  }
        else if (c[l]=='d')
        {
            for (i=1;i<=4;i++)
            {               
                    for (j=4;j>=1;j--)
                    {
                        if (a[i][j]==0)
                        {
                            for (k=j;k>=1;k--)
                            {
                                if (a[i][k]!=0)
                                {
                                    a[i][j]=a[i][k];
                                    a[i][k]=0;
                                }
                                break;
                            }
                        }
                    }                  
                    for (j=4;j>=1;j--)
                    {
                        if (a[i][j]==0)
                        break;
                        else if (a[i][j]==a[i][j-1])
                        {
                            a[i][j]=a[i][j]+a[i][j-1];
                            a[i][j-1]=0;
                            for (k=j-1;k>=1;k--)
                            {
                            a[i][k]=a[i][k-1];
                            a[i][1]=0;
                            }
                        }
                    }
                 }
      
             }
          else if (c[l]=='w')
        {
            for (j=1;j<=4;j++)
            {
                    for (i=1;i<=4;i++)
                    {
                        if (a[i][j]==0)
                        {
                            for (k=i;k<=4;i++)
                            {
                                if (a[k][j]!=0)
                                {
                                    a[i][j]=a[k][j];
                                    a[k][j]=0;
                                }
                                break;
                            }
                        }
                    }        
                    for (i=1;i<=3;i++)
                    {
                        if (a[i][j]==0)
                        break;
                        else if (a[i][j]==a[i+1][j])
                        {
                            a[i][j]=a[i][j]+a[i+1][j];
                            a[i+1][j]=0;
                            for (k=i+1;k<=3;k++)
                            {
                            a[k][j]=a[k+1][j];
                            a[4][j]=0;
                            }                           
                        }
                    }
           }
   
                  }
        else if (c[l]=='s')
        {
            for (j=1;j<=4;j++)
            {               
                    for (i=4;i>=1;i--)
                    {
                        if (a[i][j]==0)
                        {
                            for (k=i;k>=1;k--)
                            {
                                if (a[k][j]!=0)
                                {
                                    a[i][j]=a[k][j];
                                    a[k][j]=0;
                                }
                                break;
                            }
                        }
                    }                  
                    for (i=4;i>=1;i--)
                    {
                        if (a[i][j]==0)
                        break;
                        else if (a[i][j]==a[i-1][j])
                        {
                            a[i][j]=a[i][j]+a[i-1][j];
                            a[i-1][j]=0;
                            for (k=i-1;k>=1;k--)
                            {
                            a[k][j]=a[k-1][j];
                            a[1][j]=0;
                            }
                        }
                    }
                 }

             }
            i=b[l]/4+1;
            j=b[l]%4;
            while (a[i][j]!=0)
            {
                  b[l]=b[l]+1;
                  i=b[l]/4+1;
                  j=b[l]%4;
             }
              
            
                  if (j==4)
                  a[i][j]=4;
                  else
                  a[i][j]=2;                          
       }
       for (i=1;i<=4;i++)
       {
           for (j=1;j<=4;j++)
           {
               if (i==4&&j==4)
               printf ("%d\n",a[i][j]);
               else
               printf ("%d ",a[i][j]);
               }
           }
           system ("pause");
           
       }
搜索更多相关主题的帖子: include 
2014-11-17 21:53
AKSTTY
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2014-11-17
收藏
得分:0 
一个简单的2048游戏的编程,只有四步。输入输出应该没问题,就是中间的循环体不知道哪错了。
2014-11-17 21:54
快速回复:编程新手求解,请大神指点
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.034768 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved